CREATE_AGGREGATE(7pg13)

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

πŸ’– CREATE_AGGREGATE(7pg13) β€” define a new aggregate function πŸ’–

PostgreSQL 13.23 Documentation Β· manual page CREATE_AGGREGATE(7) Β· bestie, we’re going through the whole thing, no skips πŸ’…

🌸 NAME 🌸

CREATE_AGGREGATE β€” define a new aggregate function. That’s the whole assignment: teach Postgres a brand-new way to turn a pile of rows into one iconic answer. ✨

πŸ“œ SYNOPSIS πŸ“œ

Okay so here’s the exact spellbook. Do not freestyle the punctuation β€” Postgres is dramatic about that. 🫢

Form one, the classic simple-aggregate vibe:

CREATE [ OR REPLACE ] AGGREGATE name ( [ argmode ] [ argname ] arg_data_type [ , ... ] ) (
    SFUNC = sfunc,
    STYPE = state_data_type
    [ , SSPACE = state_data_size ]
    [ , FINALFUNC = ffunc ]
    [ , FINALFUNC_EXTRA ]
    [ , FINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE } ]
    [ , COMBINEFUNC = combinefunc ]
    [ , SERIALFUNC = serialfunc ]
    [ , DESERIALFUNC = deserialfunc ]
    [ , INITCOND = initial_condition ]
    [ , MSFUNC = msfunc ]
    [ , MINVFUNC = minvfunc ]
    [ , MSTYPE = mstate_data_type ]
    [ , MSSPACE = mstate_data_size ]
    [ , MFINALFUNC = mffunc ]
    [ , MFINALFUNC_EXTRA ]
    [ , MFINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE } ]
    [ , MINITCOND = minitial_condition ]
    [ , SORTOP = sort_operator ]
    [ , PARALLEL = { SAFE | RESTRICTED | UNSAFE } ]
)

Form two, the ordered-set / hypothetical-set moment:

CREATE [ OR REPLACE ] AGGREGATE name ( [ [ argmode ] [ argname ] arg_data_type [ , ... ] ]
                        ORDER BY [ argmode ] [ argname ] arg_data_type [ , ... ] ) (
    SFUNC = sfunc,
    STYPE = state_data_type
    [ , SSPACE = state_data_size ]
    [ , FINALFUNC = ffunc ]
    [ , FINALFUNC_EXTRA ]
    [ , FINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE } ]
    [ , INITCOND = initial_condition ]
    [ , PARALLEL = { SAFE | RESTRICTED | UNSAFE } ]
    [ , HYPOTHETICAL ]
)

or the old syntax β€” yes, it’s giving legacy, but it’s still valid:

CREATE [ OR REPLACE ] AGGREGATE name (
    BASETYPE = base_type,
    SFUNC = sfunc,
    STYPE = state_data_type
    [ , SSPACE = state_data_size ]
    [ , FINALFUNC = ffunc ]
    [ , FINALFUNC_EXTRA ]
    [ , FINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE } ]
    [ , COMBINEFUNC = combinefunc ]
    [ , SERIALFUNC = serialfunc ]
    [ , DESERIALFUNC = deserialfunc ]
    [ , INITCOND = initial_condition ]
    [ , MSFUNC = msfunc ]
    [ , MINVFUNC = minvfunc ]
    [ , MSTYPE = mstate_data_type ]
    [ , MSSPACE = mstate_data_size ]
    [ , MFINALFUNC = mffunc ]
    [ , MFINALFUNC_EXTRA ]
    [ , MFINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE } ]
    [ , MINITCOND = minitial_condition ]
    [ , SORTOP = sort_operator ]
)

πŸ¦‹ DESCRIPTION πŸ¦‹

CREATE AGGREGATE defines a new aggregate function. CREATE OR REPLACE AGGREGATE is the double-duty queen: it either defines a new aggregate function or replaces an existing definition. Some basic and commonly-used aggregate functions are included with the distribution; they are documented in Section 9.21. If you’re defining new types, or you need an aggregate function that isn’t already provided, then CREATE AGGREGATE is how you supply the desired features. Periodt. πŸ‘‘

