From 160d9f92140a76e41684bfc2d3522c7986b17e0e Mon Sep 17 00:00:00 2001
From: Dean Rasheed <dean.a.rasheed@gmail.com>
Date: Sat, 21 Nov 2026 12:55:12 +0000
Subject: [PATCH v1 1/2] POC: Allow isolation test steps to be declared inline.

---
 src/test/isolation/README            | 34 +++++++++-
 src/test/isolation/isolationtester.c | 94 +++++++++++++++++++++++-----
 src/test/isolation/isolationtester.h |  5 +-
 src/test/isolation/specparse.y       | 42 ++++++++++++-
 4 files changed, 155 insertions(+), 20 deletions(-)

diff --git a/src/test/isolation/README b/src/test/isolation/README
index 5818ca50038..be14754d5bf 100644
--- a/src/test/isolation/README
+++ b/src/test/isolation/README
@@ -84,10 +84,11 @@ session <name>
 
   There are normally several "session" parts in a spec file. Each
   session is executed in its own connection. A session part consists
-  of three parts: setup, teardown and one or more "steps". The per-session
+  of three parts: setup, teardown and zero or more "steps". The per-session
   setup and teardown parts have the same syntax as the per-test setup and
   teardown described above, but they are executed in each session. The setup
   part might, for example, contain a "BEGIN" command to begin a transaction.
+  Session names must be unique within a spec file.
 
   Each step has the syntax
 
@@ -109,6 +110,31 @@ permutation <step name> ...
   or leave others out.  Also, each step name can be annotated with some
   parenthesized markers, which are described below.
 
+permutation <session name> { <SQL> } ...
+
+  A permutation may also be written in a self-contained form, where each
+  step is given inline as the name of the session in which it runs, followed
+  by its SQL. For readability, this form is best split over multiple lines,
+  for example:
+
+  permutation
+    s1 { DELETE FROM target WHERE key = 1; }
+    s2 { INSERT INTO target VALUES (1, 'insert1'); }
+    s1 { COMMIT; }
+    s2 { COMMIT; }
+
+  Here the steps are executed in the order written: first the DELETE in
+  session s1, then the INSERT in session s2, and so on.  Inline steps are
+  assigned generated names of the form "<session name>[<n>]", where n is a
+  counter that starts at one for each session used in the permutation, so
+  that the names are unique within a permutation and can be identified in
+  the test output.  Also, each session name can be annotated with some
+  parenthesized markers, which are described below.
+
+  Note also that inline steps may be mixed with references to named steps
+  within the same permutation (making the permutation no longer
+  self-contained, but potentially reducing duplication of test code).
+
 Session and step names are SQL identifiers, either plain or double-quoted.
 A difference from standard SQL is that no case-folding occurs, so that
 FOO and "FOO" are the same name while FOO and Foo are different,
@@ -160,10 +186,14 @@ discouraged since the extra files are a nuisance for maintenance.
 Instead, it's usually possible to stabilize the test output by applying
 special markers to some of the step names listed in a permutation line.
 
-The general form of a permutation entry is
+The general form of a permutation entry is either
 
 	<step name> [ ( <marker> [ , <marker> ... ] ) ]
 
+or
+
+	<session name> [ ( <marker> [ , <marker> ... ] ) ] { <SQL> }
+
 where each marker defines a "blocking condition".  The step will not be
 reported as completed before all the blocking conditions are satisfied.
 The possible markers are:
diff --git a/src/test/isolation/isolationtester.c b/src/test/isolation/isolationtester.c
index e64dc020b18..ac71736396a 100644
--- a/src/test/isolation/isolationtester.c
+++ b/src/test/isolation/isolationtester.c
@@ -285,6 +285,21 @@ check_testspec(TestSpec *testspec)
 		}
 	}
 
+	/* Verify that all session names are unique. */
+	for (i = 0; i < testspec->nsessions; i++)
+	{
+		for (j = i + 1; j < testspec->nsessions; j++)
+		{
+			if (strcmp(testspec->sessions[i]->name,
+					   testspec->sessions[j]->name) == 0)
+			{
+				fprintf(stderr, "duplicate session name: %s\n",
+						testspec->sessions[i]->name);
+				exit(1);
+			}
+		}
+	}
+
 	/* Set the session index fields in steps. */
 	for (i = 0; i < testspec->nsessions; i++)
 	{
@@ -295,8 +310,9 @@ check_testspec(TestSpec *testspec)
 	}
 
 	/*
-	 * If we have manually-specified permutations, link PermutationSteps to
-	 * Steps, and fill in blocker links.
+	 * If we have manually-specified permutations, link named PermutationSteps
+	 * to Steps, assign inline Steps sessions and names, and fill in blocker
+	 * links.
 	 */
 	for (i = 0; i < testspec->npermutations; i++)
 	{
@@ -305,22 +321,72 @@ check_testspec(TestSpec *testspec)
 		for (j = 0; j < p->nsteps; j++)
 		{
 			PermutationStep *pstep = p->steps[j];
-			Step	  **this = (Step **) bsearch(pstep->name,
-												 allsteps,
-												 nallsteps,
-												 sizeof(Step *),
-												 step_bsearch_cmp);
 
-			if (this == NULL)
+			/* If it's a named PermutationStep, link it to a Step */
+			if (pstep->name)
 			{
-				fprintf(stderr, "undefined step \"%s\" specified in permutation\n",
-						pstep->name);
-				exit(1);
+				Step	  **this = (Step **) bsearch(pstep->name,
+													 allsteps,
+													 nallsteps,
+													 sizeof(Step *),
+													 step_bsearch_cmp);
+
+				if (this == NULL)
+				{
+					fprintf(stderr, "undefined step \"%s\" specified in permutation\n",
+							pstep->name);
+					exit(1);
+				}
+				pstep->step = *this;
+
+				/* Mark the step used, for check below */
+				pstep->step->used = true;
 			}
-			pstep->step = *this;
 
-			/* Mark the step used, for check below */
-			pstep->step->used = true;
+			/* Otherwise, it's an inline Step; assign it to a session */
+			else
+			{
+				for (k = 0; k < testspec->nsessions; k++)
+				{
+					if (strcmp(testspec->sessions[k]->name,
+							   pstep->step->sessionname) == 0)
+					{
+						pstep->step->session = k;
+						break;
+					}
+				}
+				if (pstep->step->session < 0)
+				{
+					fprintf(stderr, "undefined session \"%s\" specified by step in permutation\n",
+							pstep->step->sessionname);
+					exit(1);
+				}
+
+				/* Generate a name for the Step (unique within permutation) */
+				k = 1;
+				while (true)
+				{
+					char	   *name = psprintf("%s[%d]",
+												pstep->step->sessionname, k);
+					bool		unique = true;
+
+					for (int s = 0; s < p->nsteps; s++)
+					{
+						if (p->steps[s]->name &&
+							strcmp(p->steps[s]->name, name) == 0)
+						{
+							unique = false;
+							break;
+						}
+					}
+					if (unique)
+					{
+						pstep->name = pstep->step->name = name;
+						break;
+					}
+					k++;
+				}
+			}
 		}
 
 		/*
diff --git a/src/test/isolation/isolationtester.h b/src/test/isolation/isolationtester.h
index a9328a94516..a3717f0499b 100644
--- a/src/test/isolation/isolationtester.h
+++ b/src/test/isolation/isolationtester.h
@@ -32,8 +32,9 @@ typedef struct
 
 struct Step
 {
-	char	   *name;
+	char	   *name;			/* initially NULL for an inline step */
 	char	   *sql;
+	char	   *sessionname;	/* only used for inline steps */
 	/* These fields are filled by check_testspec(): */
 	int			session;		/* identifies owning session */
 	bool		used;			/* has step been used in a permutation? */
@@ -48,7 +49,7 @@ typedef enum
 
 typedef struct
 {
-	char	   *stepname;
+	char	   *stepname;		/* initially NULL for an inline step */
 	PermutationStepBlockerType blocktype;
 	int			num_notices;	/* only used for PSB_NUM_NOTICES */
 	/* These fields are filled by check_testspec(): */
diff --git a/src/test/isolation/specparse.y b/src/test/isolation/specparse.y
index f6b9058e557..f25cc6f2890 100644
--- a/src/test/isolation/specparse.y
+++ b/src/test/isolation/specparse.y
@@ -46,7 +46,7 @@ TestSpec		parseresult;			/* result of parsing is left here */
 %type <str>  opt_setup opt_teardown
 %type <str> setup
 %type <ptr_list> step_list session_list permutation_list opt_permutation_list
-%type <ptr_list> permutation_step_list blocker_list
+%type <ptr_list> permutation_step_list blocker_list opt_steps
 %type <session> session
 %type <step> step
 %type <permutation> permutation
@@ -121,7 +121,7 @@ session_list:
 		;
 
 session:
-			SESSION identifier opt_setup step_list opt_teardown
+			SESSION identifier opt_setup opt_steps opt_teardown
 			{
 				$$ = pg_malloc_object(Session);
 				$$->name = $2;
@@ -132,6 +132,15 @@ session:
 			}
 		;
 
+opt_steps:
+			/* EMPTY */
+			{
+				$$.elements = NULL;
+				$$.nelements = 0;
+			}
+			| step_list
+		;
+
 step_list:
 			step_list step
 			{
@@ -155,6 +164,7 @@ step:
 				$$ = pg_malloc_object(Step);
 				$$->name = $2;
 				$$->sql = $3;
+				$$->sessionname = NULL; /* not an inline step */
 				$$->session = -1; /* until filled */
 				$$->used = false;
 			}
@@ -231,6 +241,34 @@ permutation_step:
 				$$->nblockers = $3.nelements;
 				$$->step = NULL;
 			}
+			| identifier sqlblock
+			{
+				$$ = pg_malloc_object(PermutationStep);
+				$$->name = NULL; /* until filled */
+				$$->blockers = NULL;
+				$$->nblockers = 0;
+				$$->step = pg_malloc_object(Step);
+
+				$$->step->name = NULL; /* until filled */
+				$$->step->sql = $2;
+				$$->step->sessionname = $1;
+				$$->step->session = -1; /* until filled */
+				$$->step->used = true;
+			}
+			| identifier '(' blocker_list ')' sqlblock
+			{
+				$$ = pg_malloc_object(PermutationStep);
+				$$->name = NULL; /* until filled */
+				$$->blockers = (PermutationStepBlocker **) $3.elements;
+				$$->nblockers = $3.nelements;
+				$$->step = pg_malloc_object(Step);
+
+				$$->step->name = NULL; /* until filled */
+				$$->step->sql = $5;
+				$$->step->sessionname = $1;
+				$$->step->session = -1; /* until filled */
+				$$->step->used = true;
+			}
 		;
 
 blocker_list:
-- 
2.51.0

