@@ -691,6 +691,8 @@ UpdateIndexRelation(Oid indexoid,
691691 *nonzero to specify a preselected OID.
692692 * parentIndexRelid: if creating an index partition, the OID of the
693693 *parent index; otherwise InvalidOid.
694+ * parentConstraintId: if creating a constraint on a partition, the OID
695+ *of the constraint in the parent; otherwise InvalidOid.
694696 * relFileNode: normally, pass InvalidOid to get new storage. May be
695697 *nonzero to attach an existing valid build.
696698 * indexInfo: same info executor uses to insert into the index
@@ -722,6 +724,7 @@ UpdateIndexRelation(Oid indexoid,
722724 *(only if INDEX_CREATE_ADD_CONSTRAINT is set)
723725 * allow_system_table_mods: allow table to be a system catalog
724726 * is_internal: if true, post creation hook for new index
727+ * constraintId: if not NULL, receives OID of created constraint
725728 *
726729 * Returns the OID of the created index.
727730 */
@@ -730,6 +733,7 @@ index_create(Relation heapRelation,
730733const char * indexRelationName ,
731734Oid indexRelationId ,
732735Oid parentIndexRelid ,
736+ Oid parentConstraintId ,
733737Oid relFileNode ,
734738IndexInfo * indexInfo ,
735739List * indexColNames ,
@@ -742,7 +746,8 @@ index_create(Relation heapRelation,
742746bits16 flags ,
743747bits16 constr_flags ,
744748bool allow_system_table_mods ,
745- bool is_internal )
749+ bool is_internal ,
750+ Oid * constraintId )
746751{
747752Oid heapRelationId = RelationGetRelid (heapRelation );
748753Relation pg_class ;
@@ -989,6 +994,7 @@ index_create(Relation heapRelation,
989994if ((flags & INDEX_CREATE_ADD_CONSTRAINT )!= 0 )
990995{
991996char constraintType ;
997+ ObjectAddress localaddr ;
992998
993999if (isprimary )
9941000constraintType = CONSTRAINT_PRIMARY ;
@@ -1002,14 +1008,17 @@ index_create(Relation heapRelation,
10021008constraintType = 0 ;/* keep compiler quiet */
10031009}
10041010
1005- index_constraint_create (heapRelation ,
1011+ localaddr = index_constraint_create (heapRelation ,
10061012indexRelationId ,
1013+ parentConstraintId ,
10071014indexInfo ,
10081015indexRelationName ,
10091016constraintType ,
10101017constr_flags ,
10111018allow_system_table_mods ,
10121019is_internal );
1020+ if (constraintId )
1021+ * constraintId = localaddr .objectId ;
10131022}
10141023else
10151024{
@@ -1181,6 +1190,8 @@ index_create(Relation heapRelation,
11811190 *
11821191 * heapRelation: table owning the index (must be suitably locked by caller)
11831192 * indexRelationId: OID of the index
1193+ * parentConstraintId: if constraint is on a partition, the OID of the
1194+ *constraint in the parent.
11841195 * indexInfo: same info executor uses to insert into the index
11851196 * constraintName: what it say (generally, should match name of index)
11861197 * constraintType: one of CONSTRAINT_PRIMARY, CONSTRAINT_UNIQUE, or
@@ -1198,6 +1209,7 @@ index_create(Relation heapRelation,
11981209ObjectAddress
11991210index_constraint_create (Relation heapRelation ,
12001211Oid indexRelationId ,
1212+ Oid parentConstraintId ,
12011213IndexInfo * indexInfo ,
12021214const char * constraintName ,
12031215char constraintType ,
@@ -1212,6 +1224,9 @@ index_constraint_create(Relation heapRelation,
12121224bool deferrable ;
12131225bool initdeferred ;
12141226bool mark_as_primary ;
1227+ bool islocal ;
1228+ bool noinherit ;
1229+ int inhcount ;
12151230
12161231deferrable = (constr_flags & INDEX_CONSTR_CREATE_DEFERRABLE )!= 0 ;
12171232initdeferred = (constr_flags & INDEX_CONSTR_CREATE_INIT_DEFERRED )!= 0 ;
@@ -1246,6 +1261,19 @@ index_constraint_create(Relation heapRelation,
12461261deleteDependencyRecordsForClass (RelationRelationId ,indexRelationId ,
12471262RelationRelationId ,DEPENDENCY_AUTO );
12481263
1264+ if (OidIsValid (parentConstraintId ))
1265+ {
1266+ islocal = false;
1267+ inhcount = 1 ;
1268+ noinherit = false;
1269+ }
1270+ else
1271+ {
1272+ islocal = true;
1273+ inhcount = 0 ;
1274+ noinherit = true;
1275+ }
1276+
12491277/*
12501278 * Construct a pg_constraint entry.
12511279 */
@@ -1273,9 +1301,9 @@ index_constraint_create(Relation heapRelation,
12731301NULL ,/* no check constraint */
12741302NULL ,
12751303NULL ,
1276- true, /* islocal */
1277- 0 , /* inhcount */
1278- true, /* noinherit */
1304+ islocal ,
1305+ inhcount ,
1306+ noinherit ,
12791307is_internal );
12801308
12811309/*
@@ -1294,6 +1322,18 @@ index_constraint_create(Relation heapRelation,
12941322
12951323recordDependencyOn (& myself ,& referenced ,DEPENDENCY_INTERNAL );
12961324
1325+ /*
1326+ * Also, if this is a constraint on a partition, mark it as depending
1327+ * on the constraint in the parent.
1328+ */
1329+ if (OidIsValid (parentConstraintId ))
1330+ {
1331+ ObjectAddress parentConstr ;
1332+
1333+ ObjectAddressSet (parentConstr ,ConstraintRelationId ,parentConstraintId );
1334+ recordDependencyOn (& referenced ,& parentConstr ,DEPENDENCY_INTERNAL_AUTO );
1335+ }
1336+
12971337/*
12981338 * If the constraint is deferrable, create the deferred uniqueness
12991339 * checking trigger. (The trigger will be given an internal dependency on