From 77fa3059e6d6fe550d57ef067d0fd75273171029 Mon Sep 17 00:00:00 2001 From: jian he Date: Wed, 17 Jun 2026 11:22:37 +0800 Subject: [PATCH v10 1/1] misc refactor ATCheckCheckConstrHasEnforcedParent --- src/backend/commands/tablecmds.c | 64 ++++++++++++++++---------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 91b163e5f9c..599f77a7720 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -12957,41 +12957,43 @@ ATCheckCheckConstrHasEnforcedParent(Relation conrel, Relation rel, !parentcon->conenforced) continue; - if (constraints_equivalent(parenttup, contuple, - RelationGetDescr(conrel))) + if (!constraints_equivalent(parenttup, contuple, + RelationGetDescr(conrel))) + elog(ERROR, "child table \"%s\" has different definition for check constraint \"%s\"", + RelationGetRelationName(rel), + NameStr(parentcon->conname)); + + /* + * A parent listed in changing_conids is being changed by the same + * ALTER, but it may not have been updated yet. For regular + * inheritance, recurse upward to check whether an equivalent + * enforced parent outside the ALTER will make it remain enforced. + * Partitions cannot have multiple parents, so they do not need + * this check. + */ + if (!rel->rd_rel->relispartition && + list_member_oid(changing_conids, parentcon->oid)) { - /* - * A parent listed in changing_conids is being changed by the - * same ALTER, but it may not have been updated yet. For - * regular inheritance, recurse upward to check whether an - * equivalent enforced parent outside the ALTER will make it - * remain enforced. Partitions cannot have multiple parents, - * so they do not need this check. - */ - if (!rel->rd_rel->relispartition && - list_member_oid(changing_conids, parentcon->oid)) - { - Oid parent_enforced_parentoid = InvalidOid; + Oid parent_enforced_parentoid = InvalidOid; - if (parentrel == NULL) - parentrel = table_open(parentoid, NoLock); + if (parentrel == NULL) + parentrel = table_open(parentoid, NoLock); - if (!ATCheckCheckConstrHasEnforcedParent(conrel, - parentrel, - parenttup, - changing_conids, - &parent_enforced_parentoid)) - continue; - } - - *enforced_parentoid = parentoid; - if (parentrel != NULL) - table_close(parentrel, NoLock); - systable_endscan(pscan); - systable_endscan(scan); - table_close(inhrel, AccessShareLock); - return true; + if (!ATCheckCheckConstrHasEnforcedParent(conrel, + parentrel, + parenttup, + changing_conids, + &parent_enforced_parentoid)) + continue; } + + *enforced_parentoid = parentoid; + if (parentrel != NULL) + table_close(parentrel, NoLock); + systable_endscan(pscan); + systable_endscan(scan); + table_close(inhrel, AccessShareLock); + return true; } if (parentrel != NULL) -- 2.34.1