Re: Block various aggregate component functions from being called directly

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: David Rowley <dgrowleyml(at)gmail(dot)com>
Cc: PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Block various aggregate component functions from being called directly
Date: 2026-11-19 08:16:19
Message-ID: 0A443BC2-4693-4C82-BF16-CA4AB9C45E8D@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Nov 19, 2026, at 15:20, David Rowley <dgrowleyml(at)gmail(dot)com> wrote:
>
> 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?!

I have the same feeling.

But, IMO, as infrastructure software, PG is supposed to handle any invalid inputs gracefully. I think it’s a problem of ROI. We have limited development resources, and fixing every unusual case might not be worthwhile. Preventing this kind of unintended use seems like a reasonable way to handle that. So, I think this patch is a good trade-off.

>
> 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?

+1 from me. I think this patch creates infra that allows us to make similar functions internal easily.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Chao Li 2026-11-19 08:33:01 Re: A latent pointer increment bug in pgtypes_defmt_scan()
Previous Message Bertrand Drouvot 2026-11-19 08:05:41 Re: Detect mismatched subscription and replication slot properties