Block various aggregate component functions from being called directly

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Block various aggregate component functions from being called directly
Date: 2026-11-19 07:20:51
Message-ID: CAApHDvoswRzfAwndQOgwq+WDw55xwVGUbu8ncQFiTGMvVioatw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

We're getting bug reports (e.g., [1][2]) complaining about various
aggregate component functions that don't do the right thing in some
weird cases. E.g., no overflow error from:

SELECT int2_avg_accum_inv('{1,9223372036854775807}'::int8[], -1::int2);

But who would do that?!

It seems like the new reality that people are using LLMs or fuzz
testing, and the robots are classifying these as bugs, and we're
getting reports complaining about it.

Andres mentioned in [3] that we should consider making all aggregate
states internal so that the transition functions take an internal
parameter so they get blocked. I don't like that idea, as it means
having to palloc aggregate states where we currently don't have to.
Tom mentioned in [4] that doing this will require serial/deserial
functions for any aggregate we want to support parallel aggregates
for. Those two seem like good reasons not to. It also feels a bit
inflexible, as we might find something we don't want to or can't
switch to an internal parameter.

I think we should just block these from being called directly by
adding a new column to pg_proc that allows a function to be marked as
internal. I don't see any need to have CREATE FUNCTION support this
for UDFs, as it's fundamentally there to protect users from misusing
built-in functions that are not designed for some more creative
purposes and to stop the robots hassling us about bugs that aren't.

I got this far with the tagging:

postgres=# select proname from pg_proc where prointernal;
proname
----------------------------------
float4_accum
float8_accum
float8_combine
float8_avg
float8_var_pop
float8_var_samp
float8_stddev_pop
float8_stddev_samp
int4_avg_combine
int2_avg_accum
int4_avg_accum
int2_avg_accum_inv
int4_avg_accum_inv
int8_avg
int2int4_sum
float8_regr_accum
float8_regr_combine
range_intersect_agg_transfn
multirange_intersect_agg_transfn
(19 rows)

And the patch gives you:

postgres=# SELECT
int2_avg_accum_inv('{1,9223372036854775807}'::int8[], -1::int2);
ERROR: function int2_avg_accum_inv(bigint[], smallint) cannot be
called directly
LINE 1: SELECT int2_avg_accum_inv('{1,9223372036854775807}'::int8[],...
^
DETAIL: This function is for internal use only.

But you can still use these in CREATE AGGREGATE, and I suppose CREATE
OPERATOR too, and even CREATE FUNCTION if you want to bypass it.

I'm not convinced I've got the prointernal checks in all the right
places, and there are probably more or less functions that need marked
as internal. Certainly, the aggregate.sql tests are failing because
I've blocked functions like float8_accum and float8_regr_accum. Maybe
we could get around that by doing CREATE FUNCTION and referencing the
core function. I'd need to remove the
Assert(AggCheckCallContext(fcinfo, NULL)) though.

The attached is a proof-of-concept patch for discussion. Is it worth
me continuing with this?

David

[1] https://postgr.es/m/DS2PR18MB9275696404EDAB513A71510D13C9702@DS2PR18MB927569.namprd18.prod.outlook.com
[2] https://postgr.es/m/AC9E5138-2771-48A1-997D-8538A5FD9E01%40gmail.com
[3] https://postgr.es/m/lbvjl22swi3p3sf7nmmrlx2cq5aa55ry3c5ore2rpylsmi6s5j@xrrahkipnsce
[4] https://www.postgresql.org/message-id/1571559.1788537022@sss.pgh.pa.us

Attachment Content-Type Size
poc_introduce_internal-only_functions.patch application/octet-stream 45.9 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Kyotaro Horiguchi 2026-11-19 07:22:06 A latent pointer increment bug in pgtypes_defmt_scan()
Previous Message Fujii Masao 2026-11-19 07:19:07 Re: Fix resource leaks in pg_receivewal on LZ4 close failures