diff options
| author | Simon Riggs | 2011-02-08 12:23:20 +0000 |
|---|---|---|
| committer | Simon Riggs | 2011-02-08 12:23:20 +0000 |
| commit | 722bf7017bbe796decc79c1fde03e7a83dae9ada (patch) | |
| tree | 94145fc7a78c140f753d856bae8edf54bcce93b3 /src/test | |
| parent | 7202ad7b8dd07864092be70287fe971ec72a3fbc (diff) | |
Extend ALTER TABLE to allow Foreign Keys to be added without initial validation.
FK constraints that are marked NOT VALID may later be VALIDATED, which uses an
ShareUpdateExclusiveLock on constraint table and RowShareLock on referenced
table. Significantly reduces lock strength and duration when adding FKs.
New state visible from psql.
Simon Riggs, with reviews from Marko Tiikkaja and Robert Haas
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/regress/expected/alter_table.out | 12 | ||||
| -rw-r--r-- | src/test/regress/sql/alter_table.sql | 15 |
2 files changed, 27 insertions, 0 deletions
diff --git a/src/test/regress/expected/alter_table.out b/src/test/regress/expected/alter_table.out index 3280065f75b..d5a59d5d1d4 100644 --- a/src/test/regress/expected/alter_table.out +++ b/src/test/regress/expected/alter_table.out @@ -184,6 +184,18 @@ DETAIL: Key (a)=(5) is not present in table "tmp2". DELETE FROM tmp3 where a=5; -- Try (and succeed) ALTER TABLE tmp3 add constraint tmpconstr foreign key (a) references tmp2 match full; +ALTER TABLE tmp3 drop constraint tmpconstr; +INSERT INTO tmp3 values (5,50); +-- Try NOT VALID and then VALIDATE CONSTRAINT, but fails. Delete failure then re-validate +ALTER TABLE tmp3 add constraint tmpconstr foreign key (a) references tmp2 match full NOT VALID; +ALTER TABLE tmp3 validate constraint tmpconstr; +ERROR: insert or update on table "tmp3" violates foreign key constraint "tmpconstr" +DETAIL: Key (a)=(5) is not present in table "tmp2". +-- Delete failing row +DELETE FROM tmp3 where a=5; +-- Try (and succeed) and repeat to show it works on already valid constraint +ALTER TABLE tmp3 validate constraint tmpconstr; +ALTER TABLE tmp3 validate constraint tmpconstr; -- Try (and fail) to create constraint from tmp5(a) to tmp4(a) - unique constraint on -- tmp4 is a,b ALTER TABLE tmp5 add constraint tmpconstr foreign key(a) references tmp4(a) match full; diff --git a/src/test/regress/sql/alter_table.sql b/src/test/regress/sql/alter_table.sql index cfbfbb6b438..6531a9f162d 100644 --- a/src/test/regress/sql/alter_table.sql +++ b/src/test/regress/sql/alter_table.sql @@ -221,6 +221,21 @@ DELETE FROM tmp3 where a=5; -- Try (and succeed) ALTER TABLE tmp3 add constraint tmpconstr foreign key (a) references tmp2 match full; +ALTER TABLE tmp3 drop constraint tmpconstr; + +INSERT INTO tmp3 values (5,50); + +-- Try NOT VALID and then VALIDATE CONSTRAINT, but fails. Delete failure then re-validate +ALTER TABLE tmp3 add constraint tmpconstr foreign key (a) references tmp2 match full NOT VALID; +ALTER TABLE tmp3 validate constraint tmpconstr; + +-- Delete failing row +DELETE FROM tmp3 where a=5; + +-- Try (and succeed) and repeat to show it works on already valid constraint +ALTER TABLE tmp3 validate constraint tmpconstr; +ALTER TABLE tmp3 validate constraint tmpconstr; + -- Try (and fail) to create constraint from tmp5(a) to tmp4(a) - unique constraint on -- tmp4 is a,b |
