From 6bf23ecc98deffb5227261dac004093c11bd4cea Mon Sep 17 00:00:00 2001
From: Alexandre Felipe <o.alexandre.felipe@gmail.com>
Date: Thu, 17 Sep 2026 08:43:52 +0100
Subject: [v3.0 PATCH 1/5] pg_regress: multi-line test groups

This support multi-line test groups
instead of requiring
test: all test in a single line
it also support
test:
  tests in
  different
  lines

this will make it more readable, and showing line numbers
having one test per line makes it much easier to see how many
tests are in a group.

SYNTAX
======

1. An unindented line is a syntax error

1.a unindented line before any `test:`
Example:
```
 9 # ----------
10 top
11 # required setup steps
```
Error:
line 10: expected "test:", indented test group continuation, got "top"
1.b unindented line after a `test:`
Example:
```
16 test:
16    boolean
18 char
```
Error:
line 18: expected "test:", indented test group continuation, got "char"

2. blank lines are OK
Example:
```
test:

    boolean
    char
```

3. mixing tests in the same line and next lines is OK

Example:
```
test: boolean
    char
```

4. empty tests are errors
4.a `test:` followed by empty lines or comments
Example:
```
12 test: test_setup
13 test:
14 # ----------
15 # The first group of parallel tests
16 # ----------
17 test: boolean
```
Error:
empty test group in schedule file "./parallel_schedule" line 13

Reporting the line where the empty test started not when it was
confirmed empty.

4.b empty `test:` at the end of the file
Example:
```
364 test: tablespace
365 test:
```
Error:
empty test group in schedule file "./parallel_schedule" line 365

4.c Empty `test:` immediately followed by `test:`
Example:
```
12 test:
13 test: test_setup
```
Error:
empty test group in schedule file "./parallel_schedule" line 12
---
 src/test/regress/parallel_schedule | 274 ++++++++++++++++++++++++++---
 src/test/regress/pg_regress.c      | 265 ++++++++++++++++++++--------
 2 files changed, 439 insertions(+), 100 deletions(-)

diff --git a/src/test/regress/parallel_schedule b/src/test/regress/parallel_schedule
index bb13a7b6b8f..a7667e1cf80 100644
--- a/src/test/regress/parallel_schedule
+++ b/src/test/regress/parallel_schedule
@@ -14,21 +14,80 @@ test: test_setup
 # ----------
 # The first group of parallel tests
 # ----------
-test: boolean char name varchar text int2 int4 int8 oid float4 float8 bit numeric txid uuid enum money rangetypes pg_lsn regproc
+test: \
+    boolean \
+    char \
+    name \
+    varchar \
+    text \
+    int2 \
+    int4 \
+    int8 \
+    oid \
+    float4 \
+    float8 \
+    bit \
+    numeric \
+    txid \
+    uuid \
+    enum \
+    money \
+    rangetypes \
+    pg_lsn \
+    regproc
 
 # ----------
 # The second group of parallel tests
 # multirangetypes depends on rangetypes
 # multirangetypes shouldn't run concurrently with type_sanity
 # ----------
-test: strings md5 numerology point lseg line box path polygon circle date time timetz timestamp timestamptz interval inet macaddr macaddr8 multirangetypes
+test: \
+    strings \
+    md5 \
+    numerology \
+    point \
+    lseg \
+    line \
+    box \
+    path \
+    polygon \
+    circle \
+    date \
+    time \
+    timetz \
+    timestamp \
+    timestamptz \
+    interval \
+    inet \
+    macaddr \
+    macaddr8 \
+    multirangetypes
 
 # ----------
 # 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 tstypes regex type_sanity opr_sanity misc_sanity comments expressions unicode xid mvcc database stats_import pg_ndistinct pg_dependencies oid8 encoding euc_kr
