Eager aggregation and volatile aggregate support functions

From: Kirill Reshke <reshkekirill(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Eager aggregation and volatile aggregate support functions
Date: 2026-11-17 18:46:22
Message-ID: CALdSSPjaeJBxyzzK3F0h5zxYMDAf3K-6qfmMuk7XhVsEF43_cA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I was going through a bug report from Tomas [0] and decided to check
more on planner strict/non-strict/volatile/etc behaviour. I have found
a small issue with eager aggregation vs custom aggregates. This is
slightly different from recently reported [1] and such.

tldr repro:

BEGIN;
CREATE SCHEMA eager_volatile_min;
SET search_path = eager_volatile_min, public;
CREATE SEQUENCE calls;
CREATE FUNCTION counted_sum(bigint, bigint) RETURNS bigint
LANGUAGE plpgsql VOLATILE AS $$
BEGIN
PERFORM nextval('calls');
RETURN $1 + $2;
END
$$;
CREATE AGGREGATE counted_sum(bigint)
(SFUNC = counted_sum, STYPE = bigint, COMBINEFUNC = int8pl, INITCOND = '0');
CREATE TABLE f AS SELECT 1 AS k, i::bigint AS v FROM generate_series(1, 100) i;
CREATE TABLE m AS SELECT 1 AS k FROM generate_series(1, 100);
ANALYZE f, m;
SET LOCAL enable_eager_aggregate = on;
EXPLAIN (COSTS OFF)
SELECT f.k, counted_sum(f.v) FROM f JOIN m USING (k) GROUP BY f.k;
SELECT f.k, counted_sum(f.v) FROM f JOIN m USING (k) GROUP BY f.k;
SELECT last_value AS calls_with_eager FROM calls;
ALTER SEQUENCE calls RESTART;
SET LOCAL enable_eager_aggregate = off;
SELECT f.k, counted_sum(f.v) FROM f JOIN m USING (k) GROUP BY f.k;
SELECT last_value AS calls_without_eager FROM calls;
ROLLBACK;

on current master (11591f273e6a26fb55728b410538fa2627b19b28) last few
lines of output would be

k | counted_sum
---+-------------
1 | 505000
(1 row)

calls_with_eager
------------------
100
(1 row)

ALTER SEQUENCE
SET
k | counted_sum
---+-------------
1 | 505000
(1 row)

calls_without_eager
---------------------
10000
(1 row)

ROLLBACK

The thing is, contain_volatile_functions(Aggref) inspects the
aggregate OID/expression arguments, but not support functions.
Attached checks support functions too.

[0] https://www.postgresql.org/message-id/b368a3ca-b196-4c28-8050-50320e9c955d@vondra.me
[1] https://www.postgresql.org/message-id/CAGRkXqSc_qtpuPm34qoS1z0LVx4%2BHc%2BJcJkbDEVC%3Dz2ai4VQ1w%40mail.gmail.com

--
Best regards,
Kirill Reshke

Attachment Content-Type Size
v2-0001-Preserve-aggregate-support-function-calls.patch application/octet-stream 2.4 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message surya poondla 2026-11-17 19:23:37 Re: Bump soft open file limit (RLIMIT_NOFILE) to hard limit on startup
Previous Message Baji Shaik 2026-11-17 18:28:58 Re: using index to speedup add not null constraints to a table