From 7b04b9344fa00d922c8e40421a03f8ff44c15087 Mon Sep 17 00:00:00 2001
From: Yura Sokolov <y.sokolov@postgrespro.ru>
Date: Sun, 6 Dec 2026 17:16:48 +0300
Subject: [PATCH v1 4/8] pgbench: extract false-branch skipping; separate
 meta-command results

Extract the loop that fast-forwards past commands in a non-executed
conditional branch out of the CSTATE_SKIP_COMMAND case into the
skipFalseConditionBranch() helper.  The logic is unchanged, only its
shape.

executeMetaCommand() used to return a ConnectionStateEnum, which forced
it to set connection states (CSTATE_SLEEP, CSTATE_ABORTED, ...) that the
caller then interpreted again.  Let it return a new MetaCommandResult
(META_RESULT_END_COMMAND, META_RESULT_SLEEP, META_RESULT_WAIT_RESULT,
META_RESULT_ABORTED) describing only what happened to the meta command;
the caller owns the connection state transitions.
---
 src/bin/pgbench/pgbench.c        | 268 ++++++++++++++++++-------------
 src/tools/pgindent/typedefs.list |   1 +
 2 files changed, 153 insertions(+), 116 deletions(-)

diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index 419341ee2fe..e6db543a814 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -661,6 +661,17 @@ typedef enum
 	CSTATE_FINISHED,
 } ConnectionStateEnum;
 
+/*
+ * Result of executeMetaCommand().
+ */
+typedef enum
+{
+	META_RESULT_END_COMMAND,
+	META_RESULT_SLEEP,
+	META_RESULT_WAIT_RESULT,
+	META_RESULT_ABORTED,
+} MetaCommandResult;
+
 /*
  * Connection state.
  */
@@ -932,7 +943,8 @@ static void setIntValue(PgBenchValue *pv, int64 ival);
 static void setDoubleValue(PgBenchValue *pv, double dval);
 static bool evaluateExpr(CState *st, PgBenchExpr *expr,
 						 PgBenchValue *retval);
-static ConnectionStateEnum executeMetaCommand(RunState *run, CState *st);
+static MetaCommandResult executeMetaCommand(RunState *run, CState *st);
+static void skipFalseConditionBranch(CState *st);
 static void doLog(RunState *run, CState *st,
 				  bool tx_skipped, double latency, double lag);
 static void processXactStats(RunState *run, CState *st, bool tx_skipped);
@@ -4049,22 +4061,31 @@ advanceConnectionState(RunState *run, CState *st)
 				else if (command->type == META_COMMAND)
 				{
 					/*-----
-					 * Possible state changes when executing meta commands:
-					 * - on errors CSTATE_ABORTED
-					 * - on sleep CSTATE_SLEEP
-					 * - else CSTATE_END_COMMAND
+					 * Possible results when executing meta commands:
+					 * - on errors META_RESULT_ABORTED
+					 * - on sleep META_RESULT_SLEEP
+					 * - on \endpipeline META_RESULT_WAIT_RESULT
+					 * - else META_RESULT_END_COMMAND
 					 */
-					st->state = executeMetaCommand(run, st);
+					MetaCommandResult mres = executeMetaCommand(run, st);
 
-					if (st->state == CSTATE_SLEEP)
+					if (mres == META_RESULT_SLEEP)
 					{
+						st->state = CSTATE_SLEEP;
 						wakeup_heap_insert(&run->wakeup_heap, st, st->sleep_until);
 						if (run->sockets->batch)
 							remove_socket_from_set(run->sockets, st);
 						return;
 					}
-					if (st->state == CSTATE_ABORTED)
+					else if (mres == META_RESULT_ABORTED)
+					{
+						st->state = CSTATE_ABORTED;
 						st->estatus = ESTATUS_META_COMMAND_ERROR;
+					}
+					else if (mres == META_RESULT_WAIT_RESULT)
+						st->state = CSTATE_WAIT_RESULT;
+					else		/* META_RESULT_END_COMMAND */
+						st->state = CSTATE_END_COMMAND;
 				}
 
 				/*
@@ -4084,95 +4105,7 @@ advanceConnectionState(RunState *run, CState *st)
 			case CSTATE_SKIP_COMMAND:
 				Assert(!conditional_active(st->cstack));
 				/* quickly skip commands until something to do... */
