indxinfo[j].indisclustered = (PQgetvalue(res, j, i_indisclustered)[0] == 't');
indxinfo[j].indisreplident = (PQgetvalue(res, j, i_indisreplident)[0] == 't');
indxinfo[j].parentidx = atooid(PQgetvalue(res, j, i_parentidx));
+ indxinfo[j].partattaches = (SimplePtrList) { NULL, NULL };
contype = *(PQgetvalue(res, j, i_contype));
if (contype == 'p' || contype == 'u' || contype == 'x')
i_conoid,
i_conname,
i_confrelid,
+ i_conindid,
i_condef;
int ntups;
resetPQExpBuffer(query);
if (fout->remoteVersion >= 110000)
appendPQExpBuffer(query,
- "SELECT tableoid, oid, conname, confrelid, "
+ "SELECT tableoid, oid, conname, confrelid, conindid, "
"pg_catalog.pg_get_constraintdef(oid) AS condef "
"FROM pg_catalog.pg_constraint "
"WHERE conrelid = '%u'::pg_catalog.oid "
tbinfo->dobj.catId.oid);
else
appendPQExpBuffer(query,
- "SELECT tableoid, oid, conname, confrelid, "
+ "SELECT tableoid, oid, conname, confrelid, 0 as conindid, "
"pg_catalog.pg_get_constraintdef(oid) AS condef "
"FROM pg_catalog.pg_constraint "
"WHERE conrelid = '%u'::pg_catalog.oid "
i_conoid = PQfnumber(res, "oid");
i_conname = PQfnumber(res, "conname");
i_confrelid = PQfnumber(res, "confrelid");
+ i_conindid = PQfnumber(res, "conindid");
i_condef = PQfnumber(res, "condef");
constrinfo = (ConstraintInfo *) pg_malloc(ntups * sizeof(ConstraintInfo));
for (j = 0; j < ntups; j++)
{
+ TableInfo *reftable;
+
constrinfo[j].dobj.objType = DO_FK_CONSTRAINT;
constrinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid));
constrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid));
constrinfo[j].condeferred = false;
constrinfo[j].conislocal = true;
constrinfo[j].separate = true;
+
+ /*
+ * Restoring an FK that points to a partitioned table requires
+ * that all partition indexes have been attached beforehand.
+ * Ensure that happens by making the constraint depend on each
+ * index partition attach object.
+ */
+ reftable = findTableByOid(constrinfo[j].confrelid);
+ if (reftable && reftable->relkind == RELKIND_PARTITIONED_TABLE)
+ {
+ IndxInfo *refidx;
+ Oid indexOid = atooid(PQgetvalue(res, j, i_conindid));
+
+ if (indexOid != InvalidOid)
+ {
+ for (int k = 0; k < reftable->numIndexes; k++)
+ {
+ SimplePtrListCell *cell;
+
+ /* not our index? */
+ if (reftable->indexes[k].dobj.catId.oid != indexOid)
+ continue;
+
+ refidx = &reftable->indexes[k];
+ for (cell = refidx->partattaches.head; cell;
+ cell = cell->next)
+ addObjectDependency(&constrinfo[j].dobj,
+ ((DumpableObject *)
+ cell->ptr)->dumpId);
+ break;
+ }
+ }
+ }
}
PQclear(res);
*
* Simple list facilities for frontend code
*
- * Data structures for simple lists of OIDs and strings. The support for
- * these is very primitive compared to the backend's List facilities, but
- * it's all we need in, eg, pg_dump.
+ * Data structures for simple lists of OIDs, strings, and pointers. The
+ * support for these is very primitive compared to the backend's List
+ * facilities, but it's all we need in, eg, pg_dump.
*
*
* Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
SimpleStringListCell *tail;
} SimpleStringList;
+typedef struct SimplePtrListCell
+{
+ struct SimplePtrListCell *next;
+ void *ptr;
+} SimplePtrListCell;
+
+typedef struct SimplePtrList
+{
+ SimplePtrListCell *head;
+ SimplePtrListCell *tail;
+} SimplePtrList;
extern void simple_oid_list_append(SimpleOidList *list, Oid val);
extern bool simple_oid_list_member(SimpleOidList *list, Oid val);
extern const char *simple_string_list_not_touched(SimpleStringList *list);
+extern void simple_ptr_list_append(SimplePtrList *list, void *val);
+
#endif /* SIMPLE_LIST_H */