Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 369202b

Browse files
committed
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. Discussion: https://www.postgresql.org/message-id/flat/52a125e4-ff9a-95f5-9f61-b87cf447e4da%40eisentraut.org
1 parent 28d3c2d commit 369202b

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

src/backend/commands/tablecmds.c

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ static void RangeVarCallbackForTruncate(const RangeVar *relation,
353353
static List *MergeAttributes(List *schema, List *supers, char relpersistence,
354354
bool is_partition, List **supconstr,
355355
List **supnotnulls);
356-
static bool MergeCheckConstraint(List *constraints, char *name, Node *expr);
356+
static List *MergeCheckConstraint(List *constraints, const char *name, Node *expr);
357357
static void MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel);
358358
static void MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel);
359359
static void StoreCatalogInheritance(Oid relationId, List *supers,
@@ -2913,24 +2913,7 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
29132913
name,
29142914
RelationGetRelationName(relation))));
29152915

2916-
/* check for duplicate */
2917-
if (!MergeCheckConstraint(constraints, name, expr))
2918-
{
2919-
/* nope, this is a new one */
2920-
CookedConstraint *cooked;
2921-
2922-
cooked = (CookedConstraint *) palloc(sizeof(CookedConstraint));
2923-
cooked->contype = CONSTR_CHECK;
2924-
cooked->conoid = InvalidOid; /* until created */
2925-
cooked->name = pstrdup(name);
2926-
cooked->attnum = 0; /* not used for constraints */
2927-
cooked->expr = expr;
2928-
cooked->skip_validation = false;
2929-
cooked->is_local = false;
2930-
cooked->inhcount = 1;
2931-
cooked->is_no_inherit = false;
2932-
constraints = lappend(constraints, cooked);
2933-
}
2916+
constraints = MergeCheckConstraint(constraints, name, expr);
29342917
}
29352918
}
29362919

@@ -3277,13 +3260,17 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
32773260
*
32783261
* constraints is a list of CookedConstraint structs for previous constraints.
32793262
*
3280-
* Returns true if merged (constraint is a duplicate), or false if it's
3281-
* got a so-far-unique name, or throws error if conflict.
3263+
* If the new constraint matches an existing one, then the existing
3264+
* constraint's inheritance count is updated. If there is a conflict (same
3265+
* name but different expression), throw an error. If the constraint neither
3266+
* matches nor conflicts with an existing one, a new constraint is appended to
3267+
* the list.
32823268
*/
3283-
static bool
3284-
MergeCheckConstraint(List *constraints, char *name, Node *expr)
3269+
static List *
3270+
MergeCheckConstraint(List *constraints, const char *name, Node *expr)
32853271
{
32863272
ListCell *lc;
3273+
CookedConstraint *newcon;
32873274

32883275
foreach(lc, constraints)
32893276
{
@@ -3297,13 +3284,13 @@ MergeCheckConstraint(List *constraints, char *name, Node *expr)
32973284

32983285
if (equal(expr, ccon->expr))
32993286
{
3300-
/* OK to merge */
3287+
/* OK to merge constraint with existing */
33013288
ccon->inhcount++;
33023289
if (ccon->inhcount < 0)
33033290
ereport(ERROR,
33043291
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
33053292
errmsg("too many inheritance parents"));
3306-
return true;
3293+
return constraints;
33073294
}
33083295

33093296
ereport(ERROR,
@@ -3312,7 +3299,16 @@ MergeCheckConstraint(List *constraints, char *name, Node *expr)
33123299
name)));
33133300
}
33143301

3315-
return false;
3302+
/*
3303+
* Constraint couldn't be merged with an existing one and also didn't
3304+
* conflict with an existing one, so add it as a new one to the list.
3305+
*/
3306+
newcon = palloc0_object(CookedConstraint);
3307+
newcon->contype = CONSTR_CHECK;
3308+
newcon->name = pstrdup(name);
3309+
newcon->expr = expr;
3310+
newcon->inhcount = 1;
3311+
return lappend(constraints, newcon);
33163312
}
33173313

33183314

0 commit comments

Comments
 (0)