Stop creating constraints during DETACH CONCURRENTLY
authorÁlvaro Herrera <alvherre@kurilemu.de>
Sat, 11 Oct 2025 18:30:12 +0000 (20:30 +0200)
committerÁlvaro Herrera <alvherre@kurilemu.de>
Sat, 11 Oct 2025 18:30:12 +0000 (20:30 +0200)
Commit 71f4c8c6f74b (which implemented DETACH CONCURRENTLY) added code
to create a separate table constraint when a table is detached
concurrently, identical to the partition constraint, on the theory that
such a constraint was needed in case the optimizer had constructed any
query plans that depended on the constraint being there.  However, that
theory was apparently bogus because any such plans would be invalidated.

For hash partitioning, those constraints are problematic, because their
expressions reference the OID of the parent partitioned table, to which
the detached table is no longer related; this causes all sorts of
problems (such as inability of restoring a pg_dump of that table, and
the table no longer working properly if the partitioned table is later
dropped).

We'd like to get rid of all those constraints.  In fact, for branch
master, do that -- no longer create any substitute constraints.
However, out of fear that some users might somehow depend on these
constraints for other partitioning strategies, for stable branches
(back to 14, which added DETACH CONCURRENTLY), only do it for hash
partitioning.

(If you repeatedly DETACH CONCURRENTLY and then ATTACH a partition, then
with this constraint addition you don't need to scan the table in the
ATTACH step, which presumably is good.  But if users really valued this
feature, they would have requested that it worked for non-concurrent
DETACH also.)

Author: Haiyang Li <mohen.lhy@alibaba-inc.com>
Reported-by: Fei Changhong <feichanghong@qq.com>
Reported-by: Haiyang Li <mohen.lhy@alibaba-inc.com>
Backpatch-through: 14
Discussion: https://postgr.es/m/18371-7fef49f63de13f02@postgresql.org
Discussion: https://postgr.es/m/19070-781326347ade7c57@postgresql.org

src/backend/commands/tablecmds.c
src/test/regress/expected/alter_table.out
src/test/regress/sql/alter_table.sql

index fc89352b6617b65b11c3181592c27b2ebd807653..5fd8b51312c8b3e02d505ae42989973b1b428317 100644 (file)
@@ -722,7 +722,6 @@ static void QueuePartitionConstraintValidation(List **wqueue, Relation scanrel,
                                               List *partConstraint,
                                               bool validate_default);
 static void CloneRowTriggersToPartition(Relation parent, Relation partition);
