🌐 AI搜索 & 代理 主页
Skip to content

Commit 185e304

Browse files
author
Richard Guo
committed
Allow negative aggtransspace to indicate unbounded state size
This patch reuses the existing aggtransspace in pg_aggregate to signal that an aggregate's transition state can grow unboundedly. If aggtransspace is set to a negative value, it now indicates that the transition state may consume unpredictable or large amounts of memory, such as in aggregates like array_agg or string_agg that accumulate input rows. This information can be used by the planner to avoid applying memory-sensitive optimizations (e.g., eager aggregation) when there is a risk of excessive memory usage during partial aggregation. Bump catalog version. Per idea from Robert Haas, though applied differently than originally suggested. Discussion: https://postgr.es/m/CA+TgmoYbkvYwLa+1vOP7RDY7kO2=A7rppoPusoRXe44VDOGBPg@mail.gmail.com
1 parent 138da72 commit 185e304

File tree

6 files changed

+21
-11
lines changed

6 files changed

+21
-11
lines changed

doc/src/sgml/catalogs.sgml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,10 @@
596596
</para>
597597
<para>
598598
Approximate average size (in bytes) of the transition state
599-
data, or zero to use a default estimate
599+
data. A positive value provides an estimate; zero means to
600+
use a default estimate. A negative value indicates the state
601+
data can grow unboundedly in size, such as when the aggregate
602+
accumulates input rows (e.g., array_agg, string_agg).
600603
</para></entry>
601604
</row>
602605

doc/src/sgml/ref/create_aggregate.sgml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,13 @@ SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;
384384
<para>
385385
The approximate average size (in bytes) of the aggregate's state value.
386386
If this parameter is omitted or is zero, a default estimate is used
387-
based on the <replaceable>state_data_type</replaceable>.
387+
based on the <replaceable>state_data_type</replaceable>. If set to a
388+
negative value, it indicates the state data can grow unboundedly in
389+
size, such as when the aggregate accumulates input rows (e.g.,
390+
array_agg, string_agg).
388391
The planner uses this value to estimate the memory required for a
389-
grouped aggregate query.
392+
grouped aggregate query and to avoid optimizations that may cause
393+
excessive memory usage.
390394
</para>
391395
</listitem>
392396
</varlistentry>
@@ -568,7 +572,8 @@ SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;
568572
<para>
569573
The approximate average size (in bytes) of the aggregate's state
570574
value, when using moving-aggregate mode. This works the same as
571-
<replaceable>state_data_size</replaceable>.
575+
<replaceable>state_data_size</replaceable>, except that negative
576+
values are not used to indicate unbounded state size.
572577
</para>
573578
</listitem>
574579
</varlistentry>

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/* yyyymmddN */
60-
#define CATALOG_VERSION_NO 202510081
60+
#define CATALOG_VERSION_NO 202510082
6161

6262
#endif

src/include/catalog/pg_aggregate.dat

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -558,26 +558,28 @@
558558
aggfinalfn => 'array_agg_finalfn', aggcombinefn => 'array_agg_combine',
559559
aggserialfn => 'array_agg_serialize',
560560
aggdeserialfn => 'array_agg_deserialize', aggfinalextra => 't',
561-
aggtranstype => 'internal' },
561+
aggtranstype => 'internal', aggtransspace => '-1' },
562562
{ aggfnoid => 'array_agg(anyarray)', aggtransfn => 'array_agg_array_transfn',
563563
aggfinalfn => 'array_agg_array_finalfn',
564564
aggcombinefn => 'array_agg_array_combine',
565565
aggserialfn => 'array_agg_array_serialize',
566566
aggdeserialfn => 'array_agg_array_deserialize', aggfinalextra => 't',
567-
aggtranstype => 'internal' },
567+
aggtranstype => 'internal', aggtransspace => '-1' },
568568

569569
# text
570570
{ aggfnoid => 'string_agg(text,text)', aggtransfn => 'string_agg_transfn',
571571
aggfinalfn => 'string_agg_finalfn', aggcombinefn => 'string_agg_combine',
572572
aggserialfn => 'string_agg_serialize',
573-
aggdeserialfn => 'string_agg_deserialize', aggtranstype => 'internal' },
573+
aggdeserialfn => 'string_agg_deserialize',
574+
aggtranstype => 'internal', aggtransspace => '-1' },
574575

575576
# bytea
576577
{ aggfnoid => 'string_agg(bytea,bytea)',
577578
aggtransfn => 'bytea_string_agg_transfn',
578579
aggfinalfn => 'bytea_string_agg_finalfn',
579580
aggcombinefn => 'string_agg_combine', aggserialfn => 'string_agg_serialize',
580-
aggdeserialfn => 'string_agg_deserialize', aggtranstype => 'internal' },
581+
aggdeserialfn => 'string_agg_deserialize',
582+
aggtranstype => 'internal', aggtransspace => '-1' },
581583

582584
# range
583585
{ aggfnoid => 'range_intersect_agg(anyrange)',

src/test/regress/expected/opr_sanity.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1470,7 +1470,7 @@ WHERE aggfnoid = 0 OR aggtransfn = 0 OR
14701470
(aggkind = 'n' AND aggnumdirectargs > 0) OR
14711471
aggfinalmodify NOT IN ('r', 's', 'w') OR
14721472
aggmfinalmodify NOT IN ('r', 's', 'w') OR
1473-
aggtranstype = 0 OR aggtransspace < 0 OR aggmtransspace < 0;
1473+
aggtranstype = 0 OR aggmtransspace < 0;
14741474
ctid | aggfnoid
14751475
------+----------
14761476
(0 rows)

src/test/regress/sql/opr_sanity.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ WHERE aggfnoid = 0 OR aggtransfn = 0 OR
847847
(aggkind = 'n' AND aggnumdirectargs > 0) OR
848848
aggfinalmodify NOT IN ('r', 's', 'w') OR
849849
aggmfinalmodify NOT IN ('r', 's', 'w') OR
850-
aggtranstype = 0 OR aggtransspace < 0 OR aggmtransspace < 0;
850+
aggtranstype = 0 OR aggmtransspace < 0;
851851

852852
-- Make sure the matching pg_proc entry is sensible, too.
853853

0 commit comments

Comments
 (0)