When you’re replacing an existing definition, the argument types, result type, and number of direct arguments may not be changed. Also, the new definition must be of the same kind β€” ordinary aggregate, ordered-set aggregate, or hypothetical-set aggregate β€” as the old one. No sneaky little glow-up where the signature changes. πŸ™ˆ

If a schema name is given (for example, CREATE AGGREGATE myschema.myagg ...) then the aggregate function is created in the specified schema. Otherwise it is created in the current schema. Simple as that. ✨

An aggregate function is identified by its name and input data type(s). Two aggregates in the same schema can have the same name if they operate on different input types. The name and input data type(s) of an aggregate must also be distinct from the name and input data type(s) of every ordinary function in the same schema. This behavior is identical to overloading of ordinary function names (see CREATE FUNCTION (CREATE_FUNCTION(7))). Overloading really said β€œI’m the main character.” πŸ’…

A simple aggregate function is made from one or two ordinary functions: a state transition function sfunc, and an optional final calculation function ffunc. These are used as follows:

sfunc( internal-state, next-data-values ) ---> next-internal-state
ffunc( internal-state ) ---> aggregate-value

PostgreSQL creates a temporary variable of data type stype to hold the current internal state of the aggregate. At each input row, the aggregate argument value(s) are calculated and the state transition function is invoked with the current state value and the new argument value(s) to calculate a new internal state value. After all the rows have been processed, the final function is invoked once to calculate the aggregate’s return value. If there is no final function, then the ending state value is returned as-is. Lowkey elegant. 🌸

An aggregate function can provide an initial condition, that is, an initial value for the internal state value. This is specified and stored in the database as a value of type text, but it must be a valid external representation of a constant of the state value data type. If it is not supplied, then the state value starts out null. 🫧

If the state transition function is declared β€œstrict”, then it cannot be called with null inputs. With such a transition function, aggregate execution behaves as follows. Rows with any null input values are ignored β€” the function is not called and the previous state value is retained. If the initial state value is null, then at the first row with all-nonnull input values, the first argument value replaces the state value, and the transition function is invoked at each subsequent row with all-nonnull input values. This is handy for implementing aggregates like max. Note that this behavior is only available when state_data_type is the same as the first arg_data_type. When these types are different, you must supply a nonnull initial condition or use a nonstrict transition function. Okay so: same type or it’s not doing the magic. ⭐

If the state transition function is not strict, then it will be called unconditionally at each input row, and must deal with null inputs and null state values for itself. This allows the aggregate author to have full control over the aggregate’s handling of null values. Null handling: bespoke, on purpose. πŸŽ€

If the final function is declared β€œstrict”, then it will not be called when the ending state value is null; instead a null result will be returned automatically. (Of course this is just the normal behavior of strict functions.) In any case the final function has the option of returning a null value. For example, the final function for avg returns null when it sees there were zero input rows. Zero rows? Null. That’s the rule. πŸ’€

Sometimes it is useful to declare the final function as taking not just the state value, but extra parameters corresponding to the aggregate’s input values. The main reason for doing this is if the final function is polymorphic and the state value’s data type would be inadequate to pin down the result type. These extra parameters are always passed as NULL (and so the final function must not be strict when the FINALFUNC_EXTRA option is used), but nonetheless they are valid parameters. The final function could, for example, make use of get_fn_expr_argtype to identify the actual argument type in the current call. Extra args but make them NULL β€” chaotic, but valid. πŸŒ€

An aggregate can optionally support moving-aggregate mode, as described in Section 37.12.1. This requires specifying the MSFUNC, MINVFUNC, and MSTYPE parameters, and optionally the MSSPACE, MFINALFUNC, MFINALFUNC_EXTRA, MFINALFUNC_MODIFY, and MINITCOND parameters. Except for MINVFUNC, these parameters work like the corresponding simple-aggregate parameters without M; they define a separate implementation of the aggregate that includes an inverse transition function. Moving aggregate mode is giving β€œI can go backward too.” πŸ”₯

