@@ -7115,6 +7115,7 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
7115
7115
indxinfo[j].indisclustered = (PQgetvalue(res, j, i_indisclustered)[0] == 't');
7116
7116
indxinfo[j].indisreplident = (PQgetvalue(res, j, i_indisreplident)[0] == 't');
7117
7117
indxinfo[j].parentidx = atooid(PQgetvalue(res, j, i_parentidx));
7118
+ indxinfo[j].partattaches = (SimplePtrList) { NULL, NULL };
7118
7119
contype = *(PQgetvalue(res, j, i_contype));
7119
7120
7120
7121
if (contype == 'p' || contype == 'u' || contype == 'x')
@@ -7256,6 +7257,7 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
7256
7257
i_conoid,
7257
7258
i_conname,
7258
7259
i_confrelid,
7260
+ i_conindid,
7259
7261
i_condef;
7260
7262
int ntups;
7261
7263
@@ -7281,7 +7283,7 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
7281
7283
resetPQExpBuffer(query);
7282
7284
if (fout->remoteVersion >= 110000)
7283
7285
appendPQExpBuffer(query,
7284
- "SELECT tableoid, oid, conname, confrelid, "
7286
+ "SELECT tableoid, oid, conname, confrelid, conindid, "
7285
7287
"pg_catalog.pg_get_constraintdef(oid) AS condef "
7286
7288
"FROM pg_catalog.pg_constraint "
7287
7289
"WHERE conrelid = '%u'::pg_catalog.oid "
@@ -7290,7 +7292,7 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
7290
7292
tbinfo->dobj.catId.oid);
7291
7293
else
7292
7294
appendPQExpBuffer(query,
7293
- "SELECT tableoid, oid, conname, confrelid, "
7295
+ "SELECT tableoid, oid, conname, confrelid, 0 as conindid, "
7294
7296
"pg_catalog.pg_get_constraintdef(oid) AS condef "
7295
7297
"FROM pg_catalog.pg_constraint "
7296
7298
"WHERE conrelid = '%u'::pg_catalog.oid "
@@ -7304,12 +7306,15 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
7304
7306
i_conoid = PQfnumber(res, "oid");
7305
7307
i_conname = PQfnumber(res, "conname");
7306
7308
i_confrelid = PQfnumber(res, "confrelid");
7309
+ i_conindid = PQfnumber(res, "conindid");
7307
7310
i_condef = PQfnumber(res, "condef");
7308
7311
7309
7312
constrinfo = (ConstraintInfo *) pg_malloc(ntups * sizeof(ConstraintInfo));
7310
7313
7311
7314
for (j = 0; j < ntups; j++)
7312
7315
{
7316
+ TableInfo *reftable;
7317
+
7313
7318
constrinfo[j].dobj.objType = DO_FK_CONSTRAINT;
7314
7319
constrinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid));
7315
7320
constrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid));
@@ -7326,6 +7331,39 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
7326
7331
constrinfo[j].condeferred = false;
7327
7332
constrinfo[j].conislocal = true;
7328
7333
constrinfo[j].separate = true;
7334
+
7335
+ /*
7336
+ * Restoring an FK that points to a partitioned table requires
7337
+ * that all partition indexes have been attached beforehand.
7338
+ * Ensure that happens by making the constraint depend on each
7339
+ * index partition attach object.
7340
+ */
7341
+ reftable = findTableByOid(constrinfo[j].confrelid);
7342
+ if (reftable && reftable->relkind == RELKIND_PARTITIONED_TABLE)
7343
+ {
7344
+ IndxInfo *refidx;
7345
+ Oid indexOid = atooid(PQgetvalue(res, j, i_conindid));
7346
+
7347
+ if (indexOid != InvalidOid)
7348
+ {
7349
+ for (int k = 0; k < reftable->numIndexes; k++)
7350
+ {
7351
+ SimplePtrListCell *cell;
7352
+
7353
+ /* not our index? */
7354
+ if (reftable->indexes[k].dobj.catId.oid != indexOid)
7355
+ continue;
7356
+
7357
+ refidx = &reftable->indexes[k];
7358
+ for (cell = refidx->partattaches.head; cell;
7359
+ cell = cell->next)
7360
+ addObjectDependency(&constrinfo[j].dobj,
7361
+ ((DumpableObject *)
7362
+ cell->ptr)->dumpId);
7363
+ break;
7364
+ }
7365
+ }
7366
+ }
7329
7367
}
7330
7368
7331
7369
PQclear(res);
0 commit comments