From ae366aa57762ad0e6a1a0885a7644e79541afe39 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Wed, 23 Jan 2019 23:57:46 -0300 Subject: Detach constraints when partitions are detached MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I (Álvaro) forgot to do this in eb7ed3f30634, leading to undroppable constraints after partitions are detached. Repair. Reported-by: Amit Langote Author: Amit Langote Discussion: https://postgr.es/m/c1c9b688-b886-84f7-4048-1e4ebe9b1d06@lab.ntt.co.jp --- src/backend/commands/tablecmds.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/backend/commands/tablecmds.c') diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 28a137bb537..738c1781078 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -15095,6 +15095,7 @@ ATExecDetachPartition(Relation rel, RangeVar *name) { Oid idxid = lfirst_oid(cell); Relation idx; + Oid constrOid; if (!has_superclass(idxid)) continue; @@ -15106,6 +15107,23 @@ ATExecDetachPartition(Relation rel, RangeVar *name) 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; + + 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)); + + ConstraintSetParentConstraint(constrOid, InvalidOid); } table_close(classRel, RowExclusiveLock); -- cgit v1.2.3