Skip to content

Commit b7cc21c

Browse files
committed
pg_dump: fix dependencies on FKs to partitioned tables
Parallel-restoring a foreign key that references a partitioned table with several levels of partitions can fail: pg_restore: while PROCESSING TOC: pg_restore: from TOC entry 6684; 2606 29166 FK CONSTRAINT fk fk_a_fkey postgres pg_restore: error: could not execute query: ERROR: there is no unique constraint matching given keys for referenced table "pk" Command was: ALTER TABLE fkpart3.fk ADD CONSTRAINT fk_a_fkey FOREIGN KEY (a) REFERENCES fkpart3.pk(a); This happens in parallel restore mode because some index partitions aren't yet attached to the topmost partitioned index that the FK uses, and so the index is still invalid. The current code marks the FK as dependent on the first level of index-attach dump objects; the bug is fixed by recursively marking the FK on their children. Backpatch to 12, where FKs to partitioned tables were introduced. Reported-by: Tom Lane <tgl@sss.pgh.pa.us> Author: Álvaro Herrera <alvherre@alvh.no-ip.org> Discussion: https://postgr.es/m/3170626.1594842723@sss.pgh.pa.us Backpatch: 12-master
1 parent 1c6066f commit b7cc21c

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

src/bin/pg_dump/pg_dump.c

+34-7
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ static DumpableObject *createBoundaryObjects(void);
235235
static void addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
236236
DumpableObject *boundaryObjs);
237237

238+
static void addConstrChildIdxDeps(DumpableObject *dobj, IndxInfo *refidx);
238239
static void getDomainConstraints(Archive *fout, TypeInfo *tyinfo);
239240
static void getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind);
240241
static void makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo);
@@ -7514,25 +7515,20 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
75147515
reftable = findTableByOid(constrinfo[j].confrelid);
75157516
if (reftable && reftable->relkind == RELKIND_PARTITIONED_TABLE)
75167517
{
7517-
IndxInfo *refidx;
75187518
Oid indexOid = atooid(PQgetvalue(res, j, i_conindid));
75197519

75207520
if (indexOid != InvalidOid)
75217521
{
75227522
for (int k = 0; k < reftable->numIndexes; k++)
75237523
{
7524-
SimplePtrListCell *cell;
7524+
IndxInfo *refidx;
75257525

75267526
/* not our index? */
75277527
if (reftable->indexes[k].dobj.catId.oid != indexOid)
75287528
continue;
75297529

75307530
refidx = &reftable->indexes[k];
7531-
for (cell = refidx->partattaches.head; cell;
7532-
cell = cell->next)
7533-
addObjectDependency(&constrinfo[j].dobj,
7534-
((DumpableObject *)
7535-
cell->ptr)->dumpId);
7531+
addConstrChildIdxDeps(&constrinfo[j].dobj, refidx);
75367532
break;
75377533
}
75387534
}
@@ -7545,6 +7541,37 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
75457541
destroyPQExpBuffer(query);
75467542
}
75477543

7544+
/*
7545+
* addConstrChildIdxDeps
7546+
*
7547+
* Recursive subroutine for getConstraints
7548+
*
7549+
* Given an object representing a foreign key constraint and an index on the
7550+
* partitioned table it references, mark the constraint object as dependent
7551+
* on each partition, recursing to children until all leaves are found.
7552+
* This ensures that the FK is not restored until the index is fully marked
7553+
* valid.
7554+
*/
7555+
static void
7556+
addConstrChildIdxDeps(DumpableObject *dobj, IndxInfo *refidx)
7557+
{
7558+
SimplePtrListCell *cell;
7559+
7560+
Assert(dobj->objType == DO_FK_CONSTRAINT);
7561+
7562+
for (cell = refidx->partattaches.head; cell; cell = cell->next)
7563+
{
7564+
DumpableObject *childobj = (DumpableObject *) cell->ptr;
7565+
IndexAttachInfo *attach;
7566+
7567+
addObjectDependency(dobj, childobj->dumpId);
7568+
7569+
attach = (IndexAttachInfo *) childobj;
7570+
if (attach->partitionIdx->partattaches.head != NULL)
7571+
addConstrChildIdxDeps(dobj, attach->partitionIdx);
7572+
}
7573+
}
7574+
75487575
/*
75497576
* getDomainConstraints
75507577
*

0 commit comments

Comments
 (0)