aboutsummaryrefslogtreecommitdiff
path: root/src/backend/commands/tablecmds.c
diff options
context:
space:
mode:
authorAlvaro Herrera <alvherre@alvh.no-ip.org>2021-04-21 18:12:05 -0400
committerAlvaro Herrera <alvherre@alvh.no-ip.org>2021-04-21 18:12:05 -0400
commit7b357cc6ae553c0ecacdc11b2e5278b7bf477dba (patch)
tree8223bd010a5f1f0d1ef49bae67ebd7566e645b07 /src/backend/commands/tablecmds.c
parente014d25deade08df082d2b37de45adb0c984f563 (diff)
downloadpostgresql-7b357cc6ae553c0ecacdc11b2e5278b7bf477dba.tar.gz
postgresql-7b357cc6ae553c0ecacdc11b2e5278b7bf477dba.zip
Don't add a redundant constraint when detaching a partition
On ALTER TABLE .. DETACH CONCURRENTLY, we add a new table constraint that duplicates the partition constraint. But if the partition already has another constraint that implies that one, then that's unnecessary. We were already avoiding the addition of a duplicate constraint if there was an exact 'equal' match -- this just improves the quality of the check. Author: Justin Pryzby <pryzby@telsasoft.com> Reviewed-by: Álvaro Herrera <alvherre@alvh.no-ip.org> Discussion: https://postgr.es/m/20210410184226.GY6592@telsasoft.com
Diffstat (limited to 'src/backend/commands/tablecmds.c')
-rw-r--r--src/backend/commands/tablecmds.c61
1 files changed, 28 insertions, 33 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 096a6f28915..97cc9fd6eca 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -17915,47 +17915,42 @@ ATExecDetachPartitionFinalize(Relation rel, RangeVar *name)
* DetachAddConstraintIfNeeded
* Subroutine for ATExecDetachPartition. Create a constraint that
* takes the place of the partition constraint, but avoid creating
- * a dupe if an equivalent constraint already exists.
+ * a dupe if an constraint already exists which implies the needed
+ * constraint.
*/
static void
DetachAddConstraintIfNeeded(List **wqueue, Relation partRel)
{
- AlteredTableInfo *tab;
- Expr *constraintExpr;
- TupleDesc td = RelationGetDescr(partRel);
- Constraint *n;
+ List *constraintExpr;
- constraintExpr = make_ands_explicit(RelationGetPartitionQual(partRel));
+ constraintExpr = RelationGetPartitionQual(partRel);
+ constraintExpr = (List *) eval_const_expressions(NULL, (Node *) constraintExpr);
- /* If an identical constraint exists, we don't need to create one */
- if (td->constr && td->constr->num_check > 0)
+ /*
+ * Avoid adding a new constraint if the needed constraint is implied by an
+ * existing constraint
+ */
+ if (!PartConstraintImpliedByRelConstraint(partRel, constraintExpr))
{
- for (int i = 0; i < td->constr->num_check; i++)
- {
- Node *thisconstr;
-
- thisconstr = stringToNode(td->constr->check[i].ccbin);
-
- if (equal(constraintExpr, thisconstr))
- return;
- }
+ 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->initially_valid = true;
+ n->skip_validation = true;
+ /* It's a re-add, since it nominally already exists */
+ ATAddCheckConstraint(wqueue, tab, partRel, n,
+ true, false, true, ShareUpdateExclusiveLock);
}
-
- 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(constraintExpr);
- n->initially_valid = true;
- n->skip_validation = true;
- /* It's a re-add, since it nominally already exists */
- ATAddCheckConstraint(wqueue, tab, partRel, n,
- true, false, true, ShareUpdateExclusiveLock);
}
/*