-				while (true)
-				{
-					command = sql_script[st->use_file].commands[st->command];
-
-					/* cannot reach end of script in that state */
-					Assert(command != NULL);
-
-					/*
-					 * if this is conditional related, update conditional
-					 * state
-					 */
-					if (command->type == META_COMMAND &&
-						(command->meta == META_IF ||
-						 command->meta == META_ELIF ||
-						 command->meta == META_ELSE ||
-						 command->meta == META_ENDIF))
-					{
-						switch (conditional_stack_peek(st->cstack))
-						{
-							case IFSTATE_FALSE:
-								if (command->meta == META_IF)
-								{
-									/* nested if in skipped branch - ignore */
-									conditional_stack_push(st->cstack,
-														   IFSTATE_IGNORED);
-									st->command++;
-								}
-								else if (command->meta == META_ELIF)
-								{
-									/* we must evaluate the condition */
-									st->state = CSTATE_START_COMMAND;
-								}
-								else if (command->meta == META_ELSE)
-								{
-									/* we must execute next command */
-									conditional_stack_poke(st->cstack,
-														   IFSTATE_ELSE_TRUE);
-									st->state = CSTATE_START_COMMAND;
-									st->command++;
-								}
-								else if (command->meta == META_ENDIF)
-								{
-									Assert(!conditional_stack_empty(st->cstack));
-									conditional_stack_pop(st->cstack);
-									if (conditional_active(st->cstack))
-										st->state = CSTATE_START_COMMAND;
-									/* else state remains CSTATE_SKIP_COMMAND */
-									st->command++;
-								}
-								break;
-
-							case IFSTATE_IGNORED:
-							case IFSTATE_ELSE_FALSE:
-								if (command->meta == META_IF)
-									conditional_stack_push(st->cstack,
-														   IFSTATE_IGNORED);
-								else if (command->meta == META_ENDIF)
-								{
-									Assert(!conditional_stack_empty(st->cstack));
-									conditional_stack_pop(st->cstack);
-									if (conditional_active(st->cstack))
-										st->state = CSTATE_START_COMMAND;
-								}
-								/* could detect "else" & "elif" after "else" */
-								st->command++;
-								break;
-
-							case IFSTATE_NONE:
-							case IFSTATE_TRUE:
-							case IFSTATE_ELSE_TRUE:
-							default:
-
-								/*
-								 * inconsistent if inactive, unreachable dead
-								 * code
-								 */
-								Assert(false);
-						}
-					}
-					else
-					{
-						/* skip and consider next */
-						st->command++;
-					}
-
-					if (st->state != CSTATE_SKIP_COMMAND)
-						/* out of quick skip command loop */
-						break;
-				}
+				skipFalseConditionBranch(st);
 				break;
 
 				/*
@@ -4503,7 +4436,7 @@ advanceConnectionState(RunState *run, CState *st)
  * run->now is updated to the current time, unless the command is expected
  * to take no time to execute.
  */
-static ConnectionStateEnum
+static MetaCommandResult
 executeMetaCommand(RunState *run, CState *st)
 {
 	Command    *command = sql_script[st->use_file].commands[st->command];
@@ -4543,11 +4476,11 @@ executeMetaCommand(RunState *run, CState *st)
 		if (!evaluateSleep(&st->variables, argc, argv, &usec))
 		{
 			commandFailed(st, "sleep", "execution of meta-command failed");
-			return CSTATE_ABORTED;
+			return META_RESULT_ABORTED;
 		}
 
 		st->sleep_until = pg_time_now_lazy(run) + usec;
-		return CSTATE_SLEEP;
+		return META_RESULT_SLEEP;
 	}
 	else if (command->meta == META_SET)
 	{
@@ -4557,13 +4490,13 @@ executeMetaCommand(RunState *run, CState *st)
 		if (!evaluateExpr(st, expr, &result))
 		{
 			commandFailed(st, argv[0], "evaluation of meta-command failed");
-			return CSTATE_ABORTED;
+			return META_RESULT_ABORTED;
 		}
 
 		if (!putVariableValue(&st->variables, argv[0], argv[1], &result))
 		{
 			commandFailed(st, "set", "assignment of meta-command failed");
-			return CSTATE_ABORTED;
+			return META_RESULT_ABORTED;
 		}
 	}
 	else if (command->meta == META_IF)
