[PG19] Eager aggregation runs a volatile join clause once per group

From: shihao zhong <zhong950419(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Guofenglinux <guofenglinux(at)gmail(dot)com>, Robert Haas <robertmhaas(at)gmail(dot)com>
Subject: [PG19] Eager aggregation runs a volatile join clause once per group
Date: 2026-10-13 03:35:39
Message-ID: CAGRkXqSc_qtpuPm34qoS1z0LVx4+Hc+JcJkbDEVC=z2ai4VQ1w@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

This is High 3 from my list in [1].

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].

[1]
https://postgr.es/m/CAGRkXqSFdjqZP_kwRz7dv6LHX-CSrkDnynNXgntRsCEnQTKxmg@mail.gmail.com
[2]
https://postgr.es/m/CA+Tgmob7iSM9YkRM44VjUDuaCchW-fY54MV5njpTZTL9uNyV4w@mail.gmail.com

Thanks,
Shihao

Attachment Content-Type Size
v1-0002-Test-eager-aggregation-with-a-volatile-join-claus.patch application/octet-stream 3.6 KB
v1-0001-Don-t-use-eager-aggregation-below-volatile-clause.patch application/octet-stream 1.3 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message jian he 2026-10-13 03:40:29 Re: Row pattern recognition
Previous Message Tianyi Gao 2026-10-13 03:31:30 Use a hash table for the regexp cache