From 7b2c579aae2238901a57aac0dba3774c95b3d6cf Mon Sep 17 00:00:00 2001
From: Alexandre Felipe <o.alexandre.felipe@gmail.com>
Date: Sun, 22 Nov 2026 13:05:35 +0000
Subject: [v3.0 PATCH 5/5] pg_regress: concurrent and sequential groups.

This commit introduce concurrent and sequential test groups
whose tests can run concurrently with tests in other groups.
An item might contain a single test or multiple tests wrapped
in brackets, items in a concurrent or batch group can run in any order
items in a sequential group will run one after the other, and
can also run concurrently with tests from other groups.

This opens new ways to express the constraints to the test
execution.
---
 src/test/regress/parallel_schedule | 165 ++++++++++++++++-------------
 src/test/regress/pg_regress.c      | 152 ++++++++++++++++++++++----
 2 files changed, 222 insertions(+), 95 deletions(-)

diff --git a/src/test/regress/parallel_schedule b/src/test/regress/parallel_schedule
index a7667e1cf80..dc2a339b491 100644
--- a/src/test/regress/parallel_schedule
+++ b/src/test/regress/parallel_schedule
@@ -14,7 +14,8 @@ test: test_setup
 # ----------
 # The first group of parallel tests
 # ----------
-test: \
+test concurrently: \
+    database \
     boolean \
     char \
     name \
@@ -24,27 +25,31 @@ test: \
     int4 \
     int8 \
     oid \
-    float4 \
-    float8 \
     bit \
     numeric \
     txid \
     uuid \
     enum \
     money \
-    rangetypes \
     pg_lsn \
     regproc
+# float4 and float8 create immutable parallel-unsafe I/O functions
+test sequentially: (float4 float8) opr_sanity
+
+# multirangetypes depends on rangetypes.
+# multirangetypes and strings must finish before type_sanity
+# (strings sets text column storage to external).
+test sequentially: rangetypes (multirangetypes strings) type_sanity
 
 # ----------
 # The second group of parallel tests
-# multirangetypes depends on rangetypes
-# multirangetypes shouldn't run concurrently with type_sanity
 # ----------
-test: \
-    strings \
+test concurrently: \
     md5 \
-    numerology \
+    numerology
+
+# geometry depends on point, lseg, line, box, path, polygon, circle
+test sequentially: ( \
     point \
     lseg \
     line \
@@ -52,36 +57,35 @@ test: \
     path \
     polygon \
     circle \
+) geometry
+
+# horology depends on date, time, timetz, timestamp, timestamptz, interval
+test sequentially: ( \
     date \
     time \
     timetz \
     timestamp \
     timestamptz \
     interval \
+) horology
+
+test concurrently: \
     inet \
     macaddr \
-    macaddr8 \
-    multirangetypes
+    macaddr8
 
 # ----------
 # Another group of parallel tests
-# geometry depends on point, lseg, line, box, path, polygon, circle
-# horology depends on date, time, timetz, timestamp, timestamptz, interval
 # ----------
-test: \
-    geometry \
-    horology \
+test concurrently: \
     tstypes \
     regex \
-    type_sanity \
-    opr_sanity \
     misc_sanity \
     comments \
     expressions \
     unicode \
     xid \
     mvcc \
-    database \
     stats_import \
     pg_ndistinct \
     pg_dependencies \
@@ -123,39 +127,32 @@ test: \
     index_including \
     index_including_gist
 
+# ----------
+# sanity_check does a vacuum, affecting the sort order of SELECT *
+# results. So it should not run parallel to other tests.
+# ----------
+test: sanity_check
 # ----------
 # Another group of parallel tests
 # ----------
-test: \
-    create_aggregate \
-    create_function_sql \
+test concurrently: \
     create_cast \
-    constraints \
     triggers \
     select \
-    inherit \
     typed_table \
     vacuum \
     drop_if_exists \
-    updatable_views \
     roleattributes \
-    create_am \
     hash_func \
     errors \
     infinite_recurse
 
-# ----------
-# sanity_check does a vacuum, affecting the sort order of SELECT *
-# results. So it should not run parallel to other tests.
-# ----------
-test: sanity_check
-
 # ----------
 # Another group of parallel tests
 # aggregates depends on create_aggregate
 # join depends on create_misc
 # ----------
-test: \
+test concurrently: \
     select_into \
     select_distinct \
     select_distinct_on \
@@ -164,15 +161,11 @@ test: \
     subselect \
     union \
     case \
-    join \
-    aggregates \
     transactions \
     random \
     portals \
     arrays \
     btree_index \
-    hash_index \
-    update \
     delete \
     namespace \
     prepared_xacts
@@ -180,71 +173,85 @@ test: \
 # ----------
 # Another group of parallel tests
 # ----------
-test: \
-    brin \
+test concurrently: \
     gin \
     gist \
     spgist \
-    privileges \
     init_privs \
     security_label \
     collate \
     matview \
     lock \
-    replica_identity \
-    rowsecurity \
     object_address \
     tablesample \
     groupingsets \
     drop_operator \
     password \
     identity \
-    generated_stored \
     join_hash
 
 # ----------
 # Additional BRIN tests
 # ----------
-test: \
+test concurrently: \
     brin_bloom \
     brin_multi
 
 # ----------
 # Another group of parallel tests
 # ----------
-test: \
-    alter_generic alter_operator \
+test concurrently: \
+    alter_operator \
     create_table_like \
     create_role \
-    misc misc_functions \
+    misc_functions \
     async \
     dbsize \
-    merge \
     nls \
     sysviews \
     tsrf \
     tid tidscan tidrangescan \
-    collate.utf8 collate.icu.utf8 \
+    collate.utf8 \
     incremental_sort \
-    without_overlaps \
-    generated_virtual
+    without_overlaps
 
-# collate.linux.utf8 and collate.icu.utf8 tests cannot be run in parallel with each other
+# collate.icu.utf8, collate.linux.utf8 and collate.windows.win1252
+# all create role regress_test_role
 # psql depends on create_am
 # amutils depends on geometry, create_index_spgist, hash_index, brin
-test: \
-    amutils \
-    psql psql_crosstab psql_pipeline \
-    collate.linux.utf8 collate.windows.win1252 \
-    rules \
-    stats_ext
+test sequentially: \
+    (hash_index brin) amutils
+test sequentially: \
+    create_am (psql_crosstab psql_pipeline)
+test sequentially: \
+    collate.icu.utf8 collate.linux.utf8 collate.windows.win1252
+# generated_stored and generated_virtual both create role regress_user11
+test sequentially: generated_stored generated_virtual
+test concurrently: \
+    rules
+
+# aggregates uses the functions create_aggregate defines
+test sequentially: create_aggregate aggregates
+# constraints and replica_identity both create index pk
+test sequentially: constraints replica_identity
+# inherit and update both create list_parted
+test sequentially: inherit update
+# privileges and rowsecurity both create type t1.
+# privileges also revokes USAGE on language sql, which merge,
+# create_function_sql, and alter_generic need.
+test sequentially: privileges (rowsecurity merge create_function_sql alter_generic)
 
 # ----------
 # Run these alone so they don't run out of parallel workers
 # select_parallel depends on create_misc
 # ----------
 test: select_parallel
+# updatable_views and join both create type t1
+test sequentially: updatable_views join
 test: write_parallel
+# psql needs create_am and language sql; both are finished before this fence.
+# psql and misc both touch onek.
+test sequentially: (stats_ext psql) misc
 test: vacuum_parallel
 
 # Run this alone, because concurrent DROP TABLE would make non-superuser
@@ -260,7 +267,7 @@ test: \
 # Another group of parallel tests
 # select_views depends on create_view
 # ----------
-test: \
+test concurrently: \
     select_views \
     portals_p2 \
     portals_inflight \
@@ -271,20 +278,18 @@ test: \
     combocid \
     tsearch \
     tsdicts \
-    foreign_data \
     window \
     xmlmap \
     functional_deps \
     advisory_lock \
     indirect_toast \
     equivclass \
-    stats_rewrite \
-    alter_column_type
+    stats_rewrite
 
 # ----------
 # Another group of parallel tests (JSON related)
 # ----------
-test: \
+test concurrently: \
     json \
     jsonb \
     json_encoding \
@@ -301,7 +306,7 @@ test: \
 # NB: temp.sql does reconnects which transiently uses 2 connections,
 # so keep this parallel group to at most 19 tests
 # ----------
-test: \
+test concurrently: \
     plancache \
     limit \
     plpgsql \
@@ -312,7 +317,6 @@ test: \
     prepare \
     conversion \
     truncate \
-    alter_table \
     sequence \
     polymorphism \
     rowtypes \
@@ -321,10 +325,9 @@ test: \
     with \
     xml
 