@@ -4576,7 +4509,7 @@ executeMetaCommand(RunState *run, CState *st)
 		if (!evaluateExpr(st, expr, &result))
 		{
 			commandFailed(st, argv[0], "evaluation of meta-command failed");
-			return CSTATE_ABORTED;
+			return META_RESULT_ABORTED;
 		}
 
 		cond = valueTruth(&result);
@@ -4593,13 +4526,13 @@ executeMetaCommand(RunState *run, CState *st)
 		{
 			/* elif after executed block, skip eval and wait for endif. */
 			conditional_stack_poke(st->cstack, IFSTATE_IGNORED);
-			return CSTATE_END_COMMAND;
+			return META_RESULT_END_COMMAND;
 		}
 
 		if (!evaluateExpr(st, expr, &result))
 		{
 			commandFailed(st, argv[0], "evaluation of meta-command failed");
-			return CSTATE_ABORTED;
+			return META_RESULT_ABORTED;
 		}
 
 		cond = valueTruth(&result);
@@ -4633,7 +4566,7 @@ executeMetaCommand(RunState *run, CState *st)
 		if (!runShellCommand(&st->variables, argv[1], argv + 2, argc - 2))
 		{
 			commandFailed(st, "setshell", "execution of meta-command failed");
-			return CSTATE_ABORTED;
+			return META_RESULT_ABORTED;
 		}
 	}
 	else if (command->meta == META_SHELL)
@@ -4641,7 +4574,7 @@ executeMetaCommand(RunState *run, CState *st)
 		if (!runShellCommand(&st->variables, NULL, argv + 1, argc - 1))
 		{
 			commandFailed(st, "shell", "execution of meta-command failed");
-			return CSTATE_ABORTED;
+			return META_RESULT_ABORTED;
 		}
 	}
 	else if (command->meta == META_STARTPIPELINE)
@@ -4653,7 +4586,7 @@ executeMetaCommand(RunState *run, CState *st)
 		if (querymode == QUERY_SIMPLE)
 		{
 			commandFailed(st, "startpipeline", "cannot use pipeline mode with the simple query protocol");
-			return CSTATE_ABORTED;
+			return META_RESULT_ABORTED;
 		}
 
 		/*
@@ -4669,12 +4602,12 @@ executeMetaCommand(RunState *run, CState *st)
 		if (PQpipelineStatus(st->con) != PQ_PIPELINE_OFF)
 		{
 			commandFailed(st, "startpipeline", "already in pipeline mode");
-			return CSTATE_ABORTED;
+			return META_RESULT_ABORTED;
 		}
 		if (PQenterPipelineMode(st->con) == 0)
 		{
 			commandFailed(st, "startpipeline", "failed to enter pipeline mode");
-			return CSTATE_ABORTED;
+			return META_RESULT_ABORTED;
 		}
 	}
 	else if (command->meta == META_SYNCPIPELINE)
@@ -4682,12 +4615,12 @@ executeMetaCommand(RunState *run, CState *st)
 		if (PQpipelineStatus(st->con) != PQ_PIPELINE_ON)
 		{
 			commandFailed(st, "syncpipeline", "not in pipeline mode");
-			return CSTATE_ABORTED;
+			return META_RESULT_ABORTED;
 		}
 		if (PQsendPipelineSync(st->con) == 0)
 		{
 			commandFailed(st, "syncpipeline", "failed to send a pipeline sync");
-			return CSTATE_ABORTED;
+			return META_RESULT_ABORTED;
 		}
 		st->num_syncs++;
 	}
@@ -4696,17 +4629,17 @@ executeMetaCommand(RunState *run, CState *st)
 		if (PQpipelineStatus(st->con) != PQ_PIPELINE_ON)
 		{
 			commandFailed(st, "endpipeline", "not in pipeline mode");
-			return CSTATE_ABORTED;
+			return META_RESULT_ABORTED;
 		}
 		if (!PQpipelineSync(st->con))
 		{
 			commandFailed(st, "endpipeline", "failed to send a pipeline sync");
-			return CSTATE_ABORTED;
+			return META_RESULT_ABORTED;
 		}
 		st->num_syncs++;
 		/* Now wait for the PGRES_PIPELINE_SYNC and exit pipeline mode there */
 		/* collect pending results before getting out of pipeline mode */
