MergeAttributes code deduplication
authorPeter Eisentraut <peter@eisentraut.org>
Fri, 26 Jan 2024 08:04:27 +0000 (09:04 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Fri, 26 Jan 2024 10:05:10 +0000 (11:05 +0100)
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

src/backend/commands/tablecmds.c

index a062ec4055ef14b18e1b5ed984470c0866ded0cc..0ba09890c5d8357d082b673392b0d84a49a6cc75 100644 (file)
@@ -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);
-               }
+               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
             */