From 17517897f90eb7b2be100dade5579b55d406334e Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Thu, 8 Jun 2023 13:34:07 +0200 Subject: [PATCH 06/17] Clean up MergeCheckConstraint() If the constraint is not already in the list, add it ourselves, instead of making the caller do it. This makes the interface more consistent with other "merge" functions in this file. --- src/backend/commands/tablecmds.c | 47 ++++++++++++++------------------ 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 62b555aa20..a905140e32 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -351,7 +351,7 @@ static void RangeVarCallbackForTruncate(const RangeVar *relation, Oid relId, Oid oldRelId, void *arg); static List *MergeAttributes(List *schema, List *supers, char relpersistence, bool is_partition, List **supconstr); -static bool MergeCheckConstraint(List *constraints, char *name, Node *expr); +static List *MergeCheckConstraint(List *constraints, const char *name, Node *expr); static void MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel); static void MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel); static void StoreCatalogInheritance(Oid relationId, List *supers, @@ -2811,24 +2811,7 @@ MergeAttributes(List *schema, List *supers, char relpersistence, name, RelationGetRelationName(relation)))); - /* check for duplicate */ - if (!MergeCheckConstraint(constraints, name, expr)) - { - /* nope, this is a new one */ - CookedConstraint *cooked; - - cooked = (CookedConstraint *) palloc(sizeof(CookedConstraint)); - cooked->contype = CONSTR_CHECK; - cooked->conoid = InvalidOid; /* until created */ - cooked->name = pstrdup(name); - cooked->attnum = 0; /* not used for constraints */ - cooked->expr = expr; - cooked->skip_validation = false; - cooked->is_local = false; - cooked->inhcount = 1; - cooked->is_no_inherit = false; - constraints = lappend(constraints, cooked); - } + constraints = MergeCheckConstraint(constraints, name, expr); } } @@ -3158,13 +3141,16 @@ MergeAttributes(List *schema, List *supers, char relpersistence, * * constraints is a list of CookedConstraint structs for previous constraints. * - * Returns true if merged (constraint is a duplicate), or false if it's - * got a so-far-unique name, or throws error if conflict. + * If the constraint is a duplicate, then the existing constraint's + * inheritance count is updated. If the constraint doesn't match or conflict + * with an existing one, a new constraint is appended to the list. If there + * is a conflict (same name but different expression), throw an error. */ -static bool -MergeCheckConstraint(List *constraints, char *name, Node *expr) +static List * +MergeCheckConstraint(List *constraints, const char *name, Node *expr) { ListCell *lc; + CookedConstraint *newcon; foreach(lc, constraints) { @@ -3178,13 +3164,13 @@ MergeCheckConstraint(List *constraints, char *name, Node *expr) if (equal(expr, ccon->expr)) { - /* OK to merge */ + /* OK to merge constraint with existing */ ccon->inhcount++; if (ccon->inhcount < 0) ereport(ERROR, errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), errmsg("too many inheritance parents")); - return true; + return constraints; } ereport(ERROR, @@ -3193,7 +3179,16 @@ MergeCheckConstraint(List *constraints, char *name, Node *expr) name))); } - return false; + /* + * Constraint couldn't be merged with an existing one and also didn't + * conflict with an existing one, so add it as a new one to the list. + */ + newcon = palloc0_object(CookedConstraint); + newcon->contype = CONSTR_CHECK; + newcon->name = pstrdup(name); + newcon->expr = expr; + newcon->inhcount = 1; + return lappend(constraints, newcon); } -- 2.41.0