[PG19] Eager aggregation runs a volatile join clause once per group
Hi,
This is High 3 from my list in [1]/messages/by-id/CAGRkXqSFdjqZP_kwRz7dv6LHX-CSrkDnynNXgntRsCEnQTKxmg@mail.gmail.com.
Eager aggregation can put a partial aggregate below a join whose
clause has a volatile function. The function then runs once per
group, not once per joined row. 3a08a2a8b4f fixed the same thing for
volatile functions inside the aggregate.
create table f as select 1 as k from generate_series(1, 100);
create table dm as select 1 as k;
analyze f, dm;
create sequence sq;
select count(*) from f join dm on f.k = dm.k and nextval('sq') > 0
group by dm.k;
select last_value from sq;
The join has 100 rows. 18 calls nextval 100 times, master calls it
once. With random() < 0.5 in place of nextval, 18 returns a count
near 50, and master returns 100 or no row at all. The same happens
with where nextval('sq') > 0, a condition with no columns.
0001 does not build a grouped rel when one of the rel's join clauses
is volatile. A volatile filter on the aggregated table alone is still
allowed, it runs below the partial aggregate. 0002 adds a test. The
patches apply to master and REL_19_STABLE.
Related discussion in [2]/messages/by-id/CA+Tgmob7iSM9YkRM44VjUDuaCchW-fY54MV5njpTZTL9uNyV4w@mail.gmail.com.
[1]: /messages/by-id/CAGRkXqSFdjqZP_kwRz7dv6LHX-CSrkDnynNXgntRsCEnQTKxmg@mail.gmail.com
/messages/by-id/CAGRkXqSFdjqZP_kwRz7dv6LHX-CSrkDnynNXgntRsCEnQTKxmg@mail.gmail.com
[2]: /messages/by-id/CA+Tgmob7iSM9YkRM44VjUDuaCchW-fY54MV5njpTZTL9uNyV4w@mail.gmail.com
/messages/by-id/CA+Tgmob7iSM9YkRM44VjUDuaCchW-fY54MV5njpTZTL9uNyV4w@mail.gmail.com
Thanks,
Shihao
Attachments:
t254169_1v1-0002-Test-eager-aggregation-with-a-volatile-join-claus.patchapplication/octet-stream; name=v1-0002-Test-eager-aggregation-with-a-volatile-join-claus.patchDownload+77-1
v1-0001-Don-t-use-eager-aggregation-below-volatile-clause.patchapplication/octet-stream; name=v1-0001-Don-t-use-eager-aggregation-below-volatile-clause.patchDownload+12-1
Hi,
On Sun, Oct 12, 2026 shihao zhong wrote:
Eager aggregation can put a partial aggregate below a join whose
clause has a volatile function. The function then runs once per
group, not once per joined row.
I tested v1 against master with a differential that runs each query
with eager aggregation on and off and counts the volatile function's
evaluations (a sequence bump), requiring both the eval count and the
result to match; a coverage guard checks that EXPLAIN really shows a
partial aggregate.
Over 23 placements of the volatile -- join ON, cross-rel WHERE, LEFT
JOIN, 3-way joins (upper and lower), CASE, correlated subquery,
LATERAL, semijoin, HAVING, plus partitionwise, parallel, window
functions and an SRF in the target list -- master miscounts in every
shape where the clause sits above the partial aggregate, and v1 fixes
all of them. For the parallel ones I confirmed the plans really were
parallel.
It is not over-broad: where the volatile sits below the partial
aggregate (3-way lower join, a WHERE on the grouped rel only, LATERAL,
...) v1 keeps eager aggregation and the count stays right, so it
refuses eager aggregation only where it must. The positions I
suspected statically (LATERAL, correlated subquery, semijoin, HAVING)
were already correct on master.
SQLancer (NoREC + TLP) on the v1 build ran clean, though it rarely
generates a volatile join clause, so that is weak evidence here.
Looks good to me.
Regards,
Manu