diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 93ad49e59fd..9ef2533de9f 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -11350,7 +11350,7 @@ CloneFkReferenced(Relation parentRel, Relation partitionRel) AttrMap *attmap; ListCell *cell; SysScanDesc scan; - ScanKeyData key[2]; + ScanKeyData key[1]; HeapTuple tuple; List *clone = NIL; Relation trigrel; @@ -11368,17 +11368,15 @@ CloneFkReferenced(Relation parentRel, Relation partitionRel) ScanKeyInit(&key[0], Anum_pg_constraint_confrelid, BTEqualStrategyNumber, F_OIDEQ, ObjectIdGetDatum(RelationGetRelid(parentRel))); - ScanKeyInit(&key[1], - Anum_pg_constraint_contype, BTEqualStrategyNumber, - F_CHAREQ, CharGetDatum(CONSTRAINT_FOREIGN)); - /* This is a seqscan, as we don't have a usable index ... */ - scan = systable_beginscan(pg_constraint, InvalidOid, true, - NULL, 2, key); + + scan = systable_beginscan(pg_constraint, ConstraintForeignRelIndexId, true, + NULL, 1, key); while ((tuple = systable_getnext(scan)) != NULL) { Form_pg_constraint constrForm = (Form_pg_constraint) GETSTRUCT(tuple); - clone = lappend_oid(clone, constrForm->oid); + if (constrForm->contype == CONSTRAINT_FOREIGN) + clone = lappend_oid(clone, constrForm->oid); } systable_endscan(scan); table_close(pg_constraint, RowShareLock); @@ -19584,7 +19582,9 @@ ATPrepChangePersistence(AlteredTableInfo *tab, Relation rel, bool toLogged) BTEqualStrategyNumber, F_OIDEQ, ObjectIdGetDatum(RelationGetRelid(rel))); scan = systable_beginscan(pg_constraint, - toLogged ? ConstraintRelidTypidNameIndexId : InvalidOid, + toLogged ? + ConstraintRelidTypidNameIndexId : + ConstraintForeignRelIndexId, true, NULL, 1, skey); while (HeapTupleIsValid(tuple = systable_getnext(scan))) @@ -22668,7 +22668,7 @@ GetParentedForeignKeyRefs(Relation partition) Relation pg_constraint; HeapTuple tuple; SysScanDesc scan; - ScanKeyData key[2]; + ScanKeyData key[1]; List *constraints = NIL; /* @@ -22685,16 +22685,15 @@ GetParentedForeignKeyRefs(Relation partition) ScanKeyInit(&key[0], Anum_pg_constraint_confrelid, BTEqualStrategyNumber, F_OIDEQ, ObjectIdGetDatum(RelationGetRelid(partition))); - ScanKeyInit(&key[1], - Anum_pg_constraint_contype, BTEqualStrategyNumber, - F_CHAREQ, CharGetDatum(CONSTRAINT_FOREIGN)); - /* XXX This is a seqscan, as we don't have a usable index */ - scan = systable_beginscan(pg_constraint, InvalidOid, true, NULL, 2, key); + scan = systable_beginscan(pg_constraint, ConstraintForeignRelIndexId, true, NULL, 1, key); while ((tuple = systable_getnext(scan)) != NULL) { Form_pg_constraint constrForm = (Form_pg_constraint) GETSTRUCT(tuple); + if (constrForm->contype != CONSTRAINT_FOREIGN) + continue; + /* * We only need to process constraints that are part of larger ones. */ diff --git a/src/include/catalog/pg_constraint.h b/src/include/catalog/pg_constraint.h index e8d27546ed9..5c108255dc1 100644 --- a/src/include/catalog/pg_constraint.h +++ b/src/include/catalog/pg_constraint.h @@ -185,6 +185,8 @@ DECLARE_UNIQUE_INDEX(pg_constraint_conrelid_contypid_conname_index, 2665, Constr DECLARE_INDEX(pg_constraint_contypid_index, 2666, ConstraintTypidIndexId, pg_constraint, btree(contypid oid_ops)); DECLARE_UNIQUE_INDEX_PKEY(pg_constraint_oid_index, 2667, ConstraintOidIndexId, pg_constraint, btree(oid oid_ops)); DECLARE_INDEX(pg_constraint_conparentid_index, 2579, ConstraintParentIndexId, pg_constraint, btree(conparentid oid_ops)); +DECLARE_INDEX(pg_constraint_confrelid_index, 2775, ConstraintForeignRelIndexId, pg_constraint, btree(confrelid oid_ops)); + MAKE_SYSCACHE(CONSTROID, pg_constraint_oid_index, 16);