2 36.12. User-Defined Aggregates #
4 36.12.1. Moving-Aggregate Mode
5 36.12.2. Polymorphic and Variadic Aggregates
6 36.12.3. Ordered-Set Aggregates
7 36.12.4. Partial Aggregation
8 36.12.5. Support Functions for Aggregates
10 Aggregate functions in PostgreSQL are defined in terms of state values
11 and state transition functions. That is, an aggregate operates using a
12 state value that is updated as each successive input row is processed.
13 To define a new aggregate function, one selects a data type for the
14 state value, an initial value for the state, and a state transition
15 function. The state transition function takes the previous state value
16 and the aggregate's input value(s) for the current row, and returns a
17 new state value. A final function can also be specified, in case the
18 desired result of the aggregate is different from the data that needs
19 to be kept in the running state value. The final function takes the
20 ending state value and returns whatever is wanted as the aggregate
21 result. In principle, the transition and final functions are just
22 ordinary functions that could also be used outside the context of the
23 aggregate. (In practice, it's often helpful for performance reasons to
24 create specialized transition functions that can only work when called
25 as part of an aggregate.)
27 Thus, in addition to the argument and result data types seen by a user
28 of the aggregate, there is an internal state-value data type that might
29 be different from both the argument and result types.
31 If we define an aggregate that does not use a final function, we have
32 an aggregate that computes a running function of the column values from
33 each row. sum is an example of this kind of aggregate. sum starts at
34 zero and always adds the current row's value to its running total. For
35 example, if we want to make a sum aggregate to work on a data type for
36 complex numbers, we only need the addition function for that data type.
37 The aggregate definition would be:
38 CREATE AGGREGATE sum (complex)
45 which we might use like this:
46 SELECT sum(a) FROM test_complex;
52 (Notice that we are relying on function overloading: there is more than
53 one aggregate named sum, but PostgreSQL can figure out which kind of
54 sum applies to a column of type complex.)
56 The above definition of sum will return zero (the initial state value)
57 if there are no nonnull input values. Perhaps we want to return null in
58 that case instead — the SQL standard expects sum to behave that way. We
59 can do this simply by omitting the initcond phrase, so that the initial
60 state value is null. Ordinarily this would mean that the sfunc would
61 need to check for a null state-value input. But for sum and some other
62 simple aggregates like max and min, it is sufficient to insert the
63 first nonnull input value into the state variable and then start
64 applying the transition function at the second nonnull input value.
65 PostgreSQL will do that automatically if the initial state value is
66 null and the transition function is marked “strict” (i.e., not to be
67 called for null inputs).
69 Another bit of default behavior for a “strict” transition function is
70 that the previous state value is retained unchanged whenever a null
71 input value is encountered. Thus, null values are ignored. If you need
72 some other behavior for null inputs, do not declare your transition
73 function as strict; instead code it to test for null inputs and do
76 avg (average) is a more complex example of an aggregate. It requires
77 two pieces of running state: the sum of the inputs and the count of the
78 number of inputs. The final result is obtained by dividing these
79 quantities. Average is typically implemented by using an array as the
80 state value. For example, the built-in implementation of avg(float8)
82 CREATE AGGREGATE avg (float8)
86 finalfunc = float8_avg,
92 float8_accum requires a three-element array, not just two elements,
93 because it accumulates the sum of squares as well as the sum and count
94 of the inputs. This is so that it can be used for some other aggregates
97 Aggregate function calls in SQL allow DISTINCT and ORDER BY options
98 that control which rows are fed to the aggregate's transition function
99 and in what order. These options are implemented behind the scenes and
100 are not the concern of the aggregate's support functions.
102 For further details see the CREATE AGGREGATE command.
104 36.12.1. Moving-Aggregate Mode #
106 Aggregate functions can optionally support moving-aggregate mode, which
107 allows substantially faster execution of aggregate functions within
108 windows with moving frame starting points. (See Section 3.5 and
109 Section 4.2.8 for information about use of aggregate functions as
110 window functions.) The basic idea is that in addition to a normal
111 “forward” transition function, the aggregate provides an inverse
112 transition function, which allows rows to be removed from the
113 aggregate's running state value when they exit the window frame. For
114 example a sum aggregate, which uses addition as the forward transition
115 function, would use subtraction as the inverse transition function.
116 Without an inverse transition function, the window function mechanism
117 must recalculate the aggregate from scratch each time the frame
118 starting point moves, resulting in run time proportional to the number
119 of input rows times the average frame length. With an inverse
120 transition function, the run time is only proportional to the number of
123 The inverse transition function is passed the current state value and
124 the aggregate input value(s) for the earliest row included in the
125 current state. It must reconstruct what the state value would have been
126 if the given input row had never been aggregated, but only the rows
127 following it. This sometimes requires that the forward transition
128 function keep more state than is needed for plain aggregation mode.
129 Therefore, the moving-aggregate mode uses a completely separate
130 implementation from the plain mode: it has its own state data type, its
131 own forward transition function, and its own final function if needed.
132 These can be the same as the plain mode's data type and functions, if
133 there is no need for extra state.
135 As an example, we could extend the sum aggregate given above to support
136 moving-aggregate mode like this:
137 CREATE AGGREGATE sum (complex)
142 msfunc = complex_add,
143 minvfunc = complex_sub,
148 The parameters whose names begin with m define the moving-aggregate
149 implementation. Except for the inverse transition function minvfunc,
150 they correspond to the plain-aggregate parameters without m.
152 The forward transition function for moving-aggregate mode is not
153 allowed to return null as the new state value. If the inverse
154 transition function returns null, this is taken as an indication that
155 the inverse function cannot reverse the state calculation for this
156 particular input, and so the aggregate calculation will be redone from
157 scratch for the current frame starting position. This convention allows
158 moving-aggregate mode to be used in situations where there are some
159 infrequent cases that are impractical to reverse out of the running
160 state value. The inverse transition function can “punt” on these cases,
161 and yet still come out ahead so long as it can work for most cases. As
162 an example, an aggregate working with floating-point numbers might
163 choose to punt when a NaN (not a number) input has to be removed from
164 the running state value.
166 When writing moving-aggregate support functions, it is important to be
167 sure that the inverse transition function can reconstruct the correct
168 state value exactly. Otherwise there might be user-visible differences
169 in results depending on whether the moving-aggregate mode is used. An
170 example of an aggregate for which adding an inverse transition function
171 seems easy at first, yet where this requirement cannot be met is sum
172 over float4 or float8 inputs. A naive declaration of sum(float8) could
174 CREATE AGGREGATE unsafe_sum (float8)
183 This aggregate, however, can give wildly different results than it
184 would have without the inverse transition function. For example,
187 unsafe_sum(x) OVER (ORDER BY n ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING)
188 FROM (VALUES (1, 1.0e20::float8),
189 (2, 1.0::float8)) AS v (n,x);
191 This query returns 0 as its second result, rather than the expected
192 answer of 1. The cause is the limited precision of floating-point
193 values: adding 1 to 1e20 results in 1e20 again, and so subtracting 1e20
194 from that yields 0, not 1. Note that this is a limitation of
195 floating-point arithmetic in general, not a limitation of PostgreSQL.
197 36.12.2. Polymorphic and Variadic Aggregates #
199 Aggregate functions can use polymorphic state transition functions or
200 final functions, so that the same functions can be used to implement
201 multiple aggregates. See Section 36.2.5 for an explanation of
202 polymorphic functions. Going a step further, the aggregate function
203 itself can be specified with polymorphic input type(s) and state type,
204 allowing a single aggregate definition to serve for multiple input data
205 types. Here is an example of a polymorphic aggregate:
206 CREATE AGGREGATE array_accum (anycompatible)
208 sfunc = array_append,
209 stype = anycompatiblearray,
213 Here, the actual state type for any given aggregate call is the array
214 type having the actual input type as elements. The behavior of the
215 aggregate is to concatenate all the inputs into an array of that type.
216 (Note: the built-in aggregate array_agg provides similar functionality,
217 with better performance than this definition would have.)
219 Here's the output using two different actual data types as arguments:
220 SELECT attrelid::regclass, array_accum(attname)
222 WHERE attnum > 0 AND attrelid = 'pg_tablespace'::regclass
225 attrelid | array_accum
226 ---------------+---------------------------------------
227 pg_tablespace | {spcname,spcowner,spcacl,spcoptions}
230 SELECT attrelid::regclass, array_accum(atttypid::regtype)
232 WHERE attnum > 0 AND attrelid = 'pg_tablespace'::regclass
235 attrelid | array_accum
236 ---------------+---------------------------
237 pg_tablespace | {name,oid,aclitem[],text[]}
240 Ordinarily, an aggregate function with a polymorphic result type has a
241 polymorphic state type, as in the above example. This is necessary
242 because otherwise the final function cannot be declared sensibly: it
243 would need to have a polymorphic result type but no polymorphic
244 argument type, which CREATE FUNCTION will reject on the grounds that
245 the result type cannot be deduced from a call. But sometimes it is
246 inconvenient to use a polymorphic state type. The most common case is
247 where the aggregate support functions are to be written in C and the
248 state type should be declared as internal because there is no SQL-level
249 equivalent for it. To address this case, it is possible to declare the
250 final function as taking extra “dummy” arguments that match the input
251 arguments of the aggregate. Such dummy arguments are always passed as
252 null values since no specific value is available when the final
253 function is called. Their only use is to allow a polymorphic final
254 function's result type to be connected to the aggregate's input
255 type(s). For example, the definition of the built-in aggregate
256 array_agg is equivalent to
257 CREATE FUNCTION array_agg_transfn(internal, anynonarray)
258 RETURNS internal ...;
259 CREATE FUNCTION array_agg_finalfn(internal, anynonarray)
260 RETURNS anyarray ...;
262 CREATE AGGREGATE array_agg (anynonarray)
264 sfunc = array_agg_transfn,
266 finalfunc = array_agg_finalfn,
270 Here, the finalfunc_extra option specifies that the final function
271 receives, in addition to the state value, extra dummy argument(s)
272 corresponding to the aggregate's input argument(s). The extra
273 anynonarray argument allows the declaration of array_agg_finalfn to be
276 An aggregate function can be made to accept a varying number of
277 arguments by declaring its last argument as a VARIADIC array, in much
278 the same fashion as for regular functions; see Section 36.5.6. The
279 aggregate's transition function(s) must have the same array type as
280 their last argument. The transition function(s) typically would also be
281 marked VARIADIC, but this is not strictly required.
285 Variadic aggregates are easily misused in connection with the ORDER BY
286 option (see Section 4.2.7), since the parser cannot tell whether the
287 wrong number of actual arguments have been given in such a combination.
288 Keep in mind that everything to the right of ORDER BY is a sort key,
289 not an argument to the aggregate. For example, in
290 SELECT myaggregate(a ORDER BY a, b, c) FROM ...
292 the parser will see this as a single aggregate function argument and
293 three sort keys. However, the user might have intended
294 SELECT myaggregate(a, b, c ORDER BY a) FROM ...
296 If myaggregate is variadic, both these calls could be perfectly valid.
298 For the same reason, it's wise to think twice before creating aggregate
299 functions with the same names and different numbers of regular
302 36.12.3. Ordered-Set Aggregates #
304 The aggregates we have been describing so far are “normal” aggregates.
305 PostgreSQL also supports ordered-set aggregates, which differ from
306 normal aggregates in two key ways. First, in addition to ordinary
307 aggregated arguments that are evaluated once per input row, an
308 ordered-set aggregate can have “direct” arguments that are evaluated
309 only once per aggregation operation. Second, the syntax for the
310 ordinary aggregated arguments specifies a sort ordering for them
311 explicitly. An ordered-set aggregate is usually used to implement a
312 computation that depends on a specific row ordering, for instance rank
313 or percentile, so that the sort ordering is a required aspect of any
314 call. For example, the built-in definition of percentile_disc is
316 CREATE FUNCTION ordered_set_transition(internal, anyelement)
317 RETURNS internal ...;
318 CREATE FUNCTION percentile_disc_final(internal, float8, anyelement)
319 RETURNS anyelement ...;
321 CREATE AGGREGATE percentile_disc (float8 ORDER BY anyelement)
323 sfunc = ordered_set_transition,
325 finalfunc = percentile_disc_final,
329 This aggregate takes a float8 direct argument (the percentile fraction)
330 and an aggregated input that can be of any sortable data type. It could
331 be used to obtain a median household income like this:
332 SELECT percentile_disc(0.5) WITHIN GROUP (ORDER BY income) FROM households;
337 Here, 0.5 is a direct argument; it would make no sense for the
338 percentile fraction to be a value varying across rows.
340 Unlike the case for normal aggregates, the sorting of input rows for an
341 ordered-set aggregate is not done behind the scenes, but is the
342 responsibility of the aggregate's support functions. The typical
343 implementation approach is to keep a reference to a “tuplesort” object
344 in the aggregate's state value, feed the incoming rows into that
345 object, and then complete the sorting and read out the data in the
346 final function. This design allows the final function to perform
347 special operations such as injecting additional “hypothetical” rows
348 into the data to be sorted. While normal aggregates can often be
349 implemented with support functions written in PL/pgSQL or another PL
350 language, ordered-set aggregates generally have to be written in C,
351 since their state values aren't definable as any SQL data type. (In the
352 above example, notice that the state value is declared as type internal
353 — this is typical.) Also, because the final function performs the sort,
354 it is not possible to continue adding input rows by executing the
355 transition function again later. This means the final function is not
356 READ_ONLY; it must be declared in CREATE AGGREGATE as READ_WRITE, or as
357 SHAREABLE if it's possible for additional final-function calls to make
358 use of the already-sorted state.
360 The state transition function for an ordered-set aggregate receives the
361 current state value plus the aggregated input values for each row, and
362 returns the updated state value. This is the same definition as for
363 normal aggregates, but note that the direct arguments (if any) are not
364 provided. The final function receives the last state value, the values
365 of the direct arguments if any, and (if finalfunc_extra is specified)
366 null values corresponding to the aggregated input(s). As with normal
367 aggregates, finalfunc_extra is only really useful if the aggregate is
368 polymorphic; then the extra dummy argument(s) are needed to connect the
369 final function's result type to the aggregate's input type(s).
371 Currently, ordered-set aggregates cannot be used as window functions,
372 and therefore there is no need for them to support moving-aggregate
375 36.12.4. Partial Aggregation #
377 Optionally, an aggregate function can support partial aggregation. The
378 idea of partial aggregation is to run the aggregate's state transition
379 function over different subsets of the input data independently, and
380 then to combine the state values resulting from those subsets to
381 produce the same state value that would have resulted from scanning all
382 the input in a single operation. This mode can be used for parallel
383 aggregation by having different worker processes scan different
384 portions of a table. Each worker produces a partial state value, and at
385 the end those state values are combined to produce a final state value.
386 (In the future this mode might also be used for purposes such as
387 combining aggregations over local and remote tables; but that is not
390 To support partial aggregation, the aggregate definition must provide a
391 combine function, which takes two values of the aggregate's state type
392 (representing the results of aggregating over two subsets of the input
393 rows) and produces a new value of the state type, representing what the
394 state would have been after aggregating over the combination of those
395 sets of rows. It is unspecified what the relative order of the input
396 rows from the two sets would have been. This means that it's usually
397 impossible to define a useful combine function for aggregates that are
398 sensitive to input row order.
400 As simple examples, MAX and MIN aggregates can be made to support
401 partial aggregation by specifying the combine function as the same
402 greater-of-two or lesser-of-two comparison function that is used as
403 their transition function. SUM aggregates just need an addition
404 function as combine function. (Again, this is the same as their
405 transition function, unless the state value is wider than the input
408 The combine function is treated much like a transition function that
409 happens to take a value of the state type, not of the underlying input
410 type, as its second argument. In particular, the rules for dealing with
411 null values and strict functions are similar. Also, if the aggregate
412 definition specifies a non-null initcond, keep in mind that that will
413 be used not only as the initial state for each partial aggregation run,
414 but also as the initial state for the combine function, which will be
415 called to combine each partial result into that state.
417 If the aggregate's state type is declared as internal, it is the
418 combine function's responsibility that its result is allocated in the
419 correct memory context for aggregate state values. This means in
420 particular that when the first input is NULL it's invalid to simply
421 return the second input, as that value will be in the wrong context and
422 will not have sufficient lifespan.
424 When the aggregate's state type is declared as internal, it is usually
425 also appropriate for the aggregate definition to provide a
426 serialization function and a deserialization function, which allow such
427 a state value to be copied from one process to another. Without these
428 functions, parallel aggregation cannot be performed, and future
429 applications such as local/remote aggregation will probably not work
432 A serialization function must take a single argument of type internal
433 and return a result of type bytea, which represents the state value
434 packaged up into a flat blob of bytes. Conversely, a deserialization
435 function reverses that conversion. It must take two arguments of types
436 bytea and internal, and return a result of type internal. (The second
437 argument is unused and is always zero, but it is required for
438 type-safety reasons.) The result of the deserialization function should
439 simply be allocated in the current memory context, as unlike the
440 combine function's result, it is not long-lived.
442 Worth noting also is that for an aggregate to be executed in parallel,
443 the aggregate itself must be marked PARALLEL SAFE. The parallel-safety
444 markings on its support functions are not consulted.
446 36.12.5. Support Functions for Aggregates #
448 A function written in C can detect that it is being called as an
449 aggregate support function by calling AggCheckCallContext, for example:
450 if (AggCheckCallContext(fcinfo, NULL))
452 One reason for checking this is that when it is true, the first input
453 must be a temporary state value and can therefore safely be modified
454 in-place rather than allocating a new copy. See int8inc() for an
455 example. (While aggregate transition functions are always allowed to
456 modify the transition value in-place, aggregate final functions are
457 generally discouraged from doing so; if they do so, the behavior must
458 be declared when creating the aggregate. See CREATE AGGREGATE for more
461 The second argument of AggCheckCallContext can be used to retrieve the
462 memory context in which aggregate state values are being kept. This is
463 useful for transition functions that wish to use “expanded” objects
464 (see Section 36.13.1) as their state values. On first call, the
465 transition function should return an expanded object whose memory
466 context is a child of the aggregate state context, and then keep
467 returning the same expanded object on subsequent calls. See
468 array_append() for an example. (array_append() is not the transition
469 function of any built-in aggregate, but it is written to behave
470 efficiently when used as transition function of a custom aggregate.)
472 Another support routine available to aggregate functions written in C
473 is AggGetAggref, which returns the Aggref parse node that defines the
474 aggregate call. This is mainly useful for ordered-set aggregates, which
475 can inspect the substructure of the Aggref node to find out what sort
476 ordering they are supposed to implement. Examples can be found in
477 orderedsetaggs.c in the PostgreSQL source code.