From 74e9d6d772eb7ab6b7798c02f04209488df7666b Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@vondra.me>
Date: Wed, 25 Nov 2026 13:47:05 +0100
Subject: [PATCH v7 14/30] GEQO: Add an 80-dimension starjoin planning
 benchmark

Add starjoin_geqo_large, planning a star with 80 dimensions with GEQO
enabled, with enable_starjoin_ordering on and off, and with GEQO
disabled as a control, for geqo_threshold set to 2 and to its default
value of 12.  At 12, the fact and the dimensions count as 8 rels with
the GUC enabled, so the standard join search is used instead of GEQO.

The search selection is checked outside the timed runs, and the plan
costs are compared in stable regression output.  After warming up all
three modes at each threshold, the two modes with GEQO enabled alternate
for seven samples, with a fixed seed.  The timings are exposed through
the starjoin_geqo_timings psql variable.  Like the other _large suites,
the benchmark is commented out in parallel_schedule, and it rolls back
its objects and settings.

Update STARJOINS.md.
---
 STARJOINS.md                                  |   7 +
 .../regress/expected/starjoin_geqo_large.out  | 139 ++++++++++++++++++
 src/test/regress/parallel_schedule            |   1 +
 src/test/regress/sql/starjoin_geqo_large.sql  | 135 +++++++++++++++++
 4 files changed, 282 insertions(+)
 create mode 100644 src/test/regress/expected/starjoin_geqo_large.out
 create mode 100644 src/test/regress/sql/starjoin_geqo_large.sql

diff --git a/STARJOINS.md b/STARJOINS.md
index 84f68139c0e..2fc8a536349 100644
--- a/STARJOINS.md
+++ b/STARJOINS.md
@@ -459,3 +459,10 @@ the plans, the planning cost, or the regression tests.
 - starjoin_geqo checks which star queries are planned by GEQO, and how
   the GUC affects their plans.  It detects GEQO by setting
   `geqo_pool_size` so large that GEQO fails to allocate the population.
