Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2af39c3

Browse files
alvherremichaelpq
andcommitted
Dump foreign keys on partitioned tables
The patch that ended up as commit 3de241d ("Foreign keys on partitioned tables") lacked pg_dump tests, so the pg_dump code that was there to support it inadvertently stopped working when in later development I modified the backend code not to emit pg_trigger rows for the partitioned table itself. Bug analysis and code fix is by Michaël. I (Álvaro) added the test. Reported-by: amul sul <[email protected]> Co-authored-by: Michaël Paquier <[email protected]> Co-authored-by: Álvaro Herrera <[email protected]> Discussion: https://postgr.es/m/CAAJ_b94n=UsNVhgs97vCaWEZAMe-tGDRVuZ73oePQH=eaJKGSA@mail.gmail.com
1 parent 5586e42 commit 2af39c3

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7131,7 +7131,12 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
71317131
{
71327132
TableInfo *tbinfo = &tblinfo[i];
71337133

7134-
if (!tbinfo->hastriggers ||
7134+
/*
7135+
* For partitioned tables, foreign keys have no triggers so they
7136+
* must be included anyway in case some foreign keys are defined.
7137+
*/
7138+
if ((!tbinfo->hastriggers &&
7139+
tbinfo->relkind != RELKIND_PARTITIONED_TABLE) ||
71357140
!(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
71367141
continue;
71377142

src/bin/pg_dump/t/002_pg_dump.pl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,25 @@
631631
},
632632
},
633633

634+
'ALTER TABLE (partitioned) ADD CONSTRAINT ... FOREIGN KEY' => {
635+
create_order => 4,
636+
create_sql => 'CREATE TABLE dump_test.test_table_fk (
637+
col1 int references dump_test.test_table)
638+
PARTITION BY RANGE (col1);
639+
CREATE TABLE dump_test.test_table_fk_1
640+
PARTITION OF dump_test.test_table_fk
641+
FOR VALUES FROM (0) TO (10);',
642+
regexp => qr/
643+
\QADD CONSTRAINT test_table_fk_col1_fkey FOREIGN KEY (col1) REFERENCES dump_test.test_table\E
644+
/xm,
645+
like => {
646+
%full_runs, %dump_test_schema_runs, section_post_data => 1,
647+
},
648+
unlike => {
649+
exclude_dump_test_schema => 1,
650+
},
651+
},
652+
634653
'ALTER TABLE ONLY test_table ALTER COLUMN col1 SET STATISTICS 90' => {
635654
create_order => 93,
636655
create_sql =>

0 commit comments

Comments
 (0)