ALTER_AGGREGATE(7pg13)

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

✨ ALTER_AGGREGATE ✨

okay so bestie, this is the ALTER_AGGREGATE page from the PostgreSQL 13.23 documentation, and it's literally the glow-up station for your aggregate functions πŸ’… like we're not creating anything new here, we're just doing a lil makeover on one that already exists.


🌸 SYNOPSIS 🌸

ALTER AGGREGATE name ( aggregate_signature ) RENAME TO new_name
ALTER AGGREGATE name ( aggregate_signature )
                OWNER TO { new_owner | CURRENT_USER | SESSION_USER }
ALTER AGGREGATE name ( aggregate_signature ) SET SCHEMA new_schema

and aggregate_signature is, and I cannot stress this enough, one of these three:

* |
[ argmode ] [ argname ] argtype [ , ... ] |
[ [ argmode ] [ argname ] argtype [ , ... ] ] ORDER BY [ argmode ] [ argname ] argtype [ , ... ]

no cap, that's the whole menu 🫢


πŸ’– DESCRIPTION πŸ’–

ALTER AGGREGATE changes the definition of an aggregate function. that's the assignment. that's the vibe. ✨

but bestie, there are rules, and the rules are not optional:

  • you must own the aggregate function to use ALTER AGGREGATE, periodt.
  • if you're changing the schema of an aggregate function, you also need CREATE privilege on the new schema.
  • if you're changing the owner, you must also be a direct or indirect member of the new owning role, and that role must have CREATE privilege on the aggregate function's schema. (these restrictions are giving "enforce that altering the owner doesn't do anything you couldn't do by dropping and recreating the aggregate function" energy. however, a superuser can alter ownership of any aggregate function anyway, so 🫢 main character exemption.)

πŸŽ€ PARAMETERS πŸŽ€

  • name β€” the name (optionally schema-qualified) of an existing aggregate function. yes it has to exist already, we're altering, not manifesting πŸ¦‹
  • argmode β€” the mode of an argument: IN or VARIADIC. if you omit it, the default is IN.
  • argname β€” the name of an argument. okay so funny thing: ALTER AGGREGATE does not actually pay any attention to argument names, because only the argument data types are needed to determine the aggregate function's identity. it's lowkey ignoring them and honestly? relatable πŸ™ˆ
  • argtype β€” an input data type on which the aggregate function operates. 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.
  • new_owner β€” the new owner of the aggregate function.
  • new_schema β€” the new schema for the aggregate function.

πŸ¦‹ 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 (CREATE_AGGREGATE(7)). that's the iconic way, that's the way it's giving πŸ’…

however, it will also work to omit ORDER BY and just run the direct and aggregated argument specifications into a single list. in this abbreviated form, if VARIADIC "any" was used in both the direct and aggregated argument lists, write VARIADIC "any" only once. one time. singular. not twice. ✨


🌈 EXAMPLES 🌈

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;

this will work too:

ALTER AGGREGATE mypercentile(float8, integer) SET SCHEMA myschema;

both slay, pick your fave πŸ’–


πŸ’€ COMPATIBILITY πŸ’€

there is no ALTER AGGREGATE statement in the SQL standard. we're built different, we're off-book, we're that girl πŸ”₯


πŸ‘‘ SEE ALSO πŸ‘‘


🌸 and that's the page!! this was PostgreSQL 13.23, dated 2025, signing off with so much love, go forth and rename your aggregates responsibly bestie πŸ’œπŸŽ€

πŸ“¦ where this page comes from