+- starjoin_geqo_large plans a star with 80 dimensions with GEQO enabled,
+  with the GUC enabled and disabled, and with GEQO disabled as a
+  control, for `geqo_threshold` set to 2 and 12.  At 12, the fact and
+  the dimensions count as 8 rels with the GUC enabled, so the standard
+  join search is used.  It compares the plan costs, and reports the
+  planning times through the starjoin_geqo_timings psql variable.  It's
+  commented out in parallel_schedule, like the other _large suites.
diff --git a/src/test/regress/expected/starjoin_geqo_large.out b/src/test/regress/expected/starjoin_geqo_large.out
new file mode 100644
index 00000000000..ee07f2c9ec5
--- /dev/null
+++ b/src/test/regress/expected/starjoin_geqo_large.out
@@ -0,0 +1,139 @@
+--
+-- STARJOIN_GEQO_LARGE
+-- Benchmark an 80-dimension star with forced GEQO and with the default
+-- geqo_threshold.  Mode names describe GUC settings, not the chosen search:
+-- at threshold 2 both geqo_on modes use GEQO, without canonical pruning;
+-- at threshold 12 the star counts as 8 rels when starjoin ordering is on,
+-- and uses standard search instead.  Disabling GEQO provides a control.
+-- Each dimension has the same size, so the resulting plan costs are equal.
+--
+-- For timing measurements, run this file in a scratch database with
+-- psql -X -v ON_ERROR_STOP=1 -v starjoin_geqo_timings=1 -f <this file>.
+-- Timings are optional because they are not stable regression-test output.
+-- All objects and settings are rolled back.
+--
+BEGIN;
+DO $$
+DECLARE
+  cols text := 'id int';
+  exprs text := 'i';
+  d int;
+BEGIN
+  FOR d IN 1..80 LOOP
+    EXECUTE format('CREATE TABLE starjoin_geqo_d%s (id int PRIMARY KEY, v int)', d);
+    EXECUTE format('INSERT INTO starjoin_geqo_d%s SELECT i, i FROM generate_series(1, 10) i', d);
+    EXECUTE format('ANALYZE starjoin_geqo_d%s', d);
+    cols := cols || format(', d%s int NOT NULL REFERENCES starjoin_geqo_d%s', d, d);
+    exprs := exprs || ', 1 + i % 10';
+  END LOOP;
+  EXECUTE 'CREATE TABLE starjoin_geqo_f (' || cols || ')';
+  EXECUTE 'INSERT INTO starjoin_geqo_f SELECT ' || exprs ||
+          ' FROM generate_series(1, 100) i';
+END;
+$$;
+ANALYZE starjoin_geqo_f;
+SET LOCAL join_collapse_limit = 100;
+SET LOCAL from_collapse_limit = 100;
+SET LOCAL geqo = on;
+SET LOCAL geqo_seed = 0.42;
+SET LOCAL geqo_effort = 5;
+SET LOCAL geqo_pool_size = 0;
+SET LOCAL geqo_generations = 0;
+SET LOCAL enable_eager_aggregate = off;
+SET LOCAL max_parallel_workers_per_gather = 0;
+SET LOCAL jit = off;
+SELECT 'SELECT f.id FROM starjoin_geqo_f f ' ||
+       string_agg(format('JOIN starjoin_geqo_d%s d%s ON f.d%s = d%s.id',
+                         i, i, i, i), ' ' ORDER BY i) AS query
+  FROM generate_series(1, 80) i \gset
+-- Identify the chosen search outside the timing measurements.  GEQO cannot
+-- allocate this population, while standard search ignores geqo_pool_size.
+CREATE FUNCTION starjoin_geqo_used(query text, starjoin bool) RETURNS bool
+LANGUAGE plpgsql SET geqo_pool_size = 100000000 AS $$
+BEGIN
+  PERFORM set_config('enable_starjoin_ordering', starjoin::text, true);
+  EXECUTE 'EXPLAIN ' || query;
+  RETURN false;
+EXCEPTION WHEN internal_error THEN
+  IF SQLERRM NOT LIKE 'invalid memory alloc request size %' THEN
+    RAISE;
+  END IF;
+  RETURN true;
+END;
+$$;
+SET LOCAL geqo_threshold = 2;
+SELECT starjoin_geqo_used(:'query', true)
+       AND starjoin_geqo_used(:'query', false) AS forced_geqo;
+ forced_geqo 
+-------------
+ t
+(1 row)
+
+-- At the default threshold, the 81 rels count as 8 with starjoin ordering
+-- enabled, so only the disabled case uses GEQO.
+SET LOCAL geqo_threshold = 12;
+SELECT NOT starjoin_geqo_used(:'query', true)
+       AND starjoin_geqo_used(:'query', false) AS default_search_ok;
+ default_search_ok 
+-------------------
+ t
+(1 row)
+
+CREATE FUNCTION starjoin_geqo_measure(query text)
+RETURNS TABLE(geqo_threshold int, mode text, sample int,
+              total_cost float8, planning_ms float8)
+LANGUAGE plpgsql AS $$
+DECLARE
+  iteration int;
+  plan json;
+BEGIN
+  FOREACH geqo_threshold IN ARRAY ARRAY[2, 12] LOOP
+    PERFORM set_config('geqo_threshold', geqo_threshold::text, true);
+    -- Warm all three modes at each threshold, then alternate the geqo_on
+    -- variants across seven measurements.
+    FOR iteration IN 0..7 LOOP
+      FOREACH mode IN ARRAY
+        CASE WHEN iteration % 2 = 0
+             THEN ARRAY['geqo_on_starjoin_on', 'geqo_on_starjoin_off', 'geqo_off_starjoin_on']
+             ELSE ARRAY['geqo_on_starjoin_off', 'geqo_on_starjoin_on', 'geqo_off_starjoin_on']
+        END
+      LOOP
+        PERFORM set_config('enable_starjoin_ordering',
+                           (mode <> 'geqo_on_starjoin_off')::text, true);
+        PERFORM set_config('geqo', (mode <> 'geqo_off_starjoin_on')::text, true);
+        EXECUTE 'EXPLAIN (SUMMARY ON, FORMAT JSON) ' || query INTO plan;
+        IF iteration > 0 THEN
+          sample := iteration;
+          total_cost := (plan->0->'Plan'->>'Total Cost')::float8;
+          planning_ms := (plan->0->>'Planning Time')::float8;
+          RETURN NEXT;
+        END IF;
+      END LOOP;
+    END LOOP;
+  END LOOP;
+END;
+$$;
+CREATE TEMP TABLE starjoin_geqo_times AS
+  SELECT * FROM starjoin_geqo_measure(:'query');
+SELECT geqo_threshold, min(total_cost) = max(total_cost) AS same_plan_cost
+  FROM starjoin_geqo_times
+ GROUP BY geqo_threshold
+ ORDER BY geqo_threshold;
+ geqo_threshold | same_plan_cost 
+----------------+----------------
+              2 | t
+             12 | t
+(2 rows)
+
+\if :{?starjoin_geqo_timings}
+SELECT geqo_threshold, mode,
+       round((percentile_cont(0.5) WITHIN GROUP (ORDER BY planning_ms))::numeric, 3)
+         AS median_planning_ms,
+       round(min(planning_ms)::numeric, 3) AS min_planning_ms,
+       round(max(planning_ms)::numeric, 3) AS max_planning_ms,
+       min(total_cost) AS total_cost
+  FROM starjoin_geqo_times
+ GROUP BY geqo_threshold, mode
+ ORDER BY geqo_threshold, mode;
+\endif
+ROLLBACK;
diff --git a/src/test/regress/parallel_schedule b/src/test/regress/parallel_schedule
index b27463614ff..660f1c75611 100644
--- a/src/test/regress/parallel_schedule
+++ b/src/test/regress/parallel_schedule
@@ -153,6 +153,7 @@ test: starjoin_planning starjoin_eager starjoin_plan starjoin_width starjoin_rep
 
 # more expensive tests for starjoin planning (larger variants of earlier tests)
 # test: starjoin_eager_large starjoin_plan_large starjoin_width_large
