Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wx92G-002TCM-1F for pgsql-hackers@arkaria.postgresql.org; Thu, 20 Aug 2026 20:06:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wx92E-009ySJ-2S for pgsql-hackers@arkaria.postgresql.org; Thu, 20 Aug 2026 20:06:46 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wx92E-009ySB-0j for pgsql-hackers@lists.postgresql.org; Thu, 20 Aug 2026 20:06:46 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wx92A-00000001d2d-3k4Z for pgsql-hackers@lists.postgresql.org; Thu, 20 Aug 2026 20:06:45 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 67KK6dXT4008014; Thu, 20 Aug 2026 16:06:39 -0400 From: Tom Lane To: Andrei Lepikhov cc: David Rowley , PostgreSQL Hackers , Corey Huinker Subject: Re: Allow a prosupport function to be attached to an aggregate In-reply-to: <3954875.1787255265@sss.pgh.pa.us> References: <8f58c96d-d3c7-4c0f-9898-116f00eeaff6@gmail.com> <1379592.1787063701@sss.pgh.pa.us> <3f63c035-e0a6-4dc6-a34e-743df8c19d50@gmail.com> <3954875.1787255265@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Thu, 20 Aug 2026 15:47:45 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <4008001.1787256390.0@sss.pgh.pa.us> Date: Thu, 20 Aug 2026 16:06:39 -0400 Message-ID: <4008013.1787256399@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <4008001.1787256390.1@sss.pgh.pa.us> I wrote: > After reflection I realized that the approach I took in my first patch > is wrong: letting ALTER FUNCTION work on an aggregate is pretty > non-orthogonal, if the only property it can set is SUPPORT. The right > way to do this is to add a SUPPORT option to CREATE AGGREGATE, which > slots nicely into getting pg_dump to handle it. As attached. Grumble ... for some reason I was thinking that ProcedureCreate would handle checking for superuser when specifying a support function, but it doesn't; AggregateCreate must do that. v3 attached contains just that one addition. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name*0="v3-0001-Allow-an-aggregate-s-planner-support-function-to-.p"; name*1="atch"; charset="us-ascii" Content-ID: <4008001.1787256390.2@sss.pgh.pa.us> Content-Description: v3-0001-Allow-an-aggregate-s-planner-support-function-to-.patch Content-Transfer-Encoding: quoted-printable =46rom af5b492e92239da03d791427d2bf31657fc92e79 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 20 Aug 2026 16:03:13 -0400 Subject: [PATCH v3] Allow an aggregate's planner support function to be se= t via CREATE AGGREGATE. Commit 42473b3b3 implemented SupportRequestSimplifyAggref, but failed to think about what infrastructure would be required for an extension to use that: there is no SQL-level mechanism for attaching a planner support function to an aggregate. That seems pretty critical for a feature that's primarily intended to be used by extensions. To fix, add a SUPPORT clause to CREATE AGGREGATE, and teach pg_dump (and thereby pg_upgrade) about dumping this aggregate property. We don't need to touch ALTER AGGREGATE, because it's already the case that you're supposed to use CREATE OR REPLACE AGGREGATE if you want to alter any aggregate-specific properties set by CREATE AGGREGATE. (Maybe at some point we'll think that that policy ought to change, but I don't think this one feature moves the needle enough.) Per report from Andrei Lepikhov, who also provided some of the new documentation text. Reported-by: Andrei Lepikhov Author: Tom Lane Reviewed-by: Andrei Lepikhov Discussion: https://postgr.es/m/8f58c96d-d3c7-4c0f-9898-116f00eeaff6@gmail= .com Backpatch-through: 19 --- doc/src/sgml/ref/alter_aggregate.sgml | 7 ++++ doc/src/sgml/ref/create_aggregate.sgml | 15 +++++++++ doc/src/sgml/xfunc.sgml | 18 +++++++++- src/backend/catalog/pg_aggregate.c | 33 ++++++++++++++++++- src/backend/commands/aggregatecmds.c | 4 +++ src/bin/pg_dump/pg_dump.c | 18 ++++++++-- src/include/catalog/pg_aggregate.h | 1 + .../regress/expected/create_aggregate.out | 15 +++++++++ src/test/regress/sql/create_aggregate.sql | 8 +++++ 9 files changed, 115 insertions(+), 4 deletions(-) diff --git a/doc/src/sgml/ref/alter_aggregate.sgml b/doc/src/sgml/ref/alte= r_aggregate.sgml index d0a39ba7b5e..cc11e077561 100644 --- a/doc/src/sgml/ref/alter_aggregate.sgml +++ b/doc/src/sgml/ref/alter_aggregate.sgml @@ -149,6 +149,13 @@ ALTER AGGREGATE name ( aggregate_signatu if VARIADIC "any" was used in both the direct and aggregated argument lists, write VARIADIC "any" on= ly once. + + + ALTER AGGREGATE deals only with generic properties= of + an aggregate, such as its name. To change the aggregate-specific + properties of an aggregate such as its support functions, replace its + definition entirely with CREATE OR REPLACE AGGREGATE. + = diff --git a/doc/src/sgml/ref/create_aggregate.sgml b/doc/src/sgml/ref/cre= ate_aggregate.sgml index 0472ac2e874..89203d39221 100644 --- a/doc/src/sgml/ref/create_aggregate.sgml +++ b/doc/src/sgml/ref/create_aggregate.sgml @@ -41,6 +41,7 @@ CREATE [ OR REPLACE ] AGGREGATE nameminitial_condition= ] [ , SORTOP =3D sort_operator ] + [ , SUPPORT =3D supportfunc ] [ , PARALLEL =3D { SAFE | RESTRICTED | UNSAFE } ] ) = @@ -53,6 +54,7 @@ CREATE [ OR REPLACE ] AGGREGATE nameinitial_condition ] + [ , SUPPORT =3D supportfunc ] [ , PARALLEL =3D { SAFE | RESTRICTED | UNSAFE } ] [ , HYPOTHETICAL ] ) @@ -80,6 +82,7 @@ CREATE [ OR REPLACE ] AGGREGATE nameminitial_condition= ] [ , SORTOP =3D sort_operator ] + [ , SUPPORT =3D supportfunc ] ) @@ -628,6 +631,18 @@ SELECT col FROM tab ORDER BY col USING sortop LIMIT 1= ; = + + supportfunc + + + The name (optionally schema-qualified) of a planner supp= ort + function to use for this aggregate. See + for details. + You must be superuser to use this option. + + + + PARALLEL =3D { SAFE | RESTRICTED | UNSAFE } diff --git a/doc/src/sgml/xfunc.sgml b/doc/src/sgml/xfunc.sgml index 050e1e50bec..97f3cb625e2 100644 --- a/doc/src/sgml/xfunc.sgml +++ b/doc/src/sgml/xfunc.sgml @@ -4167,7 +4167,7 @@ extern PgStat_Kind pgstat_register_kind(PgStat_Kind = kind, = It is also possible to attach a planner support - function to an SQL-callable function (called + function to an SQL-callable function or aggregate (called its target function), and thereby provide knowledge about the target function that is too complex to be represented declaratively. Planner support functions have to be @@ -4241,6 +4241,22 @@ supportfn(internal) returns internal normal execution of the target function. = + + Aggregate function calls can also be simplified during planning. For + example, COUNT(x) can b= e + replaced by COUNT(*) + when x is known to not be null. This can = be + done by a support function that implements + the SupportRequestSimplifyAggref request type. Th= e + support function will be called for each instance of its target aggre= gate + found in a query parse tree. If it finds that the particular call ca= n be + replaced, it can build and return a new node, usually another aggrega= te + call, leaving the node it was given unmodified. As with + SupportRequestSimplify, it is the support function= 's + responsibility that the replacement be equivalent to normal execution= of + the target aggregate. + + For target functions that return boolean, it is often us= eful to estimate the fraction of rows that will be selected by a WHERE clause using that diff --git a/src/backend/catalog/pg_aggregate.c b/src/backend/catalog/pg_a= ggregate.c index 243b952b9cc..4d98a7584ae 100644 --- a/src/backend/catalog/pg_aggregate.c +++ b/src/backend/catalog/pg_aggregate.c @@ -68,6 +68,7 @@ AggregateCreate(const char *aggName, char finalfnModify, char mfinalfnModify, List *aggsortopName, + List *aggsupportfuncName, Oid aggTransType, int32 aggTransSpace, Oid aggmTransType, @@ -92,6 +93,7 @@ AggregateCreate(const char *aggName, Oid minvtransfn =3D InvalidOid; /* can be omitted */ Oid mfinalfn =3D InvalidOid; /* can be omitted */ Oid sortop =3D InvalidOid; /* can be omitted */ + Oid supportfn =3D InvalidOid; /* can be omitted */ Oid *aggArgTypes =3D parameterTypes->values; bool mtransIsStrict =3D false; Oid rettype; @@ -581,6 +583,35 @@ AggregateCreate(const char *aggName, false, -1); } = + /* + * Validate the planner support function, if present. + */ + if (aggsupportfuncName) + { + /* signature is always support(internal) returns internal */ + fnArgs[0] =3D INTERNALOID; + + supportfn =3D lookup_agg_function(aggsupportfuncName, 1, + fnArgs, InvalidOid, + &rettype); + + if (rettype !=3D INTERNALOID) + ereport(ERROR, + (errcode(ERRCODE_DATATYPE_MISMATCH), + errmsg("return type of support function %s is not %s", + NameListToString(aggsupportfuncName), + format_type_be(INTERNALOID)))); + + /* + * Specifying a support function requires superuser, same as in CREATE + * FUNCTION. + */ + if (!superuser()) + ereport(ERROR, + (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), + errmsg("must be superuser to specify a support function"))); + } + /* * permission checks on used types */ @@ -639,7 +670,7 @@ AggregateCreate(const char *aggName, PointerGetDatum(NULL), /* trftypes */ NIL, /* trfoids */ PointerGetDatum(NULL), /* proconfig */ - InvalidOid, /* no prosupport */ + supportfn, /* prosupport */ 1, /* procost */ 0); /* prorows */ procOid =3D myself.objectId; diff --git a/src/backend/commands/aggregatecmds.c b/src/backend/commands/a= ggregatecmds.c index 41b45dc6402..d26cfd8081d 100644 --- a/src/backend/commands/aggregatecmds.c +++ b/src/backend/commands/aggregatecmds.c @@ -74,6 +74,7 @@ DefineAggregate(ParseState *pstate, char finalfuncModify =3D 0; char mfinalfuncModify =3D 0; List *sortoperatorName =3D NIL; + List *supportfuncName =3D NIL; TypeName *baseType =3D NULL; TypeName *transType =3D NULL; TypeName *mtransType =3D NULL; @@ -155,6 +156,8 @@ DefineAggregate(ParseState *pstate, mfinalfuncModify =3D extractModify(defel); else if (strcmp(defel->defname, "sortop") =3D=3D 0) sortoperatorName =3D defGetQualifiedName(defel); + else if (strcmp(defel->defname, "support") =3D=3D 0) + supportfuncName =3D defGetQualifiedName(defel); else if (strcmp(defel->defname, "basetype") =3D=3D 0) baseType =3D defGetTypeName(defel); else if (strcmp(defel->defname, "hypothetical") =3D=3D 0) @@ -462,6 +465,7 @@ DefineAggregate(ParseState *pstate, finalfuncModify, mfinalfuncModify, sortoperatorName, /* sort operator name */ + supportfuncName, /* planner support func name */ transTypeId, /* transition data type */ transSpace, /* transition space */ mtransTypeId, /* transition data type */ diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index feed88f9854..db14834e430 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -15365,6 +15365,7 @@ dumpAgg(Archive *fout, const AggInfo *agginfo) const char *agginitval; const char *aggminitval; const char *proparallel; + const char *prosupport; char defaultfinalmodify; = /* Do nothing if not dumping schema */ @@ -15413,11 +15414,18 @@ dumpAgg(Archive *fout, const AggInfo *agginfo) if (fout->remoteVersion >=3D 110000) appendPQExpBufferStr(query, "aggfinalmodify,\n" - "aggmfinalmodify\n"); + "aggmfinalmodify,\n"); else appendPQExpBufferStr(query, "'0' AS aggfinalmodify,\n" - "'0' AS aggmfinalmodify\n"); + "'0' AS aggmfinalmodify,\n"); + + if (fout->remoteVersion >=3D 120000) + appendPQExpBufferStr(query, + "prosupport\n"); + else + appendPQExpBufferStr(query, + "'-' AS prosupport\n"); = appendPQExpBufferStr(query, "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p " @@ -15459,6 +15467,7 @@ dumpAgg(Archive *fout, const AggInfo *agginfo) agginitval =3D PQgetvalue(res, 0, i_agginitval); aggminitval =3D PQgetvalue(res, 0, i_aggminitval); proparallel =3D PQgetvalue(res, 0, PQfnumber(res, "proparallel")); + prosupport =3D PQgetvalue(res, 0, PQfnumber(res, "prosupport")); = { char *funcargs; @@ -15587,6 +15596,11 @@ dumpAgg(Archive *fout, const AggInfo *agginfo) free(aggsortconvop); } = + if (strcmp(prosupport, "-") !=3D 0) + { + appendPQExpBuffer(details, ",\n SUPPORT =3D %s", prosupport); + } + if (aggkind =3D=3D AGGKIND_HYPOTHETICAL) appendPQExpBufferStr(details, ",\n HYPOTHETICAL"); = diff --git a/src/include/catalog/pg_aggregate.h b/src/include/catalog/pg_a= ggregate.h index 2b4f5dae5f2..2668b035859 100644 --- a/src/include/catalog/pg_aggregate.h +++ b/src/include/catalog/pg_aggregate.h @@ -175,6 +175,7 @@ extern ObjectAddress AggregateCreate(const char *aggNa= me, char finalfnModify, char mfinalfnModify, List *aggsortopName, + List *aggsupportfuncName, Oid aggTransType, int32 aggTransSpace, Oid aggmTransType, diff --git a/src/test/regress/expected/create_aggregate.out b/src/test/reg= ress/expected/create_aggregate.out index dcf69094237..68062620efa 100644 --- a/src/test/regress/expected/create_aggregate.out +++ b/src/test/regress/expected/create_aggregate.out @@ -30,12 +30,27 @@ CREATE AGGREGATE oldcnt ( -- aggregate that only cares about null/nonnull input CREATE AGGREGATE newcnt ("any") ( sfunc =3D int8inc_any, stype =3D int8, + support =3D int8inc_support, initcond =3D '0' ); COMMENT ON AGGREGATE nosuchagg (*) IS 'should fail'; ERROR: aggregate nosuchagg(*) does not exist COMMENT ON AGGREGATE newcnt (*) IS 'an agg(*) comment'; COMMENT ON AGGREGATE newcnt ("any") IS 'an agg(any) comment'; +-- verify that newcnt's support function enables run-condition optimizati= on +EXPLAIN (COSTS OFF) +SELECT * FROM + (SELECT newcnt(ten) OVER (RANGE BETWEEN CURRENT ROW AND CURRENT ROW) c + FROM tenk1) t +WHERE c =3D 1; + QUERY PLAN = +------------------------------------------------------------- + WindowAgg + Window: w1 AS (RANGE BETWEEN CURRENT ROW AND CURRENT ROW) + Run Condition: (newcnt(tenk1.ten) OVER w1 =3D 1) + -> Seq Scan on tenk1 +(4 rows) + -- multi-argument aggregate create function sum3(int8,int8,int8) returns int8 as 'select $1 + $2 + $3' language sql strict immutable; diff --git a/src/test/regress/sql/create_aggregate.sql b/src/test/regress/= sql/create_aggregate.sql index d4b4036fd7d..6b88b9735af 100644 --- a/src/test/regress/sql/create_aggregate.sql +++ b/src/test/regress/sql/create_aggregate.sql @@ -35,6 +35,7 @@ CREATE AGGREGATE oldcnt ( -- aggregate that only cares about null/nonnull input CREATE AGGREGATE newcnt ("any") ( sfunc =3D int8inc_any, stype =3D int8, + support =3D int8inc_support, initcond =3D '0' ); = @@ -42,6 +43,13 @@ COMMENT ON AGGREGATE nosuchagg (*) IS 'should fail'; COMMENT ON AGGREGATE newcnt (*) IS 'an agg(*) comment'; COMMENT ON AGGREGATE newcnt ("any") IS 'an agg(any) comment'; = +-- verify that newcnt's support function enables run-condition optimizati= on +EXPLAIN (COSTS OFF) +SELECT * FROM + (SELECT newcnt(ten) OVER (RANGE BETWEEN CURRENT ROW AND CURRENT ROW) c + FROM tenk1) t +WHERE c =3D 1; + -- multi-argument aggregate create function sum3(int8,int8,int8) returns int8 as 'select $1 + $2 + $3' language sql strict immutable; -- = 2.52.0 ------- =_aaaaaaaaaa0--