From 63171b354f90db33b726dd1b6789807623da7488 Mon Sep 17 00:00:00 2001
From: Zhong ShiHao <zhong950419@gmail.com>
Date: Sun, 30 Aug 2026 21:58:16 -0400
Subject: [PATCH v1 2/2] Lower generate_subscripts()'s prorows estimate to
 match unnest()

Now that generate_subscripts() has a support function, prorows is
consulted only when that function declines: when the dimension number
isn't known at plan time, or when a dimension above the first is
requested for a non-constant array.  The old value of 1000 has no
particular justification and is ten times what unnest() assumes for
an array of unknown size.  Use 100, so the two functions estimate
alike when nothing better is available.
---
 src/include/catalog/pg_proc.dat           |  4 ++--
 src/test/regress/expected/planner_est.out | 20 ++++++++++----------
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index e35379afd33..05dfc9f5c3e 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -1711,12 +1711,12 @@
   proargtypes => 'anycompatiblearray anycompatible',
   prosrc => 'array_positions' },
 { oid => '1191', descr => 'array subscripts generator',
-  proname => 'generate_subscripts', prorows => '1000',
+  proname => 'generate_subscripts', prorows => '100',
   prosupport => 'generate_subscripts_support', proretset => 't',
   prorettype => 'int4', proargtypes => 'anyarray int4 bool',
   prosrc => 'generate_subscripts' },
 { oid => '1192', descr => 'array subscripts generator',
-  proname => 'generate_subscripts', prorows => '1000',
+  proname => 'generate_subscripts', prorows => '100',
   prosupport => 'generate_subscripts_support', proretset => 't',
   prorettype => 'int4', proargtypes => 'anyarray int4',
   prosrc => 'generate_subscripts_nodir' },
diff --git a/src/test/regress/expected/planner_est.out b/src/test/regress/expected/planner_est.out
index 91305e360f8..5e892ef6728 100644
--- a/src/test/regress/expected/planner_est.out
+++ b/src/test/regress/expected/planner_est.out
@@ -299,9 +299,9 @@ true, true, false, true);
 SELECT explain_mask_costs($$
 SELECT * FROM generate_subscripts(ARRAY[1, 2, (SELECT 3)], (SELECT 1)) g(s);$$,
 false, true, false, true);
-                          explain_mask_costs                           
------------------------------------------------------------------------
- Function Scan on generate_subscripts g  (cost=N..N rows=1000 width=N)
+                          explain_mask_costs                          
+----------------------------------------------------------------------
+ Function Scan on generate_subscripts g  (cost=N..N rows=100 width=N)
    InitPlan expr_1
      ->  Result  (cost=N..N rows=1 width=N)
    InitPlan expr_2
@@ -311,9 +311,9 @@ false, true, false, true);
 SELECT explain_mask_costs($$
 SELECT * FROM generate_subscripts(ARRAY[1, 2, (SELECT 3)], 2) g(s);$$,
 false, true, false, true);
-                          explain_mask_costs                           
------------------------------------------------------------------------
- Function Scan on generate_subscripts g  (cost=N..N rows=1000 width=N)
+                          explain_mask_costs                          
+----------------------------------------------------------------------
+ Function Scan on generate_subscripts g  (cost=N..N rows=100 width=N)
    InitPlan expr_1
      ->  Result  (cost=N..N rows=1 width=N)
 (3 rows)
@@ -342,11 +342,11 @@ true, true, false, true);
 SELECT explain_mask_costs($$
 SELECT * FROM subscript_table_1 t, LATERAL generate_subscripts(t.a, 2) g(s);$$,
 false, true, false, true);
-                             explain_mask_costs                              
------------------------------------------------------------------------------
- Nested Loop  (cost=N..N rows=100000 width=N)
+                             explain_mask_costs                             
+----------------------------------------------------------------------------
+ Nested Loop  (cost=N..N rows=10000 width=N)
    ->  Seq Scan on subscript_table_1 t  (cost=N..N rows=100 width=N)
-   ->  Function Scan on generate_subscripts g  (cost=N..N rows=1000 width=N)
+   ->  Function Scan on generate_subscripts g  (cost=N..N rows=100 width=N)
 (3 rows)
 
 RESET enable_memoize;
-- 
2.37.1 (Apple Git-137.1)