+# test: starjoin_geqo_large
 
 # run tablespace test at the end because it drops the tablespace created during
 # setup that other tests may use.
diff --git a/src/test/regress/sql/starjoin_geqo_large.sql b/src/test/regress/sql/starjoin_geqo_large.sql
new file mode 100644
index 00000000000..b79366554d5
--- /dev/null
+++ b/src/test/regress/sql/starjoin_geqo_large.sql
@@ -0,0 +1,135 @@
+--
+-- STARJOIN_GEQO_LARGE
+-- Benchmark an 80-dimension star with forced GEQO and with the default
+-- geqo_threshold.  Mode names describe GUC settings, not the chosen search:
+-- at threshold 2 both geqo_on modes use GEQO, without canonical pruning;
+-- at threshold 12 the star counts as 8 rels when starjoin ordering is on,
+-- and uses standard search instead.  Disabling GEQO provides a control.
+-- Each dimension has the same size, so the resulting plan costs are equal.
+--
+-- For timing measurements, run this file in a scratch database with
+-- psql -X -v ON_ERROR_STOP=1 -v starjoin_geqo_timings=1 -f <this file>.
+-- Timings are optional because they are not stable regression-test output.
+-- All objects and settings are rolled back.
+--
+
+BEGIN;
+
+DO $$
+DECLARE
+  cols text := 'id int';
+  exprs text := 'i';
+  d int;
+BEGIN
+  FOR d IN 1..80 LOOP
+    EXECUTE format('CREATE TABLE starjoin_geqo_d%s (id int PRIMARY KEY, v int)', d);
+    EXECUTE format('INSERT INTO starjoin_geqo_d%s SELECT i, i FROM generate_series(1, 10) i', d);
+    EXECUTE format('ANALYZE starjoin_geqo_d%s', d);
+    cols := cols || format(', d%s int NOT NULL REFERENCES starjoin_geqo_d%s', d, d);
+    exprs := exprs || ', 1 + i % 10';
+  END LOOP;
+  EXECUTE 'CREATE TABLE starjoin_geqo_f (' || cols || ')';
+  EXECUTE 'INSERT INTO starjoin_geqo_f SELECT ' || exprs ||
+          ' FROM generate_series(1, 100) i';
+END;
+$$;
+ANALYZE starjoin_geqo_f;
+
+SET LOCAL join_collapse_limit = 100;
+SET LOCAL from_collapse_limit = 100;
+SET LOCAL geqo = on;
+SET LOCAL geqo_seed = 0.42;
+SET LOCAL geqo_effort = 5;
+SET LOCAL geqo_pool_size = 0;
+SET LOCAL geqo_generations = 0;
+SET LOCAL enable_eager_aggregate = off;
+SET LOCAL max_parallel_workers_per_gather = 0;
+SET LOCAL jit = off;
+
+SELECT 'SELECT f.id FROM starjoin_geqo_f f ' ||
+       string_agg(format('JOIN starjoin_geqo_d%s d%s ON f.d%s = d%s.id',
+                         i, i, i, i), ' ' ORDER BY i) AS query
+  FROM generate_series(1, 80) i \gset
+
+-- Identify the chosen search outside the timing measurements.  GEQO cannot
+-- allocate this population, while standard search ignores geqo_pool_size.
+CREATE FUNCTION starjoin_geqo_used(query text, starjoin bool) RETURNS bool
+LANGUAGE plpgsql SET geqo_pool_size = 100000000 AS $$
+BEGIN
+  PERFORM set_config('enable_starjoin_ordering', starjoin::text, true);
+  EXECUTE 'EXPLAIN ' || query;
+  RETURN false;
+EXCEPTION WHEN internal_error THEN
+  IF SQLERRM NOT LIKE 'invalid memory alloc request size %' THEN
+    RAISE;
+  END IF;
+  RETURN true;
+END;
+$$;
+
+SET LOCAL geqo_threshold = 2;
+SELECT starjoin_geqo_used(:'query', true)
+       AND starjoin_geqo_used(:'query', false) AS forced_geqo;
+
+-- At the default threshold, the 81 rels count as 8 with starjoin ordering
+-- enabled, so only the disabled case uses GEQO.
+SET LOCAL geqo_threshold = 12;
+SELECT NOT starjoin_geqo_used(:'query', true)
+       AND starjoin_geqo_used(:'query', false) AS default_search_ok;
+
+CREATE FUNCTION starjoin_geqo_measure(query text)
+RETURNS TABLE(geqo_threshold int, mode text, sample int,
+              total_cost float8, planning_ms float8)
+LANGUAGE plpgsql AS $$
+DECLARE
+  iteration int;
+  plan json;
+BEGIN
+  FOREACH geqo_threshold IN ARRAY ARRAY[2, 12] LOOP
+    PERFORM set_config('geqo_threshold', geqo_threshold::text, true);
+    -- Warm all three modes at each threshold, then alternate the geqo_on
+    -- variants across seven measurements.
+    FOR iteration IN 0..7 LOOP
+      FOREACH mode IN ARRAY
+        CASE WHEN iteration % 2 = 0
+             THEN ARRAY['geqo_on_starjoin_on', 'geqo_on_starjoin_off', 'geqo_off_starjoin_on']
+             ELSE ARRAY['geqo_on_starjoin_off', 'geqo_on_starjoin_on', 'geqo_off_starjoin_on']
+        END
+      LOOP
+        PERFORM set_config('enable_starjoin_ordering',
+                           (mode <> 'geqo_on_starjoin_off')::text, true);
+        PERFORM set_config('geqo', (mode <> 'geqo_off_starjoin_on')::text, true);
+        EXECUTE 'EXPLAIN (SUMMARY ON, FORMAT JSON) ' || query INTO plan;
+        IF iteration > 0 THEN
+          sample := iteration;
+          total_cost := (plan->0->'Plan'->>'Total Cost')::float8;
+          planning_ms := (plan->0->>'Planning Time')::float8;
+          RETURN NEXT;
+        END IF;
+      END LOOP;
+    END LOOP;
+  END LOOP;
+END;
+$$;
+
+CREATE TEMP TABLE starjoin_geqo_times AS
+  SELECT * FROM starjoin_geqo_measure(:'query');
+
+SELECT geqo_threshold, min(total_cost) = max(total_cost) AS same_plan_cost
+  FROM starjoin_geqo_times
+ GROUP BY geqo_threshold
+ ORDER BY geqo_threshold;
+
+\if :{?starjoin_geqo_timings}
+SELECT geqo_threshold, mode,
+       round((percentile_cont(0.5) WITHIN GROUP (ORDER BY planning_ms))::numeric, 3)
+         AS median_planning_ms,
+       round(min(planning_ms)::numeric, 3) AS min_planning_ms,
+       round(max(planning_ms)::numeric, 3) AS max_planning_ms,
+       min(total_cost) AS total_cost
+  FROM starjoin_geqo_times
+ GROUP BY geqo_threshold, mode
+ ORDER BY geqo_threshold, mode;
+\endif
+
+ROLLBACK;
-- 
2.56.1

