Eager aggregation and volatile aggregate support functions

Started by Kirill Reshke8 days ago1 messageshackers
Jump to latest
#1Kirill Reshke
reshkekirill@gmail.com

I was going through a bug report from Tomas [0]/messages/by-id/b368a3ca-b196-4c28-8050-50320e9c955d@vondra.me 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]/messages/by-id/CAGRkXqSc_qtpuPm34qoS1z0LVx4+Hc+JcJkbDEVC=z2ai4VQ1w@mail.gmail.com 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]: /messages/by-id/b368a3ca-b196-4c28-8050-50320e9c955d@vondra.me
[1]: /messages/by-id/CAGRkXqSc_qtpuPm34qoS1z0LVx4+Hc+JcJkbDEVC=z2ai4VQ1w@mail.gmail.com

--
Best regards,
Kirill Reshke

Attachments:

t259289_1
v2-0001-Preserve-aggregate-support-function-calls.patchapplication/octet-stream; name=v2-0001-Preserve-aggregate-support-function-calls.patchDownload+25-1