ALTER_AGGREGATE(7pg16)
π ALTER AGGREGATE π
okay bestie, grab your iced coffee because we're diving into ALTER AGGREGATE β that's the ALTER_AGGREGATE page from the PostgreSQL 16.15 Documentation, and yes it's giving "I just need to tweak one tiny thing about my aggregate function" energy. no cap, this one's actually pretty chill. β¨
πΈ NAME πΈ
ALTER_AGGREGATE β change the definition of an aggregate function. that's it, that's the whole vibe. π
β¨ SYNOPSIS β¨
ALTER AGGREGATE name ( aggregate_signature ) RENAME TO new_name
ALTER AGGREGATE name ( aggregate_signature )
OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
ALTER AGGREGATE name ( aggregate_signature ) SET SCHEMA new_schema
where aggregate_signature is:
* |
[ argmode ] [ argname ] argtype [ , ... ] |
[ [ argmode ] [ argname ] argtype [ , ... ] ] ORDER BY [ argmode ] [ argname ] argtype [ , ... ]
look at that signature block, it's literally a little choose-your-own-adventure π¦ β you either go *, or a plain list of args, or the fancy ordered-set version with ORDER BY in the middle. iconic.
π DESCRIPTION π
ALTER AGGREGATE changes the definition of an aggregate function. lowkey self-explanatory but we love a clear statement. π₯
okay so here's the part you actually need to know, bestie β the permission tea: β
- you must own the aggregate function to use
ALTER AGGREGATE. periodt. - to change the schema of an aggregate function, you must also have
CREATEprivilege on the new schema. - to alter the owner, you must be able to
SET ROLEto the new owning role, and that role must haveCREATEprivilege on the aggregate function's schema.
and why these restrictions? (the docs are being extra and explaining, but it's actually useful): they enforce that altering the owner doesn't let you do anything you couldn't already do by dropping and recreating the aggregate function. iykyk. π
but also β a superuser can alter ownership of any aggregate function anyway. main character energy, no rules apply. π
π PARAMETERS π
nameβ the name of an existing aggregate function. optionally schema-qualified, so likemyschema.myaggif you're feeling fancy. πargmodeβ the mode of an argument:INorVARIADIC. if you omit it, the default isIN. easy.argnameβ the name of an argument. but heads up bestie:ALTER AGGREGATEdoes not actually pay any attention to argument names, since only the argument data types are needed to determine the aggregate function's identity. so you can write it, it just won't be the thing doing the work. πargtypeβ an input data type on which the aggregate function operates. some specifics, because this is where the β¨spiceβ¨ lives:- to reference a zero-argument aggregate function, write
*in place of the list of argument specifications. - to reference an ordered-set aggregate function, write
ORDER BYbetween the direct and aggregated argument specifications.
- to reference a zero-argument aggregate function, write
new_nameβ the new name of the aggregate function. the glow-up name. πnew_ownerβ the new owner of the aggregate function. handing over the keys. πnew_schemaβ the new schema for the aggregate function. moving day. π¦
π¦ NOTES π¦
the recommended syntax for referencing an ordered-set aggregate is to write ORDER BY between the direct and aggregated argument specifications, in the same style as in CREATE AGGREGATE. (consistency slay π)
however β and this is the fun part β it will also work to omit ORDER BY and just run the direct and aggregated argument specifications into a single list. the abbreviated form, if you will.
tiny caveat so you don't trip: in this abbreviated form, ifVARIADIC "any"was used in both the direct and aggregated argument lists, writeVARIADIC "any"only once. not twice. one time. π
β EXAMPLES β
honestly the examples are the best part, let's go:
to rename the aggregate function myavg for type integer to my_average:
ALTER AGGREGATE myavg(integer) RENAME TO my_average;
to change the owner of the aggregate function myavg for type integer to joe:
ALTER AGGREGATE myavg(integer) OWNER TO joe;
to move the ordered-set aggregate mypercentile with direct argument of type float8 and aggregated argument of type integer into schema myschema:
ALTER AGGREGATE mypercentile(float8 ORDER BY integer) SET SCHEMA myschema;
and this will work too (the abbreviated form from the notes, out in the wild π):
ALTER AGGREGATE mypercentile(float8, integer) SET SCHEMA myschema;
π COMPATIBILITY π
there is no ALTER AGGREGATE statement in the SQL standard. so if you're out here thinking "surely the standard has this," nope. it's a PostgreSQL thing. take it up with the vibes. π
πΈ SEE ALSO πΈ
CREATE AGGREGATE(CREATE_AGGREGATE(7))DROP AGGREGATE(DROP_AGGREGATE(7))
like, make it, change it, yeet it. the full lifecycle. π«Ά
and that's the whole page bestie β brought to you by PostgreSQL 16.15, dated 2026, and filed under ALTER AGGREGATE(7). go forth and alter your aggregates responsibly. πβ¨π
π¦ where this page comes from
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms