From 8a906deb0a402eb6245dd523239a314ad64033f7 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 02/30] Show the width-dependent cost issue in
 starjoin_width

Lower work_mem to 128kB, so that the hash join to wc needs multiple
batches.  With 4MB the plans with enable_starjoin_ordering on and off
were the same, and the issue was visible only in starjoin_width_large.

Update the descriptions of the issue and of the test in STARJOINS.md.
---
 STARJOINS.md                                 |  9 +++---
 src/test/regress/expected/starjoin_width.out | 34 +++++++++++---------
 src/test/regress/sql/starjoin_width.sql      | 10 +++---
 3 files changed, 29 insertions(+), 24 deletions(-)

diff --git a/STARJOINS.md b/STARJOINS.md
index a2cb2ee67e7..9af5dd47069 100644
--- a/STARJOINS.md
+++ b/STARJOINS.md
@@ -225,8 +225,8 @@ the plans, the planning cost, or the regression tests.
   dimension needs multiple batches, and the cost of batching depends on
   the width of the outer rows, so it's much cheaper to join the large
   dimension before the wide ones.  If the canonical order joins it last,
-  the planner misses the cheaper plan.  This is shown by
-  starjoin_width_large, and by the width_cost_higher check in
+  the planner misses the cheaper plan.  This is shown by starjoin_width
+  and starjoin_width_large, and by the width_cost_higher check in
   starjoin_other.  There's no fix, as this comes with considering a
   single order.
 - With GROUP BY on a column of a dimension, eager aggregation can place
@@ -277,8 +277,9 @@ the plans, the planning cost, or the regression tests.
   EXPLAIN ANALYZE output, to ignore platform-dependent memory usage and
   hash table sizing, while retaining the actual row counts and the
   distinction between single-batch and multi-batch hash joins.  With
-  `work_mem` set to 4MB in starjoin_width, its plans are the same with
-  the GUC enabled and disabled.
+  `work_mem` set to 128kB in starjoin_width, the hash join to its
+  largest dimension needs multiple batches, and the plan with the GUC
+  enabled is more expensive.
 - The _large variants of these three suites use larger tables, and
   starjoin_eager_large and starjoin_width_large set `work_mem` to 400kB,
   so that their hash joins need multiple batches.  They're too expensive
