v52-0001-refactor-buildExpressionExecutionStates.no-cfbot

application/octet-stream

Filename: v52-0001-refactor-buildExpressionExecutionStates.no-cfbot
Type: application/octet-stream
Part: 0
Message: Re: Add SPLIT PARTITION/MERGE PARTITIONS commands
From 82fc52c6382c9a05a48076881812dac0768d2ce2 Mon Sep 17 00:00:00 2001
From: jian he <jian.universality@gmail.com>
Date: Fri, 22 Aug 2025 11:31:08 +0800
Subject: [PATCH v52 1/1] refactor buildExpressionExecutionStates

buildExpressionExecutionStates seems not necessary,
so remove it.
while at it, also did some minor refactor.
---
 src/backend/commands/tablecmds.c | 68 +++++++++++++-------------------
 1 file changed, 28 insertions(+), 40 deletions(-)

diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index cc24a92c504..bf50d209d1c 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -22097,42 +22097,6 @@ GetAttributeStorage(Oid atttypid, const char *storagemode)
 }
 
 
-/*
- * buildExpressionExecutionStates: build the needed expression execution states
- * for new partition (newPartRel) checks and initialize expressions for
- * generated columns. All expressions should be created in "tab"
- * (AlteredTableInfo structure).
- */
-static void
-buildExpressionExecutionStates(AlteredTableInfo *tab, Relation newPartRel, EState *estate)
-{
-	/* Build the needed expression execution states. */
-	foreach_ptr(NewConstraint, con, tab->constraints)
-	{
-		switch (con->contype)
-		{
-			case CONSTR_CHECK:
-				con->qualstate = ExecPrepareExpr((Expr *) expand_generated_columns_in_expr(con->qual, newPartRel, 1), estate);
-				break;
-			case CONSTR_FOREIGN:
-				/* Nothing to do here. */
-				break;
-			case CONSTR_NOTNULL:
-				/* Nothing to do here. */
-				break;
-			default:
-				elog(ERROR, "unrecognized constraint type: %d",
-					 (int) con->contype);
-		}
-	}
-
-	foreach_ptr(NewColumnValue, ex, tab->newvals)
-	{
-		/* Expression already planned. */
-		ex->exprstate = ExecInitExpr((Expr *) ex->expr, NULL);
-	}
-}
-
 /*
  * evaluateGeneratedExpressionsAndCheckConstraints: evaluate any generated
  * expressions for "tab" (AlteredTableInfo structure) whose inputs come from
@@ -22400,11 +22364,12 @@ createTableConstraints(List **wqueue, AlteredTableInfo *tab,
 	 */
 	foreach_ptr(CookedConstraint, ccon, cookedConstraints)
 	{
-		if (!ccon->skip_validation && ccon->contype == CONSTR_CHECK)
+		if (!ccon->skip_validation)
 		{
 			Node	   *qual;
 			Bitmapset  *attnums = NULL;
 
+			Assert(ccon->contype == CONSTR_CHECK);
 			qual = expand_generated_columns_in_expr(ccon->expr, newRel, 1);
 			pull_varattnos(qual, 1, &attnums);
 
@@ -22419,7 +22384,7 @@ createTableConstraints(List **wqueue, AlteredTableInfo *tab,
 
 				newcon = (NewConstraint *) palloc0(sizeof(NewConstraint));
 				newcon->name = ccon->name;
-				newcon->contype = ccon->contype;
+				newcon->contype = CONSTR_CHECK;
 				newcon->qual = qual;
 
 				tab->constraints = lappend(tab->constraints, newcon);
@@ -22573,7 +22538,7 @@ createPartitionTable(List **wqueue, RangeVar *newPartName,
 /*
  * MergePartitionsMoveRows: scan partitions to be merged (mergingPartitions)
  * of the partitioned table and move rows into the new partition
- * (newPartRel). We also reevaulate check constraints against these rows.
+ * (newPartRel). We also vertify check constraints against these rows.
  */
 static void
 MergePartitionsMoveRows(List **wqueue, List *mergingPartitions, Relation newPartRel)
@@ -22595,7 +22560,30 @@ MergePartitionsMoveRows(List **wqueue, List *mergingPartitions, Relation newPart
 	/* Generate the constraint and default execution states. */
 	estate = CreateExecutorState();
 
-	buildExpressionExecutionStates(tab, newPartRel, estate);
+	/*
+	 * Build the needed expression execution states.
+	 * Here, we expect only NOT NULL and CHECK constraint.
+	*/
+	foreach_ptr(NewConstraint, con, tab->constraints)
+	{
+		switch (con->contype)
+		{
+			case CONSTR_CHECK:
+				/* we already expanded virtual expression in createTableConstraints */
+				con->qualstate = ExecPrepareExpr((Expr *) con->qual, estate);
+				break;
+			case CONSTR_NOTNULL:
+				/* Nothing to do here. */
+				break;
+			default:
+				elog(ERROR, "unrecognized constraint type: %d",
+					 (int) con->contype);
+		}
+	}
+
+	/* Expression already planned in createTableConstraints */
+	foreach_ptr(NewColumnValue, ex, tab->newvals)
+		ex->exprstate = ExecInitExpr((Expr *) ex->expr, NULL);
 
 	econtext = GetPerTupleExprContext(estate);
 
-- 
2.34.1