v41-0001-misc-fixes-in-createTableConstraints.no-cfbot

application/octet-stream

Filename: v41-0001-misc-fixes-in-createTableConstraints.no-cfbot
Type: application/octet-stream
Part: 0
Message: Re: Add SPLIT PARTITION/MERGE PARTITIONS commands
From bc971ce6a8ccf17be9dbf2caec36edf549f66b2f Mon Sep 17 00:00:00 2001
From: jian he <jian.universality@gmail.com>
Date: Fri, 6 Jun 2025 15:07:32 +0800
Subject: [PATCH v41 1/1] misc fixes in createTableConstraints

---
 src/backend/commands/tablecmds.c | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 70e76c677c8..84d961c0e2d 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -22169,23 +22169,19 @@ createTableConstraints(Relation modelRel, Relation newRel)
 		if (attribute->atthasdef)
 		{
 			Node	   *this_default = NULL;
-			AttrDefault *attrdef = constr->defval;
 			bool		found_whole_row;
-			int16		num;
+			AttrNumber	num;
 			Node	   *def;
 
-			/* Find default in constraint structure */
-			for (int i = 0; i < constr->num_defval; i++)
+			if (attribute->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
+				this_default = build_generation_expression(modelRel, attribute->attnum);
+			else
 			{
-				if (attrdef[i].adnum == parent_attno)
-				{
-					this_default = stringToNode(attrdef[i].adbin);
-					break;
-				}
+				this_default = TupleDescGetDefault(tupleDesc, attribute->attnum);
+				if (this_default == NULL)
+					elog(ERROR, "default expression not found for attribute %d of relation \"%s\"",
+						 attribute->attnum, RelationGetRelationName(modelRel));
 			}
-			if (this_default == NULL)
-				elog(ERROR, "default expression not found for attribute %d of relation \"%s\"",
-					 parent_attno, RelationGetRelationName(modelRel));
 
 			num = attmap->attnums[parent_attno - 1];
 			def = map_variable_attnos(this_default, 1, 0, attmap, InvalidOid, &found_whole_row);
@@ -22195,7 +22191,7 @@ createTableConstraints(Relation modelRel, Relation newRel)
 			 * that defaults cannot contain any vars, so it's OK that the
 			 * error message refers to generated columns.
 			 */
-			if (found_whole_row)
+			if (found_whole_row && attribute->attgenerated != '\0')
 				ereport(ERROR,
 						errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 						errmsg("cannot convert whole-row table reference"),
@@ -22253,7 +22249,7 @@ createTableConstraints(Relation modelRel, Relation newRel)
 	}
 
 	/* Store CHECK constraints. */
-	StoreConstraints(newRel, cookedConstraints, false);
+	StoreConstraints(newRel, cookedConstraints, true);
 
 	/* Don't need the cookedConstraints any more. */
 	list_free_deep(cookedConstraints);
-- 
2.34.1