6 -- First test, check and cascade
8 CREATE TABLE PKTABLE ( ptest1 int PRIMARY KEY, ptest2 text );
9 CREATE TABLE FKTABLE ( ftest1 int REFERENCES PKTABLE MATCH FULL ON DELETE CASCADE ON UPDATE CASCADE, ftest2 int );
10 -- Insert test data into PKTABLE
11 INSERT INTO PKTABLE VALUES (1, 'Test1');
12 INSERT INTO PKTABLE VALUES (2, 'Test2');
13 INSERT INTO PKTABLE VALUES (3, 'Test3');
14 INSERT INTO PKTABLE VALUES (4, 'Test4');
15 INSERT INTO PKTABLE VALUES (5, 'Test5');
16 -- Insert successful rows into FK TABLE
17 INSERT INTO FKTABLE VALUES (1, 2);
18 INSERT INTO FKTABLE VALUES (2, 3);
19 INSERT INTO FKTABLE VALUES (3, 4);
20 INSERT INTO FKTABLE VALUES (NULL, 1);
21 -- Insert a failed row into FK TABLE
22 INSERT INTO FKTABLE VALUES (100, 2);
23 ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"
24 DETAIL: Key (ftest1)=(100) is not present in table "pktable".
26 SELECT * FROM FKTABLE;
35 -- Delete a row from PK TABLE
36 DELETE FROM PKTABLE WHERE ptest1=1;
37 -- Check FKTABLE for removal of matched row
38 SELECT * FROM FKTABLE;
46 -- Update a row from PK TABLE
47 UPDATE PKTABLE SET ptest1=1 WHERE ptest1=2;
48 -- Check FKTABLE for update of matched row
49 SELECT * FROM FKTABLE;
60 -- check set NULL and table constraint on multiple columns
62 CREATE TABLE PKTABLE ( ptest1 int, ptest2 int, ptest3 text, PRIMARY KEY(ptest1, ptest2) );
63 CREATE TABLE FKTABLE ( ftest1 int, ftest2 int, ftest3 int, CONSTRAINT constrname FOREIGN KEY(ftest1, ftest2)
64 REFERENCES PKTABLE MATCH FULL ON DELETE SET NULL ON UPDATE SET NULL);
66 COMMENT ON CONSTRAINT constrname_wrong ON FKTABLE IS 'fk constraint comment';
67 ERROR: constraint "constrname_wrong" for table "fktable" does not exist
68 COMMENT ON CONSTRAINT constrname ON FKTABLE IS 'fk constraint comment';
69 COMMENT ON CONSTRAINT constrname ON FKTABLE IS NULL;
70 -- Insert test data into PKTABLE
71 INSERT INTO PKTABLE VALUES (1, 2, 'Test1');
72 INSERT INTO PKTABLE VALUES (1, 3, 'Test1-2');
73 INSERT INTO PKTABLE VALUES (2, 4, 'Test2');
74 INSERT INTO PKTABLE VALUES (3, 6, 'Test3');
75 INSERT INTO PKTABLE VALUES (4, 8, 'Test4');
76 INSERT INTO PKTABLE VALUES (5, 10, 'Test5');
77 -- Insert successful rows into FK TABLE
78 INSERT INTO FKTABLE VALUES (1, 2, 4);
79 INSERT INTO FKTABLE VALUES (1, 3, 5);
80 INSERT INTO FKTABLE VALUES (2, 4, 8);
81 INSERT INTO FKTABLE VALUES (3, 6, 12);
82 INSERT INTO FKTABLE VALUES (NULL, NULL, 0);
83 -- Insert failed rows into FK TABLE
84 INSERT INTO FKTABLE VALUES (100, 2, 4);
85 ERROR: insert or update on table "fktable" violates foreign key constraint "constrname"
86 DETAIL: Key (ftest1, ftest2)=(100, 2) is not present in table "pktable".
87 INSERT INTO FKTABLE VALUES (2, 2, 4);
88 ERROR: insert or update on table "fktable" violates foreign key constraint "constrname"
89 DETAIL: Key (ftest1, ftest2)=(2, 2) is not present in table "pktable".
90 INSERT INTO FKTABLE VALUES (NULL, 2, 4);
91 ERROR: insert or update on table "fktable" violates foreign key constraint "constrname"
92 DETAIL: MATCH FULL does not allow mixing of null and nonnull key values.
93 INSERT INTO FKTABLE VALUES (1, NULL, 4);
94 ERROR: insert or update on table "fktable" violates foreign key constraint "constrname"
95 DETAIL: MATCH FULL does not allow mixing of null and nonnull key values.
97 SELECT * FROM FKTABLE;
98 ftest1 | ftest2 | ftest3
99 --------+--------+--------
107 -- Delete a row from PK TABLE
108 DELETE FROM PKTABLE WHERE ptest1=1 and ptest2=2;
109 -- Check FKTABLE for removal of matched row
110 SELECT * FROM FKTABLE;
111 ftest1 | ftest2 | ftest3
112 --------+--------+--------
120 -- Delete another row from PK TABLE
121 DELETE FROM PKTABLE WHERE ptest1=5 and ptest2=10;
122 -- Check FKTABLE (should be no change)
123 SELECT * FROM FKTABLE;
124 ftest1 | ftest2 | ftest3
125 --------+--------+--------
133 -- Update a row from PK TABLE
134 UPDATE PKTABLE SET ptest1=1 WHERE ptest1=2;
135 -- Check FKTABLE for update of matched row
136 SELECT * FROM FKTABLE;
137 ftest1 | ftest2 | ftest3
138 --------+--------+--------
146 -- Check update with part of key null
147 UPDATE FKTABLE SET ftest1 = NULL WHERE ftest1 = 1;
148 ERROR: insert or update on table "fktable" violates foreign key constraint "constrname"
149 DETAIL: MATCH FULL does not allow mixing of null and nonnull key values.
150 -- Check update with old and new key values equal
151 UPDATE FKTABLE SET ftest1 = 1 WHERE ftest1 = 1;
152 -- Try altering the column type where foreign keys are involved
153 ALTER TABLE PKTABLE ALTER COLUMN ptest1 TYPE bigint;
154 ALTER TABLE FKTABLE ALTER COLUMN ftest1 TYPE bigint;
155 SELECT * FROM PKTABLE;
156 ptest1 | ptest2 | ptest3
157 --------+--------+---------
164 SELECT * FROM FKTABLE;
165 ftest1 | ftest2 | ftest3
166 --------+--------+--------
174 DROP TABLE PKTABLE CASCADE;
175 NOTICE: drop cascades to constraint constrname on table fktable
178 -- check set default and table constraint on multiple columns
180 CREATE TABLE PKTABLE ( ptest1 int, ptest2 int, ptest3 text, PRIMARY KEY(ptest1, ptest2) );
181 CREATE TABLE FKTABLE ( ftest1 int DEFAULT -1, ftest2 int DEFAULT -2, ftest3 int, CONSTRAINT constrname2 FOREIGN KEY(ftest1, ftest2)
182 REFERENCES PKTABLE MATCH FULL ON DELETE SET DEFAULT ON UPDATE SET DEFAULT);
183 -- Insert a value in PKTABLE for default
184 INSERT INTO PKTABLE VALUES (-1, -2, 'The Default!');
185 -- Insert test data into PKTABLE
186 INSERT INTO PKTABLE VALUES (1, 2, 'Test1');
187 INSERT INTO PKTABLE VALUES (1, 3, 'Test1-2');
188 INSERT INTO PKTABLE VALUES (2, 4, 'Test2');
189 INSERT INTO PKTABLE VALUES (3, 6, 'Test3');
190 INSERT INTO PKTABLE VALUES (4, 8, 'Test4');
191 INSERT INTO PKTABLE VALUES (5, 10, 'Test5');
192 -- Insert successful rows into FK TABLE
193 INSERT INTO FKTABLE VALUES (1, 2, 4);
194 INSERT INTO FKTABLE VALUES (1, 3, 5);
195 INSERT INTO FKTABLE VALUES (2, 4, 8);
196 INSERT INTO FKTABLE VALUES (3, 6, 12);
197 INSERT INTO FKTABLE VALUES (NULL, NULL, 0);
198 -- Insert failed rows into FK TABLE
199 INSERT INTO FKTABLE VALUES (100, 2, 4);
200 ERROR: insert or update on table "fktable" violates foreign key constraint "constrname2"
201 DETAIL: Key (ftest1, ftest2)=(100, 2) is not present in table "pktable".
202 INSERT INTO FKTABLE VALUES (2, 2, 4);
203 ERROR: insert or update on table "fktable" violates foreign key constraint "constrname2"
204 DETAIL: Key (ftest1, ftest2)=(2, 2) is not present in table "pktable".
205 INSERT INTO FKTABLE VALUES (NULL, 2, 4);
206 ERROR: insert or update on table "fktable" violates foreign key constraint "constrname2"
207 DETAIL: MATCH FULL does not allow mixing of null and nonnull key values.
208 INSERT INTO FKTABLE VALUES (1, NULL, 4);
209 ERROR: insert or update on table "fktable" violates foreign key constraint "constrname2"
210 DETAIL: MATCH FULL does not allow mixing of null and nonnull key values.
212 SELECT * FROM FKTABLE;
213 ftest1 | ftest2 | ftest3
214 --------+--------+--------
222 -- Delete a row from PK TABLE
223 DELETE FROM PKTABLE WHERE ptest1=1 and ptest2=2;
224 -- Check FKTABLE to check for removal
225 SELECT * FROM FKTABLE;
226 ftest1 | ftest2 | ftest3
227 --------+--------+--------
235 -- Delete another row from PK TABLE
236 DELETE FROM PKTABLE WHERE ptest1=5 and ptest2=10;
237 -- Check FKTABLE (should be no change)
238 SELECT * FROM FKTABLE;
239 ftest1 | ftest2 | ftest3
240 --------+--------+--------
248 -- Update a row from PK TABLE
249 UPDATE PKTABLE SET ptest1=1 WHERE ptest1=2;
250 -- Check FKTABLE for update of matched row
251 SELECT * FROM FKTABLE;
252 ftest1 | ftest2 | ftest3
253 --------+--------+--------
261 -- this should fail for lack of CASCADE
263 ERROR: cannot drop table pktable because other objects depend on it
264 DETAIL: constraint constrname2 on table fktable depends on table pktable
265 HINT: Use DROP ... CASCADE to drop the dependent objects too.
266 DROP TABLE PKTABLE CASCADE;
267 NOTICE: drop cascades to constraint constrname2 on table fktable
270 -- First test, check with no on delete or on update
272 CREATE TABLE PKTABLE ( ptest1 int PRIMARY KEY, ptest2 text );
273 CREATE TABLE FKTABLE ( ftest1 int REFERENCES PKTABLE MATCH FULL, ftest2 int );
274 -- Insert test data into PKTABLE
275 INSERT INTO PKTABLE VALUES (1, 'Test1');
276 INSERT INTO PKTABLE VALUES (2, 'Test2');
277 INSERT INTO PKTABLE VALUES (3, 'Test3');
278 INSERT INTO PKTABLE VALUES (4, 'Test4');
279 INSERT INTO PKTABLE VALUES (5, 'Test5');
280 -- Insert successful rows into FK TABLE
281 INSERT INTO FKTABLE VALUES (1, 2);
282 INSERT INTO FKTABLE VALUES (2, 3);
283 INSERT INTO FKTABLE VALUES (3, 4);
284 INSERT INTO FKTABLE VALUES (NULL, 1);
285 -- Insert a failed row into FK TABLE
286 INSERT INTO FKTABLE VALUES (100, 2);
287 ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"
288 DETAIL: Key (ftest1)=(100) is not present in table "pktable".
290 SELECT * FROM FKTABLE;
300 SELECT * FROM PKTABLE;
310 -- Delete a row from PK TABLE (should fail)
311 DELETE FROM PKTABLE WHERE ptest1=1;
312 ERROR: update or delete on table "pktable" violates foreign key constraint "fktable_ftest1_fkey" on table "fktable"
313 DETAIL: Key (ptest1)=(1) is still referenced from table "fktable".
314 -- Delete a row from PK TABLE (should succeed)
315 DELETE FROM PKTABLE WHERE ptest1=5;
316 -- Check PKTABLE for deletes
317 SELECT * FROM PKTABLE;
326 -- Update a row from PK TABLE (should fail)
327 UPDATE PKTABLE SET ptest1=0 WHERE ptest1=2;
328 ERROR: update or delete on table "pktable" violates foreign key constraint "fktable_ftest1_fkey" on table "fktable"
329 DETAIL: Key (ptest1)=(2) is still referenced from table "fktable".
330 -- Update a row from PK TABLE (should succeed)
331 UPDATE PKTABLE SET ptest1=0 WHERE ptest1=4;
332 -- Check PKTABLE for updates
333 SELECT * FROM PKTABLE;
345 -- Check initial check upon ALTER TABLE
347 CREATE TABLE PKTABLE ( ptest1 int, ptest2 int, PRIMARY KEY(ptest1, ptest2) );
348 CREATE TABLE FKTABLE ( ftest1 int, ftest2 int );
349 INSERT INTO PKTABLE VALUES (1, 2);
350 INSERT INTO FKTABLE VALUES (1, NULL);
351 ALTER TABLE FKTABLE ADD FOREIGN KEY(ftest1, ftest2) REFERENCES PKTABLE MATCH FULL;
352 ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_ftest2_fkey"
353 DETAIL: MATCH FULL does not allow mixing of null and nonnull key values.
357 -- Base test restricting update/delete
358 CREATE TABLE PKTABLE ( ptest1 int, ptest2 int, ptest3 int, ptest4 text, PRIMARY KEY(ptest1, ptest2, ptest3) );
359 CREATE TABLE FKTABLE ( ftest1 int, ftest2 int, ftest3 int, ftest4 int, CONSTRAINT constrname3
360 FOREIGN KEY(ftest1, ftest2, ftest3) REFERENCES PKTABLE);
361 -- Insert Primary Key values
362 INSERT INTO PKTABLE VALUES (1, 2, 3, 'test1');
363 INSERT INTO PKTABLE VALUES (1, 3, 3, 'test2');
364 INSERT INTO PKTABLE VALUES (2, 3, 4, 'test3');
365 INSERT INTO PKTABLE VALUES (2, 4, 5, 'test4');
366 -- Insert Foreign Key values
367 INSERT INTO FKTABLE VALUES (1, 2, 3, 1);
368 INSERT INTO FKTABLE VALUES (NULL, 2, 3, 2);
369 INSERT INTO FKTABLE VALUES (2, NULL, 3, 3);
370 INSERT INTO FKTABLE VALUES (NULL, 2, 7, 4);
371 INSERT INTO FKTABLE VALUES (NULL, 3, 4, 5);
372 -- Insert a failed values
373 INSERT INTO FKTABLE VALUES (1, 2, 7, 6);
374 ERROR: insert or update on table "fktable" violates foreign key constraint "constrname3"
375 DETAIL: Key (ftest1, ftest2, ftest3)=(1, 2, 7) is not present in table "pktable".
377 SELECT * from FKTABLE;
378 ftest1 | ftest2 | ftest3 | ftest4
379 --------+--------+--------+--------
387 -- Try to update something that should fail
388 UPDATE PKTABLE set ptest2=5 where ptest2=2;
389 ERROR: update or delete on table "pktable" violates foreign key constraint "constrname3" on table "fktable"
390 DETAIL: Key (ptest1, ptest2, ptest3)=(1, 2, 3) is still referenced from table "fktable".
391 -- Try to update something that should succeed
392 UPDATE PKTABLE set ptest1=1 WHERE ptest2=3;
393 -- Try to delete something that should fail
394 DELETE FROM PKTABLE where ptest1=1 and ptest2=2 and ptest3=3;
395 ERROR: update or delete on table "pktable" violates foreign key constraint "constrname3" on table "fktable"
396 DETAIL: Key (ptest1, ptest2, ptest3)=(1, 2, 3) is still referenced from table "fktable".
397 -- Try to delete something that should work
398 DELETE FROM PKTABLE where ptest1=2;
399 -- Show PKTABLE and FKTABLE
400 SELECT * from PKTABLE;
401 ptest1 | ptest2 | ptest3 | ptest4
402 --------+--------+--------+--------
408 SELECT * from FKTABLE;
409 ftest1 | ftest2 | ftest3 | ftest4
410 --------+--------+--------+--------
420 -- restrict with null values
421 CREATE TABLE PKTABLE ( ptest1 int, ptest2 int, ptest3 int, ptest4 text, UNIQUE(ptest1, ptest2, ptest3) );
422 CREATE TABLE FKTABLE ( ftest1 int, ftest2 int, ftest3 int, ftest4 int, CONSTRAINT constrname3
423 FOREIGN KEY(ftest1, ftest2, ftest3) REFERENCES PKTABLE (ptest1, ptest2, ptest3));
424 INSERT INTO PKTABLE VALUES (1, 2, 3, 'test1');
425 INSERT INTO PKTABLE VALUES (1, 3, NULL, 'test2');
426 INSERT INTO PKTABLE VALUES (2, NULL, 4, 'test3');
427 INSERT INTO FKTABLE VALUES (1, 2, 3, 1);
428 DELETE FROM PKTABLE WHERE ptest1 = 2;
429 SELECT * FROM PKTABLE;
430 ptest1 | ptest2 | ptest3 | ptest4
431 --------+--------+--------+--------
436 SELECT * FROM FKTABLE;
437 ftest1 | ftest2 | ftest3 | ftest4
438 --------+--------+--------+--------
444 -- cascade update/delete
445 CREATE TABLE PKTABLE ( ptest1 int, ptest2 int, ptest3 int, ptest4 text, PRIMARY KEY(ptest1, ptest2, ptest3) );
446 CREATE TABLE FKTABLE ( ftest1 int, ftest2 int, ftest3 int, ftest4 int, CONSTRAINT constrname3
447 FOREIGN KEY(ftest1, ftest2, ftest3) REFERENCES PKTABLE
448 ON DELETE CASCADE ON UPDATE CASCADE);
449 -- Insert Primary Key values
450 INSERT INTO PKTABLE VALUES (1, 2, 3, 'test1');
451 INSERT INTO PKTABLE VALUES (1, 3, 3, 'test2');
452 INSERT INTO PKTABLE VALUES (2, 3, 4, 'test3');
453 INSERT INTO PKTABLE VALUES (2, 4, 5, 'test4');
454 -- Insert Foreign Key values
455 INSERT INTO FKTABLE VALUES (1, 2, 3, 1);
456 INSERT INTO FKTABLE VALUES (NULL, 2, 3, 2);
457 INSERT INTO FKTABLE VALUES (2, NULL, 3, 3);
458 INSERT INTO FKTABLE VALUES (NULL, 2, 7, 4);
459 INSERT INTO FKTABLE VALUES (NULL, 3, 4, 5);
460 -- Insert a failed values
461 INSERT INTO FKTABLE VALUES (1, 2, 7, 6);
462 ERROR: insert or update on table "fktable" violates foreign key constraint "constrname3"
463 DETAIL: Key (ftest1, ftest2, ftest3)=(1, 2, 7) is not present in table "pktable".
465 SELECT * from FKTABLE;
466 ftest1 | ftest2 | ftest3 | ftest4
467 --------+--------+--------+--------
475 -- Try to update something that will cascade
476 UPDATE PKTABLE set ptest2=5 where ptest2=2;
477 -- Try to update something that should not cascade
478 UPDATE PKTABLE set ptest1=1 WHERE ptest2=3;
479 -- Show PKTABLE and FKTABLE
480 SELECT * from PKTABLE;
481 ptest1 | ptest2 | ptest3 | ptest4
482 --------+--------+--------+--------
489 SELECT * from FKTABLE;
490 ftest1 | ftest2 | ftest3 | ftest4
491 --------+--------+--------+--------
499 -- Try to delete something that should cascade
500 DELETE FROM PKTABLE where ptest1=1 and ptest2=5 and ptest3=3;
501 -- Show PKTABLE and FKTABLE
502 SELECT * from PKTABLE;
503 ptest1 | ptest2 | ptest3 | ptest4
504 --------+--------+--------+--------
510 SELECT * from FKTABLE;
511 ftest1 | ftest2 | ftest3 | ftest4
512 --------+--------+--------+--------
519 -- Try to delete something that should not have a cascade
520 DELETE FROM PKTABLE where ptest1=2;
521 -- Show PKTABLE and FKTABLE
522 SELECT * from PKTABLE;
523 ptest1 | ptest2 | ptest3 | ptest4
524 --------+--------+--------+--------
529 SELECT * from FKTABLE;
530 ftest1 | ftest2 | ftest3 | ftest4
531 --------+--------+--------+--------
540 -- set null update / set default delete
541 CREATE TABLE PKTABLE ( ptest1 int, ptest2 int, ptest3 int, ptest4 text, PRIMARY KEY(ptest1, ptest2, ptest3) );
542 CREATE TABLE FKTABLE ( ftest1 int DEFAULT 0, ftest2 int, ftest3 int, ftest4 int, CONSTRAINT constrname3
543 FOREIGN KEY(ftest1, ftest2, ftest3) REFERENCES PKTABLE
544 ON DELETE SET DEFAULT ON UPDATE SET NULL);
545 -- Insert Primary Key values
546 INSERT INTO PKTABLE VALUES (1, 2, 3, 'test1');
547 INSERT INTO PKTABLE VALUES (1, 3, 3, 'test2');
548 INSERT INTO PKTABLE VALUES (2, 3, 4, 'test3');
549 INSERT INTO PKTABLE VALUES (2, 4, 5, 'test4');
550 -- Insert Foreign Key values
551 INSERT INTO FKTABLE VALUES (1, 2, 3, 1);
552 INSERT INTO FKTABLE VALUES (2, 3, 4, 1);
553 INSERT INTO FKTABLE VALUES (NULL, 2, 3, 2);
554 INSERT INTO FKTABLE VALUES (2, NULL, 3, 3);
555 INSERT INTO FKTABLE VALUES (NULL, 2, 7, 4);
556 INSERT INTO FKTABLE VALUES (NULL, 3, 4, 5);
557 -- Insert a failed values
558 INSERT INTO FKTABLE VALUES (1, 2, 7, 6);
559 ERROR: insert or update on table "fktable" violates foreign key constraint "constrname3"
560 DETAIL: Key (ftest1, ftest2, ftest3)=(1, 2, 7) is not present in table "pktable".
562 SELECT * from FKTABLE;
563 ftest1 | ftest2 | ftest3 | ftest4
564 --------+--------+--------+--------
573 -- Try to update something that will set null
574 UPDATE PKTABLE set ptest2=5 where ptest2=2;
575 -- Try to update something that should not set null
576 UPDATE PKTABLE set ptest2=2 WHERE ptest2=3 and ptest1=1;
577 -- Show PKTABLE and FKTABLE
578 SELECT * from PKTABLE;
579 ptest1 | ptest2 | ptest3 | ptest4
580 --------+--------+--------+--------
587 SELECT * from FKTABLE;
588 ftest1 | ftest2 | ftest3 | ftest4
589 --------+--------+--------+--------
598 -- Try to delete something that should set default
599 DELETE FROM PKTABLE where ptest1=2 and ptest2=3 and ptest3=4;
600 -- Show PKTABLE and FKTABLE
601 SELECT * from PKTABLE;
602 ptest1 | ptest2 | ptest3 | ptest4
603 --------+--------+--------+--------
609 SELECT * from FKTABLE;
610 ftest1 | ftest2 | ftest3 | ftest4
611 --------+--------+--------+--------
620 -- Try to delete something that should not set default
621 DELETE FROM PKTABLE where ptest2=5;
622 -- Show PKTABLE and FKTABLE
623 SELECT * from PKTABLE;
624 ptest1 | ptest2 | ptest3 | ptest4
625 --------+--------+--------+--------
630 SELECT * from FKTABLE;
631 ftest1 | ftest2 | ftest3 | ftest4
632 --------+--------+--------+--------
643 -- set default update / set null delete
644 CREATE TABLE PKTABLE ( ptest1 int, ptest2 int, ptest3 int, ptest4 text, PRIMARY KEY(ptest1, ptest2, ptest3) );
645 CREATE TABLE FKTABLE ( ftest1 int DEFAULT 0, ftest2 int DEFAULT -1, ftest3 int DEFAULT -2, ftest4 int, CONSTRAINT constrname3
646 FOREIGN KEY(ftest1, ftest2, ftest3) REFERENCES PKTABLE
647 ON DELETE SET NULL ON UPDATE SET DEFAULT);
648 -- Insert Primary Key values
649 INSERT INTO PKTABLE VALUES (1, 2, 3, 'test1');
650 INSERT INTO PKTABLE VALUES (1, 3, 3, 'test2');
651 INSERT INTO PKTABLE VALUES (2, 3, 4, 'test3');
652 INSERT INTO PKTABLE VALUES (2, 4, 5, 'test4');
653 INSERT INTO PKTABLE VALUES (2, -1, 5, 'test5');
654 -- Insert Foreign Key values
655 INSERT INTO FKTABLE VALUES (1, 2, 3, 1);
656 INSERT INTO FKTABLE VALUES (2, 3, 4, 1);
657 INSERT INTO FKTABLE VALUES (2, 4, 5, 1);
658 INSERT INTO FKTABLE VALUES (NULL, 2, 3, 2);
659 INSERT INTO FKTABLE VALUES (2, NULL, 3, 3);
660 INSERT INTO FKTABLE VALUES (NULL, 2, 7, 4);
661 INSERT INTO FKTABLE VALUES (NULL, 3, 4, 5);
662 -- Insert a failed values
663 INSERT INTO FKTABLE VALUES (1, 2, 7, 6);
664 ERROR: insert or update on table "fktable" violates foreign key constraint "constrname3"
665 DETAIL: Key (ftest1, ftest2, ftest3)=(1, 2, 7) is not present in table "pktable".
667 SELECT * from FKTABLE;
668 ftest1 | ftest2 | ftest3 | ftest4
669 --------+--------+--------+--------
679 -- Try to update something that will fail
680 UPDATE PKTABLE set ptest2=5 where ptest2=2;
681 ERROR: insert or update on table "fktable" violates foreign key constraint "constrname3"
682 DETAIL: Key (ftest1, ftest2, ftest3)=(0, -1, -2) is not present in table "pktable".
683 -- Try to update something that will set default
684 UPDATE PKTABLE set ptest1=0, ptest2=-1, ptest3=-2 where ptest2=2;
685 UPDATE PKTABLE set ptest2=10 where ptest2=4;
686 -- Try to update something that should not set default
687 UPDATE PKTABLE set ptest2=2 WHERE ptest2=3 and ptest1=1;
688 -- Show PKTABLE and FKTABLE
689 SELECT * from PKTABLE;
690 ptest1 | ptest2 | ptest3 | ptest4
691 --------+--------+--------+--------
699 SELECT * from FKTABLE;
700 ftest1 | ftest2 | ftest3 | ftest4
701 --------+--------+--------+--------
711 -- Try to delete something that should set null
712 DELETE FROM PKTABLE where ptest1=2 and ptest2=3 and ptest3=4;
713 -- Show PKTABLE and FKTABLE
714 SELECT * from PKTABLE;
715 ptest1 | ptest2 | ptest3 | ptest4
716 --------+--------+--------+--------
723 SELECT * from FKTABLE;
724 ftest1 | ftest2 | ftest3 | ftest4
725 --------+--------+--------+--------
735 -- Try to delete something that should not set null
736 DELETE FROM PKTABLE where ptest2=-1 and ptest3=5;
737 -- Show PKTABLE and FKTABLE
738 SELECT * from PKTABLE;
739 ptest1 | ptest2 | ptest3 | ptest4
740 --------+--------+--------+--------
746 SELECT * from FKTABLE;
747 ftest1 | ftest2 | ftest3 | ftest4
748 --------+--------+--------+--------
760 CREATE TABLE PKTABLE (ptest1 int PRIMARY KEY);
761 CREATE TABLE FKTABLE_FAIL1 ( ftest1 int, CONSTRAINT fkfail1 FOREIGN KEY (ftest2) REFERENCES PKTABLE);
762 ERROR: column "ftest2" referenced in foreign key constraint does not exist
763 CREATE TABLE FKTABLE_FAIL2 ( ftest1 int, CONSTRAINT fkfail1 FOREIGN KEY (ftest1) REFERENCES PKTABLE(ptest2));
764 ERROR: column "ptest2" referenced in foreign key constraint does not exist
765 DROP TABLE FKTABLE_FAIL1;
766 ERROR: table "fktable_fail1" does not exist
767 DROP TABLE FKTABLE_FAIL2;
768 ERROR: table "fktable_fail2" does not exist
770 -- Test for referencing column number smaller than referenced constraint
771 CREATE TABLE PKTABLE (ptest1 int, ptest2 int, UNIQUE(ptest1, ptest2));
772 CREATE TABLE FKTABLE_FAIL1 (ftest1 int REFERENCES pktable(ptest1));
773 ERROR: there is no unique constraint matching given keys for referenced table "pktable"
774 DROP TABLE FKTABLE_FAIL1;
775 ERROR: table "fktable_fail1" does not exist
778 -- Tests for mismatched types
780 -- Basic one column, two table setup
781 CREATE TABLE PKTABLE (ptest1 int PRIMARY KEY);
782 INSERT INTO PKTABLE VALUES(42);
783 -- This next should fail, because int=inet does not exist
784 CREATE TABLE FKTABLE (ftest1 inet REFERENCES pktable);
785 ERROR: foreign key constraint "fktable_ftest1_fkey" cannot be implemented
786 DETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: inet and integer.
787 -- This should also fail for the same reason, but here we
788 -- give the column name
789 CREATE TABLE FKTABLE (ftest1 inet REFERENCES pktable(ptest1));
790 ERROR: foreign key constraint "fktable_ftest1_fkey" cannot be implemented
791 DETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: inet and integer.
792 -- This should succeed, even though they are different types,
793 -- because int=int8 exists and is a member of the integer opfamily
794 CREATE TABLE FKTABLE (ftest1 int8 REFERENCES pktable);
795 -- Check it actually works
796 INSERT INTO FKTABLE VALUES(42); -- should succeed
797 INSERT INTO FKTABLE VALUES(43); -- should fail
798 ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"
799 DETAIL: Key (ftest1)=(43) is not present in table "pktable".
800 UPDATE FKTABLE SET ftest1 = ftest1; -- should succeed
801 UPDATE FKTABLE SET ftest1 = ftest1 + 1; -- should fail
802 ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"
803 DETAIL: Key (ftest1)=(43) is not present in table "pktable".
805 -- This should fail, because we'd have to cast numeric to int which is
806 -- not an implicit coercion (or use numeric=numeric, but that's not part
807 -- of the integer opfamily)
808 CREATE TABLE FKTABLE (ftest1 numeric REFERENCES pktable);
809 ERROR: foreign key constraint "fktable_ftest1_fkey" cannot be implemented
810 DETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: numeric and integer.
812 -- On the other hand, this should work because int implicitly promotes to
813 -- numeric, and we allow promotion on the FK side
814 CREATE TABLE PKTABLE (ptest1 numeric PRIMARY KEY);
815 INSERT INTO PKTABLE VALUES(42);
816 CREATE TABLE FKTABLE (ftest1 int REFERENCES pktable);
817 -- Check it actually works
818 INSERT INTO FKTABLE VALUES(42); -- should succeed
819 INSERT INTO FKTABLE VALUES(43); -- should fail
820 ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"
821 DETAIL: Key (ftest1)=(43) is not present in table "pktable".
822 UPDATE FKTABLE SET ftest1 = ftest1; -- should succeed
823 UPDATE FKTABLE SET ftest1 = ftest1 + 1; -- should fail
824 ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"
825 DETAIL: Key (ftest1)=(43) is not present in table "pktable".
828 -- Two columns, two tables
829 CREATE TABLE PKTABLE (ptest1 int, ptest2 inet, PRIMARY KEY(ptest1, ptest2));
830 -- This should fail, because we just chose really odd types
831 CREATE TABLE FKTABLE (ftest1 cidr, ftest2 timestamp, FOREIGN KEY(ftest1, ftest2) REFERENCES pktable);
832 ERROR: foreign key constraint "fktable_ftest1_ftest2_fkey" cannot be implemented
833 DETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: cidr and integer.
834 -- Again, so should this...
835 CREATE TABLE FKTABLE (ftest1 cidr, ftest2 timestamp, FOREIGN KEY(ftest1, ftest2) REFERENCES pktable(ptest1, ptest2));
836 ERROR: foreign key constraint "fktable_ftest1_ftest2_fkey" cannot be implemented
837 DETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: cidr and integer.
838 -- This fails because we mixed up the column ordering
839 CREATE TABLE FKTABLE (ftest1 int, ftest2 inet, FOREIGN KEY(ftest2, ftest1) REFERENCES pktable);
840 ERROR: foreign key constraint "fktable_ftest2_ftest1_fkey" cannot be implemented
841 DETAIL: Key columns "ftest2" and "ptest1" are of incompatible types: inet and integer.
843 CREATE TABLE FKTABLE (ftest1 int, ftest2 inet, FOREIGN KEY(ftest2, ftest1) REFERENCES pktable(ptest1, ptest2));
844 ERROR: foreign key constraint "fktable_ftest2_ftest1_fkey" cannot be implemented
845 DETAIL: Key columns "ftest2" and "ptest1" are of incompatible types: inet and integer.
847 CREATE TABLE FKTABLE (ftest1 int, ftest2 inet, FOREIGN KEY(ftest1, ftest2) REFERENCES pktable(ptest2, ptest1));
848 ERROR: foreign key constraint "fktable_ftest1_ftest2_fkey" cannot be implemented
849 DETAIL: Key columns "ftest1" and "ptest2" are of incompatible types: integer and inet.
851 CREATE TABLE FKTABLE (ftest1 int, ftest2 inet, FOREIGN KEY(ftest2, ftest1) REFERENCES pktable(ptest2, ptest1));
854 CREATE TABLE FKTABLE (ftest1 int, ftest2 inet, FOREIGN KEY(ftest1, ftest2) REFERENCES pktable(ptest1, ptest2));
857 -- Two columns, same table
858 -- Make sure this still works...
859 CREATE TABLE PKTABLE (ptest1 int, ptest2 inet, ptest3 int, ptest4 inet, PRIMARY KEY(ptest1, ptest2), FOREIGN KEY(ptest3,
860 ptest4) REFERENCES pktable(ptest1, ptest2));
863 CREATE TABLE PKTABLE (ptest1 int, ptest2 inet, ptest3 int, ptest4 inet, PRIMARY KEY(ptest1, ptest2), FOREIGN KEY(ptest3,
864 ptest4) REFERENCES pktable);
866 -- This shouldn't (mixed up columns)
867 CREATE TABLE PKTABLE (ptest1 int, ptest2 inet, ptest3 int, ptest4 inet, PRIMARY KEY(ptest1, ptest2), FOREIGN KEY(ptest3,
868 ptest4) REFERENCES pktable(ptest2, ptest1));
869 ERROR: foreign key constraint "pktable_ptest3_ptest4_fkey" cannot be implemented
870 DETAIL: Key columns "ptest3" and "ptest2" are of incompatible types: integer and inet.
871 -- Nor should this... (same reason, we have 4,3 referencing 1,2 which mismatches types
872 CREATE TABLE PKTABLE (ptest1 int, ptest2 inet, ptest3 int, ptest4 inet, PRIMARY KEY(ptest1, ptest2), FOREIGN KEY(ptest4,
873 ptest3) REFERENCES pktable(ptest1, ptest2));
874 ERROR: foreign key constraint "pktable_ptest4_ptest3_fkey" cannot be implemented
875 DETAIL: Key columns "ptest4" and "ptest1" are of incompatible types: inet and integer.
876 -- Not this one either... Same as the last one except we didn't defined the columns being referenced.
877 CREATE TABLE PKTABLE (ptest1 int, ptest2 inet, ptest3 int, ptest4 inet, PRIMARY KEY(ptest1, ptest2), FOREIGN KEY(ptest4,
878 ptest3) REFERENCES pktable);
879 ERROR: foreign key constraint "pktable_ptest4_ptest3_fkey" cannot be implemented
880 DETAIL: Key columns "ptest4" and "ptest1" are of incompatible types: inet and integer.
882 -- Now some cases with inheritance
883 -- Basic 2 table case: 1 column of matching types.
884 create table pktable_base (base1 int not null);
885 create table pktable (ptest1 int, primary key(base1), unique(base1, ptest1)) inherits (pktable_base);
886 create table fktable (ftest1 int references pktable(base1));
887 -- now some ins, upd, del
888 insert into pktable(base1) values (1);
889 insert into pktable(base1) values (2);
890 -- let's insert a non-existent fktable value
891 insert into fktable(ftest1) values (3);
892 ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_fkey"
893 DETAIL: Key (ftest1)=(3) is not present in table "pktable".
894 -- let's make a valid row for that
895 insert into pktable(base1) values (3);
896 insert into fktable(ftest1) values (3);
897 -- let's try removing a row that should fail from pktable
898 delete from pktable where base1>2;
899 ERROR: update or delete on table "pktable" violates foreign key constraint "fktable_ftest1_fkey" on table "fktable"
900 DETAIL: Key (base1)=(3) is still referenced from table "fktable".
901 -- okay, let's try updating all of the base1 values to *4
902 -- which should fail.
903 update pktable set base1=base1*4;
904 ERROR: update or delete on table "pktable" violates foreign key constraint "fktable_ftest1_fkey" on table "fktable"
905 DETAIL: Key (base1)=(3) is still referenced from table "fktable".
906 -- okay, let's try an update that should work.
907 update pktable set base1=base1*4 where base1<3;
908 -- and a delete that should work
909 delete from pktable where base1>3;
913 -- Now 2 columns 2 tables, matching types
914 create table fktable (ftest1 int, ftest2 int, foreign key(ftest1, ftest2) references pktable(base1, ptest1));
915 -- now some ins, upd, del
916 insert into pktable(base1, ptest1) values (1, 1);
917 insert into pktable(base1, ptest1) values (2, 2);
918 -- let's insert a non-existent fktable value
919 insert into fktable(ftest1, ftest2) values (3, 1);
920 ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_ftest1_ftest2_fkey"
921 DETAIL: Key (ftest1, ftest2)=(3, 1) is not present in table "pktable".
922 -- let's make a valid row for that
923 insert into pktable(base1,ptest1) values (3, 1);
924 insert into fktable(ftest1, ftest2) values (3, 1);
925 -- let's try removing a row that should fail from pktable
926 delete from pktable where base1>2;
927 ERROR: update or delete on table "pktable" violates foreign key constraint "fktable_ftest1_ftest2_fkey" on table "fktable"
928 DETAIL: Key (base1, ptest1)=(3, 1) is still referenced from table "fktable".
929 -- okay, let's try updating all of the base1 values to *4
930 -- which should fail.
931 update pktable set base1=base1*4;
932 ERROR: update or delete on table "pktable" violates foreign key constraint "fktable_ftest1_ftest2_fkey" on table "fktable"
933 DETAIL: Key (base1, ptest1)=(3, 1) is still referenced from table "fktable".
934 -- okay, let's try an update that should work.
935 update pktable set base1=base1*4 where base1<3;
936 -- and a delete that should work
937 delete from pktable where base1>3;
941 drop table pktable_base;
942 -- Now we'll do one all in 1 table with 2 columns of matching types
943 create table pktable_base(base1 int not null, base2 int);
944 create table pktable(ptest1 int, ptest2 int, primary key(base1, ptest1), foreign key(base2, ptest2) references
945 pktable(base1, ptest1)) inherits (pktable_base);
946 insert into pktable (base1, ptest1, base2, ptest2) values (1, 1, 1, 1);
947 insert into pktable (base1, ptest1, base2, ptest2) values (2, 1, 1, 1);
948 insert into pktable (base1, ptest1, base2, ptest2) values (2, 2, 2, 1);
949 insert into pktable (base1, ptest1, base2, ptest2) values (1, 3, 2, 2);
950 -- fails (3,2) isn't in base1, ptest1
951 insert into pktable (base1, ptest1, base2, ptest2) values (2, 3, 3, 2);
952 ERROR: insert or update on table "pktable" violates foreign key constraint "pktable_base2_ptest2_fkey"
953 DETAIL: Key (base2, ptest2)=(3, 2) is not present in table "pktable".
954 -- fails (2,2) is being referenced
955 delete from pktable where base1=2;
956 ERROR: update or delete on table "pktable" violates foreign key constraint "pktable_base2_ptest2_fkey" on table "pktable"
957 DETAIL: Key (base1, ptest1)=(2, 2) is still referenced from table "pktable".
958 -- fails (1,1) is being referenced (twice)
959 update pktable set base1=3 where base1=1;
960 ERROR: update or delete on table "pktable" violates foreign key constraint "pktable_base2_ptest2_fkey" on table "pktable"
961 DETAIL: Key (base1, ptest1)=(1, 1) is still referenced from table "pktable".
962 -- this sequence of two deletes will work, since after the first there will be no (2,*) references
963 delete from pktable where base2=2;
964 delete from pktable where base1=2;
966 drop table pktable_base;
967 -- 2 columns (2 tables), mismatched types
968 create table pktable_base(base1 int not null);
969 create table pktable(ptest1 inet, primary key(base1, ptest1)) inherits (pktable_base);
970 -- just generally bad types (with and without column references on the referenced table)
971 create table fktable(ftest1 cidr, ftest2 int[], foreign key (ftest1, ftest2) references pktable);
972 ERROR: foreign key constraint "fktable_ftest1_ftest2_fkey" cannot be implemented
973 DETAIL: Key columns "ftest1" and "base1" are of incompatible types: cidr and integer.
974 create table fktable(ftest1 cidr, ftest2 int[], foreign key (ftest1, ftest2) references pktable(base1, ptest1));
975 ERROR: foreign key constraint "fktable_ftest1_ftest2_fkey" cannot be implemented
976 DETAIL: Key columns "ftest1" and "base1" are of incompatible types: cidr and integer.
977 -- let's mix up which columns reference which
978 create table fktable(ftest1 int, ftest2 inet, foreign key(ftest2, ftest1) references pktable);
979 ERROR: foreign key constraint "fktable_ftest2_ftest1_fkey" cannot be implemented
980 DETAIL: Key columns "ftest2" and "base1" are of incompatible types: inet and integer.
981 create table fktable(ftest1 int, ftest2 inet, foreign key(ftest2, ftest1) references pktable(base1, ptest1));
982 ERROR: foreign key constraint "fktable_ftest2_ftest1_fkey" cannot be implemented
983 DETAIL: Key columns "ftest2" and "base1" are of incompatible types: inet and integer.
984 create table fktable(ftest1 int, ftest2 inet, foreign key(ftest1, ftest2) references pktable(ptest1, base1));
985 ERROR: foreign key constraint "fktable_ftest1_ftest2_fkey" cannot be implemented
986 DETAIL: Key columns "ftest1" and "ptest1" are of incompatible types: integer and inet.
988 drop table pktable_base;
989 -- 2 columns (1 table), mismatched types
990 create table pktable_base(base1 int not null, base2 int);
991 create table pktable(ptest1 inet, ptest2 inet[], primary key(base1, ptest1), foreign key(base2, ptest2) references
992 pktable(base1, ptest1)) inherits (pktable_base);
993 ERROR: foreign key constraint "pktable_base2_ptest2_fkey" cannot be implemented
994 DETAIL: Key columns "ptest2" and "ptest1" are of incompatible types: inet[] and inet.
995 create table pktable(ptest1 inet, ptest2 inet, primary key(base1, ptest1), foreign key(base2, ptest2) references
996 pktable(ptest1, base1)) inherits (pktable_base);
997 ERROR: foreign key constraint "pktable_base2_ptest2_fkey" cannot be implemented
998 DETAIL: Key columns "base2" and "ptest1" are of incompatible types: integer and inet.
999 create table pktable(ptest1 inet, ptest2 inet, primary key(base1, ptest1), foreign key(ptest2, base2) references
1000 pktable(base1, ptest1)) inherits (pktable_base);
1001 ERROR: foreign key constraint "pktable_ptest2_base2_fkey" cannot be implemented
1002 DETAIL: Key columns "ptest2" and "base1" are of incompatible types: inet and integer.
1003 create table pktable(ptest1 inet, ptest2 inet, primary key(base1, ptest1), foreign key(ptest2, base2) references
1004 pktable(base1, ptest1)) inherits (pktable_base);
1005 ERROR: foreign key constraint "pktable_ptest2_base2_fkey" cannot be implemented
1006 DETAIL: Key columns "ptest2" and "base1" are of incompatible types: inet and integer.
1008 ERROR: table "pktable" does not exist
1009 drop table pktable_base;
1011 -- Deferrable constraints
1013 -- deferrable, explicitly deferred
1014 CREATE TABLE pktable (
1015 id INT4 PRIMARY KEY,
1018 CREATE TABLE fktable (
1019 id INT4 PRIMARY KEY,
1020 fk INT4 REFERENCES pktable DEFERRABLE
1022 -- default to immediate: should fail
1023 INSERT INTO fktable VALUES (5, 10);
1024 ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey"
1025 DETAIL: Key (fk)=(10) is not present in table "pktable".
1026 -- explicitly defer the constraint
1028 SET CONSTRAINTS ALL DEFERRED;
1029 INSERT INTO fktable VALUES (10, 15);
1030 INSERT INTO pktable VALUES (15, 0); -- make the FK insert valid
1032 DROP TABLE fktable, pktable;
1033 -- deferrable, initially deferred
1034 CREATE TABLE pktable (
1035 id INT4 PRIMARY KEY,
1038 CREATE TABLE fktable (
1039 id INT4 PRIMARY KEY,
1040 fk INT4 REFERENCES pktable DEFERRABLE INITIALLY DEFERRED
1042 -- default to deferred, should succeed
1044 INSERT INTO fktable VALUES (100, 200);
1045 INSERT INTO pktable VALUES (200, 500); -- make the FK insert valid
1047 -- default to deferred, explicitly make immediate
1049 SET CONSTRAINTS ALL IMMEDIATE;
1051 INSERT INTO fktable VALUES (500, 1000);
1052 ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey"
1053 DETAIL: Key (fk)=(1000) is not present in table "pktable".
1055 DROP TABLE fktable, pktable;
1056 -- tricky behavior: according to SQL99, if a deferred constraint is set
1057 -- to 'immediate' mode, it should be checked for validity *immediately*,
1058 -- not when the current transaction commits (i.e. the mode change applies
1060 CREATE TABLE pktable (
1061 id INT4 PRIMARY KEY,
1064 CREATE TABLE fktable (
1065 id INT4 PRIMARY KEY,
1066 fk INT4 REFERENCES pktable DEFERRABLE
1069 SET CONSTRAINTS ALL DEFERRED;
1070 -- should succeed, for now
1071 INSERT INTO fktable VALUES (1000, 2000);
1072 -- should cause transaction abort, due to preceding error
1073 SET CONSTRAINTS ALL IMMEDIATE;
1074 ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey"
1075 DETAIL: Key (fk)=(2000) is not present in table "pktable".
1076 INSERT INTO pktable VALUES (2000, 3); -- too late
1077 ERROR: current transaction is aborted, commands ignored until end of transaction block
1079 DROP TABLE fktable, pktable;
1080 -- deferrable, initially deferred
1081 CREATE TABLE pktable (
1082 id INT4 PRIMARY KEY,
1085 CREATE TABLE fktable (
1086 id INT4 PRIMARY KEY,
1087 fk INT4 REFERENCES pktable DEFERRABLE INITIALLY DEFERRED
1091 INSERT INTO fktable VALUES (100, 200);
1092 -- error here on commit
1094 ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey"
1095 DETAIL: Key (fk)=(200) is not present in table "pktable".
1096 DROP TABLE pktable, fktable;
1097 -- test notice about expensive referential integrity checks,
1098 -- where the index cannot be used because of type incompatibilities.
1099 CREATE TEMP TABLE pktable (
1100 id1 INT4 PRIMARY KEY,
1101 id2 VARCHAR(4) UNIQUE,
1103 UNIQUE(id1, id2, id3)
1105 CREATE TEMP TABLE fktable (
1106 x1 INT4 REFERENCES pktable(id1),
1107 x2 VARCHAR(4) REFERENCES pktable(id2),
1108 x3 REAL REFERENCES pktable(id3),
1112 -- check individual constraints with alter table.
1114 -- varchar does not promote to real
1115 ALTER TABLE fktable ADD CONSTRAINT fk_2_3
1116 FOREIGN KEY (x2) REFERENCES pktable(id3);
1117 ERROR: foreign key constraint "fk_2_3" cannot be implemented
1118 DETAIL: Key columns "x2" and "id3" are of incompatible types: character varying and real.
1120 ALTER TABLE fktable ADD CONSTRAINT fk_2_1
1121 FOREIGN KEY (x2) REFERENCES pktable(id1);
1122 ERROR: foreign key constraint "fk_2_1" cannot be implemented
1123 DETAIL: Key columns "x2" and "id1" are of incompatible types: character varying and integer.
1124 -- real does not promote to int4
1125 ALTER TABLE fktable ADD CONSTRAINT fk_3_1
1126 FOREIGN KEY (x3) REFERENCES pktable(id1);
1127 ERROR: foreign key constraint "fk_3_1" cannot be implemented
1128 DETAIL: Key columns "x3" and "id1" are of incompatible types: real and integer.
1129 -- int4 does not promote to text
1130 ALTER TABLE fktable ADD CONSTRAINT fk_1_2
1131 FOREIGN KEY (x1) REFERENCES pktable(id2);
1132 ERROR: foreign key constraint "fk_1_2" cannot be implemented
1133 DETAIL: Key columns "x1" and "id2" are of incompatible types: integer and character varying.
1135 -- int4 promotes to real
1136 ALTER TABLE fktable ADD CONSTRAINT fk_1_3
1137 FOREIGN KEY (x1) REFERENCES pktable(id3);
1138 -- text is compatible with varchar
1139 ALTER TABLE fktable ADD CONSTRAINT fk_4_2
1140 FOREIGN KEY (x4) REFERENCES pktable(id2);
1141 -- int2 is part of integer opfamily as of 8.0
1142 ALTER TABLE fktable ADD CONSTRAINT fk_5_1
1143 FOREIGN KEY (x5) REFERENCES pktable(id1);
1144 -- check multikey cases, especially out-of-order column lists
1145 -- these should work
1146 ALTER TABLE fktable ADD CONSTRAINT fk_123_123
1147 FOREIGN KEY (x1,x2,x3) REFERENCES pktable(id1,id2,id3);
1148 ALTER TABLE fktable ADD CONSTRAINT fk_213_213
1149 FOREIGN KEY (x2,x1,x3) REFERENCES pktable(id2,id1,id3);
1150 ALTER TABLE fktable ADD CONSTRAINT fk_253_213
1151 FOREIGN KEY (x2,x5,x3) REFERENCES pktable(id2,id1,id3);
1152 -- these should fail
1153 ALTER TABLE fktable ADD CONSTRAINT fk_123_231
1154 FOREIGN KEY (x1,x2,x3) REFERENCES pktable(id2,id3,id1);
1155 ERROR: foreign key constraint "fk_123_231" cannot be implemented
1156 DETAIL: Key columns "x1" and "id2" are of incompatible types: integer and character varying.
1157 ALTER TABLE fktable ADD CONSTRAINT fk_241_132
1158 FOREIGN KEY (x2,x4,x1) REFERENCES pktable(id1,id3,id2);
1159 ERROR: foreign key constraint "fk_241_132" cannot be implemented
1160 DETAIL: Key columns "x2" and "id1" are of incompatible types: character varying and integer.
1161 DROP TABLE pktable, fktable;
1162 -- test a tricky case: we can elide firing the FK check trigger during
1163 -- an UPDATE if the UPDATE did not change the foreign key
1164 -- field. However, we can't do this if our transaction was the one that
1165 -- created the updated row and the trigger is deferred, since our UPDATE
1166 -- will have invalidated the original newly-inserted tuple, and therefore
1167 -- cause the on-INSERT RI trigger not to be fired.
1168 CREATE TEMP TABLE pktable (
1172 CREATE TEMP TABLE fktable (
1174 fk int references pktable deferrable initially deferred
1176 INSERT INTO pktable VALUES (5, 10);
1178 -- doesn't match PK, but no error yet
1179 INSERT INTO fktable VALUES (0, 20);
1181 UPDATE fktable SET id = id + 1;
1182 -- should catch error from initial INSERT
1184 ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey"
1185 DETAIL: Key (fk)=(20) is not present in table "pktable".
1186 -- check same case when insert is in a different subtransaction than update
1188 -- doesn't match PK, but no error yet
1189 INSERT INTO fktable VALUES (0, 20);
1190 -- UPDATE will be in a subxact
1193 UPDATE fktable SET id = id + 1;
1194 -- should catch error from initial INSERT
1196 ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey"
1197 DETAIL: Key (fk)=(20) is not present in table "pktable".
1199 -- INSERT will be in a subxact
1201 -- doesn't match PK, but no error yet
1202 INSERT INTO fktable VALUES (0, 20);
1203 RELEASE SAVEPOINT savept1;
1205 UPDATE fktable SET id = id + 1;
1206 -- should catch error from initial INSERT
1208 ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey"
1209 DETAIL: Key (fk)=(20) is not present in table "pktable".
1211 -- doesn't match PK, but no error yet
1212 INSERT INTO fktable VALUES (0, 20);
1213 -- UPDATE will be in a subxact
1216 UPDATE fktable SET id = id + 1;
1217 -- Roll back the UPDATE
1218 ROLLBACK TO savept1;
1219 -- should catch error from initial INSERT
1221 ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey"
1222 DETAIL: Key (fk)=(20) is not present in table "pktable".
1224 -- check ALTER CONSTRAINT
1226 INSERT INTO fktable VALUES (1, 5);
1227 ALTER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey DEFERRABLE INITIALLY IMMEDIATE;
1229 -- doesn't match FK, should throw error now
1230 UPDATE pktable SET id = 10 WHERE id = 5;
1231 ERROR: update or delete on table "pktable" violates foreign key constraint "fktable_fk_fkey" on table "fktable"
1232 DETAIL: Key (id)=(5) is still referenced from table "fktable".
1235 -- doesn't match PK, should throw error now
1236 INSERT INTO fktable VALUES (0, 20);
1237 ERROR: insert or update on table "fktable" violates foreign key constraint "fktable_fk_fkey"
1238 DETAIL: Key (fk)=(20) is not present in table "pktable".
1240 -- try additional syntax
1241 ALTER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey NOT DEFERRABLE;
1243 ALTER TABLE fktable ALTER CONSTRAINT fktable_fk_fkey NOT DEFERRABLE INITIALLY DEFERRED;
1244 ERROR: constraint declared INITIALLY DEFERRED must be DEFERRABLE
1245 LINE 1: ...e ALTER CONSTRAINT fktable_fk_fkey NOT DEFERRABLE INITIALLY ...
1247 -- test order of firing of FK triggers when several RI-induced changes need to
1248 -- be made to the same row. This was broken by subtransaction-related
1250 CREATE TEMP TABLE users (
1252 name VARCHAR NOT NULL
1254 INSERT INTO users VALUES (1, 'Jozko');
1255 INSERT INTO users VALUES (2, 'Ferko');
1256 INSERT INTO users VALUES (3, 'Samko');
1257 CREATE TEMP TABLE tasks (
1259 owner INT REFERENCES users ON UPDATE CASCADE ON DELETE SET NULL,
1260 worker INT REFERENCES users ON UPDATE CASCADE ON DELETE SET NULL,
1261 checked_by INT REFERENCES users ON UPDATE CASCADE ON DELETE SET NULL
1263 INSERT INTO tasks VALUES (1,1,NULL,NULL);
1264 INSERT INTO tasks VALUES (2,2,2,NULL);
1265 INSERT INTO tasks VALUES (3,3,3,3);
1266 SELECT * FROM tasks;
1267 id | owner | worker | checked_by
1268 ----+-------+--------+------------
1274 UPDATE users SET id = 4 WHERE id = 3;
1275 SELECT * FROM tasks;
1276 id | owner | worker | checked_by
1277 ----+-------+--------+------------
1283 DELETE FROM users WHERE id = 4;
1284 SELECT * FROM tasks;
1285 id | owner | worker | checked_by
1286 ----+-------+--------+------------
1292 -- could fail with only 2 changes to make, if row was already updated
1294 UPDATE tasks set id=id WHERE id=2;
1295 SELECT * FROM tasks;
1296 id | owner | worker | checked_by
1297 ----+-------+--------+------------
1303 DELETE FROM users WHERE id = 2;
1304 SELECT * FROM tasks;
1305 id | owner | worker | checked_by
1306 ----+-------+--------+------------
1314 -- Test self-referential FK with CASCADE (bug #6268)
1316 create temp table selfref (
1319 foreign key (b) references selfref (a)
1320 on update cascade on delete cascade
1322 insert into selfref (a, b)
1327 update selfref set a = 123 where a = 0;
1328 select a, b from selfref;
1335 update selfref set a = 456 where a = 123;
1336 select a, b from selfref;
1345 -- Test that SET DEFAULT actions recognize updates to default values
1347 create temp table defp (f1 int primary key);
1348 create temp table defc (f1 int default 0
1349 references defp on delete set default);
1350 insert into defp values (0), (1), (2);
1351 insert into defc values (2);
1358 delete from defp where f1 = 2;
1365 delete from defp where f1 = 0; -- fail
1366 ERROR: update or delete on table "defp" violates foreign key constraint "defc_f1_fkey" on table "defc"
1367 DETAIL: Key (f1)=(0) is still referenced from table "defc".
1368 alter table defc alter column f1 set default 1;
1369 delete from defp where f1 = 0;
1376 delete from defp where f1 = 1; -- fail
1377 ERROR: update or delete on table "defp" violates foreign key constraint "defc_f1_fkey" on table "defc"
1378 DETAIL: Key (f1)=(1) is still referenced from table "defc".
1380 -- Test the difference between NO ACTION and RESTRICT
1382 create temp table pp (f1 int primary key);
1383 create temp table cc (f1 int references pp on update no action on delete no action);
1384 insert into pp values(12);
1385 insert into pp values(11);
1386 update pp set f1=f1+1;
1387 insert into cc values(13);
1388 update pp set f1=f1+1;
1389 update pp set f1=f1+1; -- fail
1390 ERROR: update or delete on table "pp" violates foreign key constraint "cc_f1_fkey" on table "cc"
1391 DETAIL: Key (f1)=(13) is still referenced from table "cc".
1392 delete from pp where f1 = 13; -- fail
1393 ERROR: update or delete on table "pp" violates foreign key constraint "cc_f1_fkey" on table "cc"
1394 DETAIL: Key (f1)=(13) is still referenced from table "cc".
1396 create temp table pp (f1 int primary key);
1397 create temp table cc (f1 int references pp on update restrict on delete restrict);
1398 insert into pp values(12);
1399 insert into pp values(11);
1400 update pp set f1=f1+1;
1401 insert into cc values(13);
1402 update pp set f1=f1+1; -- fail
1403 ERROR: update or delete on table "pp" violates foreign key constraint "cc_f1_fkey" on table "cc"
1404 DETAIL: Key (f1)=(13) is still referenced from table "cc".
1405 delete from pp where f1 = 13; -- fail
1406 ERROR: update or delete on table "pp" violates foreign key constraint "cc_f1_fkey" on table "cc"
1407 DETAIL: Key (f1)=(13) is still referenced from table "cc".
1410 -- Test interaction of foreign-key optimization with rules (bug #14219)
1412 create temp table t1 (a integer primary key, b text);
1413 create temp table t2 (a integer primary key, b integer references t1);
1414 create rule r1 as on delete to t1 do delete from t2 where t2.b = old.a;
1415 explain (costs off) delete from t1 where a = 1;
1417 --------------------------------------------
1420 -> Index Scan using t1_pkey on t1
1426 -> Index Scan using t1_pkey on t1
1430 delete from t1 where a = 1;
1431 -- Test a primary key with attributes located in later attnum positions
1432 -- compared to the fk attributes.
1433 create table pktable2 (a int, b int, c int, d int, e int, primary key (d, e));
1434 create table fktable2 (d int, e int, foreign key (d, e) references pktable2);
1435 insert into pktable2 values (1, 2, 3, 4, 5);
1436 insert into fktable2 values (4, 5);
1437 delete from pktable2;
1438 ERROR: update or delete on table "pktable2" violates foreign key constraint "fktable2_d_e_fkey" on table "fktable2"
1439 DETAIL: Key (d, e)=(4, 5) is still referenced from table "fktable2".
1440 update pktable2 set d = 5;
1441 ERROR: update or delete on table "pktable2" violates foreign key constraint "fktable2_d_e_fkey" on table "fktable2"
1442 DETAIL: Key (d, e)=(4, 5) is still referenced from table "fktable2".
1443 drop table pktable2, fktable2;
1444 -- Test truncation of long foreign key names
1445 create table pktable1 (a int primary key);
1446 create table pktable2 (a int, b int, primary key (a, b));
1447 create table fktable2 (
1450 very_very_long_column_name_to_exceed_63_characters int,
1451 foreign key (very_very_long_column_name_to_exceed_63_characters) references pktable1,
1452 foreign key (a, very_very_long_column_name_to_exceed_63_characters) references pktable2,
1453 foreign key (a, very_very_long_column_name_to_exceed_63_characters) references pktable2
1455 select conname from pg_constraint where conrelid = 'fktable2'::regclass order by conname;
1457 -----------------------------------------------------------------
1458 fktable2_a_very_very_long_column_name_to_exceed_63_charac_fkey1
1459 fktable2_a_very_very_long_column_name_to_exceed_63_charact_fkey
1460 fktable2_very_very_long_column_name_to_exceed_63_character_fkey
1463 drop table pktable1, pktable2, fktable2;
1465 -- Test deferred FK check on a tuple deleted by a rolled-back subtransaction
1467 create table pktable2(f1 int primary key);
1468 create table fktable2(f1 int references pktable2 deferrable initially deferred);
1469 insert into pktable2 values(1);
1471 insert into fktable2 values(1);
1473 delete from fktable2;
1477 insert into fktable2 values(2);
1479 delete from fktable2;
1482 ERROR: insert or update on table "fktable2" violates foreign key constraint "fktable2_f1_fkey"
1483 DETAIL: Key (f1)=(2) is not present in table "pktable2".
1485 -- Test that we prevent dropping FK constraint with pending trigger events
1488 insert into fktable2 values(2);
1489 alter table fktable2 drop constraint fktable2_f1_fkey;
1490 ERROR: cannot ALTER TABLE "fktable2" because it has pending trigger events
1493 delete from pktable2 where f1 = 1;
1494 alter table fktable2 drop constraint fktable2_f1_fkey;
1495 ERROR: cannot ALTER TABLE "pktable2" because it has pending trigger events
1497 drop table pktable2, fktable2;
1499 -- Test keys that "look" different but compare as equal
1501 create table pktable2 (a float8, b float8, primary key (a, b));
1502 create table fktable2 (x float8, y float8, foreign key (x, y) references pktable2 (a, b) on update cascade);
1503 insert into pktable2 values ('-0', '-0');
1504 insert into fktable2 values ('-0', '-0');
1505 select * from pktable2;
1511 select * from fktable2;
1517 update pktable2 set a = '0' where a = '-0';
1518 select * from pktable2;
1524 -- should have updated fktable2.x
1525 select * from fktable2;
1531 drop table pktable2, fktable2;
1533 -- Foreign keys and partitioned tables
1535 -- Creation of a partitioned hierarchy with irregular definitions
1536 CREATE TABLE fk_notpartitioned_pk (fdrop1 int, a int, fdrop2 int, b int,
1537 PRIMARY KEY (a, b));
1538 ALTER TABLE fk_notpartitioned_pk DROP COLUMN fdrop1, DROP COLUMN fdrop2;
1539 CREATE TABLE fk_partitioned_fk (b int, fdrop1 int, a int) PARTITION BY RANGE (a, b);
1540 ALTER TABLE fk_partitioned_fk DROP COLUMN fdrop1;
1541 CREATE TABLE fk_partitioned_fk_1 (fdrop1 int, fdrop2 int, a int, fdrop3 int, b int);
1542 ALTER TABLE fk_partitioned_fk_1 DROP COLUMN fdrop1, DROP COLUMN fdrop2, DROP COLUMN fdrop3;
1543 ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_1 FOR VALUES FROM (0,0) TO (1000,1000);
1544 ALTER TABLE fk_partitioned_fk ADD FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk;
1545 CREATE TABLE fk_partitioned_fk_2 (b int, fdrop1 int, fdrop2 int, a int);
1546 ALTER TABLE fk_partitioned_fk_2 DROP COLUMN fdrop1, DROP COLUMN fdrop2;
1547 ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_2 FOR VALUES FROM (1000,1000) TO (2000,2000);
1548 CREATE TABLE fk_partitioned_fk_3 (fdrop1 int, fdrop2 int, fdrop3 int, fdrop4 int, b int, a int)
1549 PARTITION BY HASH (a);
1550 ALTER TABLE fk_partitioned_fk_3 DROP COLUMN fdrop1, DROP COLUMN fdrop2,
1551 DROP COLUMN fdrop3, DROP COLUMN fdrop4;
1552 CREATE TABLE fk_partitioned_fk_3_0 PARTITION OF fk_partitioned_fk_3 FOR VALUES WITH (MODULUS 5, REMAINDER 0);
1553 CREATE TABLE fk_partitioned_fk_3_1 PARTITION OF fk_partitioned_fk_3 FOR VALUES WITH (MODULUS 5, REMAINDER 1);
1554 ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_3
1555 FOR VALUES FROM (2000,2000) TO (3000,3000);
1556 -- Creating a foreign key with ONLY on a partitioned table referencing
1557 -- a non-partitioned table fails.
1558 ALTER TABLE ONLY fk_partitioned_fk ADD FOREIGN KEY (a, b)
1559 REFERENCES fk_notpartitioned_pk;
1560 ERROR: cannot use ONLY for foreign key on partitioned table "fk_partitioned_fk" referencing relation "fk_notpartitioned_pk"
1561 -- Adding a NOT VALID foreign key on a partitioned table referencing
1562 -- a non-partitioned table fails.
1563 ALTER TABLE fk_partitioned_fk ADD FOREIGN KEY (a, b)
1564 REFERENCES fk_notpartitioned_pk NOT VALID;
1565 ERROR: cannot add NOT VALID foreign key on partitioned table "fk_partitioned_fk" referencing relation "fk_notpartitioned_pk"
1566 DETAIL: This feature is not yet supported on partitioned tables.
1567 -- these inserts, targeting both the partition directly as well as the
1568 -- partitioned table, should all fail
1569 INSERT INTO fk_partitioned_fk (a,b) VALUES (500, 501);
1570 ERROR: insert or update on table "fk_partitioned_fk_1" violates foreign key constraint "fk_partitioned_fk_a_b_fkey"
1571 DETAIL: Key (a, b)=(500, 501) is not present in table "fk_notpartitioned_pk".
1572 INSERT INTO fk_partitioned_fk_1 (a,b) VALUES (500, 501);
1573 ERROR: insert or update on table "fk_partitioned_fk_1" violates foreign key constraint "fk_partitioned_fk_a_b_fkey"
1574 DETAIL: Key (a, b)=(500, 501) is not present in table "fk_notpartitioned_pk".
1575 INSERT INTO fk_partitioned_fk (a,b) VALUES (1500, 1501);
1576 ERROR: insert or update on table "fk_partitioned_fk_2" violates foreign key constraint "fk_partitioned_fk_a_b_fkey"
1577 DETAIL: Key (a, b)=(1500, 1501) is not present in table "fk_notpartitioned_pk".
1578 INSERT INTO fk_partitioned_fk_2 (a,b) VALUES (1500, 1501);
1579 ERROR: insert or update on table "fk_partitioned_fk_2" violates foreign key constraint "fk_partitioned_fk_a_b_fkey"
1580 DETAIL: Key (a, b)=(1500, 1501) is not present in table "fk_notpartitioned_pk".
1581 INSERT INTO fk_partitioned_fk (a,b) VALUES (2500, 2502);
1582 ERROR: insert or update on table "fk_partitioned_fk_3_1" violates foreign key constraint "fk_partitioned_fk_a_b_fkey"
1583 DETAIL: Key (a, b)=(2500, 2502) is not present in table "fk_notpartitioned_pk".
1584 INSERT INTO fk_partitioned_fk_3 (a,b) VALUES (2500, 2502);
1585 ERROR: insert or update on table "fk_partitioned_fk_3_1" violates foreign key constraint "fk_partitioned_fk_a_b_fkey"
1586 DETAIL: Key (a, b)=(2500, 2502) is not present in table "fk_notpartitioned_pk".
1587 INSERT INTO fk_partitioned_fk (a,b) VALUES (2501, 2503);
1588 ERROR: insert or update on table "fk_partitioned_fk_3_0" violates foreign key constraint "fk_partitioned_fk_a_b_fkey"
1589 DETAIL: Key (a, b)=(2501, 2503) is not present in table "fk_notpartitioned_pk".
1590 INSERT INTO fk_partitioned_fk_3 (a,b) VALUES (2501, 2503);
1591 ERROR: insert or update on table "fk_partitioned_fk_3_0" violates foreign key constraint "fk_partitioned_fk_a_b_fkey"
1592 DETAIL: Key (a, b)=(2501, 2503) is not present in table "fk_notpartitioned_pk".
1593 -- but if we insert the values that make them valid, then they work
1594 INSERT INTO fk_notpartitioned_pk VALUES (500, 501), (1500, 1501),
1595 (2500, 2502), (2501, 2503);
1596 INSERT INTO fk_partitioned_fk (a,b) VALUES (500, 501);
1597 INSERT INTO fk_partitioned_fk (a,b) VALUES (1500, 1501);
1598 INSERT INTO fk_partitioned_fk (a,b) VALUES (2500, 2502);
1599 INSERT INTO fk_partitioned_fk (a,b) VALUES (2501, 2503);
1600 -- this update fails because there is no referenced row
1601 UPDATE fk_partitioned_fk SET a = a + 1 WHERE a = 2501;
1602 ERROR: insert or update on table "fk_partitioned_fk_3_1" violates foreign key constraint "fk_partitioned_fk_a_b_fkey"
1603 DETAIL: Key (a, b)=(2502, 2503) is not present in table "fk_notpartitioned_pk".
1604 -- but we can fix it thusly:
1605 INSERT INTO fk_notpartitioned_pk (a,b) VALUES (2502, 2503);
1606 UPDATE fk_partitioned_fk SET a = a + 1 WHERE a = 2501;
1607 -- these updates would leave lingering rows in the referencing table; disallow
1608 UPDATE fk_notpartitioned_pk SET b = 502 WHERE a = 500;
1609 ERROR: update or delete on table "fk_notpartitioned_pk" violates foreign key constraint "fk_partitioned_fk_a_b_fkey" on table "fk_partitioned_fk"
1610 DETAIL: Key (a, b)=(500, 501) is still referenced from table "fk_partitioned_fk".
1611 UPDATE fk_notpartitioned_pk SET b = 1502 WHERE a = 1500;
1612 ERROR: update or delete on table "fk_notpartitioned_pk" violates foreign key constraint "fk_partitioned_fk_a_b_fkey" on table "fk_partitioned_fk"
1613 DETAIL: Key (a, b)=(1500, 1501) is still referenced from table "fk_partitioned_fk".
1614 UPDATE fk_notpartitioned_pk SET b = 2504 WHERE a = 2500;
1615 ERROR: update or delete on table "fk_notpartitioned_pk" violates foreign key constraint "fk_partitioned_fk_a_b_fkey" on table "fk_partitioned_fk"
1616 DETAIL: Key (a, b)=(2500, 2502) is still referenced from table "fk_partitioned_fk".
1617 -- check psql behavior
1618 \d fk_notpartitioned_pk
1619 Table "public.fk_notpartitioned_pk"
1620 Column | Type | Collation | Nullable | Default
1621 --------+---------+-----------+----------+---------
1622 a | integer | | not null |
1623 b | integer | | not null |
1625 "fk_notpartitioned_pk_pkey" PRIMARY KEY, btree (a, b)
1627 TABLE "fk_partitioned_fk" CONSTRAINT "fk_partitioned_fk_a_b_fkey" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b)
1629 ALTER TABLE fk_partitioned_fk DROP CONSTRAINT fk_partitioned_fk_a_b_fkey;
1631 DROP TABLE fk_notpartitioned_pk, fk_partitioned_fk;
1632 -- Altering a type referenced by a foreign key needs to drop/recreate the FK.
1633 -- Ensure that works.
1634 CREATE TABLE fk_notpartitioned_pk (a INT, PRIMARY KEY(a), CHECK (a > 0));
1635 CREATE TABLE fk_partitioned_fk (a INT REFERENCES fk_notpartitioned_pk(a) PRIMARY KEY) PARTITION BY RANGE(a);
1636 CREATE TABLE fk_partitioned_fk_1 PARTITION OF fk_partitioned_fk FOR VALUES FROM (MINVALUE) TO (MAXVALUE);
1637 INSERT INTO fk_notpartitioned_pk VALUES (1);
1638 INSERT INTO fk_partitioned_fk VALUES (1);
1639 ALTER TABLE fk_notpartitioned_pk ALTER COLUMN a TYPE bigint;
1640 DELETE FROM fk_notpartitioned_pk WHERE a = 1;
1641 ERROR: update or delete on table "fk_notpartitioned_pk" violates foreign key constraint "fk_partitioned_fk_a_fkey" on table "fk_partitioned_fk"
1642 DETAIL: Key (a)=(1) is still referenced from table "fk_partitioned_fk".
1643 DROP TABLE fk_notpartitioned_pk, fk_partitioned_fk;
1644 -- Test some other exotic foreign key features: MATCH SIMPLE, ON UPDATE/DELETE
1646 CREATE TABLE fk_notpartitioned_pk (a int, b int, primary key (a, b));
1647 CREATE TABLE fk_partitioned_fk (a int default 2501, b int default 142857) PARTITION BY LIST (a);
1648 CREATE TABLE fk_partitioned_fk_1 PARTITION OF fk_partitioned_fk FOR VALUES IN (NULL,500,501,502);
1649 ALTER TABLE fk_partitioned_fk ADD FOREIGN KEY (a, b)
1650 REFERENCES fk_notpartitioned_pk MATCH SIMPLE
1651 ON DELETE SET NULL ON UPDATE SET NULL;
1652 CREATE TABLE fk_partitioned_fk_2 PARTITION OF fk_partitioned_fk FOR VALUES IN (1500,1502);
1653 CREATE TABLE fk_partitioned_fk_3 (a int, b int);
1654 ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_3 FOR VALUES IN (2500,2501,2502,2503);
1655 -- this insert fails
1656 INSERT INTO fk_partitioned_fk (a, b) VALUES (2502, 2503);
1657 ERROR: insert or update on table "fk_partitioned_fk_3" violates foreign key constraint "fk_partitioned_fk_a_b_fkey"
1658 DETAIL: Key (a, b)=(2502, 2503) is not present in table "fk_notpartitioned_pk".
1659 INSERT INTO fk_partitioned_fk_3 (a, b) VALUES (2502, 2503);
1660 ERROR: insert or update on table "fk_partitioned_fk_3" violates foreign key constraint "fk_partitioned_fk_a_b_fkey"
1661 DETAIL: Key (a, b)=(2502, 2503) is not present in table "fk_notpartitioned_pk".
1662 -- but since the FK is MATCH SIMPLE, this one doesn't
1663 INSERT INTO fk_partitioned_fk_3 (a, b) VALUES (2502, NULL);
1664 -- now create the referenced row ...
1665 INSERT INTO fk_notpartitioned_pk VALUES (2502, 2503);
1666 --- and now the same insert work
1667 INSERT INTO fk_partitioned_fk_3 (a, b) VALUES (2502, 2503);
1668 -- this always works
1669 INSERT INTO fk_partitioned_fk (a,b) VALUES (NULL, NULL);
1671 INSERT INTO fk_notpartitioned_pk VALUES (1, 2);
1672 CREATE TABLE fk_partitioned_fk_full (x int, y int) PARTITION BY RANGE (x);
1673 CREATE TABLE fk_partitioned_fk_full_1 PARTITION OF fk_partitioned_fk_full DEFAULT;
1674 INSERT INTO fk_partitioned_fk_full VALUES (1, NULL);
1675 ALTER TABLE fk_partitioned_fk_full ADD FOREIGN KEY (x, y) REFERENCES fk_notpartitioned_pk MATCH FULL; -- fails
1676 ERROR: insert or update on table "fk_partitioned_fk_full_1" violates foreign key constraint "fk_partitioned_fk_full_x_y_fkey"
1677 DETAIL: MATCH FULL does not allow mixing of null and nonnull key values.
1678 TRUNCATE fk_partitioned_fk_full;
1679 ALTER TABLE fk_partitioned_fk_full ADD FOREIGN KEY (x, y) REFERENCES fk_notpartitioned_pk MATCH FULL;
1680 INSERT INTO fk_partitioned_fk_full VALUES (1, NULL); -- fails
1681 ERROR: insert or update on table "fk_partitioned_fk_full_1" violates foreign key constraint "fk_partitioned_fk_full_x_y_fkey"
1682 DETAIL: MATCH FULL does not allow mixing of null and nonnull key values.
1683 DROP TABLE fk_partitioned_fk_full;
1684 -- ON UPDATE SET NULL
1685 SELECT tableoid::regclass, a, b FROM fk_partitioned_fk WHERE b IS NULL ORDER BY a;
1687 ---------------------+------+---
1688 fk_partitioned_fk_3 | 2502 |
1689 fk_partitioned_fk_1 | |
1692 UPDATE fk_notpartitioned_pk SET a = a + 1 WHERE a = 2502;
1693 SELECT tableoid::regclass, a, b FROM fk_partitioned_fk WHERE b IS NULL ORDER BY a;
1695 ---------------------+------+---
1696 fk_partitioned_fk_3 | 2502 |
1697 fk_partitioned_fk_1 | |
1698 fk_partitioned_fk_1 | |
1701 -- ON DELETE SET NULL
1702 INSERT INTO fk_partitioned_fk VALUES (2503, 2503);
1703 SELECT count(*) FROM fk_partitioned_fk WHERE a IS NULL;
1709 DELETE FROM fk_notpartitioned_pk;
1710 SELECT count(*) FROM fk_partitioned_fk WHERE a IS NULL;
1716 -- ON UPDATE/DELETE SET DEFAULT
1717 ALTER TABLE fk_partitioned_fk DROP CONSTRAINT fk_partitioned_fk_a_b_fkey;
1718 ALTER TABLE fk_partitioned_fk ADD FOREIGN KEY (a, b)
1719 REFERENCES fk_notpartitioned_pk
1720 ON DELETE SET DEFAULT ON UPDATE SET DEFAULT;
1721 INSERT INTO fk_notpartitioned_pk VALUES (2502, 2503);
1722 INSERT INTO fk_partitioned_fk_3 (a, b) VALUES (2502, 2503);
1723 -- this fails, because the defaults for the referencing table are not present
1724 -- in the referenced table:
1725 UPDATE fk_notpartitioned_pk SET a = 1500 WHERE a = 2502;
1726 ERROR: insert or update on table "fk_partitioned_fk_3" violates foreign key constraint "fk_partitioned_fk_a_b_fkey"
1727 DETAIL: Key (a, b)=(2501, 142857) is not present in table "fk_notpartitioned_pk".
1728 -- but inserting the row we can make it work:
1729 INSERT INTO fk_notpartitioned_pk VALUES (2501, 142857);
1730 UPDATE fk_notpartitioned_pk SET a = 1500 WHERE a = 2502;
1731 SELECT * FROM fk_partitioned_fk WHERE b = 142857;
1737 -- ON UPDATE/DELETE CASCADE
1738 ALTER TABLE fk_partitioned_fk DROP CONSTRAINT fk_partitioned_fk_a_b_fkey;
1739 ALTER TABLE fk_partitioned_fk ADD FOREIGN KEY (a, b)
1740 REFERENCES fk_notpartitioned_pk
1741 ON DELETE CASCADE ON UPDATE CASCADE;
1742 UPDATE fk_notpartitioned_pk SET a = 2502 WHERE a = 2501;
1743 SELECT * FROM fk_partitioned_fk WHERE b = 142857;
1749 -- Now you see it ...
1750 SELECT * FROM fk_partitioned_fk WHERE b = 142857;
1756 DELETE FROM fk_notpartitioned_pk WHERE b = 142857;
1758 SELECT * FROM fk_partitioned_fk WHERE a = 142857;
1763 -- verify that DROP works
1764 DROP TABLE fk_partitioned_fk_2;
1765 -- Test behavior of the constraint together with attaching and detaching
1767 CREATE TABLE fk_partitioned_fk_2 PARTITION OF fk_partitioned_fk FOR VALUES IN (1500,1502);
1768 ALTER TABLE fk_partitioned_fk DETACH PARTITION fk_partitioned_fk_2;
1770 DROP TABLE fk_partitioned_fk;
1771 -- constraint should still be there
1772 \d fk_partitioned_fk_2;
1773 Table "public.fk_partitioned_fk_2"
1774 Column | Type | Collation | Nullable | Default
1775 --------+---------+-----------+----------+---------
1776 a | integer | | | 2501
1777 b | integer | | | 142857
1778 Foreign-key constraints:
1779 "fk_partitioned_fk_a_b_fkey" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) ON UPDATE CASCADE ON DELETE CASCADE
1782 ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_2 FOR VALUES IN (1500,1502);
1783 DROP TABLE fk_partitioned_fk_2;
1784 CREATE TABLE fk_partitioned_fk_2 (b int, c text, a int,
1785 FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk ON UPDATE CASCADE ON DELETE CASCADE);
1786 ALTER TABLE fk_partitioned_fk_2 DROP COLUMN c;
1787 ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_2 FOR VALUES IN (1500,1502);
1788 -- should have only one constraint
1789 \d fk_partitioned_fk_2
1790 Table "public.fk_partitioned_fk_2"
1791 Column | Type | Collation | Nullable | Default
1792 --------+---------+-----------+----------+---------
1795 Partition of: fk_partitioned_fk FOR VALUES IN (1500, 1502)
1796 Foreign-key constraints:
1797 TABLE "fk_partitioned_fk" CONSTRAINT "fk_partitioned_fk_a_b_fkey" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) ON UPDATE CASCADE ON DELETE CASCADE
1799 DROP TABLE fk_partitioned_fk_2;
1800 CREATE TABLE fk_partitioned_fk_4 (a int, b int, FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) ON UPDATE CASCADE ON DELETE CASCADE) PARTITION BY RANGE (b, a);
1801 CREATE TABLE fk_partitioned_fk_4_1 PARTITION OF fk_partitioned_fk_4 FOR VALUES FROM (1,1) TO (100,100);
1802 CREATE TABLE fk_partitioned_fk_4_2 (a int, b int, FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) ON UPDATE SET NULL);
1803 ALTER TABLE fk_partitioned_fk_4 ATTACH PARTITION fk_partitioned_fk_4_2 FOR VALUES FROM (100,100) TO (1000,1000);
1804 ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_4 FOR VALUES IN (3500,3502);
1805 ALTER TABLE fk_partitioned_fk DETACH PARTITION fk_partitioned_fk_4;
1806 ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_4 FOR VALUES IN (3500,3502);
1807 -- should only have one constraint
1808 \d fk_partitioned_fk_4
1809 Partitioned table "public.fk_partitioned_fk_4"
1810 Column | Type | Collation | Nullable | Default
1811 --------+---------+-----------+----------+---------
1814 Partition of: fk_partitioned_fk FOR VALUES IN (3500, 3502)
1815 Partition key: RANGE (b, a)
1816 Foreign-key constraints:
1817 TABLE "fk_partitioned_fk" CONSTRAINT "fk_partitioned_fk_a_b_fkey" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) ON UPDATE CASCADE ON DELETE CASCADE
1818 Number of partitions: 2 (Use \d+ to list them.)
1820 \d fk_partitioned_fk_4_1
1821 Table "public.fk_partitioned_fk_4_1"
1822 Column | Type | Collation | Nullable | Default
1823 --------+---------+-----------+----------+---------
1826 Partition of: fk_partitioned_fk_4 FOR VALUES FROM (1, 1) TO (100, 100)
1827 Foreign-key constraints:
1828 TABLE "fk_partitioned_fk" CONSTRAINT "fk_partitioned_fk_a_b_fkey" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) ON UPDATE CASCADE ON DELETE CASCADE
1830 -- this one has an FK with mismatched properties
1831 \d fk_partitioned_fk_4_2
1832 Table "public.fk_partitioned_fk_4_2"
1833 Column | Type | Collation | Nullable | Default
1834 --------+---------+-----------+----------+---------
1837 Partition of: fk_partitioned_fk_4 FOR VALUES FROM (100, 100) TO (1000, 1000)
1838 Foreign-key constraints:
1839 "fk_partitioned_fk_4_2_a_b_fkey" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) ON UPDATE SET NULL
1840 TABLE "fk_partitioned_fk" CONSTRAINT "fk_partitioned_fk_a_b_fkey" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) ON UPDATE CASCADE ON DELETE CASCADE
1842 CREATE TABLE fk_partitioned_fk_5 (a int, b int,
1843 FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE,
1844 FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE)
1845 PARTITION BY RANGE (a);
1846 CREATE TABLE fk_partitioned_fk_5_1 (a int, b int, FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk);
1847 ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_5 FOR VALUES IN (4500);
1848 ALTER TABLE fk_partitioned_fk_5 ATTACH PARTITION fk_partitioned_fk_5_1 FOR VALUES FROM (0) TO (10);
1849 ALTER TABLE fk_partitioned_fk DETACH PARTITION fk_partitioned_fk_5;
1850 ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_5 FOR VALUES IN (4500);
1851 -- this one has two constraints, similar but not quite the one in the parent,
1852 -- so it gets a new one
1853 \d fk_partitioned_fk_5
1854 Partitioned table "public.fk_partitioned_fk_5"
1855 Column | Type | Collation | Nullable | Default
1856 --------+---------+-----------+----------+---------
1859 Partition of: fk_partitioned_fk FOR VALUES IN (4500)
1860 Partition key: RANGE (a)
1861 Foreign-key constraints:
1862 "fk_partitioned_fk_5_a_b_fkey" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE
1863 "fk_partitioned_fk_5_a_b_fkey1" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
1864 TABLE "fk_partitioned_fk" CONSTRAINT "fk_partitioned_fk_a_b_fkey" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) ON UPDATE CASCADE ON DELETE CASCADE
1865 Number of partitions: 1 (Use \d+ to list them.)
1867 -- verify that it works to reattaching a child with multiple candidate
1869 ALTER TABLE fk_partitioned_fk_5 DETACH PARTITION fk_partitioned_fk_5_1;
1870 ALTER TABLE fk_partitioned_fk_5 ATTACH PARTITION fk_partitioned_fk_5_1 FOR VALUES FROM (0) TO (10);
1871 \d fk_partitioned_fk_5_1
1872 Table "public.fk_partitioned_fk_5_1"
1873 Column | Type | Collation | Nullable | Default
1874 --------+---------+-----------+----------+---------
1877 Partition of: fk_partitioned_fk_5 FOR VALUES FROM (0) TO (10)
1878 Foreign-key constraints:
1879 "fk_partitioned_fk_5_1_a_b_fkey" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b)
1880 TABLE "fk_partitioned_fk_5" CONSTRAINT "fk_partitioned_fk_5_a_b_fkey" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE
1881 TABLE "fk_partitioned_fk_5" CONSTRAINT "fk_partitioned_fk_5_a_b_fkey1" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) MATCH FULL ON UPDATE CASCADE ON DELETE CASCADE
1882 TABLE "fk_partitioned_fk" CONSTRAINT "fk_partitioned_fk_a_b_fkey" FOREIGN KEY (a, b) REFERENCES fk_notpartitioned_pk(a, b) ON UPDATE CASCADE ON DELETE CASCADE
1884 -- verify that attaching a table checks that the existing data satisfies the
1886 CREATE TABLE fk_partitioned_fk_2 (a int, b int) PARTITION BY RANGE (b);
1887 CREATE TABLE fk_partitioned_fk_2_1 PARTITION OF fk_partitioned_fk_2 FOR VALUES FROM (0) TO (1000);
1888 CREATE TABLE fk_partitioned_fk_2_2 PARTITION OF fk_partitioned_fk_2 FOR VALUES FROM (1000) TO (2000);
1889 INSERT INTO fk_partitioned_fk_2 VALUES (1600, 601), (1600, 1601);
1890 ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_2
1891 FOR VALUES IN (1600);
1892 ERROR: insert or update on table "fk_partitioned_fk_2_1" violates foreign key constraint "fk_partitioned_fk_a_b_fkey"
1893 DETAIL: Key (a, b)=(1600, 601) is not present in table "fk_notpartitioned_pk".
1894 INSERT INTO fk_notpartitioned_pk VALUES (1600, 601), (1600, 1601);
1895 ALTER TABLE fk_partitioned_fk ATTACH PARTITION fk_partitioned_fk_2
1896 FOR VALUES IN (1600);
1897 -- leave these tables around intentionally
1898 -- test the case when the referenced table is owned by a different user
1899 create role regress_other_partitioned_fk_owner;
1900 grant references on fk_notpartitioned_pk to regress_other_partitioned_fk_owner;
1901 set role regress_other_partitioned_fk_owner;
1902 create table other_partitioned_fk(a int, b int) partition by list (a);
1903 create table other_partitioned_fk_1 partition of other_partitioned_fk
1904 for values in (2048);
1905 insert into other_partitioned_fk
1906 select 2048, x from generate_series(1,10) x;
1908 alter table other_partitioned_fk add foreign key (a, b)
1909 references fk_notpartitioned_pk(a, b);
1910 ERROR: insert or update on table "other_partitioned_fk_1" violates foreign key constraint "other_partitioned_fk_a_b_fkey"
1911 DETAIL: Key (a, b)=(2048, 1) is not present in table "fk_notpartitioned_pk".
1912 -- add the missing keys and retry
1914 insert into fk_notpartitioned_pk (a, b)
1915 select 2048, x from generate_series(1,10) x;
1916 set role regress_other_partitioned_fk_owner;
1917 alter table other_partitioned_fk add foreign key (a, b)
1918 references fk_notpartitioned_pk(a, b);
1920 drop table other_partitioned_fk;
1922 revoke all on fk_notpartitioned_pk from regress_other_partitioned_fk_owner;
1923 drop role regress_other_partitioned_fk_owner;
1924 -- Test creating a constraint at the parent that already exists in partitions.
1925 -- There should be no duplicated constraints, and attempts to drop the
1926 -- constraint in partitions should raise appropriate errors.
1927 create schema fkpart0
1928 create table pkey (a int primary key)
1929 create table fk_part (a int) partition by list (a)
1930 create table fk_part_1 partition of fk_part
1931 (foreign key (a) references fkpart0.pkey) for values in (1)
1932 create table fk_part_23 partition of fk_part
1933 (foreign key (a) references fkpart0.pkey) for values in (2, 3)
1934 partition by list (a)
1935 create table fk_part_23_2 partition of fk_part_23 for values in (2);
1936 alter table fkpart0.fk_part add foreign key (a) references fkpart0.pkey;
1937 \d fkpart0.fk_part_1 \\ -- should have only one FK
1938 Table "fkpart0.fk_part_1"
1939 Column | Type | Collation | Nullable | Default
1940 --------+---------+-----------+----------+---------
1942 Partition of: fkpart0.fk_part FOR VALUES IN (1)
1943 Foreign-key constraints:
1944 TABLE "fkpart0.fk_part" CONSTRAINT "fk_part_a_fkey" FOREIGN KEY (a) REFERENCES fkpart0.pkey(a)
1946 alter table fkpart0.fk_part_1 drop constraint fk_part_1_a_fkey;
1947 ERROR: cannot drop inherited constraint "fk_part_1_a_fkey" of relation "fk_part_1"
1948 \d fkpart0.fk_part_23 \\ -- should have only one FK
1949 Partitioned table "fkpart0.fk_part_23"
1950 Column | Type | Collation | Nullable | Default
1951 --------+---------+-----------+----------+---------
1953 Partition of: fkpart0.fk_part FOR VALUES IN (2, 3)
1954 Partition key: LIST (a)
1955 Foreign-key constraints:
1956 TABLE "fkpart0.fk_part" CONSTRAINT "fk_part_a_fkey" FOREIGN KEY (a) REFERENCES fkpart0.pkey(a)
1957 Number of partitions: 1 (Use \d+ to list them.)
1959 \d fkpart0.fk_part_23_2 \\ -- should have only one FK
1960 Table "fkpart0.fk_part_23_2"
1961 Column | Type | Collation | Nullable | Default
1962 --------+---------+-----------+----------+---------
1964 Partition of: fkpart0.fk_part_23 FOR VALUES IN (2)
1965 Foreign-key constraints:
1966 TABLE "fkpart0.fk_part" CONSTRAINT "fk_part_a_fkey" FOREIGN KEY (a) REFERENCES fkpart0.pkey(a)
1968 alter table fkpart0.fk_part_23 drop constraint fk_part_23_a_fkey;
1969 ERROR: cannot drop inherited constraint "fk_part_23_a_fkey" of relation "fk_part_23"
1970 alter table fkpart0.fk_part_23_2 drop constraint fk_part_23_a_fkey;
1971 ERROR: cannot drop inherited constraint "fk_part_23_a_fkey" of relation "fk_part_23_2"
1972 create table fkpart0.fk_part_4 partition of fkpart0.fk_part for values in (4);
1973 \d fkpart0.fk_part_4
1974 Table "fkpart0.fk_part_4"
1975 Column | Type | Collation | Nullable | Default
1976 --------+---------+-----------+----------+---------
1978 Partition of: fkpart0.fk_part FOR VALUES IN (4)
1979 Foreign-key constraints:
1980 TABLE "fkpart0.fk_part" CONSTRAINT "fk_part_a_fkey" FOREIGN KEY (a) REFERENCES fkpart0.pkey(a)
1982 alter table fkpart0.fk_part_4 drop constraint fk_part_a_fkey;
1983 ERROR: cannot drop inherited constraint "fk_part_a_fkey" of relation "fk_part_4"
1984 create table fkpart0.fk_part_56 partition of fkpart0.fk_part
1985 for values in (5,6) partition by list (a);
1986 create table fkpart0.fk_part_56_5 partition of fkpart0.fk_part_56
1988 \d fkpart0.fk_part_56
1989 Partitioned table "fkpart0.fk_part_56"
1990 Column | Type | Collation | Nullable | Default
1991 --------+---------+-----------+----------+---------
1993 Partition of: fkpart0.fk_part FOR VALUES IN (5, 6)
1994 Partition key: LIST (a)
1995 Foreign-key constraints:
1996 TABLE "fkpart0.fk_part" CONSTRAINT "fk_part_a_fkey" FOREIGN KEY (a) REFERENCES fkpart0.pkey(a)
1997 Number of partitions: 1 (Use \d+ to list them.)
1999 alter table fkpart0.fk_part_56 drop constraint fk_part_a_fkey;
2000 ERROR: cannot drop inherited constraint "fk_part_a_fkey" of relation "fk_part_56"
2001 alter table fkpart0.fk_part_56_5 drop constraint fk_part_a_fkey;
2002 ERROR: cannot drop inherited constraint "fk_part_a_fkey" of relation "fk_part_56_5"
2003 -- verify that attaching and detaching partitions maintains the right set of
2005 create schema fkpart1
2006 create table pkey (a int primary key)
2007 create table fk_part (a int) partition by list (a)
2008 create table fk_part_1 partition of fk_part for values in (1) partition by list (a)
2009 create table fk_part_1_1 partition of fk_part_1 for values in (1);
2010 alter table fkpart1.fk_part add foreign key (a) references fkpart1.pkey;
2011 insert into fkpart1.fk_part values (1); -- should fail
2012 ERROR: insert or update on table "fk_part_1_1" violates foreign key constraint "fk_part_a_fkey"
2013 DETAIL: Key (a)=(1) is not present in table "pkey".
2014 insert into fkpart1.pkey values (1);
2015 insert into fkpart1.fk_part values (1);
2016 delete from fkpart1.pkey where a = 1; -- should fail
2017 ERROR: update or delete on table "pkey" violates foreign key constraint "fk_part_a_fkey" on table "fk_part"
2018 DETAIL: Key (a)=(1) is still referenced from table "fk_part".
2019 alter table fkpart1.fk_part detach partition fkpart1.fk_part_1;
2020 create table fkpart1.fk_part_1_2 partition of fkpart1.fk_part_1 for values in (2);
2021 insert into fkpart1.fk_part_1 values (2); -- should fail
2022 ERROR: insert or update on table "fk_part_1_2" violates foreign key constraint "fk_part_a_fkey"
2023 DETAIL: Key (a)=(2) is not present in table "pkey".
2024 delete from fkpart1.pkey where a = 1;
2025 ERROR: update or delete on table "pkey" violates foreign key constraint "fk_part_a_fkey" on table "fk_part_1"
2026 DETAIL: Key (a)=(1) is still referenced from table "fk_part_1".
2027 -- verify that attaching and detaching partitions manipulates the inheritance
2028 -- properties of their FK constraints correctly
2029 create schema fkpart2
2030 create table pkey (a int primary key)
2031 create table fk_part (a int, constraint fkey foreign key (a) references fkpart2.pkey) partition by list (a)
2032 create table fk_part_1 partition of fkpart2.fk_part for values in (1) partition by list (a)
2033 create table fk_part_1_1 (a int, constraint my_fkey foreign key (a) references fkpart2.pkey);
2034 alter table fkpart2.fk_part_1 attach partition fkpart2.fk_part_1_1 for values in (1);
2035 alter table fkpart2.fk_part_1 drop constraint fkey; -- should fail
2036 ERROR: cannot drop inherited constraint "fkey" of relation "fk_part_1"
2037 alter table fkpart2.fk_part_1_1 drop constraint my_fkey; -- should fail
2038 ERROR: cannot drop inherited constraint "my_fkey" of relation "fk_part_1_1"
2039 alter table fkpart2.fk_part detach partition fkpart2.fk_part_1;
2040 alter table fkpart2.fk_part_1 drop constraint fkey; -- ok
2041 alter table fkpart2.fk_part_1_1 drop constraint my_fkey; -- doesn't exist
2042 ERROR: constraint "my_fkey" of relation "fk_part_1_1" does not exist
2043 -- verify constraint deferrability
2044 create schema fkpart3
2045 create table pkey (a int primary key)
2046 create table fk_part (a int, constraint fkey foreign key (a) references fkpart3.pkey deferrable initially immediate) partition by list (a)
2047 create table fk_part_1 partition of fkpart3.fk_part for values in (1) partition by list (a)
2048 create table fk_part_1_1 partition of fkpart3.fk_part_1 for values in (1)
2049 create table fk_part_2 partition of fkpart3.fk_part for values in (2);
2051 set constraints fkpart3.fkey deferred;
2052 insert into fkpart3.fk_part values (1);
2053 insert into fkpart3.pkey values (1);
2056 set constraints fkpart3.fkey deferred;
2057 delete from fkpart3.pkey;
2058 delete from fkpart3.fk_part;
2060 drop schema fkpart0, fkpart1, fkpart2, fkpart3 cascade;
2061 NOTICE: drop cascades to 10 other objects
2062 DETAIL: drop cascades to table fkpart3.pkey
2063 drop cascades to table fkpart3.fk_part
2064 drop cascades to table fkpart2.pkey
2065 drop cascades to table fkpart2.fk_part
2066 drop cascades to table fkpart2.fk_part_1
2067 drop cascades to table fkpart1.pkey
2068 drop cascades to table fkpart1.fk_part
2069 drop cascades to table fkpart1.fk_part_1
2070 drop cascades to table fkpart0.pkey
2071 drop cascades to table fkpart0.fk_part
2072 -- Test a partitioned table as referenced table.
2073 -- Verify basic functionality with a regular partition creation and a partition
2074 -- with a different column layout, as well as partitions added (created and
2075 -- attached) after creating the foreign key.
2076 CREATE SCHEMA fkpart3;
2077 SET search_path TO fkpart3;
2078 CREATE TABLE pk (a int PRIMARY KEY) PARTITION BY RANGE (a);
2079 CREATE TABLE pk1 PARTITION OF pk FOR VALUES FROM (0) TO (1000);
2080 CREATE TABLE pk2 (b int, a int);
2081 ALTER TABLE pk2 DROP COLUMN b;
2082 ALTER TABLE pk2 ALTER a SET NOT NULL;
2083 ALTER TABLE pk ATTACH PARTITION pk2 FOR VALUES FROM (1000) TO (2000);
2084 CREATE TABLE fk (a int) PARTITION BY RANGE (a);
2085 CREATE TABLE fk1 PARTITION OF fk FOR VALUES FROM (0) TO (750);
2086 ALTER TABLE fk ADD FOREIGN KEY (a) REFERENCES pk;
2087 CREATE TABLE fk2 (b int, a int) ;
2088 ALTER TABLE fk2 DROP COLUMN b;
2089 ALTER TABLE fk ATTACH PARTITION fk2 FOR VALUES FROM (750) TO (3500);
2090 CREATE TABLE pk3 PARTITION OF pk FOR VALUES FROM (2000) TO (3000);
2091 CREATE TABLE pk4 (LIKE pk);
2092 ALTER TABLE pk ATTACH PARTITION pk4 FOR VALUES FROM (3000) TO (4000);
2093 CREATE TABLE pk5 (c int, b int, a int NOT NULL) PARTITION BY RANGE (a);
2094 ALTER TABLE pk5 DROP COLUMN b, DROP COLUMN c;
2095 CREATE TABLE pk51 PARTITION OF pk5 FOR VALUES FROM (4000) TO (4500);
2096 CREATE TABLE pk52 PARTITION OF pk5 FOR VALUES FROM (4500) TO (5000);
2097 ALTER TABLE pk ATTACH PARTITION pk5 FOR VALUES FROM (4000) TO (5000);
2098 CREATE TABLE fk3 PARTITION OF fk FOR VALUES FROM (3500) TO (5000);
2099 -- these should fail: referenced value not present
2100 INSERT into fk VALUES (1);
2101 ERROR: insert or update on table "fk1" violates foreign key constraint "fk_a_fkey"
2102 DETAIL: Key (a)=(1) is not present in table "pk".
2103 INSERT into fk VALUES (1000);
2104 ERROR: insert or update on table "fk2" violates foreign key constraint "fk_a_fkey"
2105 DETAIL: Key (a)=(1000) is not present in table "pk".
2106 INSERT into fk VALUES (2000);
2107 ERROR: insert or update on table "fk2" violates foreign key constraint "fk_a_fkey"
2108 DETAIL: Key (a)=(2000) is not present in table "pk".
2109 INSERT into fk VALUES (3000);
2110 ERROR: insert or update on table "fk2" violates foreign key constraint "fk_a_fkey"
2111 DETAIL: Key (a)=(3000) is not present in table "pk".
2112 INSERT into fk VALUES (4000);
2113 ERROR: insert or update on table "fk3" violates foreign key constraint "fk_a_fkey"
2114 DETAIL: Key (a)=(4000) is not present in table "pk".
2115 INSERT into fk VALUES (4500);
2116 ERROR: insert or update on table "fk3" violates foreign key constraint "fk_a_fkey"
2117 DETAIL: Key (a)=(4500) is not present in table "pk".
2118 -- insert into the referenced table, now they should work
2119 INSERT into pk VALUES (1), (1000), (2000), (3000), (4000), (4500);
2120 INSERT into fk VALUES (1), (1000), (2000), (3000), (4000), (4500);
2121 -- should fail: referencing value present
2122 DELETE FROM pk WHERE a = 1;
2123 ERROR: update or delete on table "pk1" violates foreign key constraint "fk_a_fkey1" on table "fk"
2124 DETAIL: Key (a)=(1) is still referenced from table "fk".
2125 DELETE FROM pk WHERE a = 1000;
2126 ERROR: update or delete on table "pk2" violates foreign key constraint "fk_a_fkey2" on table "fk"
2127 DETAIL: Key (a)=(1000) is still referenced from table "fk".
2128 DELETE FROM pk WHERE a = 2000;
2129 ERROR: update or delete on table "pk3" violates foreign key constraint "fk_a_fkey3" on table "fk"
2130 DETAIL: Key (a)=(2000) is still referenced from table "fk".
2131 DELETE FROM pk WHERE a = 3000;
2132 ERROR: update or delete on table "pk4" violates foreign key constraint "fk_a_fkey4" on table "fk"
2133 DETAIL: Key (a)=(3000) is still referenced from table "fk".
2134 DELETE FROM pk WHERE a = 4000;
2135 ERROR: update or delete on table "pk51" violates foreign key constraint "fk_a_fkey6" on table "fk"
2136 DETAIL: Key (a)=(4000) is still referenced from table "fk".
2137 DELETE FROM pk WHERE a = 4500;
2138 ERROR: update or delete on table "pk52" violates foreign key constraint "fk_a_fkey7" on table "fk"
2139 DETAIL: Key (a)=(4500) is still referenced from table "fk".
2140 UPDATE pk SET a = 2 WHERE a = 1;
2141 ERROR: update or delete on table "pk1" violates foreign key constraint "fk_a_fkey1" on table "fk"
2142 DETAIL: Key (a)=(1) is still referenced from table "fk".
2143 UPDATE pk SET a = 1002 WHERE a = 1000;
2144 ERROR: update or delete on table "pk2" violates foreign key constraint "fk_a_fkey2" on table "fk"
2145 DETAIL: Key (a)=(1000) is still referenced from table "fk".
2146 UPDATE pk SET a = 2002 WHERE a = 2000;
2147 ERROR: update or delete on table "pk3" violates foreign key constraint "fk_a_fkey3" on table "fk"
2148 DETAIL: Key (a)=(2000) is still referenced from table "fk".
2149 UPDATE pk SET a = 3002 WHERE a = 3000;
2150 ERROR: update or delete on table "pk4" violates foreign key constraint "fk_a_fkey4" on table "fk"
2151 DETAIL: Key (a)=(3000) is still referenced from table "fk".
2152 UPDATE pk SET a = 4002 WHERE a = 4000;
2153 ERROR: update or delete on table "pk51" violates foreign key constraint "fk_a_fkey6" on table "fk"
2154 DETAIL: Key (a)=(4000) is still referenced from table "fk".
2155 UPDATE pk SET a = 4502 WHERE a = 4500;
2156 ERROR: update or delete on table "pk52" violates foreign key constraint "fk_a_fkey7" on table "fk"
2157 DETAIL: Key (a)=(4500) is still referenced from table "fk".
2158 -- now they should work
2160 UPDATE pk SET a = 2 WHERE a = 1;
2161 DELETE FROM pk WHERE a = 2;
2162 UPDATE pk SET a = 1002 WHERE a = 1000;
2163 DELETE FROM pk WHERE a = 1002;
2164 UPDATE pk SET a = 2002 WHERE a = 2000;
2165 DELETE FROM pk WHERE a = 2002;
2166 UPDATE pk SET a = 3002 WHERE a = 3000;
2167 DELETE FROM pk WHERE a = 3002;
2168 UPDATE pk SET a = 4002 WHERE a = 4000;
2169 DELETE FROM pk WHERE a = 4002;
2170 UPDATE pk SET a = 4502 WHERE a = 4500;
2171 DELETE FROM pk WHERE a = 4502;
2172 CREATE SCHEMA fkpart4;
2173 SET search_path TO fkpart4;
2174 -- dropping/detaching PARTITIONs is prevented if that would break
2175 -- a foreign key's existing data
2176 CREATE TABLE droppk (a int PRIMARY KEY) PARTITION BY RANGE (a);
2177 CREATE TABLE droppk1 PARTITION OF droppk FOR VALUES FROM (0) TO (1000);
2178 CREATE TABLE droppk_d PARTITION OF droppk DEFAULT;
2179 CREATE TABLE droppk2 PARTITION OF droppk FOR VALUES FROM (1000) TO (2000)
2180 PARTITION BY RANGE (a);
2181 CREATE TABLE droppk21 PARTITION OF droppk2 FOR VALUES FROM (1000) TO (1400);
2182 CREATE TABLE droppk2_d PARTITION OF droppk2 DEFAULT;
2183 INSERT into droppk VALUES (1), (1000), (1500), (2000);
2184 CREATE TABLE dropfk (a int REFERENCES droppk);
2185 INSERT into dropfk VALUES (1), (1000), (1500), (2000);
2186 -- these should all fail
2187 ALTER TABLE droppk DETACH PARTITION droppk_d;
2188 ERROR: removing partition "droppk_d" violates foreign key constraint "dropfk_a_fkey5"
2189 DETAIL: Key (a)=(2000) is still referenced from table "dropfk".
2190 ALTER TABLE droppk2 DETACH PARTITION droppk2_d;
2191 ERROR: removing partition "droppk2_d" violates foreign key constraint "dropfk_a_fkey4"
2192 DETAIL: Key (a)=(1500) is still referenced from table "dropfk".
2193 ALTER TABLE droppk DETACH PARTITION droppk1;
2194 ERROR: removing partition "droppk1" violates foreign key constraint "dropfk_a_fkey1"
2195 DETAIL: Key (a)=(1) is still referenced from table "dropfk".
2196 ALTER TABLE droppk DETACH PARTITION droppk2;
2197 ERROR: removing partition "droppk2" violates foreign key constraint "dropfk_a_fkey2"
2198 DETAIL: Key (a)=(1000) is still referenced from table "dropfk".
2199 ALTER TABLE droppk2 DETACH PARTITION droppk21;
2200 ERROR: removing partition "droppk21" violates foreign key constraint "dropfk_a_fkey3"
2201 DETAIL: Key (a)=(1000) is still referenced from table "dropfk".
2202 -- dropping partitions is disallowed
2203 DROP TABLE droppk_d;
2204 ERROR: cannot drop table droppk_d because other objects depend on it
2205 DETAIL: constraint dropfk_a_fkey on table dropfk depends on table droppk_d
2206 HINT: Use DROP ... CASCADE to drop the dependent objects too.
2207 DROP TABLE droppk2_d;
2208 ERROR: cannot drop table droppk2_d because other objects depend on it
2209 DETAIL: constraint dropfk_a_fkey on table dropfk depends on table droppk2_d
2210 HINT: Use DROP ... CASCADE to drop the dependent objects too.
2212 ERROR: cannot drop table droppk1 because other objects depend on it
2213 DETAIL: constraint dropfk_a_fkey on table dropfk depends on table droppk1
2214 HINT: Use DROP ... CASCADE to drop the dependent objects too.
2216 ERROR: cannot drop table droppk2 because other objects depend on it
2217 DETAIL: constraint dropfk_a_fkey on table dropfk depends on table droppk2
2218 HINT: Use DROP ... CASCADE to drop the dependent objects too.
2219 DROP TABLE droppk21;
2220 ERROR: cannot drop table droppk21 because other objects depend on it
2221 DETAIL: constraint dropfk_a_fkey on table dropfk depends on table droppk21
2222 HINT: Use DROP ... CASCADE to drop the dependent objects too.
2224 -- dropping partitions is disallowed, even when no referencing values
2225 DROP TABLE droppk_d;
2226 ERROR: cannot drop table droppk_d because other objects depend on it
2227 DETAIL: constraint dropfk_a_fkey on table dropfk depends on table droppk_d
2228 HINT: Use DROP ... CASCADE to drop the dependent objects too.
2229 DROP TABLE droppk2_d;
2230 ERROR: cannot drop table droppk2_d because other objects depend on it
2231 DETAIL: constraint dropfk_a_fkey on table dropfk depends on table droppk2_d
2232 HINT: Use DROP ... CASCADE to drop the dependent objects too.
2234 ERROR: cannot drop table droppk1 because other objects depend on it
2235 DETAIL: constraint dropfk_a_fkey on table dropfk depends on table droppk1
2236 HINT: Use DROP ... CASCADE to drop the dependent objects too.
2237 -- but DETACH is allowed, and DROP afterwards works
2238 ALTER TABLE droppk2 DETACH PARTITION droppk21;
2240 ERROR: cannot drop table droppk2 because other objects depend on it
2241 DETAIL: constraint dropfk_a_fkey on table dropfk depends on table droppk2
2242 HINT: Use DROP ... CASCADE to drop the dependent objects too.
2243 -- Verify that initial constraint creation and cloning behave correctly
2244 CREATE SCHEMA fkpart5;
2245 SET search_path TO fkpart5;
2246 CREATE TABLE pk (a int PRIMARY KEY) PARTITION BY LIST (a);
2247 CREATE TABLE pk1 PARTITION OF pk FOR VALUES IN (1) PARTITION BY LIST (a);
2248 CREATE TABLE pk11 PARTITION OF pk1 FOR VALUES IN (1);
2249 CREATE TABLE fk (a int) PARTITION BY LIST (a);
2250 CREATE TABLE fk1 PARTITION OF fk FOR VALUES IN (1) PARTITION BY LIST (a);
2251 CREATE TABLE fk11 PARTITION OF fk1 FOR VALUES IN (1);
2252 ALTER TABLE fk ADD FOREIGN KEY (a) REFERENCES pk;
2253 CREATE TABLE pk2 PARTITION OF pk FOR VALUES IN (2);
2254 CREATE TABLE pk3 (a int NOT NULL) PARTITION BY LIST (a);
2255 CREATE TABLE pk31 PARTITION OF pk3 FOR VALUES IN (31);
2256 CREATE TABLE pk32 (b int, a int NOT NULL);
2257 ALTER TABLE pk32 DROP COLUMN b;
2258 ALTER TABLE pk3 ATTACH PARTITION pk32 FOR VALUES IN (32);
2259 ALTER TABLE pk ATTACH PARTITION pk3 FOR VALUES IN (31, 32);
2260 CREATE TABLE fk2 PARTITION OF fk FOR VALUES IN (2);
2261 CREATE TABLE fk3 (b int, a int);
2262 ALTER TABLE fk3 DROP COLUMN b;
2263 ALTER TABLE fk ATTACH PARTITION fk3 FOR VALUES IN (3);
2264 SELECT pg_describe_object('pg_constraint'::regclass, oid, 0), confrelid::regclass,
2265 CASE WHEN conparentid <> 0 THEN pg_describe_object('pg_constraint'::regclass, conparentid, 0) ELSE 'TOP' END
2266 FROM pg_catalog.pg_constraint
2267 WHERE conrelid IN (SELECT relid FROM pg_partition_tree('fk'))
2268 ORDER BY conrelid::regclass::text, conname;
2269 pg_describe_object | confrelid | case
2270 ------------------------------------+-----------+-----------------------------------
2271 constraint fk_a_fkey on table fk | pk | TOP
2272 constraint fk_a_fkey1 on table fk | pk1 | constraint fk_a_fkey on table fk
2273 constraint fk_a_fkey2 on table fk | pk11 | constraint fk_a_fkey1 on table fk
2274 constraint fk_a_fkey3 on table fk | pk2 | constraint fk_a_fkey on table fk
2275 constraint fk_a_fkey4 on table fk | pk3 | constraint fk_a_fkey on table fk
2276 constraint fk_a_fkey5 on table fk | pk31 | constraint fk_a_fkey4 on table fk
2277 constraint fk_a_fkey6 on table fk | pk32 | constraint fk_a_fkey4 on table fk
2278 constraint fk_a_fkey on table fk1 | pk | constraint fk_a_fkey on table fk
2279 constraint fk_a_fkey on table fk11 | pk | constraint fk_a_fkey on table fk1
2280 constraint fk_a_fkey on table fk2 | pk | constraint fk_a_fkey on table fk
2281 constraint fk_a_fkey on table fk3 | pk | constraint fk_a_fkey on table fk
2284 CREATE TABLE fk4 (LIKE fk);
2285 INSERT INTO fk4 VALUES (50);
2286 ALTER TABLE fk ATTACH PARTITION fk4 FOR VALUES IN (50);
2287 ERROR: insert or update on table "fk4" violates foreign key constraint "fk_a_fkey"
2288 DETAIL: Key (a)=(50) is not present in table "pk".
2289 -- Verify constraint deferrability
2290 CREATE SCHEMA fkpart9;
2291 SET search_path TO fkpart9;
2292 CREATE TABLE pk (a int PRIMARY KEY) PARTITION BY LIST (a);
2293 CREATE TABLE pk1 PARTITION OF pk FOR VALUES IN (1, 2) PARTITION BY LIST (a);
2294 CREATE TABLE pk11 PARTITION OF pk1 FOR VALUES IN (1);
2295 CREATE TABLE pk3 PARTITION OF pk FOR VALUES IN (3);
2296 CREATE TABLE fk (a int REFERENCES pk DEFERRABLE INITIALLY IMMEDIATE);
2297 INSERT INTO fk VALUES (1); -- should fail
2298 ERROR: insert or update on table "fk" violates foreign key constraint "fk_a_fkey"
2299 DETAIL: Key (a)=(1) is not present in table "pk".
2301 SET CONSTRAINTS fk_a_fkey DEFERRED;
2302 INSERT INTO fk VALUES (1);
2303 COMMIT; -- should fail
2304 ERROR: insert or update on table "fk" violates foreign key constraint "fk_a_fkey"
2305 DETAIL: Key (a)=(1) is not present in table "pk".
2307 SET CONSTRAINTS fk_a_fkey DEFERRED;
2308 INSERT INTO fk VALUES (1);
2309 INSERT INTO pk VALUES (1);
2312 SET CONSTRAINTS fk_a_fkey DEFERRED;
2313 DELETE FROM pk WHERE a = 1;
2314 DELETE FROM fk WHERE a = 1;
2316 -- Verify constraint deferrability when changed by ALTER
2317 -- Partitioned table at referencing end
2318 CREATE TABLE pt(f1 int, f2 int, f3 int, PRIMARY KEY(f1,f2));
2319 CREATE TABLE ref(f1 int, f2 int, f3 int)
2320 PARTITION BY list(f1);
2321 CREATE TABLE ref1 PARTITION OF ref FOR VALUES IN (1);
2322 CREATE TABLE ref2 PARTITION OF ref FOR VALUES in (2);
2323 ALTER TABLE ref ADD FOREIGN KEY(f1,f2) REFERENCES pt;
2324 ALTER TABLE ref ALTER CONSTRAINT ref_f1_f2_fkey
2325 DEFERRABLE INITIALLY DEFERRED;
2326 INSERT INTO pt VALUES(1,2,3);
2327 INSERT INTO ref VALUES(1,2,3);
2333 -- Multi-level partitioning at referencing end
2334 CREATE TABLE pt(f1 int, f2 int, f3 int, PRIMARY KEY(f1,f2));
2335 CREATE TABLE ref(f1 int, f2 int, f3 int)
2336 PARTITION BY list(f1);
2337 CREATE TABLE ref1_2 PARTITION OF ref FOR VALUES IN (1, 2) PARTITION BY list (f2);
2338 CREATE TABLE ref1 PARTITION OF ref1_2 FOR VALUES IN (1);
2339 CREATE TABLE ref2 PARTITION OF ref1_2 FOR VALUES IN (2) PARTITION BY list (f2);
2340 CREATE TABLE ref22 PARTITION OF ref2 FOR VALUES IN (2);
2341 ALTER TABLE ref ADD FOREIGN KEY(f1,f2) REFERENCES pt;
2342 INSERT INTO pt VALUES(1,2,3);
2343 INSERT INTO ref VALUES(1,2,3);
2344 ALTER TABLE ref22 ALTER CONSTRAINT ref_f1_f2_fkey
2345 DEFERRABLE INITIALLY IMMEDIATE; -- fails
2346 ERROR: cannot alter constraint "ref_f1_f2_fkey" on relation "ref22"
2347 DETAIL: Constraint "ref_f1_f2_fkey" is derived from constraint "ref_f1_f2_fkey" of relation "ref".
2348 HINT: You may alter the constraint it derives from, instead.
2349 ALTER TABLE ref ALTER CONSTRAINT ref_f1_f2_fkey
2350 DEFERRABLE INITIALLY DEFERRED;
2356 -- Partitioned table at referenced end
2357 CREATE TABLE pt(f1 int, f2 int, f3 int, PRIMARY KEY(f1,f2))
2358 PARTITION BY LIST(f1);
2359 CREATE TABLE pt1 PARTITION OF pt FOR VALUES IN (1);
2360 CREATE TABLE pt2 PARTITION OF pt FOR VALUES IN (2);
2361 CREATE TABLE ref(f1 int, f2 int, f3 int);
2362 ALTER TABLE ref ADD FOREIGN KEY(f1,f2) REFERENCES pt;
2363 ALTER TABLE ref ALTER CONSTRAINT ref_f1_f2_fkey
2364 DEFERRABLE INITIALLY DEFERRED;
2365 INSERT INTO pt VALUES(1,2,3);
2366 INSERT INTO ref VALUES(1,2,3);
2372 -- Multi-level partitioning at at referenced end
2373 CREATE TABLE pt(f1 int, f2 int, f3 int, PRIMARY KEY(f1,f2))
2374 PARTITION BY LIST(f1);
2375 CREATE TABLE pt1_2 PARTITION OF pt FOR VALUES IN (1, 2) PARTITION BY LIST (f1);
2376 CREATE TABLE pt1 PARTITION OF pt1_2 FOR VALUES IN (1);
2377 CREATE TABLE pt2 PARTITION OF pt1_2 FOR VALUES IN (2);
2378 CREATE TABLE ref(f1 int, f2 int, f3 int);
2379 ALTER TABLE ref ADD FOREIGN KEY(f1,f2) REFERENCES pt;
2380 ALTER TABLE ref ALTER CONSTRAINT ref_f1_f2_fkey1
2381 DEFERRABLE INITIALLY DEFERRED; -- fails
2382 ERROR: cannot alter constraint "ref_f1_f2_fkey1" on relation "ref"
2383 DETAIL: Constraint "ref_f1_f2_fkey1" is derived from constraint "ref_f1_f2_fkey" of relation "ref".
2384 HINT: You may alter the constraint it derives from, instead.
2385 ALTER TABLE ref ALTER CONSTRAINT ref_f1_f2_fkey
2386 DEFERRABLE INITIALLY DEFERRED;
2387 INSERT INTO pt VALUES(1,2,3);
2388 INSERT INTO ref VALUES(1,2,3);
2394 DROP SCHEMA fkpart9 CASCADE;
2395 NOTICE: drop cascades to 2 other objects
2396 DETAIL: drop cascades to table pk
2397 drop cascades to table fk
2398 -- Verify ON UPDATE/DELETE behavior
2399 CREATE SCHEMA fkpart6;
2400 SET search_path TO fkpart6;
2401 CREATE TABLE pk (a int PRIMARY KEY) PARTITION BY RANGE (a);
2402 CREATE TABLE pk1 PARTITION OF pk FOR VALUES FROM (1) TO (100) PARTITION BY RANGE (a);
2403 CREATE TABLE pk11 PARTITION OF pk1 FOR VALUES FROM (1) TO (50);
2404 CREATE TABLE pk12 PARTITION OF pk1 FOR VALUES FROM (50) TO (100);
2405 CREATE TABLE fk (a int) PARTITION BY RANGE (a);
2406 CREATE TABLE fk1 PARTITION OF fk FOR VALUES FROM (1) TO (100) PARTITION BY RANGE (a);
2407 CREATE TABLE fk11 PARTITION OF fk1 FOR VALUES FROM (1) TO (10);
2408 CREATE TABLE fk12 PARTITION OF fk1 FOR VALUES FROM (10) TO (100);
2409 ALTER TABLE fk ADD FOREIGN KEY (a) REFERENCES pk ON UPDATE CASCADE ON DELETE CASCADE;
2410 CREATE TABLE fk_d PARTITION OF fk DEFAULT;
2411 INSERT INTO pk VALUES (1);
2412 INSERT INTO fk VALUES (1);
2413 UPDATE pk SET a = 20;
2414 SELECT tableoid::regclass, * FROM fk;
2420 DELETE FROM pk WHERE a = 20;
2421 SELECT tableoid::regclass, * FROM fk;
2428 INSERT INTO pk VALUES (20), (50);
2429 CREATE TABLE fk (a int) PARTITION BY RANGE (a);
2430 CREATE TABLE fk1 PARTITION OF fk FOR VALUES FROM (1) TO (100) PARTITION BY RANGE (a);
2431 CREATE TABLE fk11 PARTITION OF fk1 FOR VALUES FROM (1) TO (10);
2432 CREATE TABLE fk12 PARTITION OF fk1 FOR VALUES FROM (10) TO (100);
2433 ALTER TABLE fk ADD FOREIGN KEY (a) REFERENCES pk ON UPDATE SET NULL ON DELETE SET NULL;
2434 CREATE TABLE fk_d PARTITION OF fk DEFAULT;
2435 INSERT INTO fk VALUES (20), (50);
2436 UPDATE pk SET a = 21 WHERE a = 20;
2437 DELETE FROM pk WHERE a = 50;
2438 SELECT tableoid::regclass, * FROM fk;
2447 INSERT INTO pk VALUES (20), (30), (50);
2448 CREATE TABLE fk (id int, a int DEFAULT 50) PARTITION BY RANGE (a);
2449 CREATE TABLE fk1 PARTITION OF fk FOR VALUES FROM (1) TO (100) PARTITION BY RANGE (a);
2450 CREATE TABLE fk11 PARTITION OF fk1 FOR VALUES FROM (1) TO (10);
2451 CREATE TABLE fk12 PARTITION OF fk1 FOR VALUES FROM (10) TO (100);
2452 ALTER TABLE fk ADD FOREIGN KEY (a) REFERENCES pk ON UPDATE SET DEFAULT ON DELETE SET DEFAULT;
2453 CREATE TABLE fk_d PARTITION OF fk DEFAULT;
2454 INSERT INTO fk VALUES (1, 20), (2, 30);
2455 DELETE FROM pk WHERE a = 20 RETURNING *;
2461 UPDATE pk SET a = 90 WHERE a = 30 RETURNING *;
2467 SELECT tableoid::regclass, * FROM fk;
2469 ----------+----+----
2476 INSERT INTO pk VALUES (20), (30);
2477 CREATE TABLE fk (a int DEFAULT 50) PARTITION BY RANGE (a);
2478 CREATE TABLE fk1 PARTITION OF fk FOR VALUES FROM (1) TO (100) PARTITION BY RANGE (a);
2479 CREATE TABLE fk11 PARTITION OF fk1 FOR VALUES FROM (1) TO (10);
2480 CREATE TABLE fk12 PARTITION OF fk1 FOR VALUES FROM (10) TO (100);
2481 ALTER TABLE fk ADD FOREIGN KEY (a) REFERENCES pk ON UPDATE RESTRICT ON DELETE RESTRICT;
2482 CREATE TABLE fk_d PARTITION OF fk DEFAULT;
2483 INSERT INTO fk VALUES (20), (30);
2484 DELETE FROM pk WHERE a = 20;
2485 ERROR: update or delete on table "pk11" violates foreign key constraint "fk_a_fkey2" on table "fk"
2486 DETAIL: Key (a)=(20) is still referenced from table "fk".
2487 UPDATE pk SET a = 90 WHERE a = 30;
2488 ERROR: update or delete on table "pk11" violates foreign key constraint "fk_a_fkey2" on table "fk"
2489 DETAIL: Key (a)=(30) is still referenced from table "fk".
2490 SELECT tableoid::regclass, * FROM fk;
2498 -- test for reported bug: relispartition not set
2499 -- https://postgr.es/m/CA+HiwqHMsRtRYRWYTWavKJ8x14AFsv7bmAV46mYwnfD3vy8goQ@mail.gmail.com
2500 CREATE SCHEMA fkpart7
2501 CREATE TABLE pkpart (a int) PARTITION BY LIST (a)
2502 CREATE TABLE pkpart1 PARTITION OF pkpart FOR VALUES IN (1);
2503 ALTER TABLE fkpart7.pkpart1 ADD PRIMARY KEY (a);
2504 ALTER TABLE fkpart7.pkpart ADD PRIMARY KEY (a);
2505 CREATE TABLE fkpart7.fk (a int REFERENCES fkpart7.pkpart);
2506 DROP SCHEMA fkpart7 CASCADE;
2507 NOTICE: drop cascades to 2 other objects
2508 DETAIL: drop cascades to table fkpart7.pkpart
2509 drop cascades to table fkpart7.fk
2510 -- ensure we check partitions are "not used" when dropping constraints
2511 CREATE SCHEMA fkpart8
2512 CREATE TABLE tbl1(f1 int PRIMARY KEY)
2513 CREATE TABLE tbl2(f1 int REFERENCES tbl1 DEFERRABLE INITIALLY DEFERRED) PARTITION BY RANGE(f1)
2514 CREATE TABLE tbl2_p1 PARTITION OF tbl2 FOR VALUES FROM (minvalue) TO (maxvalue);
2515 INSERT INTO fkpart8.tbl1 VALUES(1);
2517 INSERT INTO fkpart8.tbl2 VALUES(1);
2518 ALTER TABLE fkpart8.tbl2 DROP CONSTRAINT tbl2_f1_fkey;
2519 ERROR: cannot ALTER TABLE "tbl2_p1" because it has pending trigger events
2521 DROP SCHEMA fkpart8 CASCADE;
2522 NOTICE: drop cascades to 2 other objects
2523 DETAIL: drop cascades to table fkpart8.tbl1
2524 drop cascades to table fkpart8.tbl2
2525 -- ensure FK referencing a multi-level partitioned table are
2526 -- enforce reference to sub-children.
2527 CREATE SCHEMA fkpart9
2528 CREATE TABLE pk (a INT PRIMARY KEY) PARTITION BY RANGE (a)
2530 fk_a INT REFERENCES pk(a) ON DELETE CASCADE
2532 CREATE TABLE pk1 PARTITION OF pk FOR VALUES FROM (30) TO (50) PARTITION BY RANGE (a)
2533 CREATE TABLE pk11 PARTITION OF pk1 FOR VALUES FROM (30) TO (40);
2534 INSERT INTO fkpart9.pk VALUES (35);
2535 INSERT INTO fkpart9.fk VALUES (35);
2536 DELETE FROM fkpart9.pk WHERE a=35;
2537 SELECT * FROM fkpart9.pk;
2542 SELECT * FROM fkpart9.fk;
2547 DROP SCHEMA fkpart9 CASCADE;
2548 NOTICE: drop cascades to 2 other objects
2549 DETAIL: drop cascades to table fkpart9.pk
2550 drop cascades to table fkpart9.fk
2551 -- test that ri_Check_Pk_Match() scans the correct partition for a deferred
2552 -- ON DELETE/UPDATE NO ACTION constraint
2553 CREATE SCHEMA fkpart10
2554 CREATE TABLE tbl1(f1 int PRIMARY KEY) PARTITION BY RANGE(f1)
2555 CREATE TABLE tbl1_p1 PARTITION OF tbl1 FOR VALUES FROM (minvalue) TO (1)
2556 CREATE TABLE tbl1_p2 PARTITION OF tbl1 FOR VALUES FROM (1) TO (maxvalue)
2557 CREATE TABLE tbl2(f1 int REFERENCES tbl1 DEFERRABLE INITIALLY DEFERRED);
2558 INSERT INTO fkpart10.tbl1 VALUES (0), (1);
2559 INSERT INTO fkpart10.tbl2 VALUES (0), (1);
2561 DELETE FROM fkpart10.tbl1 WHERE f1 = 0;
2562 UPDATE fkpart10.tbl1 SET f1 = 2 WHERE f1 = 1;
2563 INSERT INTO fkpart10.tbl1 VALUES (0), (1);
2565 DROP SCHEMA fkpart10 CASCADE;
2566 NOTICE: drop cascades to 2 other objects
2567 DETAIL: drop cascades to table fkpart10.tbl1
2568 drop cascades to table fkpart10.tbl2