Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2ba5b2d

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 914140e commit 2ba5b2d

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/bin/pg_dump/pg_dump.c

+32-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);
@@ -7517,25 +7518,20 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
75177518
reftable = findTableByOid(constrinfo[j].confrelid);
75187519
if (reftable && reftable->relkind == RELKIND_PARTITIONED_TABLE)
75197520
{
7520-
IndxInfo *refidx;
75217521
Oid indexOid = atooid(PQgetvalue(res, j, i_conindid));
75227522

75237523
if (indexOid != InvalidOid)
75247524
{
75257525
for (int k = 0; k < reftable->numIndexes; k++)
75267526
{
7527-
SimplePtrListCell *cell;
7527+
IndxInfo *refidx;
75287528

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

75337533
refidx = &reftable->indexes[k];
7534-
for (cell = refidx->partattaches.head; cell;
7535-
cell = cell->next)
7536-
addObjectDependency(&constrinfo[j].dobj,
7537-
((DumpableObject *)
7538-
cell->ptr)->dumpId);
7534+
addConstrChildIdxDeps(&constrinfo[j].dobj, refidx);
75397535
break;
75407536
}
75417537
}
@@ -7548,6 +7544,35 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
75487544
destroyPQExpBuffer(query);
75497545
}
75507546

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

0 commit comments

Comments
 (0)