+test: \
+    geometry \
+    horology \
+    tstypes \
+    regex \
+    type_sanity \
+    opr_sanity \
+    misc_sanity \
+    comments \
+    expressions \
+    unicode \
+    xid \
+    mvcc \
+    database \
+    stats_import \
+    pg_ndistinct \
+    pg_dependencies \
+    oid8 \
+    encoding \
+    euc_kr
 
 # ----------
 # Load huge amounts of data
@@ -36,19 +95,54 @@ test: geometry horology tstypes regex type_sanity opr_sanity misc_sanity comment
 # execute two copy tests in parallel, to check that copy itself
 # is concurrent safe.
 # ----------
-test: copy copyselect copydml copyencoding insert insert_conflict
+test: \
+    copy \
+    copyselect \
+    copydml \
+    copyencoding \
+    insert \
+    insert_conflict
 
 # ----------
 # More groups of parallel tests
 # Note: many of the tests in later groups depend on create_index
 # ----------
-test: create_function_c create_misc create_operator create_procedure create_table create_type create_schema
-test: create_index create_index_spgist create_view index_including index_including_gist
+test: \
+    create_function_c \
+    create_misc \
+    create_operator \
+    create_procedure \
+    create_table \
+    create_type \
+    create_schema
+
+test: \
+    create_index \
+    create_index_spgist \
+    create_view \
+    index_including \
+    index_including_gist
 
 # ----------
 # Another group of parallel tests
 # ----------
-test: create_aggregate create_function_sql create_cast constraints triggers select inherit typed_table vacuum drop_if_exists updatable_views roleattributes create_am hash_func errors infinite_recurse
+test: \
+    create_aggregate \
+    create_function_sql \
+    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 *
@@ -61,27 +155,89 @@ test: sanity_check
 # aggregates depends on create_aggregate
 # join depends on create_misc
 # ----------
-test: select_into select_distinct select_distinct_on select_implicit select_having subselect union case join aggregates transactions random portals arrays btree_index hash_index update delete namespace prepared_xacts
+test: \
+    select_into \
+    select_distinct \
+    select_distinct_on \
+    select_implicit \
+    select_having \
+    subselect \
+    union \
+    case \
+    join \
+    aggregates \
+    transactions \
+    random \
+    portals \
+    arrays \
+    btree_index \
+    hash_index \
+    update \
+    delete \
+    namespace \
+    prepared_xacts
 
 # ----------
 # Another group of parallel tests
 # ----------
-test: brin 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
+test: \
+    brin \
+    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: brin_bloom brin_multi
+test: \
+    brin_bloom \
+    brin_multi
 
 # ----------
 # Another group of parallel tests
 # ----------
-test: create_table_like alter_generic alter_operator misc async dbsize merge misc_functions nls sysviews tsrf tid tidscan tidrangescan collate.utf8 collate.icu.utf8 incremental_sort create_role without_overlaps generated_virtual
+test: \
+    alter_generic alter_operator \
+    create_table_like \
+    create_role \
+    misc misc_functions \
+    async \
+    dbsize \
+    merge \
+    nls \
+    sysviews \
+    tsrf \
+    tid tidscan tidrangescan \
+    collate.utf8 collate.icu.utf8 \
+    incremental_sort \
+    without_overlaps \
+    generated_virtual
 
 # collate.linux.utf8 and collate.icu.utf8 tests cannot be run in parallel with each other
 # psql depends on create_am
 # amutils depends on geometry, create_index_spgist, hash_index, brin
-test: rules psql psql_crosstab psql_pipeline amutils stats_ext collate.linux.utf8 collate.windows.win1252
+test: \
+    amutils \
+    psql psql_crosstab psql_pipeline \
+    collate.linux.utf8 collate.windows.win1252 \
+    rules \
+    stats_ext
 
 # ----------
 # Run these alone so they don't run out of parallel workers
@@ -96,18 +252,48 @@ test: vacuum_parallel
 test: maintain_every
 
 # no relation related tests can be put in this group
