v3-0001-minor-refactoring-DefineRelation-StoreAttrDefa.no-cfbot

application/octet-stream

Filename: v3-0001-minor-refactoring-DefineRelation-StoreAttrDefa.no-cfbot
Type: application/octet-stream
Part: 0
Message: Re: bug when apply fast default mechanism for adding new column over domain with default value
From 71be37ae101836a5dd272a81e32df5c2d53c3f4a Mon Sep 17 00:00:00 2001
From: jian he <jian.universality@gmail.com>
Date: Mon, 3 Mar 2025 22:15:00 +0800
Subject: [PATCH v3 1/1] minor refactoring DefineRelation, StoreAttrDefault

make StoreAttrDefault as the single place for setting pg_attribute.atthasdef to true.
---
 src/backend/catalog/pg_attrdef.c | 23 ++++++++++++-----------
 src/backend/commands/tablecmds.c |  8 --------
 2 files changed, 12 insertions(+), 19 deletions(-)

diff --git a/src/backend/catalog/pg_attrdef.c b/src/backend/catalog/pg_attrdef.c
index 1f95bc6f752..706cc82e296 100644
--- a/src/backend/catalog/pg_attrdef.c
+++ b/src/backend/catalog/pg_attrdef.c
@@ -47,6 +47,9 @@ StoreAttrDefault(Relation rel, AttrNumber attnum,
 	Oid			attrdefOid;
 	ObjectAddress colobject,
 				defobject;
+	Datum		valuesAtt[Natts_pg_attribute] = {0};
+	bool		nullsAtt[Natts_pg_attribute] = {0};
+	bool		replacesAtt[Natts_pg_attribute] = {0};
 
 	adrel = table_open(AttrDefaultRelationId, RowExclusiveLock);
 
@@ -92,20 +95,18 @@ StoreAttrDefault(Relation rel, AttrNumber attnum,
 			 attnum, RelationGetRelid(rel));
 	attStruct = (Form_pg_attribute) GETSTRUCT(atttup);
 	attgenerated = attStruct->attgenerated;
-	if (!attStruct->atthasdef)
-	{
-		Datum		valuesAtt[Natts_pg_attribute] = {0};
-		bool		nullsAtt[Natts_pg_attribute] = {0};
-		bool		replacesAtt[Natts_pg_attribute] = {0};
+	if (attStruct->atthasdef)
+		elog(ERROR, "expect atthasdef be false for attribute %d of relation %u",
+			 attnum, RelationGetRelid(rel));
 
-		valuesAtt[Anum_pg_attribute_atthasdef - 1] = BoolGetDatum(true);
-		replacesAtt[Anum_pg_attribute_atthasdef - 1] = true;
+	valuesAtt[Anum_pg_attribute_atthasdef - 1] = BoolGetDatum(true);
+	replacesAtt[Anum_pg_attribute_atthasdef - 1] = true;
 
-		atttup = heap_modify_tuple(atttup, RelationGetDescr(attrrel),
-								   valuesAtt, nullsAtt, replacesAtt);
+	atttup = heap_modify_tuple(atttup, RelationGetDescr(attrrel),
+								valuesAtt, nullsAtt, replacesAtt);
+
+	CatalogTupleUpdate(attrrel, &atttup->t_self, atttup);
 
-		CatalogTupleUpdate(attrrel, &atttup->t_self, atttup);
-	}
 	table_close(attrrel, RowExclusiveLock);
 	heap_freetuple(atttup);
 
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index cf519fb77d9..74a23242044 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -941,10 +941,6 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
 	 * while raw defaults go into a list of RawColumnDefault structs that will
 	 * be processed by AddRelationNewConstraints.  (We can't deal with raw
 	 * expressions until we can do transformExpr.)
-	 *
-	 * We can set the atthasdef flags now in the tuple descriptor; this just
-	 * saves StoreAttrDefault from having to do an immediate update of the
-	 * pg_attribute rows.
 	 */
 	rawDefaults = NIL;
 	cookedDefaults = NIL;
@@ -953,10 +949,8 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
 	foreach(listptr, stmt->tableElts)
 	{
 		ColumnDef  *colDef = lfirst(listptr);
-		Form_pg_attribute attr;
 
 		attnum++;
-		attr = TupleDescAttr(descriptor, attnum - 1);
 
 		if (colDef->raw_default != NULL)
 		{
@@ -969,7 +963,6 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
 			rawEnt->raw_default = colDef->raw_default;
 			rawEnt->generated = colDef->generated;
 			rawDefaults = lappend(rawDefaults, rawEnt);
-			attr->atthasdef = true;
 		}
 		else if (colDef->cooked_default != NULL)
 		{
@@ -987,7 +980,6 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
 			cooked->inhcount = 0;	/* ditto */
 			cooked->is_no_inherit = false;
 			cookedDefaults = lappend(cookedDefaults, cooked);
-			attr->atthasdef = true;
 		}
 
 		populate_compact_attribute(descriptor, attnum - 1);
-- 
2.34.1