ALTER_AGGREGATE(7pg16)

πŸ“¦ version 16.15, in opensuse-leap 16.0

πŸ’… 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 CREATE privilege on the new schema.
  • to alter the owner, you must be able to SET ROLE to the new owning role, and that role must have CREATE privilege 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 like myschema.myagg if you're feeling fancy. πŸ’…
  • argmode β€” the mode of an argument: IN or VARIADIC. if you omit it, the default is IN. easy.
  • argname β€” the name of an argument. but heads up bestie: ALTER AGGREGATE does 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 BY between the direct and aggregated argument specifications.
  • 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, if VARIADIC "any" was used in both the direct and aggregated argument lists, write VARIADIC "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 🌸

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