Simplify coding to detach constraints when detaching partition
authorAlvaro Herrera
Thu, 24 Jan 2019 14:18:35 +0000 (11:18 -0300)
committerAlvaro Herrera
Thu, 24 Jan 2019 14:18:35 +0000 (11:18 -0300)
The original coding was too baroque and led to an use-after-release
mistake, noticed by buildfarm member prion.

Discussion: https://postgr.es/m/21693.1548305934@sss.pgh.pa.us

src/backend/commands/tablecmds.c

index 32eb5f88bf70717deeb8ca768ead4b99a1ddeece..747acbd2b917272ff60716b5e1744354bbd0e576 100644 (file)
@@ -15244,24 +15244,14 @@ ATExecDetachPartition(Relation rel, RangeVar *name)
        idx = index_open(idxid, AccessExclusiveLock);
        IndexSetParentIndex(idx, InvalidOid);
        update_relispartition(classRel, idxid, false);
-       index_close(idx, NoLock);
-
-       /*
-        * Detach any constraints associated with the index too.  Only UNIQUE
-        * and PRIMARY KEY index constraints can be inherited, so no need
-        * to check for others.
-        */
-       if (!idx->rd_index->indisprimary && !idx->rd_index->indisunique)
-           continue;
 
+       /* If there's a constraint associated with the index, detach it too */
        constrOid = get_relation_idx_constraint_oid(RelationGetRelid(partRel),
                                                    idxid);
-       if (!OidIsValid(constrOid))
-           elog(ERROR, "missing pg_constraint entry of index \"%s\" of partition \"%s\"",
-                RelationGetRelationName(idx),
-                RelationGetRelationName(partRel));
+       if (OidIsValid(constrOid))
+           ConstraintSetParentConstraint(constrOid, InvalidOid);
 
-       ConstraintSetParentConstraint(constrOid, InvalidOid);
+       index_close(idx, NoLock);
    }
    heap_close(classRel, RowExclusiveLock);