From 64444ce071f6b04d3fc836f436fa08108a6d11e2 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Fri, 26 Jan 2024 09:04:27 +0100 Subject: [PATCH] MergeAttributes code deduplication The code handling NOT NULL constraints is duplicated in blocks merging the attribute definition incrementally. Deduplicate that code. Author: Ashutosh Bapat Discussion: https://www.postgresql.org/message-id/flat/52a125e4-ff9a-95f5-9f61-b87cf447e4da@eisentraut.org --- src/backend/commands/tablecmds.c | 98 ++++++++++++-------------------- 1 file changed, 35 insertions(+), 63 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index a062ec4055..0ba09890c5 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -2815,37 +2815,6 @@ MergeAttributes(List *columns, const List *supers, char relpersistence, attributeName), errdetail("%s versus %s", prevdef->compression, newdef->compression))); - /* - * In regular inheritance, columns in the parent's primary key - * get an extra not-null constraint. - */ - if (bms_is_member(parent_attno - FirstLowInvalidHeapAttributeNumber, - pkattrs)) - { - CookedConstraint *nn; - - nn = palloc(sizeof(CookedConstraint)); - nn->contype = CONSTR_NOTNULL; - nn->conoid = InvalidOid; - nn->name = NULL; - nn->attnum = exist_attno; - nn->expr = NULL; - nn->skip_validation = false; - nn->is_local = false; - nn->inhcount = 1; - nn->is_no_inherit = false; - - nnconstraints = lappend(nnconstraints, nn); - } - - /* - * mark attnotnull if parent has it and it's not NO INHERIT - */ - if (bms_is_member(parent_attno, nncols) || - bms_is_member(parent_attno - FirstLowInvalidHeapAttributeNumber, - pkattrs)) - prevdef->is_not_null = true; - /* * Check for GENERATED conflicts */ @@ -2877,45 +2846,48 @@ MergeAttributes(List *columns, const List *supers, char relpersistence, */ newdef->inhcount = 1; newdef->is_local = false; - /* mark attnotnull if parent has it and it's not NO INHERIT */ - if (bms_is_member(parent_attno, nncols) || - bms_is_member(parent_attno - FirstLowInvalidHeapAttributeNumber, - pkattrs)) - newdef->is_not_null = true; inh_columns = lappend(inh_columns, newdef); + newattmap->attnums[parent_attno - 1] = ++child_attno; - /* - * In regular inheritance, columns in the parent's primary key - * get an extra not-null constraint. Partitioning doesn't - * need this, because the PK itself is going to be cloned to - * the partition. - */ - if (!is_partition && - bms_is_member(parent_attno - - FirstLowInvalidHeapAttributeNumber, - pkattrs)) - { - CookedConstraint *nn; - - nn = palloc(sizeof(CookedConstraint)); - nn->contype = CONSTR_NOTNULL; - nn->conoid = InvalidOid; - nn->name = NULL; - nn->attnum = newattmap->attnums[parent_attno - 1]; - nn->expr = NULL; - nn->skip_validation = false; - nn->is_local = false; - nn->inhcount = 1; - nn->is_no_inherit = false; - - nnconstraints = lappend(nnconstraints, nn); - } - /* remember for default processing below */ savedef = newdef; } + /* + * mark attnotnull if parent has it and it's not NO INHERIT + */ + if (bms_is_member(parent_attno, nncols) || + bms_is_member(parent_attno - FirstLowInvalidHeapAttributeNumber, + pkattrs)) + savedef->is_not_null = true; + + /* + * In regular inheritance, columns in the parent's primary key get + * an extra not-null constraint. Partitioning doesn't need this, + * because the PK itself is going to be cloned to the partition. + */ + if (!is_partition && + bms_is_member(parent_attno - + FirstLowInvalidHeapAttributeNumber, + pkattrs)) + { + CookedConstraint *nn; + + nn = palloc(sizeof(CookedConstraint)); + nn->contype = CONSTR_NOTNULL; + nn->conoid = InvalidOid; + nn->name = NULL; + nn->attnum = newattmap->attnums[parent_attno - 1]; + nn->expr = NULL; + nn->skip_validation = false; + nn->is_local = false; + nn->inhcount = 1; + nn->is_no_inherit = false; + + nnconstraints = lappend(nnconstraints, nn); + } + /* * Locate default/generation expression if any */