-		return CSTATE_WAIT_RESULT;
+		return META_RESULT_WAIT_RESULT;
 	}
 
 	/*
@@ -4715,7 +4648,110 @@ executeMetaCommand(RunState *run, CState *st)
 	 */
 	run->now = 0;
 
-	return CSTATE_END_COMMAND;
+	return META_RESULT_END_COMMAND;
+}
+
+/*
+ * Skip commands in an inactive conditional branch.
+ *
+ * Fast-forwards st->command, updating the conditional stack, until the
+ * skip can end:
+ *
+ * - \elif after a false \if is met: the condition must be evaluated;
+ * - \else after a false \if is met: the else-branch becomes active;
+ * - \endif ends the inactive conditional and the enclosing conditional
+ *   (if any) is active.
+ *
+ * A nested \if inside a skipped branch is pushed as IFSTATE_IGNORED, so
+ * its body is skipped as well.
+ */
+static void
+skipFalseConditionBranch(CState *st)
+{
+	Command    *command;
+
+	while (st->state == CSTATE_SKIP_COMMAND)
+	{
+		command = sql_script[st->use_file].commands[st->command];
+
+		/* cannot reach end of script in that state */
+		Assert(command != NULL);
+
+		/*
+		 * if this is conditional related, update conditional state
+		 */
+		if (command->type != META_COMMAND ||
+			(command->meta != META_IF &&
+			 command->meta != META_ELIF &&
+			 command->meta != META_ELSE &&
+			 command->meta != META_ENDIF))
+		{
+			/* skip and consider next */
+			st->command++;
+			continue;
+		}
+
+		switch (conditional_stack_peek(st->cstack))
+		{
+			case IFSTATE_FALSE:
+				if (command->meta == META_IF)
+				{
+					/* nested if in skipped branch - ignore */
+					conditional_stack_push(st->cstack,
+										   IFSTATE_IGNORED);
+					st->command++;
+				}
+				else if (command->meta == META_ELIF)
+				{
+					/* we must evaluate the condition */
+					st->state = CSTATE_START_COMMAND;
+				}
+				else if (command->meta == META_ELSE)
+				{
+					/* we must execute next command */
+					conditional_stack_poke(st->cstack,
+										   IFSTATE_ELSE_TRUE);
+					st->state = CSTATE_START_COMMAND;
+					st->command++;
+				}
+				else if (command->meta == META_ENDIF)
+				{
+					Assert(!conditional_stack_empty(st->cstack));
+					conditional_stack_pop(st->cstack);
+					if (conditional_active(st->cstack))
+						st->state = CSTATE_START_COMMAND;
+					/* else state remains CSTATE_SKIP_COMMAND */
+					st->command++;
+				}
+				break;
+
+			case IFSTATE_IGNORED:
+			case IFSTATE_ELSE_FALSE:
+				if (command->meta == META_IF)
+					conditional_stack_push(st->cstack,
+										   IFSTATE_IGNORED);
+				else if (command->meta == META_ENDIF)
+				{
+					Assert(!conditional_stack_empty(st->cstack));
+					conditional_stack_pop(st->cstack);
+					if (conditional_active(st->cstack))
+						st->state = CSTATE_START_COMMAND;
+				}
+				/* could detect "else" & "elif" after "else" */
+				st->command++;
+				break;
+
+			case IFSTATE_NONE:
+			case IFSTATE_TRUE:
+			case IFSTATE_ELSE_TRUE:
+			default:
+
+				/*
+				 * inconsistent if inactive, unreachable dead code
+				 */
+				Assert(false);
+		}
+	}
 }
 
 /*
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 3d89d2c4a82..d85d44752bc 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -1765,6 +1765,7 @@ MergeStmt
 MergeSupportFunc
 MergeWhenClause
 MetaCommand
+MetaCommandResult
 MinMaxAggInfo
 MinMaxAggPath
 MinMaxExpr
-- 
2.43.0

