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/[email protected]
[1] 
https://www.postgresql.org/message-id/CAGRkXqSc_qtpuPm34qoS1z0LVx4%2BHc%2BJcJkbDEVC%3Dz2ai4VQ1w%40mail.gmail.com

-- 
Best regards,
Kirill Reshke

Attachment: v2-0001-Preserve-aggregate-support-function-calls.patch
Description: Binary data

Reply via email to