ALTER_AGGREGATE(7pg17)
π ALTER AGGREGATE π
okay so bestie, quick vibe check β this is the PostgreSQL 17.11 Documentation page for ALTER AGGREGATE(7), and it's literally the glow-up page for your aggregate functions. no cap, it's giving "makeover montage" energy. let's get into it π
β¨ 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 π
ALTER AGGREGATE changes the definition of an aggregate function. that's it, that's the whole thing β it's the makeover, the rebrand, the "new year new me" for an existing aggregate β¨
but before you start editing, there are rules, and the rules are real:
- you must own the aggregate function to use
ALTER AGGREGATE. it's your aggregate, bestie. - 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.
why all the rules? because (and this is the whole point) these restrictions enforce that altering the owner doesn't do anything you couldn't do by dropping and recreating the aggregate function. like, no sneaky privilege laundering, okay? π
but 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.argmodeβ the mode of an argument:INorVARIADIC. if omitted, the default isIN. easy.argnameβ the name of an argument. and here's the tea: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 yeah, the name is decorative. cute but unemployed πargtypeβ an input data type on which the aggregate function operates. details below, because this one's doing the most:- 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. slay, rebranded π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. that's the iconic way, the way the universe intended π
however β and this is lowkey important β 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. don't be extra about it β¨
π₯ EXAMPLES π₯
okay bestie, real-world demonstrations, let's go:
1. rename the aggregate function myavg for type integer to my_average:
ALTER AGGREGATE myavg(integer) RENAME TO my_average;
2. change the owner of the aggregate function myavg for type integer to joe:
ALTER AGGREGATE myavg(integer) OWNER TO joe;
3. 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 β plot twist β this will work too:
ALTER AGGREGATE mypercentile(float8, integer) SET SCHEMA myschema;
see? both slay. the abbreviated form really said "same energy" π«Ά
π COMPATIBILITY π
there is no ALTER AGGREGATE statement in the SQL standard. so this is a PostgreSQL-exclusive little treat, iykyk π
π SEE ALSO π
CREATE AGGREGATE(CREATE_AGGREGATE(7))DROP AGGREGATE(DROP_AGGREGATE(7))
and that's the page, bestie π this iconic little doc is from PostgreSQL 17.11, dated 2026, and it lives at ALTER AGGREGATE(7) β go forth and rebrand your aggregates, you absolute legend π¦β¨π
π¦ where this page comes from
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-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