Skip to content

Commit 93ad00c

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 42f70cd commit 93ad00c

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/bin/pg_dump/pg_dump.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -7140,7 +7140,12 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
71407140
{
71417141
TableInfo *tbinfo = &tblinfo[i];
71427142

7143-
if (!tbinfo->hastriggers ||
7143+
/*
7144+
* For partitioned tables, foreign keys have no triggers so they
7145+
* must be included anyway in case some foreign keys are defined.
7146+
*/
7147+
if ((!tbinfo->hastriggers &&
7148+
tbinfo->relkind != RELKIND_PARTITIONED_TABLE) ||
71447149
!(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
71457150
continue;
71467151

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

+19
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)