ALTER_AGGREGATE(7pg18)

๐Ÿ“ฆ version 18.6, in opensuse-leap 16.0

โœจ ALTER AGGREGATE(7) โœจ

okay so bestie, we're in the PostgreSQL 18.6 Documentation and today's main character is ALTER_AGGREGATE โ€” the glow-up page for your aggregate functions ๐Ÿ’…


๐ŸŒธ NAME ๐ŸŒธ

ALTER_AGGREGATE โ€” change the definition of an aggregate function


๐ŸŽ€ 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 [ , ... ]

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

So like, ALTER AGGREGATE changes the definition of an aggregate function. That's the whole vibe. Rename it, re-own it, move it to a new schema โ€” she contains multitudes ๐Ÿ’–

Now the rules, and listen, because these matter:

  • You must own the aggregate function to use ALTER AGGREGATE. no cap.
  • Wanna change the schema of an aggregate function? You also need CREATE privilege on the new schema. both things, bestie.
  • Wanna alter the owner? You have to be able to SET ROLE to the new owning role, and that role must have CREATE privilege on the aggregate function's schema. yes, both.
Why all the gatekeeping? These restrictions enforce that altering the owner doesn't do anything you couldn't do by dropping and recreating the aggregate function. It's giving "no sneaky shortcuts" ๐Ÿ™ˆ

But also โ€” plot twist โ€” a superuser can alter ownership of any aggregate function anyway. main character energy, honestly ๐Ÿ‘‘


โœจ PARAMETERS โœจ

  • name โ€” the name (optionally schema-qualified) of an existing aggregate function. yeah, she has to already exist, we're not manifesting here ๐Ÿ’ซ
  • argmode โ€” the mode of an argument: IN or VARIADIC. If you justโ€ฆ don't say anything? The default is IN. she's chill like that.
  • argname โ€” the name of an argument. lowkey important tea: 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 like, don't stress about it ๐Ÿซถ
  • argtype โ€” an input data type on which the aggregate function operates. And here's the spicy part:
    • 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. iconic separator behavior ๐ŸŒˆ
  • new_name โ€” the new name of the aggregate function. rebranding era ๐Ÿ’…
  • new_owner โ€” the new owner of the aggregate function. passing the crown ๐Ÿ‘‘
  • new_schema โ€” the new schema for the aggregate function. she's moving out ๐Ÿงณ

๐Ÿ’œ NOTES ๐Ÿ’œ

The recommended syntax for referencing an ordered-set aggregate is to write ORDER BY between the direct and aggregated argument specifications โ€” same style as in CREATE AGGREGATE. consistency queen โœจ

However! 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.

โš ๏ธ heads up for the abbreviated form: if VARIADIC "any" was used in both the direct and aggregated argument lists, write VARIADIC "any" only once. not twice. once. fr fr.

๐Ÿ”ฅ EXAMPLES ๐Ÿ”ฅ

Rename the aggregate function myavg for type integer to my_average:

ALTER AGGREGATE myavg(integer) RENAME TO my_average;

Change the owner of the aggregate function myavg for type integer to joe:

ALTER AGGREGATE myavg(integer) OWNER TO joe;

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 bestie, this will work too:

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

see? both slay ๐Ÿ’–


๐ŸŒˆ COMPATIBILITY ๐ŸŒˆ

There is no ALTER AGGREGATE statement in the SQL standard. we're built different, it's fine ๐Ÿซถ


๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘


and that's the whole page, bestie ๐Ÿ’œ go forth and alter those aggregates responsibly.

signing off from <strong>PostgreSQL 18.6</strong>, dated <strong>2026</strong>, in the one and only <strong>ALTER AGGREGATE(7)</strong> โ€” mwah ๐ŸŽ€โœจ

๐Ÿ“ฆ where this page comes from