Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3dadcb2

Browse files
committed
pg_dump: fix dependencies on FKs to partitioned tables
Parallel-restoring a foreign key that references a partitioned tablewith 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 postgrespg_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 partitionsaren'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 asdependent on the first level of index-attach dump objects; the bug isfixed 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.usBackpatch: 12-master
1 parent42566a2 commit3dadcb2

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

‎src/bin/pg_dump/pg_dump.c

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ static DumpableObject *createBoundaryObjects(void);
241241
static void addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
242242
DumpableObject *boundaryObjs);
243243

244+
static void addConstrChildIdxDeps(DumpableObject *dobj, IndxInfo *refidx);
244245
static void getDomainConstraints(Archive *fout, TypeInfo *tyinfo);
245246
static void getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind);
246247
static void makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo);
@@ -7391,25 +7392,20 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
73917392
reftable = findTableByOid(constrinfo[j].confrelid);
73927393
if (reftable && reftable->relkind == RELKIND_PARTITIONED_TABLE)
73937394
{
7394-
IndxInfo *refidx;
73957395
OidindexOid = atooid(PQgetvalue(res, j, i_conindid));
73967396

73977397
if (indexOid != InvalidOid)
73987398
{
73997399
for (int k = 0; k < reftable->numIndexes; k++)
74007400
{
7401-
SimplePtrListCell *cell;
7401+
IndxInfo *refidx;
74027402

74037403
/* not our index? */
74047404
if (reftable->indexes[k].dobj.catId.oid != indexOid)
74057405
continue;
74067406

74077407
refidx = &reftable->indexes[k];
7408-
for (cell = refidx->partattaches.head; cell;
7409-
cell = cell->next)
7410-
addObjectDependency(&constrinfo[j].dobj,
7411-
((DumpableObject *)
7412-
cell->ptr)->dumpId);
7408+
addConstrChildIdxDeps(&constrinfo[j].dobj, refidx);
74137409
break;
74147410
}
74157411
}
@@ -7422,6 +7418,37 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
74227418
destroyPQExpBuffer(query);
74237419
}
74247420

7421+
/*
7422+
* addConstrChildIdxDeps
7423+
*
7424+
* Recursive subroutine for getConstraints
7425+
*
7426+
* Given an object representing a foreign key constraint and an index on the
7427+
* partitioned table it references, mark the constraint object as dependent
7428+
* on each partition, recursing to children until all leaves are found.
7429+
* This ensures that the FK is not restored until the index is fully marked
7430+
* valid.
7431+
*/
7432+
static void
7433+
addConstrChildIdxDeps(DumpableObject *dobj, IndxInfo *refidx)
7434+
{
7435+
SimplePtrListCell *cell;
7436+
7437+
Assert(dobj->objType == DO_FK_CONSTRAINT);
7438+
7439+
for (cell = refidx->partattaches.head; cell; cell = cell->next)
7440+
{
7441+
DumpableObject *childobj = (DumpableObject *) cell->ptr;
7442+
IndexAttachInfo *attach;
7443+
7444+
addObjectDependency(dobj, childobj->dumpId);
7445+
7446+
attach = (IndexAttachInfo *) childobj;
7447+
if (attach->partitionIdx->partattaches.head != NULL)
7448+
addConstrChildIdxDeps(dobj, attach->partitionIdx);
7449+
}
7450+
}
7451+
74257452
/*
74267453
* getDomainConstraints
74277454
*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp