aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Eisentraut <peter@eisentraut.org>2024-01-26 09:04:27 +0100
committerPeter Eisentraut <peter@eisentraut.org>2024-01-26 11:05:10 +0100
commit64444ce071f6b04d3fc836f436fa08108a6d11e2 (patch)
tree002f42a42378965d84f02152dc3f852fa1284d4e
parentf2bf8fb04886e3ea82e7f7f86696ac78e06b7e60 (diff)
downloadpostgresql-64444ce071f6b04d3fc836f436fa08108a6d11e2.tar.gz
postgresql-64444ce071f6b04d3fc836f436fa08108a6d11e2.zip
MergeAttributes code deduplication
The code handling NOT NULL constraints is duplicated in blocks merging the attribute definition incrementally. Deduplicate that code. Author: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/52a125e4-ff9a-95f5-9f61-b87cf447e4da@eisentraut.org
-rw-r--r--src/backend/commands/tablecmds.c98
1 files changed, 35 insertions, 63 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index a062ec4055e..0ba09890c5d 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -2816,37 +2816,6 @@ MergeAttributes(List *columns, const List *supers, char relpersistence,
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
*/
if (prevdef->generated != newdef->generated)
@@ -2877,46 +2846,49 @@ 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);
- }
+ newattmap->attnums[parent_attno - 1] = ++child_attno;
/* 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
*/
if (attribute->atthasdef)