The syntax with ORDER BY in the parameter list creates a special type of aggregate called an ordered-set aggregate; or if HYPOTHETICAL is specified, then a hypothetical-set aggregate is created. These aggregates operate over groups of sorted values in order-dependent ways, so that specification of an input sort order is an essential part of a call. Also, they can have direct arguments, which are arguments that are evaluated only once per aggregation rather than once per input row. Hypothetical-set aggregates are a subclass of ordered-set aggregates in which some of the direct arguments are required to match, in number and data types, the aggregated argument columns. This allows the values of those direct arguments to be added to the collection of aggregate-input rows as an additional β€œhypothetical” row. Direct arguments: evaluated once, not per row. We love efficiency. 🧚

An aggregate can optionally support partial aggregation, as described in Section 37.12.4. This requires specifying the COMBINEFUNC parameter. If the state_data_type is internal, it’s usually also appropriate to provide the SERIALFUNC and DESERIALFUNC parameters so that parallel aggregation is possible. Note that the aggregate must also be marked PARALLEL SAFE to enable parallel aggregation. Parallel aggregation said β€œmark me PARALLEL SAFE or I’m not showing up.” 🌈

Aggregates that behave like MIN or MAX can sometimes be optimized by looking into an index instead of scanning every input row. If this aggregate can be so optimized, indicate it by specifying a sort operator. The basic requirement is that the aggregate must yield the first element in the sort ordering induced by the operator; in other words:

SELECT agg(col) FROM tab;

must be equivalent to:

SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;

Further assumptions are that the aggregate ignores null inputs, and that it delivers a null result if and only if there were no non-null inputs. Ordinarily, a data type’s < operator is the proper sort operator for MIN, and > is the proper sort operator for MAX. Note that the optimization will never actually take effect unless the specified operator is the β€œless than” or β€œgreater than” strategy member of a B-tree index operator class. Index shortcut only works if the operator is truly in its B-tree operator-class era. πŸ‘‘

To be able to create an aggregate function, you must have USAGE privilege on the argument types, the state type(s), and the return type, as well as EXECUTE privilege on the supporting functions. Privileges check: required, no cap. 🫢

πŸ’… PARAMETERS πŸ’…

okay bestie, this is the part where we go through literally every single knob you can turn. there are a LOT of them. do not panic. we go one by one, no skipping, because each one is doing something real ✨

  • name β€” the name of the aggregate function you're creating, and yes it can be schema-qualified. go off, put it in a schema. πŸ‘‘
  • argmode β€” the mode of an argument, and your choices are IN or VARIADIC. that's it. that's the whole list. (aggregate functions do not support OUT arguments, so don't even try it πŸ’€) if you just don't say anything, the default is IN. and important: only the last argument can be marked VARIADIC. one variadic king per aggregate, that's the rule.
  • argname β€” the name of an argument. rn this is only useful for documentation purposes, so it's very "for the aesthetic" of it all. if you omit it, the argument just has no name. nameless. free. πŸ¦‹
  • arg_data_type β€” an input data type that this aggregate function operates on. and here's the cute part: to create a zero-argument aggregate function, you write * in place of the whole list of argument specifications. (a famous example of one of these is count(*), so you've definitely used one before, iykyk.)
  • base_type β€” in the old syntax for CREATE AGGREGATE, the input data type gets specified by a basetype parameter instead of being written next to the aggregate name. look at the legacy syntax being all vintage and complicated πŸ™ˆ note that this syntax allows only one input parameter. and if you want a zero-argument aggregate function with this syntax, you specify the basetype as "ANY" β€” and that's "ANY" with quotes, not *. different thing!! also: ordered-set aggregates cannot be defined with the old syntax. periodt.
  • sfunc β€” the name of the state transition function, called for each input row. for a normal N-argument aggregate function, sfunc must take N+1 arguments β€” the first one being of type state_data_type and the rest matching the declared input data type(s) of the aggregate. the function must return a value of type state_data_type. think of it like: it takes the current state value plus the current input data value(s), and returns the next state value. glow-up for your state. ✨