-# rowtypes_alter relies on cached plans surviving across statements, which
-# concurrent DDL can prevent, and on parallel workers being available, so run
-# it by itself
-test: rowtypes_alter
+# partition_prune's temp tables p1 and p2 are visible to alter_table's
+# pg_class scan
+test sequentially: partition_prune alter_table
 
 # ----------
 # Another group of parallel tests
@@ -332,14 +335,12 @@ test: rowtypes_alter
 # The stats test resets stats, so nothing else needing stats access can be in
 # this group.
 # ----------
-test: \
+test concurrently: \
     partition_join \
-    partition_prune \
     reloptions \
     hash_part \
     indexing \
     partition_aggregate \
-    partition_info \
     tuplesort \
     explain \
     memoize \
@@ -352,11 +353,25 @@ test: \
 # ----------
 # Another group of parallel tests (compression)
 # ----------
-test: \
+test concurrently: \
     compression \
     compression_lz4 \
-    compression_pglz \
-    cluster
+    compression_pglz
+
+# cluster creates clstr_fdw, which foreign_data expects to be gone
+test sequentially: cluster foreign_data
+
+
+# alter_table creates p1, at_tab1, and attbl, which partition_info and
+# alter_column_type also create. Keep those two behind this fence.
+test: \
+    alter_column_type \
+    partition_info
+
+# rowtypes_alter relies on cached plans surviving across statements, which
+# concurrent DDL can prevent, and on parallel workers being available, so run
+# it by itself
+test: rowtypes_alter
 
 # event_trigger depends on create_am and cannot run concurrently with
 # any test that runs DDL
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index d12b2b29880..0ea87e16b86 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -1818,12 +1818,19 @@ dep_free(_dep_graph *g)
 
 /*
  * schedule = ( space | comment | group )+
- * group    = "test:" name+
+ * group    = "test" [ "batch" | "concurrently" | "sequentially" ] ":" items
+ * items    = name+                          -- batch (default) and concurrently
+ *          | ( "(" name+ ")" | name )+      -- sequentially
  * name     = (space "\" space comment? newline)+ test-name
- * comment = "#" .*
- * space = [ \t]*
- * newline = "\n" | "\r\n"
+ * comment  = "#" .*
+ * space    = [ \t]*
+ * newline  = "\n" | "\r\n"
  * test-name = [^ \t\n\r\\#]+
+ *
+ * batch runs its names in parallel and shifts the barrier. concurrently and
+ * sequentially share one next barrier, created when it is missing, so those
+ * groups overlap. A parenthesized sequential list hangs off an auxiliary
+ * node so the next item still splits a single edge.
  */
 typedef struct
 {
@@ -1835,6 +1842,7 @@ typedef struct
 	char	   *p;
 	_dep_graph *graph;
 	_dep_node  *fence;
+	_dep_node  *open;			/* next barrier; NULL if none yet */
 } _sched;
 
 /*
@@ -1919,10 +1927,7 @@ schedule_read_name(_sched *s)
 	}
 
 	if (len == 0)
-		bail("syntax error in schedule file \"%s\" line %d: "
-			 "expected test name, got \"%.*s\"",
-			 s->schedule, s->line_num,
-			 (int) strcspn(s->p, "\r\n"), s->p);
+		return NULL;
     
 	name = pg_malloc(len + 1);
 	strncpy(name, s->p, len);
@@ -1931,13 +1936,80 @@ schedule_read_name(_sched *s)
 	return name;
 }
 
+/*
+ * One item from prev toward next: a name, or "(" names ")" joined by an
+ * auxiliary node. next may be NULL; the caller links the returned node.
+ * NULL means this group is finished.
+ */
+static _dep_node *
+schedule_read_item(_sched *s, _dep_node *prev, _dep_node *next)
+{
+	char	   *name;
+	_dep_node  *aux;
+	_dep_node  *test;
+	int			count;
+
+	name = schedule_read_name(s);
+
+	/* a test name */
+	if (name != NULL)
+	{
+		test = dep_add(s->graph, name);
+		dep_link(prev, test);
+		if (next != NULL)
+			dep_link(test, next);
+		return test;
+	}
+
+	/* end of a test group */
+	if (schedule_at_eol(s->p))
+		return NULL;
+
+	/* an subgroup in brackets */
+	if (*s->p != '(')
+		bail("syntax error in schedule file \"%s\" %d: expected test name or '(' but got \"%s\"",
+			 s->schedule, s->line_num, s->p);
+	s->p++; /* skip the '(' */
+	aux = dep_add(s->graph, NULL);
+	count = 0;
+
+	/* process all tests in the group */
+	while((name = schedule_read_name(s)) != NULL)
+	{
+		test = dep_add(s->graph, name);
+		dep_link(prev, test);
+		dep_link(test, aux);
+		count++;
+	}
+
+	if (*s->p != ')')
+		bail("syntax error in schedule file \"%s\" %d: unclosed '('",
+			 s->schedule, s->line_num);
+
+	if (count == 0)
+		bail("empty test group in schedule file \"%s\" line %d",
+			 s->schedule, s->line_num);
+
+	s->p++; /* skip the ')' */
+
+	/* establish the dependency between this group and next fence */
+	if (next != NULL)
+		dep_link(aux, next);
+	return aux;
+}
+
 /* One group, or false at end of file. */
 static bool
 schedule_read_group(_sched *s)
 {
 	int start_line;
 	int			num_tests = 0;
+	bool		concurrent = false;
+	bool		sequential = false;
+	bool		batch;
 	_dep_node  *next;
+	_dep_node  *prev;
+	_dep_node  *item;
 
 	/* discard white spaces and comments */
 	for (;;)
@@ -1958,10 +2030,24 @@ schedule_read_group(_sched *s)
 			 s->schedule, s->line_num,
 			 (int) strcspn(s->p, "\r\n"), s->p);
 
-	
-	/* skip spaces after `test` */
+	/* skip spaces after `test`, then an optional kind */
 	s->p += 4;
 	schedule_skip_whitespace(s);
+	if (strncmp(s->p, "batch", 5) == 0)
+	{
+		s->p += 5;
+	}
+	else if (strncmp(s->p, "concurrently", 12) == 0)
+	{
+		s->p += 12;
+		concurrent = true;
+	}
+	else if (strncmp(s->p, "sequentially", 12) == 0)
+	{
+		s->p += 12;
+		sequential = true;
+	}
+	schedule_skip_whitespace(s);
 
 	/* check for the colon separator */
 	if (*(s->p++) != ':')
@@ -1969,23 +2055,42 @@ schedule_read_group(_sched *s)
 			 s->schedule, s->line_num);
 	start_line = s->line_num;
 
-	next = dep_add(s->graph, NULL);
-	for (;;)
+	/*
+	 * A batch shifts onto the pending barrier and then gets a new one.
+	 * Sequential and concurrent groups share the pending barrier, creating
+	 * it only when there is none.
+	 */
+	batch = !concurrent && !sequential;
+	if (batch && s->open != NULL)
 	{
-		char	   *name = schedule_read_name(s);
-		_dep_node  *test;
+		s->fence = s->open;
+		s->open = NULL;
+	}
+	if (s->open == NULL)
+		s->open = dep_add(s->graph, NULL);
+	next = s->open;
 
-		if (name == NULL)
+	/* Sequential walks prev. Linking each item to the barrier would skip the rest. */
+	prev = s->fence;
+	for (;;)
+	{
+		item = schedule_read_item(s, prev, sequential ? NULL : next);
+		if (item == NULL)
 			break;
 		num_tests++;
-		test = dep_add(s->graph, name);
-		dep_link(s->fence, test);
-		dep_link(test, next);
+		if (sequential)
+			prev = item;
 	}
 	if (num_tests == 0)
 		bail("empty test group in schedule file \"%s\" line %d",
 			 s->schedule, start_line);
-	s->fence = next;
+	if (sequential)
+		dep_link(prev, next);
+	if (batch)
+	{
+		s->fence = next;
+		s->open = NULL;
+	}
 	return true;
 }
 
@@ -2016,7 +2121,14 @@ log_group_concurrency(instr_time *test_start, instr_time *test_stop,
 		note("effective concurrency %.2f / %d", total / (group_stop - group_start), num_tests);
 }
 
-/* Wait until one running slot exits. Other tests keep running. */
+/*
+ * reap_one - return the index of the slot that exited
+ *
+ * Wait until one running slot exits. Assume that every background
+ * process is a test. The diff processes that compare results on failures
+ * don't affect this because they are started sycnhronously so none of
+ * those will be running concurrently with this function.
+ */
 static int
 reap_one(PID_TYPE *pids, int *statuses, instr_time *stoptimes, int nslots)
 {
-- 
2.53.0