-static void DetachAddConstraintIfNeeded(List **wqueue, Relation partRel);
 static void DropClonedTriggersFromPartition(Oid partitionId);
 static ObjectAddress ATExecDetachPartition(List **wqueue, AlteredTableInfo *tab,
                                           Relation rel, RangeVar *name,
@@ -20941,12 +20940,6 @@ ATExecDetachPartition(List **wqueue, AlteredTableInfo *tab, Relation rel,
        char       *parentrelname;
        char       *partrelname;
 
-       /*
-        * Add a new constraint to the partition being detached, which
-        * supplants the partition constraint (unless there is one already).
-        */
-       DetachAddConstraintIfNeeded(wqueue, partRel);
-
        /*
         * We're almost done now; the only traces that remain are the
         * pg_inherits tuple and the partition's relpartbounds.  Before we can
@@ -21403,49 +21396,6 @@ ATExecDetachPartitionFinalize(Relation rel, RangeVar *name)
    return address;
 }
 
-/*
- * DetachAddConstraintIfNeeded
- *     Subroutine for ATExecDetachPartition.  Create a constraint that
- *     takes the place of the partition constraint, but avoid creating
- *     a dupe if a constraint already exists which implies the needed
- *     constraint.
- */
-static void
-DetachAddConstraintIfNeeded(List **wqueue, Relation partRel)
-{
-   List       *constraintExpr;
-
-   constraintExpr = RelationGetPartitionQual(partRel);
-   constraintExpr = (List *) eval_const_expressions(NULL, (Node *) constraintExpr);
-
-   /*
-    * Avoid adding a new constraint if the needed constraint is implied by an
-    * existing constraint
-    */
-   if (!PartConstraintImpliedByRelConstraint(partRel, constraintExpr))
-   {
-       AlteredTableInfo *tab;
-       Constraint *n;
-
-       tab = ATGetQueueEntry(wqueue, partRel);
-
-       /* Add constraint on partition, equivalent to the partition constraint */
-       n = makeNode(Constraint);
-       n->contype = CONSTR_CHECK;
-       n->conname = NULL;
-       n->location = -1;
-       n->is_no_inherit = false;
-       n->raw_expr = NULL;
-       n->cooked_expr = nodeToString(make_ands_explicit(constraintExpr));
-       n->is_enforced = true;
-       n->initially_valid = true;
-       n->skip_validation = true;
-       /* It's a re-add, since it nominally already exists */
-       ATAddCheckNNConstraint(wqueue, tab, partRel, n,
-                              true, false, true, ShareUpdateExclusiveLock);
-   }
-}
-
 /*
  * DropClonedTriggersFromPartition
  *     subroutine for ATExecDetachPartition to remove any triggers that were
index a08f115b0e5a264fa7578d838d0d5aa039581667..5e98bbf2425ad8e5699b7e4a6f6d1d146c7984ea 100644 (file)
@@ -4470,27 +4470,15 @@ ALTER TABLE range_parted2 DETACH PARTITION part_rp CONCURRENTLY;
 Partition key: RANGE (a)
 Number of partitions: 0
 
--- constraint should be created
-\d part_rp
-              Table "public.part_rp"
- Column |  Type   | Collation | Nullable | Default 
---------+---------+-----------+----------+---------
- a      | integer |           |          | 
-Check constraints:
-    "part_rp_a_check" CHECK (a IS NOT NULL AND a >= 0 AND a < 100)
-
-CREATE TABLE part_rp100 PARTITION OF range_parted2 (CHECK (a>=123 AND a<133 AND a IS NOT NULL)) FOR VALUES FROM (100) to (200);
-ALTER TABLE range_parted2 DETACH PARTITION part_rp100 CONCURRENTLY;
--- redundant constraint should not be created
-\d part_rp100
-             Table "public.part_rp100"
- Column |  Type   | Collation | Nullable | Default 
---------+---------+-----------+----------+---------
- a      | integer |           |          | 
-Check constraints:
-    "part_rp100_a_check" CHECK (a >= 123 AND a < 133 AND a IS NOT NULL)
-
 DROP TABLE range_parted2;
+-- Test that hash partitions continue to work after they're concurrently
+-- detached (bugs #18371, #19070)
+CREATE TABLE hash_parted2 (a int) PARTITION BY HASH(a);
+CREATE TABLE part_hp PARTITION OF hash_parted2 FOR VALUES WITH (MODULUS 2, REMAINDER 0);
+ALTER TABLE hash_parted2 DETACH PARTITION part_hp CONCURRENTLY;
+DROP TABLE hash_parted2;
+INSERT INTO part_hp VALUES (1);
+DROP TABLE part_hp;
 -- Check ALTER TABLE commands for partitioned tables and partitions
 -- cannot add/drop column to/from *only* the parent
 ALTER TABLE ONLY list_parted2 ADD COLUMN c int;
index 90bf5c17682388c5417cd80d2b0cab2e05567c5a..417202430a5ccd799f2c30b7511dbdac7f8cbab3 100644 (file)
@@ -2826,14 +2826,17 @@ DROP TABLE part_rpd;
 -- works fine
 ALTER TABLE range_parted2 DETACH PARTITION part_rp CONCURRENTLY;
 \d+ range_parted2
--- constraint should be created
-\d part_rp
-CREATE TABLE part_rp100 PARTITION OF range_parted2 (CHECK (a>=123 AND a<133 AND a IS NOT NULL)) FOR VALUES FROM (100) to (200);
-ALTER TABLE range_parted2 DETACH PARTITION part_rp100 CONCURRENTLY;
--- redundant constraint should not be created
-\d part_rp100
 DROP TABLE range_parted2;
 
+-- Test that hash partitions continue to work after they're concurrently
+-- detached (bugs #18371, #19070)
+CREATE TABLE hash_parted2 (a int) PARTITION BY HASH(a);
+CREATE TABLE part_hp PARTITION OF hash_parted2 FOR VALUES WITH (MODULUS 2, REMAINDER 0);
+ALTER TABLE hash_parted2 DETACH PARTITION part_hp CONCURRENTLY;
+DROP TABLE hash_parted2;
+INSERT INTO part_hp VALUES (1);
+DROP TABLE part_hp;
+
 -- Check ALTER TABLE commands for partitioned tables and partitions
 
 -- cannot add/drop column to/from *only* the parent