-test: publication subscription
+test: \
+    publication \
+    subscription
 
 # ----------
 # Another group of parallel tests
 # select_views depends on create_view
 # ----------
-test: select_views portals_p2 portals_inflight foreign_key dependency guc bitmapops combocid tsearch tsdicts foreign_data window xmlmap functional_deps advisory_lock indirect_toast equivclass stats_rewrite alter_column_type
+test: \
+    select_views \
+    portals_p2 \
+    portals_inflight \
+    foreign_key \
+    dependency \
+    guc \
+    bitmapops \
+    combocid \
+    tsearch \
+    tsdicts \
+    foreign_data \
+    window \
+    xmlmap \
+    functional_deps \
+    advisory_lock \
+    indirect_toast \
+    equivclass \
+    stats_rewrite \
+    alter_column_type
 
 # ----------
 # Another group of parallel tests (JSON related)
 # ----------
-test: json jsonb json_encoding jsonpath jsonpath_encoding jsonb_jsonpath sqljson sqljson_queryfuncs sqljson_jsontable
+test: \
+    json \
+    jsonb \
+    json_encoding \
+    jsonpath \
+    jsonpath_encoding \
+    jsonb_jsonpath \
+    sqljson \
+    sqljson_queryfuncs \
+    sqljson_jsontable
 
 # ----------
 # Another group of parallel tests
@@ -115,7 +301,25 @@ test: json jsonb json_encoding jsonpath jsonpath_encoding jsonb_jsonpath sqljson
 # NB: temp.sql does reconnects which transiently uses 2 connections,
 # so keep this parallel group to at most 19 tests
 # ----------
-test: plancache limit plpgsql copy2 temp domain rangefuncs prepare conversion truncate alter_table sequence polymorphism rowtypes returning largeobject with xml
+test: \
+    plancache \
+    limit \
+    plpgsql \
+    copy2 \
+    temp \
+    domain \
+    rangefuncs \
+    prepare \
+    conversion \
+    truncate \
+    alter_table \
+    sequence \
+    polymorphism \
+    rowtypes \
+    returning \
+    largeobject \
+    with \
+    xml
 
 # rowtypes_alter relies on cached plans surviving across statements, which
 # concurrent DDL can prevent, and on parallel workers being available, so run
@@ -128,26 +332,50 @@ test: rowtypes_alter
 # The stats test resets stats, so nothing else needing stats access can be in
 # this group.
 # ----------
-test: partition_join partition_prune reloptions hash_part indexing partition_aggregate partition_info tuplesort explain memoize stats predicate numa eager_aggregate planner_est attribute_version
-
+test: \
+    partition_join \
+    partition_prune \
+    reloptions \
+    hash_part \
+    indexing \
+    partition_aggregate \
+    partition_info \
+    tuplesort \
+    explain \
+    memoize \
+    stats \
+    predicate \
+    numa \
+    eager_aggregate \
+    planner_est \
+    attribute_version
 # ----------
 # Another group of parallel tests (compression)
 # ----------
-test: compression compression_lz4 compression_pglz cluster
+test: \
+    compression \
+    compression_lz4 \
+    compression_pglz \
+    cluster
 
 # event_trigger depends on create_am and cannot run concurrently with
 # any test that runs DDL
 # oidjoins is read-only, though, and should run late for best coverage
-test: oidjoins event_trigger
+test: \
+    oidjoins \
+    event_trigger
 
 
 # event_trigger_login cannot run concurrently with any other tests because
 # on-login event handling could catch connection of a concurrent test.
-test: event_trigger_login
+test: \
+    event_trigger_login
 
 # this test also uses event triggers, so likewise run it by itself
-test: fast_default
+test: \
+    fast_default
 
 # run tablespace test at the end because it drops the tablespace created during
 # setup that other tests may use.
-test: tablespace
+test: \
+    tablespace
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index 13944701bc7..3a46d42eea8 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -81,6 +81,10 @@ const char *pretty_diff_opts = "--strip-trailing-cr -U3";
  */
 #define WAIT_TICKS_PER_SECOND 20
 
+/*
+ * Maximum supported number of concurrent tests
+ */
+#define MAX_PARALLEL_TESTS 100
 typedef enum TAPtype
 {
 	DIAG = 0,
@@ -105,7 +109,7 @@ char	   *bindir = PGBINDIR;
 char	   *launcher = NULL;
 static _stringlist *loadextension = NULL;
 static int	max_connections = 0;
-static int	max_concurrent_tests = 0;
+static int	max_concurrent_tests = MAX_PARALLEL_TESTS;
 static char *encoding = NULL;
 static _stringlist *schedulelist = NULL;
 static _stringlist *extra_tests = NULL;
@@ -1709,14 +1713,181 @@ log_child_failure(int exitstatus)
 		diag("(test process exited with unrecognized status %d)", exitstatus);
 }
 
+
+
+/*
+ * schedule = ( space | comment | group )+
+ * group    = "test:" name+
+ * name     = (space "\" space comment? newline)+ test-name
+ * comment = "#" .*
+ * space = [ \t]*
+ * newline = "\n" | "\r\n"
+ * test-name = [^ \t\n\r\\#]+
+ */
+typedef struct
+{
+	const char *schedule;
+	FILE	   *file;
+	int			line_num;
+	bool		line_has_name;
+	char		buf[1024];
+	char	   *p;
+} _sched;
+
+/*
+ * Read a line from the schedule file.
+ */
+static bool
+schedule_read_line(_sched *s)
+{
+	/* fgets overrides buf[N-1] only when the line does not fit */
+	const int N = sizeof(s->buf);
+	s->buf[0] = '\0';
+	s->buf[N - 2] = '\0';
+	s->p = s->buf;
+	if (fgets(s->buf, N, s->file) == NULL)
+		return false;
+	s->line_num++;
+	if (s->buf[N - 2] != '\0' && s->buf[N - 2] != '\n')
+		bail("line too long in schedule file \"%s\" line %d",
+			 s->schedule, s->line_num);
+	s->p = s->buf;
+	return true;
+}
+
+/*
+ * check whether the character at the current cursor marks the end of a line.
+ */
+static bool
+schedule_at_eol(const char *p)
+{
+	return *p == '\0' || *p == '\n' || *p == '\r' || *p == '#';
+}
+
+/*
+ * Skip leading whitespace characters.
+ */
+static void schedule_skip_whitespace(_sched *s)
+{
+	while (*s->p == ' ' || *s->p == '\t')
+		s->p++;
+}
+
+/* Read on test name, returns NULL if the next token is not a valid test name */
+static char *
+schedule_read_name(_sched *s)
+{
+	size_t		len;
+	char	   *name;
+
+	/* Discard witespaces comments and read more lines if necessary */
+	for(;;){
+		schedule_skip_whitespace(s);
+
+		/* test continues on next line */
+		if (*s->p == '\\')
+		{
+			s->p++;
+			/* accept spaces and comments after the backslash */
+			schedule_skip_whitespace(s);
+			if (!schedule_at_eol(s->p))
+				bail("syntax error in schedule file \"%s\" line %d: "
+					"test name after \\",
+					s->schedule, s->line_num);
+			/* read the next line */
+			if (!schedule_read_line(s))
+				return NULL;
+			continue;
+		}
+		/* end of line, also end of test list */
+		if (schedule_at_eol(s->p))
+		{
+			return NULL;
+		}
+		/* possibly a test */
+		break;
+	}
+
+	/* scan one single test name */
+	for (len = 0; len < sizeof(s->buf) ; ++len)
+	{
+		if (s->p[len] < '-' || s->p[len] > 'z')
+			break;
+	}
+
+	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);
+    
+	name = pg_malloc(len + 1);
+	strncpy(name, s->p, len);
+	s->p += len;
+	return name;
+}
+
+/* One group, or false at end of file. */
+static bool
+schedule_read_group(_sched *s, char **tests, int *num_tests)
+{
+	int start_line;
+	*num_tests = 0;
+	/* discard white spaces and comments */
+	for (;;)
+	{
+		schedule_skip_whitespace(s);
+		if (schedule_at_eol(s->p))
+		{
+			if (!schedule_read_line(s))
+				return false;
+			continue;
+		}
+		break;
+	}
+	/* check the magic word test */
+	if (strncmp(s->p, "test", 4) != 0)
+		bail("syntax error in schedule file \"%s\" line %d: "
+			 "expected \"test:\", indented test group continuation, got \"%.*s\"",
+			 s->schedule, s->line_num,
+			 (int) strcspn(s->p, "\r\n"), s->p);
+
+	
+	/* skip spaces after `test` */
+	s->p += 4;
+	schedule_skip_whitespace(s);
+
+	/* check for the colon separator */
+	if (*(s->p++) != ':')
+		bail("syntax error in schedule file \"%s\" line %d: expected \":\" after \"test\"",
+			 s->schedule, s->line_num);
+	start_line = s->line_num;
+
+	/* read test names */
+	*num_tests = 0;
+	for (;;)
+	{
+		char	   *name = schedule_read_name(s);
+		if (name == NULL)
+			break;
+		if (*num_tests >= max_concurrent_tests)
+			bail("too many tests (%d) in schedule file \"%s\" line %d, test %s",
+				*num_tests, s->schedule, s->line_num, name);
+		tests[(*num_tests)++] = name;
+	}
+	if (*num_tests == 0)
+		bail("empty test group in schedule file \"%s\" line %d",
+			 s->schedule, start_line);
+	return true;
+}
+
 /*
- * Run all the tests specified in one schedule file
+ * Run all the tests specified in one schedule file.
  */
 static void
 run_schedule(const char *schedule, test_start_function startfunc,
 			 postprocess_result_function postfunc)
 {
-#define MAX_PARALLEL_TESTS 100
 	char	   *tests[MAX_PARALLEL_TESTS];
 	_stringlist *resultfiles[MAX_PARALLEL_TESTS];
 	_stringlist *expectfiles[MAX_PARALLEL_TESTS];
@@ -1725,84 +1896,25 @@ run_schedule(const char *schedule, test_start_function startfunc,
 	instr_time	starttimes[MAX_PARALLEL_TESTS];
 	instr_time	stoptimes[MAX_PARALLEL_TESTS];
 	int			statuses[MAX_PARALLEL_TESTS];
-	char		scbuf[1024];
-	FILE	   *scf;
-	int			line_num = 0;
+	_sched		rd;
+	int			num_tests;
 
 	memset(tests, 0, sizeof(tests));
 	memset(resultfiles, 0, sizeof(resultfiles));
 	memset(expectfiles, 0, sizeof(expectfiles));
 	memset(tags, 0, sizeof(tags));
 
-	scf = fopen(schedule, "r");
-	if (!scf)
+	memset(&rd, 0, sizeof(rd));
+	rd.schedule = schedule;
+	rd.p = rd.buf;
+	rd.file = fopen(schedule, "r");
+	if (!rd.file)
 		bail("could not open file \"%s\" for reading: %m", schedule);
 
-	while (fgets(scbuf, sizeof(scbuf), scf))
+	while (schedule_read_group(&rd, tests, &num_tests))
 	{
-		char	   *test = NULL;
-		char	   *c;
-		int			num_tests;
-		bool		inword;
 		int			i;
 
-		line_num++;
-
-		/* strip trailing whitespace, especially the newline */
-		i = strlen(scbuf);
-		while (i > 0 && isspace((unsigned char) scbuf[i - 1]))
-			scbuf[--i] = '\0';
-
-		if (scbuf[0] == '\0' || scbuf[0] == '#')
-			continue;
-		if (strncmp(scbuf, "test: ", 6) == 0)
-			test = scbuf + 6;
-		else
-		{
-			bail("syntax error in schedule file \"%s\" line %d: %s",
-				 schedule, line_num, scbuf);
-		}
-
-		num_tests = 0;
-		inword = false;
-		for (c = test;; c++)
-		{
-			if (*c == '\0' || isspace((unsigned char) *c))
-			{
-				if (inword)
-				{
-					/* Reached end of a test name */
-					char		sav;
-
-					if (num_tests >= MAX_PARALLEL_TESTS)
-					{
-						bail("too many parallel tests (more than %d) in schedule file \"%s\" line %d: %s",
-							 MAX_PARALLEL_TESTS, schedule, line_num, scbuf);
-					}
-					sav = *c;
-					*c = '\0';
-					tests[num_tests] = pg_strdup(test);
-					num_tests++;
-					*c = sav;
-					inword = false;
-				}
-				if (*c == '\0')
-					break;		/* loop exit is here */
-			}
-			else if (!inword)
-			{
-				/* Start of a test name */
-				test = c;
-				inword = true;
-			}
-		}
-
-		if (num_tests == 0)
-		{
-			bail("syntax error in schedule file \"%s\" line %d: %s",
-				 schedule, line_num, scbuf);
-		}
-
 		if (num_tests == 1)
 		{
 			pids[0] = (startfunc) (tests[0], &resultfiles[0], &expectfiles[0], &tags[0]);
@@ -1810,11 +1922,6 @@ run_schedule(const char *schedule, test_start_function startfunc,
 			wait_for_tests(pids, statuses, stoptimes, NULL, 1);
 			/* status line is finished below */
 		}
-		else if (max_concurrent_tests > 0 && max_concurrent_tests < num_tests)
-		{
-			bail("too many parallel tests (more than %d) in schedule file \"%s\" line %d: %s",
-				 max_concurrent_tests, schedule, line_num, scbuf);
-		}
 		else if (max_connections > 0 && max_connections < num_tests)
 		{
 			int			oldest = 0;
@@ -1912,7 +2019,7 @@ run_schedule(const char *schedule, test_start_function startfunc,
 		}
 	}
 
-	fclose(scf);
+	fclose(rd.file);
 }
 
 /*
@@ -2113,7 +2220,7 @@ help(void)
 	printf(_("      --max-connections=N       maximum number of concurrent connections\n"));
 	printf(_("                                (default is 0, meaning unlimited)\n"));
 	printf(_("      --max-concurrent-tests=N  maximum number of concurrent tests in schedule\n"));
-	printf(_("                                (default is 0, meaning unlimited)\n"));
+	printf(_("                                default: %d\n"), MAX_PARALLEL_TESTS);
 	printf(_("      --outputdir=DIR           place output files in DIR (default \".\")\n"));
 	printf(_("      --schedule=FILE           use test ordering schedule from FILE\n"));
 	printf(_("                                (can be used multiple times to concatenate)\n"));
@@ -2295,6 +2402,10 @@ regression_main(int argc, char *argv[],
 				break;
 			case 25:
 				max_concurrent_tests = atoi(optarg);
+				if (max_concurrent_tests <= 0 || max_concurrent_tests > MAX_PARALLEL_TESTS)
+					bail("--max-concurrent-tests=%s expected to be integer in range 1-%d" , optarg, MAX_PARALLEL_TESTS);
+				if (max_concurrent_tests > MAX_PARALLEL_TESTS)
+					bail("value for --max-concurrent-tests must be no more than %d", MAX_PARALLEL_TESTS);
 				break;
 			case 26:
 				expecteddir = pg_strdup(optarg);
-- 
2.53.0