for ordered-set (including hypothetical-set) aggregates, the state transition function receives only the current state value and the aggregated arguments β€” not the direct arguments. otherwise it's the same deal.

  • state_data_type β€” the data type for the aggregate's state value. that's the whole job. simple. iconic. πŸ’œ
  • state_data_size β€” the approximate average size (in bytes) of the aggregate's state value. if you omit this or set it to zero, a default estimate gets used based on the state_data_type. the planner uses this value to estimate how much memory a grouped aggregate query needs β€” so if you know your state is thicc, say so. πŸ“
  • ffunc β€” the name of the final function, called to compute the aggregate's result after all the input rows have been traversed. for a normal aggregate, this function must take a single argument of type state_data_type. the return data type of the aggregate is defined as the return type of this function β€” so literally whatever ffunc returns is your aggregate's type. if ffunc isn't specified, then the ending state value is used as the aggregate's result, and the return type is state_data_type. no final function? no problem, the state carries it home. 🫢

for ordered-set (including hypothetical-set) aggregates, the final function receives not only the final state value, but also the values of all the direct arguments.

if FINALFUNC_EXTRA is specified, then on top of the final state value and any direct arguments, the final function also receives extra NULL values corresponding to the aggregate's regular (aggregated) arguments. mainly useful to allow correct resolution of the aggregate result type when you're defining a polymorphic aggregate. very "here's some extra nulls, don't ask, it's for type reasons" vibes.

  • FINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE } β€” this one specifies whether the final function is a pure function that does not modify its arguments. READ_ONLY means it does not; the other two values mean it may change the transition state value. see Notes below for more detail (go read them, bestie, they're relevant). the default is READ_ONLY β€” except for ordered-set aggregates, where the default is READ_WRITE. sneakily different defaults, keep that in mind 😭
  • combinefunc β€” the combinefunc function may optionally be specified to let the aggregate function support partial aggregation. if you provide it, combinefunc must combine two state_data_type values β€” each one containing the result of aggregation over some subset of the input values β€” to produce a new state_data_type that represents the result of aggregating over both sets of inputs. you can think of it as an sfunc, but instead of acting on an individual input row and adding it to the running aggregate state, it adds another aggregate state to the running state. states combining with states. very main character energy. πŸ”₯

the combinefunc must be declared as taking two arguments of the state_data_type and returning a value of the state_data_type. optionally this function may be "strict" β€” in which case it won't be called when either of the input states are null, and the other state gets taken as the correct result. nice and chill.

for aggregate functions whose state_data_type is internal, the combinefunc must not be strict. in that case combinefunc has to make sure null states are handled correctly and that the state being returned is properly stored in the aggregate memory context. not optional, this one matters.

  • serialfunc β€” an aggregate function whose state_data_type is internal can participate in parallel aggregation only if it has a serialfunc function, which must serialize the aggregate state into a bytea value so it can be transmitted to another process. this function must take a single argument of type internal and return type bytea. and a corresponding deserialfunc is also required β€” you can't just have half the pair, that's not how any of this works. 🧚
  • deserialfunc β€” deserialize a previously serialized aggregate state back into state_data_type. this function must take two arguments of types bytea and internal, and produce a result of type internal. (note: that second, internal argument is unused β€” but it's required for type safety reasons. yes, it's a decorative argument. it's there for the ~vibes~ of type safety. πŸ™ˆ)
  • initial_condition β€” the initial setting for the state value. this must be a string constant in the form accepted for the data type state_data_type. if it's not specified, the state value starts out null. blank slate, fresh start, no baggage. 🌸
  • msfunc β€” the name of the forward state transition function to be called for each input row in moving-aggregate mode. this is exactly like the regular transition function, except that its first argument and its result are of type mstate_data_type, which might be different from state_data_type. same energy, different type. ✨
  • minvfunc β€” the name of the inverse state transition function, to be used in moving-aggregate mode. same argument and result types as msfunc, but it's used to remove a value from the current aggregate state rather than add a value to it. subtraction era. and heads up: the inverse transition function must have the same strictness attribute as the forward state transition function. matching strictness, always. πŸ’…
  • mstate_data_type β€” the data type for the aggregate's state value, when you're using moving-aggregate mode.
  • mstate_data_size β€” the approximate average size (in bytes) of the aggregate's state value when using moving-aggregate mode. works the same as state_data_size. byte for byte, bestie.
  • mffunc β€” the name of the final function called to compute the aggregate's result after all input rows have been traversed, when using moving-aggregate mode. works the same as ffunc, except its first argument's type is mstate_data_type, and extra dummy arguments are specified by writing MFINALFUNC_EXTRA. and the aggregate result type determined by mffunc or mstate_data_type must match that determined by the aggregate's regular implementation. they have to agree, no cap. 🀝
  • MFINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE } β€” this option is like FINALFUNC_MODIFY, but it describes the behavior of the moving-aggregate final function. same rules, different function. πŸŽ€
  • minitial_condition β€” the initial setting for the state value, when using moving-aggregate mode. works the same as initial_condition. we love consistency.
  • sort_operator β€” the associated sort operator for a MIN- or MAX-like aggregate. this is just an operator name (possibly schema-qualified). the operator is assumed to have the same input data types as the aggregate, which must be a single-argument normal aggregate. so: one argument, normal aggregate, or it doesn't apply. πŸ”
  • PARALLEL = { SAFE | RESTRICTED | UNSAFE } β€” the meanings of PARALLEL SAFE, PARALLEL RESTRICTED, and PARALLEL UNSAFE are the same as in CREATE FUNCTION (CREATE_FUNCTION(7)). an aggregate won't be considered for parallelization if it's marked PARALLEL UNSAFE (which is the default!) or PARALLEL RESTRICTED. and ngl this part trips people up: the parallel-safety markings of the aggregate's support functions are not consulted by the planner β€” only the marking of the aggregate itself. so mark the aggregate, not just its little helpers. πŸ’€
  • HYPOTHETICAL β€” for ordered-set aggregates only. this flag specifies that the aggregate arguments are to be processed according to the requirements for hypothetical-set aggregates: that is, the last few direct arguments must match the data types of the aggregated (WITHIN GROUP) arguments. important caveat: the HYPOTHETICAL flag has no effect on run-time behavior β€” it only affects parse-time resolution of the data types and collations of the aggregate's arguments. so it's like a purely administrative vibe check at parse time. πŸ‘‘

and one last thing before we move on: the parameters of CREATE AGGREGATE can be written in any order, not just the order illustrated above. so go ahead, shuffle them, put sort_operator first, live your truth. the parser does not care, bestie πŸ¦‹

🌸 NOTES 🌸

Okay so listen, bestie πŸ’– in the parameters where you're naming support functions, you can write a schema name if you need to, like SFUNC = public.sum. But do not write argument types there, okay? The argument types of the support functions are determined from the other parameters, periodt.

Normally, PostgreSQL functions are expected to be true functions that do not modify their input values. But an aggregate transition function, when it's used in the context of an aggregate, is allowed to cheat and modify its transition-state argument in place 😌 and honestly? This can give you substantial performance benefits compared to making a fresh copy of the transition state every single time. Slay for efficiency πŸ’…

Same vibe for the final function: while an aggregate final function is normally expected not to modify its input values, sometimes it is impractical to avoid modifying the transition-state argument. That behavior has to be declared using the FINALFUNC_MODIFY parameter. The READ_WRITE value indicates that the final function modifies the transition state in unspecified ways 😬 This value prevents use of the aggregate as a window function, and it also prevents merging of transition states for aggregate calls that share the same input values and transition functions. The SHAREABLE value indicates that the transition function cannot be applied after the final function, but multiple final-function calls can be performed on the ending transition state value. This value prevents use of the aggregate as a window function, but it allows merging of transition states. (That is, the optimization of interest here is not applying the same final function repeatedly, but applying different final functions to the same ending transition state value. This is allowed as long as none of the final functions are marked READ_WRITE.) 🫢


If an aggregate supports moving-aggregate mode, it will improve calculation efficiency when the aggregate is used as a window function for a window with moving frame start (that is, a frame start mode other than UNBOUNDED PRECEDING). Conceptually, the forward transition function adds input values to the aggregate's state when they enter the window frame from the bottom, and the inverse transition function removes them again when they leave the frame at the top. So, when values are removed, they are always removed in the same order they were added. Whenever the inverse transition function is invoked, it will thus receive the earliest added but not yet removed argument value(s). The inverse transition function can assume that at least one row will remain in the current state after it removes the oldest row. (When this would not be the case, the window function mechanism simply starts a fresh aggregation, rather than using the inverse transition function.) ✨

The forward transition function for moving-aggregate mode is not allowed to return NULL as the new state value. If the inverse transition function returns NULL, this is taken as an indication that the inverse function cannot reverse the state calculation for this particular input, and so the aggregate calculation will be redone from scratch for the current frame starting position. This convention allows moving-aggregate mode to be used in situations where there are some infrequent cases that are impractical to reverse out of the running state value. Lowkey iconic behavior tbh πŸ’€

If no moving-aggregate implementation is supplied, the aggregate can still be used with moving frames, but PostgreSQL will recompute the whole aggregation whenever the start of the frame moves. Note that whether or not the aggregate supports moving-aggregate mode, PostgreSQL can handle a moving frame end without recalculation; this is done by continuing to add new values to the aggregate's state. This is why use of an aggregate as a window function requires that the final function be read-only: it must not damage the aggregate's state value, so that the aggregation can be continued even after an aggregate result value has been obtained for one set of frame boundaries. No cap 🧒


The syntax for ordered-set aggregates allows VARIADIC to be specified for both the last direct parameter and the last aggregated (WITHIN GROUP) parameter. However, the current implementation restricts use of VARIADIC in two ways. First, ordered-set aggregates can only use VARIADIC "any", not other variadic array types. Second, if the last direct parameter is VARIADIC "any", then there can be only one aggregated parameter and it must also be VARIADIC "any". (In the representation used in the system catalogs, these two parameters are merged into a single VARIADIC "any" item, since pg_proc cannot represent functions with more than one VARIADIC parameter.) If the aggregate is a hypothetical-set aggregate, the direct arguments that match the VARIADIC "any" parameter are the hypothetical ones; any preceding parameters represent additional direct arguments that are not constrained to match the aggregated arguments. It's giving rules-on-rules but make it make sense 🌈

Currently, ordered-set aggregates do not need to support moving-aggregate mode, since they cannot be used as window functions. Okay werk πŸ‘‘

Partial (including parallel) aggregation is currently not supported for ordered-set aggregates. Also, it will never be used for aggregate calls that include DISTINCT or ORDER BY clauses, since those semantics cannot be supported during partial aggregation. Not me pointing at the fine print πŸ™ˆ

πŸ’– EXAMPLES πŸ’–

Literally just: See Section 37.12. That's the whole section, bestie πŸŽ€

🌟 COMPATIBILITY 🌟

CREATE AGGREGATE is a PostgreSQL language extension. The SQL standard does not provide for user-defined aggregate functions. So yeah, this is a PostgreSQL main-character move πŸ‘‘βœ¨

πŸ¦‹ SEE ALSO πŸ¦‹

ALTER AGGREGATE (ALTER_AGGREGATE(7)), DROP AGGREGATE (DROP_AGGREGATE(7))

Signed off with so much love from the PostgreSQL 13.23 manual, dated 2025 β€” stay slaying, bestie πŸ’…πŸŽ€πŸ’–

πŸ“¦ where this page comes from