Change one loop in ATRewriteTable to use 1-based attnums
authorÁlvaro Herrera <alvherre@alvh.no-ip.org>
Fri, 21 Mar 2025 09:55:06 +0000 (10:55 +0100)
committerÁlvaro Herrera <alvherre@alvh.no-ip.org>
Fri, 21 Mar 2025 09:55:06 +0000 (10:55 +0100)
All TupleDescAttr() calls in tablecmds.c that aren't in loops across all
attributes use AttrNumber-style indexes (1-based); there was only one
place in ATRewriteTable that was stashing 0-based indexes in a list for
later processing.  Switch that to use attnums for consistency.

Author: jian he <jian.universality@gmail.com>
Discussion: https://postgr.es/m/CACJufxEoYA5ScUr2=CmA1xcpaS_1ixneDbEkVU77X1ctGxY2mA@mail.gmail.com

src/backend/commands/tablecmds.c

index 129c97fdf28f253b9e87b6f73d36ccb9cc304853..1202544ebd02d91aa6010b50c3939cea461c3b29 100644 (file)
@@ -6189,7 +6189,7 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
            Form_pg_attribute attr = TupleDescAttr(newTupDesc, i);
 
            if (attr->attnotnull && !attr->attisdropped)
-               notnull_attrs = lappend_int(notnull_attrs, i);
+               notnull_attrs = lappend_int(notnull_attrs, attr->attnum);
        }
        if (notnull_attrs)
            needscan = true;
@@ -6370,20 +6370,18 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
            /* Now check any constraints on the possibly-changed tuple */
            econtext->ecxt_scantuple = insertslot;
 
-           foreach(l, notnull_attrs)
+           foreach_int(attn, notnull_attrs)
            {
-               int         attn = lfirst_int(l);
-
-               if (slot_attisnull(insertslot, attn + 1))
+               if (slot_attisnull(insertslot, attn))
                {
-                   Form_pg_attribute attr = TupleDescAttr(newTupDesc, attn);
+                   Form_pg_attribute attr = TupleDescAttr(newTupDesc, attn - 1);
 
                    ereport(ERROR,
                            (errcode(ERRCODE_NOT_NULL_VIOLATION),
                             errmsg("column \"%s\" of relation \"%s\" contains null values",
                                    NameStr(attr->attname),
                                    RelationGetRelationName(oldrel)),
-                            errtablecol(oldrel, attn + 1)));
+                            errtablecol(oldrel, attn)));
                }
            }