Commit 2ba5b2db authored by Alvaro Herrera's avatar Alvaro Herrera

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: default avatarTom 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
parent 914140e8
...@@ -235,6 +235,7 @@ static DumpableObject *createBoundaryObjects(void); ...@@ -235,6 +235,7 @@ static DumpableObject *createBoundaryObjects(void);
static void addBoundaryDependencies(DumpableObject **dobjs, int numObjs, static void addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
DumpableObject *boundaryObjs); DumpableObject *boundaryObjs);
static void addConstrChildIdxDeps(DumpableObject *dobj, IndxInfo *refidx);
static void getDomainConstraints(Archive *fout, TypeInfo *tyinfo); static void getDomainConstraints(Archive *fout, TypeInfo *tyinfo);
static void getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind); static void getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind);
static void makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo); static void makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo);
...@@ -7517,25 +7518,20 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables) ...@@ -7517,25 +7518,20 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
reftable = findTableByOid(constrinfo[j].confrelid); reftable = findTableByOid(constrinfo[j].confrelid);
if (reftable && reftable->relkind == RELKIND_PARTITIONED_TABLE) if (reftable && reftable->relkind == RELKIND_PARTITIONED_TABLE)
{ {
IndxInfo *refidx;
Oid indexOid = atooid(PQgetvalue(res, j, i_conindid)); Oid indexOid = atooid(PQgetvalue(res, j, i_conindid));
if (indexOid != InvalidOid) if (indexOid != InvalidOid)
{ {
for (int k = 0; k < reftable->numIndexes; k++) for (int k = 0; k < reftable->numIndexes; k++)
{ {
SimplePtrListCell *cell; IndxInfo *refidx;
/* not our index? */ /* not our index? */
if (reftable->indexes[k].dobj.catId.oid != indexOid) if (reftable->indexes[k].dobj.catId.oid != indexOid)
continue; continue;
refidx = &reftable->indexes[k]; refidx = &reftable->indexes[k];
for (cell = refidx->partattaches.head; cell; addConstrChildIdxDeps(&constrinfo[j].dobj, refidx);
cell = cell->next)
addObjectDependency(&constrinfo[j].dobj,
((DumpableObject *)
cell->ptr)->dumpId);
break; break;
} }
} }
...@@ -7548,6 +7544,35 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables) ...@@ -7548,6 +7544,35 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
destroyPQExpBuffer(query); destroyPQExpBuffer(query);
} }
/*
* addConstrChildIdxDeps
*
* Recursive subroutine for getConstraints
*
* Given an object representing a foreign key constraint and an index on the
* partitioned table it references, mark the constraint object as dependent
* on the DO_INDEX_ATTACH object of each index partition, recursively
* drilling down to their partitions if any. This ensures that the FK is not
* restored until the index is fully marked valid.
*/
static void
addConstrChildIdxDeps(DumpableObject *dobj, IndxInfo *refidx)
{
SimplePtrListCell *cell;
Assert(dobj->objType == DO_FK_CONSTRAINT);
for (cell = refidx->partattaches.head; cell; cell = cell->next)
{
IndexAttachInfo *attach = (IndexAttachInfo *) cell->ptr;
addObjectDependency(dobj, attach->dobj.dumpId);
if (attach->partitionIdx->partattaches.head != NULL)
addConstrChildIdxDeps(dobj, attach->partitionIdx);
}
}
/* /*
* getDomainConstraints * getDomainConstraints
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment