@@ -353,7 +353,7 @@ static void RangeVarCallbackForTruncate(const RangeVar *relation,
353353static List *MergeAttributes(List *schema, List *supers, char relpersistence,
354354 bool is_partition, List **supconstr,
355355 List **supnotnulls);
356- staticbool MergeCheckConstraint(List *constraints, char *name, Node *expr);
356+ staticList * MergeCheckConstraint(List *constraints, const char *name, Node *expr);
357357static void MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel);
358358static void MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel);
359359static 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- staticbool
3284- MergeCheckConstraint(List *constraints, char *name, Node *expr)
3269+ staticList *
3270+ MergeCheckConstraint(List *constraints,const char *name, Node *expr)
32853271{
32863272ListCell *lc;
3273+ CookedConstraint *newcon;
32873274
32883275foreach(lc, constraints)
32893276{
@@ -3297,13 +3284,13 @@ MergeCheckConstraint(List *constraints, char *name, Node *expr)
32973284
32983285if (equal(expr, ccon->expr))
32993286{
3300- /* OK to merge */
3287+ /* OK to mergeconstraint with existing */
33013288ccon->inhcount++;
33023289if (ccon->inhcount < 0)
33033290ereport(ERROR,
33043291errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
33053292errmsg("too many inheritance parents"));
3306- returntrue ;
3293+ returnconstraints ;
33073294}
33083295
33093296ereport(ERROR,
@@ -3312,7 +3299,16 @@ MergeCheckConstraint(List *constraints, char *name, Node *expr)
33123299name)));
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