diff --git a/src/test/regress/expected/starjoin_width.out b/src/test/regress/expected/starjoin_width.out
index db9949add4f..a146d95618d 100644
--- a/src/test/regress/expected/starjoin_width.out
+++ b/src/test/regress/expected/starjoin_width.out
@@ -30,13 +30,15 @@ CREATE TABLE starjoin_width_wf (id int, a int NOT NULL REFERENCES starjoin_width
 INSERT INTO starjoin_width_wf SELECT i, 1 + i % 100, 1 + (i * 7) % 100, 1 + (i * 13) % 10000 FROM generate_series(1,10000) i;
 VACUUM ANALYZE starjoin_width_wa, starjoin_width_wb, starjoin_width_wc, starjoin_width_wf;
 SET max_parallel_workers_per_gather = 0;
-SET work_mem = '4MB';
+-- Use a small work_mem, so that the hash join to wc (the largest dimension)
+-- needs multiple batches. With the default (4MB) the hash table fits in
+-- memory, and the plans with the GUC on and off would be the same.
+SET work_mem = '128kB';
 SET enable_nestloop = off; SET enable_mergejoin = off;
 -- All the joins preserve the cardinality, but the cost of a batched hash join
 -- depends on the width of the outer rows. So when the hash join to wc needs
--- multiple batches (as in starjoin_width_large), the plan with the GUC off
--- joins wc before the wide dimensions (wa, wb), while the canonical order
--- joins it last. Otherwise the plans are the same.
+-- multiple batches, the plan with the GUC off joins wc before the wide
+-- dimensions (wa, wb), while the canonical order joins it last.
 -- pure FK joins, GUC on
 SELECT pg_temp.starjoin_explain($$
 SELECT wa.t, wb.t, wc.v FROM starjoin_width_wf AS wf JOIN starjoin_width_wa AS wa ON wf.a = wa.id JOIN starjoin_width_wb AS wb ON wf.b = wb.id JOIN starjoin_width_wc AS wc ON wf.c = wc.id
@@ -57,7 +59,7 @@ $$) AS "QUERY PLAN";
                Buckets: N  Batches: 1  Memory Usage: NkB
                ->  Seq Scan on starjoin_width_wb wb (actual rows=100.00 loops=1)
    ->  Hash (actual rows=10000.00 loops=1)
-         Buckets: N  Batches: 1  Memory Usage: NkB
+         Buckets: N  Batches: multiple  Memory Usage: NkB
          ->  Seq Scan on starjoin_width_wc wc (actual rows=10000.00 loops=1)
 (16 rows)
 
@@ -66,24 +68,24 @@ SET enable_starjoin_ordering = off;
 SELECT pg_temp.starjoin_explain($$
 SELECT wa.t, wb.t, wc.v FROM starjoin_width_wf AS wf JOIN starjoin_width_wa AS wa ON wf.a = wa.id JOIN starjoin_width_wb AS wb ON wf.b = wb.id JOIN starjoin_width_wc AS wc ON wf.c = wc.id
 $$) AS "QUERY PLAN";
-                                      QUERY PLAN                                       
----------------------------------------------------------------------------------------
+                                       QUERY PLAN                                        
+-----------------------------------------------------------------------------------------
  Hash Join (actual rows=10000.00 loops=1)
-   Hash Cond: (wf.c = wc.id)
+   Hash Cond: (wf.b = wb.id)
    ->  Hash Join (actual rows=10000.00 loops=1)
-         Hash Cond: (wf.b = wb.id)
+         Hash Cond: (wf.a = wa.id)
          ->  Hash Join (actual rows=10000.00 loops=1)
-               Hash Cond: (wf.a = wa.id)
+               Hash Cond: (wf.c = wc.id)
                ->  Seq Scan on starjoin_width_wf wf (actual rows=10000.00 loops=1)
-               ->  Hash (actual rows=100.00 loops=1)
-                     Buckets: N  Batches: 1  Memory Usage: NkB
-                     ->  Seq Scan on starjoin_width_wa wa (actual rows=100.00 loops=1)
+               ->  Hash (actual rows=10000.00 loops=1)
+                     Buckets: N  Batches: multiple  Memory Usage: NkB
+                     ->  Seq Scan on starjoin_width_wc wc (actual rows=10000.00 loops=1)
          ->  Hash (actual rows=100.00 loops=1)
                Buckets: N  Batches: 1  Memory Usage: NkB
-               ->  Seq Scan on starjoin_width_wb wb (actual rows=100.00 loops=1)
-   ->  Hash (actual rows=10000.00 loops=1)
+               ->  Seq Scan on starjoin_width_wa wa (actual rows=100.00 loops=1)
+   ->  Hash (actual rows=100.00 loops=1)
          Buckets: N  Batches: 1  Memory Usage: NkB
-         ->  Seq Scan on starjoin_width_wc wc (actual rows=10000.00 loops=1)
+         ->  Seq Scan on starjoin_width_wb wb (actual rows=100.00 loops=1)
 (16 rows)
 
 DROP TABLE starjoin_width_wf, starjoin_width_wa, starjoin_width_wb, starjoin_width_wc;
diff --git a/src/test/regress/sql/starjoin_width.sql b/src/test/regress/sql/starjoin_width.sql
index 45a37a88869..a01f1c9d5ef 100644
--- a/src/test/regress/sql/starjoin_width.sql
+++ b/src/test/regress/sql/starjoin_width.sql
@@ -37,14 +37,16 @@ INSERT INTO starjoin_width_wf SELECT i, 1 + i % 100, 1 + (i * 7) % 100, 1 + (i *
 VACUUM ANALYZE starjoin_width_wa, starjoin_width_wb, starjoin_width_wc, starjoin_width_wf;
 
 SET max_parallel_workers_per_gather = 0;
-SET work_mem = '4MB';
+-- Use a small work_mem, so that the hash join to wc (the largest dimension)
+-- needs multiple batches. With the default (4MB) the hash table fits in
+-- memory, and the plans with the GUC on and off would be the same.
+SET work_mem = '128kB';
 SET enable_nestloop = off; SET enable_mergejoin = off;
 
 -- All the joins preserve the cardinality, but the cost of a batched hash join
 -- depends on the width of the outer rows. So when the hash join to wc needs
--- multiple batches (as in starjoin_width_large), the plan with the GUC off
--- joins wc before the wide dimensions (wa, wb), while the canonical order
--- joins it last. Otherwise the plans are the same.
+-- multiple batches, the plan with the GUC off joins wc before the wide
+-- dimensions (wa, wb), while the canonical order joins it last.
 
 -- pure FK joins, GUC on
 SELECT pg_temp.starjoin_explain($$
-- 
2.56.1

