]> begriffs open source - ai-pg/blob - full-docs/src/sgml/man7/CREATE_AGGREGATE.7
WIP: toc builder
[ai-pg] / full-docs / src / sgml / man7 / CREATE_AGGREGATE.7
1 '\" t
2 .\"     Title: CREATE AGGREGATE
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
10 .TH "CREATE AGGREGATE" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 CREATE_AGGREGATE \- define a new aggregate function
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 CREATE [ OR REPLACE ] AGGREGATE \fIname\fR ( [ \fIargmode\fR ] [ \fIargname\fR ] \fIarg_data_type\fR [ , \&.\&.\&. ] ) (
36     SFUNC = \fIsfunc\fR,
37     STYPE = \fIstate_data_type\fR
38     [ , SSPACE = \fIstate_data_size\fR ]
39     [ , FINALFUNC = \fIffunc\fR ]
40     [ , FINALFUNC_EXTRA ]
41     [ , FINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE } ]
42     [ , COMBINEFUNC = \fIcombinefunc\fR ]
43     [ , SERIALFUNC = \fIserialfunc\fR ]
44     [ , DESERIALFUNC = \fIdeserialfunc\fR ]
45     [ , INITCOND = \fIinitial_condition\fR ]
46     [ , MSFUNC = \fImsfunc\fR ]
47     [ , MINVFUNC = \fIminvfunc\fR ]
48     [ , MSTYPE = \fImstate_data_type\fR ]
49     [ , MSSPACE = \fImstate_data_size\fR ]
50     [ , MFINALFUNC = \fImffunc\fR ]
51     [ , MFINALFUNC_EXTRA ]
52     [ , MFINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE } ]
53     [ , MINITCOND = \fIminitial_condition\fR ]
54     [ , SORTOP = \fIsort_operator\fR ]
55     [ , PARALLEL = { SAFE | RESTRICTED | UNSAFE } ]
56 )
57
58 CREATE [ OR REPLACE ] AGGREGATE \fIname\fR ( [ [ \fIargmode\fR ] [ \fIargname\fR ] \fIarg_data_type\fR [ , \&.\&.\&. ] ]
59                         ORDER BY [ \fIargmode\fR ] [ \fIargname\fR ] \fIarg_data_type\fR [ , \&.\&.\&. ] ) (
60     SFUNC = \fIsfunc\fR,
61     STYPE = \fIstate_data_type\fR
62     [ , SSPACE = \fIstate_data_size\fR ]
63     [ , FINALFUNC = \fIffunc\fR ]
64     [ , FINALFUNC_EXTRA ]
65     [ , FINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE } ]
66     [ , INITCOND = \fIinitial_condition\fR ]
67     [ , PARALLEL = { SAFE | RESTRICTED | UNSAFE } ]
68     [ , HYPOTHETICAL ]
69 )
70
71 or the old syntax
72
73 CREATE [ OR REPLACE ] AGGREGATE \fIname\fR (
74     BASETYPE = \fIbase_type\fR,
75     SFUNC = \fIsfunc\fR,
76     STYPE = \fIstate_data_type\fR
77     [ , SSPACE = \fIstate_data_size\fR ]
78     [ , FINALFUNC = \fIffunc\fR ]
79     [ , FINALFUNC_EXTRA ]
80     [ , FINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE } ]
81     [ , COMBINEFUNC = \fIcombinefunc\fR ]
82     [ , SERIALFUNC = \fIserialfunc\fR ]
83     [ , DESERIALFUNC = \fIdeserialfunc\fR ]
84     [ , INITCOND = \fIinitial_condition\fR ]
85     [ , MSFUNC = \fImsfunc\fR ]
86     [ , MINVFUNC = \fIminvfunc\fR ]
87     [ , MSTYPE = \fImstate_data_type\fR ]
88     [ , MSSPACE = \fImstate_data_size\fR ]
89     [ , MFINALFUNC = \fImffunc\fR ]
90     [ , MFINALFUNC_EXTRA ]
91     [ , MFINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE } ]
92     [ , MINITCOND = \fIminitial_condition\fR ]
93     [ , SORTOP = \fIsort_operator\fR ]
94 )
95 .fi
96 .SH "DESCRIPTION"
97 .PP
98 \fBCREATE AGGREGATE\fR
99 defines a new aggregate function\&.
100 \fBCREATE OR REPLACE AGGREGATE\fR
101 will either define a new aggregate function or replace an existing definition\&. Some basic and commonly\-used aggregate functions are included with the distribution; they are documented in
102 Section\ \&9.21\&. If one defines new types or needs an aggregate function not already provided, then
103 \fBCREATE AGGREGATE\fR
104 can be used to provide the desired features\&.
105 .PP
106 When 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\&.
107 .PP
108 If a schema name is given (for example,
109 CREATE AGGREGATE myschema\&.myagg \&.\&.\&.) then the aggregate function is created in the specified schema\&. Otherwise it is created in the current schema\&.
110 .PP
111 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
112 CREATE FUNCTION (\fBCREATE_FUNCTION\fR(7)))\&.
113 .PP
114 A simple aggregate function is made from one or two ordinary functions: a state transition function
115 \fIsfunc\fR, and an optional final calculation function
116 \fIffunc\fR\&. These are used as follows:
117 .sp
118 .if n \{\
119 .RS 4
120 .\}
121 .nf
122 \fIsfunc\fR( internal\-state, next\-data\-values ) \-\-\-> next\-internal\-state
123 \fIffunc\fR( internal\-state ) \-\-\-> aggregate\-value
124 .fi
125 .if n \{\
126 .RE
127 .\}
128 .PP
129 PostgreSQL
130 creates a temporary variable of data type
131 \fIstype\fR
132 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\*(Aqs return value\&. If there is no final function then the ending state value is returned as\-is\&.
133 .PP
134 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
135 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\&.
136 .PP
137 If the state transition function is declared
138 \(lqstrict\(rq, 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
139 \fBmax\fR\&. Note that this behavior is only available when
140 \fIstate_data_type\fR
141 is the same as the first
142 \fIarg_data_type\fR\&. When these types are different, you must supply a nonnull initial condition or use a nonstrict transition function\&.
143 .PP
144 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\*(Aqs handling of null values\&.
145 .PP
146 If the final function is declared
147 \(lqstrict\(rq, 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
148 \fBavg\fR
149 returns null when it sees there were zero input rows\&.
150 .PP
151 Sometimes it is useful to declare the final function as taking not just the state value, but extra parameters corresponding to the aggregate\*(Aqs input values\&. The main reason for doing this is if the final function is polymorphic and the state value\*(Aqs 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
152 FINALFUNC_EXTRA
153 option is used), but nonetheless they are valid parameters\&. The final function could for example make use of
154 \fBget_fn_expr_argtype\fR
155 to identify the actual argument type in the current call\&.
156 .PP
157 An aggregate can optionally support
158 moving\-aggregate mode, as described in
159 Section\ \&36.12.1\&. This requires specifying the
160 MSFUNC,
161 MINVFUNC, and
162 MSTYPE
163 parameters, and optionally the
164 MSSPACE,
165 MFINALFUNC,
166 MFINALFUNC_EXTRA,
167 MFINALFUNC_MODIFY, and
168 MINITCOND
169 parameters\&. Except for
170 MINVFUNC, these parameters work like the corresponding simple\-aggregate parameters without
171 M; they define a separate implementation of the aggregate that includes an inverse transition function\&.
172 .PP
173 The syntax with
174 ORDER BY
175 in the parameter list creates a special type of aggregate called an
176 ordered\-set aggregate; or if
177 HYPOTHETICAL
178 is specified, then a
179 hypothetical\-set aggregate
180 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
181 direct
182 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
183 \(lqhypothetical\(rq
184 row\&.
185 .PP
186 An aggregate can optionally support
187 partial aggregation, as described in
188 Section\ \&36.12.4\&. This requires specifying the
189 COMBINEFUNC
190 parameter\&. If the
191 \fIstate_data_type\fR
192 is
193 internal, it\*(Aqs usually also appropriate to provide the
194 SERIALFUNC
195 and
196 DESERIALFUNC
197 parameters so that parallel aggregation is possible\&. Note that the aggregate must also be marked
198 PARALLEL SAFE
199 to enable parallel aggregation\&.
200 .PP
201 Aggregates that behave like
202 \fBMIN\fR
203 or
204 \fBMAX\fR
205 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
206 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:
207 .sp
208 .if n \{\
209 .RS 4
210 .\}
211 .nf
212 SELECT agg(col) FROM tab;
213 .fi
214 .if n \{\
215 .RE
216 .\}
217 .sp
218 must be equivalent to:
219 .sp
220 .if n \{\
221 .RS 4
222 .\}
223 .nf
224 SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;
225 .fi
226 .if n \{\
227 .RE
228 .\}
229 .sp
230 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\*(Aqs
231 <
232 operator is the proper sort operator for
233 \fBMIN\fR, and
234 >
235 is the proper sort operator for
236 \fBMAX\fR\&. Note that the optimization will never actually take effect unless the specified operator is the
237 \(lqless than\(rq
238 or
239 \(lqgreater than\(rq
240 strategy member of a B\-tree index operator class\&.
241 .PP
242 To be able to create an aggregate function, you must have
243 USAGE
244 privilege on the argument types, the state type(s), and the return type, as well as
245 EXECUTE
246 privilege on the supporting functions\&.
247 .SH "PARAMETERS"
248 .PP
249 \fIname\fR
250 .RS 4
251 The name (optionally schema\-qualified) of the aggregate function to create\&.
252 .RE
253 .PP
254 \fIargmode\fR
255 .RS 4
256 The mode of an argument:
257 IN
258 or
259 VARIADIC\&. (Aggregate functions do not support
260 OUT
261 arguments\&.) If omitted, the default is
262 IN\&. Only the last argument can be marked
263 VARIADIC\&.
264 .RE
265 .PP
266 \fIargname\fR
267 .RS 4
268 The name of an argument\&. This is currently only useful for documentation purposes\&. If omitted, the argument has no name\&.
269 .RE
270 .PP
271 \fIarg_data_type\fR
272 .RS 4
273 An input data type on which this aggregate function operates\&. To create a zero\-argument aggregate function, write
274 *
275 in place of the list of argument specifications\&. (An example of such an aggregate is
276 \fBcount(*)\fR\&.)
277 .RE
278 .PP
279 \fIbase_type\fR
280 .RS 4
281 In the old syntax for
282 \fBCREATE AGGREGATE\fR, the input data type is specified by a
283 basetype
284 parameter rather than being written next to the aggregate name\&. Note that this syntax allows only one input parameter\&. To define a zero\-argument aggregate function with this syntax, specify the
285 basetype
286 as
287 "ANY"
288 (not
289 *)\&. Ordered\-set aggregates cannot be defined with the old syntax\&.
290 .RE
291 .PP
292 \fIsfunc\fR
293 .RS 4
294 The name of the state transition function to be called for each input row\&. For a normal
295 \fIN\fR\-argument aggregate function, the
296 \fIsfunc\fR
297 must take
298 \fIN\fR+1 arguments, the first being of type
299 \fIstate_data_type\fR
300 and the rest matching the declared input data type(s) of the aggregate\&. The function must return a value of type
301 \fIstate_data_type\fR\&. This function takes the current state value and the current input data value(s), and returns the next state value\&.
302 .sp
303 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 is the same\&.
304 .RE
305 .PP
306 \fIstate_data_type\fR
307 .RS 4
308 The data type for the aggregate\*(Aqs state value\&.
309 .RE
310 .PP
311 \fIstate_data_size\fR
312 .RS 4
313 The approximate average size (in bytes) of the aggregate\*(Aqs state value\&. If this parameter is omitted or is zero, a default estimate is used based on the
314 \fIstate_data_type\fR\&. The planner uses this value to estimate the memory required for a grouped aggregate query\&.
315 .RE
316 .PP
317 \fIffunc\fR
318 .RS 4
319 The name of the final function called to compute the aggregate\*(Aqs result after all input rows have been traversed\&. For a normal aggregate, this function must take a single argument of type
320 \fIstate_data_type\fR\&. The return data type of the aggregate is defined as the return type of this function\&. If
321 \fIffunc\fR
322 is not specified, then the ending state value is used as the aggregate\*(Aqs result, and the return type is
323 \fIstate_data_type\fR\&.
324 .sp
325 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\&.
326 .sp
327 If
328 FINALFUNC_EXTRA
329 is specified, then in addition to the final state value and any direct arguments, the final function receives extra NULL values corresponding to the aggregate\*(Aqs regular (aggregated) arguments\&. This is mainly useful to allow correct resolution of the aggregate result type when a polymorphic aggregate is being defined\&.
330 .RE
331 .PP
332 FINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE }
333 .RS 4
334 This option specifies whether the final function is a pure function that does not modify its arguments\&.
335 READ_ONLY
336 indicates it does not; the other two values indicate that it may change the transition state value\&. See
337 Notes
338 below for more detail\&. The default is
339 READ_ONLY, except for ordered\-set aggregates, for which the default is
340 READ_WRITE\&.
341 .RE
342 .PP
343 \fIcombinefunc\fR
344 .RS 4
345 The
346 \fIcombinefunc\fR
347 function may optionally be specified to allow the aggregate function to support partial aggregation\&. If provided, the
348 \fIcombinefunc\fR
349 must combine two
350 \fIstate_data_type\fR
351 values, each containing the result of aggregation over some subset of the input values, to produce a new
352 \fIstate_data_type\fR
353 that represents the result of aggregating over both sets of inputs\&. This function can be thought of as an
354 \fIsfunc\fR, where instead of acting upon an individual input row and adding it to the running aggregate state, it adds another aggregate state to the running state\&.
355 .sp
356 The
357 \fIcombinefunc\fR
358 must be declared as taking two arguments of the
359 \fIstate_data_type\fR
360 and returning a value of the
361 \fIstate_data_type\fR\&. Optionally this function may be
362 \(lqstrict\(rq\&. In this case the function will not be called when either of the input states are null; the other state will be taken as the correct result\&.
363 .sp
364 For aggregate functions whose
365 \fIstate_data_type\fR
366 is
367 internal, the
368 \fIcombinefunc\fR
369 must not be strict\&. In this case the
370 \fIcombinefunc\fR
371 must ensure that null states are handled correctly and that the state being returned is properly stored in the aggregate memory context\&.
372 .RE
373 .PP
374 \fIserialfunc\fR
375 .RS 4
376 An aggregate function whose
377 \fIstate_data_type\fR
378 is
379 internal
380 can participate in parallel aggregation only if it has a
381 \fIserialfunc\fR
382 function, which must serialize the aggregate state into a
383 bytea
384 value for transmission to another process\&. This function must take a single argument of type
385 internal
386 and return type
387 bytea\&. A corresponding
388 \fIdeserialfunc\fR
389 is also required\&.
390 .RE
391 .PP
392 \fIdeserialfunc\fR
393 .RS 4
394 Deserialize a previously serialized aggregate state back into
395 \fIstate_data_type\fR\&. This function must take two arguments of types
396 bytea
397 and
398 internal, and produce a result of type
399 internal\&. (Note: the second,
400 internal
401 argument is unused, but is required for type safety reasons\&.)
402 .RE
403 .PP
404 \fIinitial_condition\fR
405 .RS 4
406 The initial setting for the state value\&. This must be a string constant in the form accepted for the data type
407 \fIstate_data_type\fR\&. If not specified, the state value starts out null\&.
408 .RE
409 .PP
410 \fImsfunc\fR
411 .RS 4
412 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 result are of type
413 \fImstate_data_type\fR, which might be different from
414 \fIstate_data_type\fR\&.
415 .RE
416 .PP
417 \fIminvfunc\fR
418 .RS 4
419 The name of the inverse state transition function to be used in moving\-aggregate mode\&. This function has the same argument and result types as
420 \fImsfunc\fR, but it is used to remove a value from the current aggregate state, rather than add a value to it\&. The inverse transition function must have the same strictness attribute as the forward state transition function\&.
421 .RE
422 .PP
423 \fImstate_data_type\fR
424 .RS 4
425 The data type for the aggregate\*(Aqs state value, when using moving\-aggregate mode\&.
426 .RE
427 .PP
428 \fImstate_data_size\fR
429 .RS 4
430 The approximate average size (in bytes) of the aggregate\*(Aqs state value, when using moving\-aggregate mode\&. This works the same as
431 \fIstate_data_size\fR\&.
432 .RE
433 .PP
434 \fImffunc\fR
435 .RS 4
436 The name of the final function called to compute the aggregate\*(Aqs result after all input rows have been traversed, when using moving\-aggregate mode\&. This works the same as
437 \fIffunc\fR, except that its first argument\*(Aqs type is
438 \fImstate_data_type\fR
439 and extra dummy arguments are specified by writing
440 MFINALFUNC_EXTRA\&. The aggregate result type determined by
441 \fImffunc\fR
442 or
443 \fImstate_data_type\fR
444 must match that determined by the aggregate\*(Aqs regular implementation\&.
445 .RE
446 .PP
447 MFINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE }
448 .RS 4
449 This option is like
450 FINALFUNC_MODIFY, but it describes the behavior of the moving\-aggregate final function\&.
451 .RE
452 .PP
453 \fIminitial_condition\fR
454 .RS 4
455 The initial setting for the state value, when using moving\-aggregate mode\&. This works the same as
456 \fIinitial_condition\fR\&.
457 .RE
458 .PP
459 \fIsort_operator\fR
460 .RS 4
461 The associated sort operator for a
462 \fBMIN\fR\- or
463 \fBMAX\fR\-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)\&.
464 .RE
465 .PP
466 PARALLEL = { SAFE | RESTRICTED | UNSAFE }
467 .RS 4
468 The meanings of
469 PARALLEL SAFE,
470 PARALLEL RESTRICTED, and
471 PARALLEL UNSAFE
472 are the same as in
473 \fBCREATE FUNCTION\fR\&. An aggregate will not be considered for parallelization if it is marked
474 PARALLEL UNSAFE
475 (which is the default!) or
476 PARALLEL RESTRICTED\&. Note that the parallel\-safety markings of the aggregate\*(Aqs support functions are not consulted by the planner, only the marking of the aggregate itself\&.
477 .RE
478 .PP
479 HYPOTHETICAL
480 .RS 4
481 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\&. The
482 HYPOTHETICAL
483 flag has no effect on run\-time behavior, only on parse\-time resolution of the data types and collations of the aggregate\*(Aqs arguments\&.
484 .RE
485 .PP
486 The parameters of
487 \fBCREATE AGGREGATE\fR
488 can be written in any order, not just the order illustrated above\&.
489 .SH "NOTES"
490 .PP
491 In parameters that specify support function names, you can write a schema name if needed, for example
492 SFUNC = public\&.sum\&. Do not write argument types there, however \(em the argument types of the support functions are determined from other parameters\&.
493 .PP
494 Ordinarily, PostgreSQL functions are expected to be true functions that do not modify their input values\&. However, an aggregate transition function,
495 \fIwhen used in the context of an aggregate\fR, is allowed to cheat and modify its transition\-state argument in place\&. This can provide substantial performance benefits compared to making a fresh copy of the transition state each time\&.
496 .PP
497 Likewise, 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\&. Such behavior must be declared using the
498 FINALFUNC_MODIFY
499 parameter\&. The
500 READ_WRITE
501 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
502 SHAREABLE
503 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
504 READ_WRITE\&.)
505 .PP
506 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
507 UNBOUNDED PRECEDING)\&. Conceptually, the forward transition function adds input values to the aggregate\*(Aqs 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\&.)
508 .PP
509 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\&.
510 .PP
511 If no moving\-aggregate implementation is supplied, the aggregate can still be used with moving frames, but
512 PostgreSQL
513 will recompute the whole aggregation whenever the start of the frame moves\&. Note that whether or not the aggregate supports moving\-aggregate mode,
514 PostgreSQL
515 can handle a moving frame end without recalculation; this is done by continuing to add new values to the aggregate\*(Aqs 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\*(Aqs state value, so that the aggregation can be continued even after an aggregate result value has been obtained for one set of frame boundaries\&.
516 .PP
517 The syntax for ordered\-set aggregates allows
518 VARIADIC
519 to be specified for both the last direct parameter and the last aggregated (WITHIN GROUP) parameter\&. However, the current implementation restricts use of
520 VARIADIC
521 in two ways\&. First, ordered\-set aggregates can only use
522 VARIADIC "any", not other variadic array types\&. Second, if the last direct parameter is
523 VARIADIC "any", then there can be only one aggregated parameter and it must also be
524 VARIADIC "any"\&. (In the representation used in the system catalogs, these two parameters are merged into a single
525 VARIADIC "any"
526 item, since
527 pg_proc
528 cannot represent functions with more than one
529 VARIADIC
530 parameter\&.) If the aggregate is a hypothetical\-set aggregate, the direct arguments that match the
531 VARIADIC "any"
532 parameter are the hypothetical ones; any preceding parameters represent additional direct arguments that are not constrained to match the aggregated arguments\&.
533 .PP
534 Currently, ordered\-set aggregates do not need to support moving\-aggregate mode, since they cannot be used as window functions\&.
535 .PP
536 Partial (including parallel) aggregation is currently not supported for ordered\-set aggregates\&. Also, it will never be used for aggregate calls that include
537 DISTINCT
538 or
539 ORDER BY
540 clauses, since those semantics cannot be supported during partial aggregation\&.
541 .SH "EXAMPLES"
542 .PP
543 See
544 Section\ \&36.12\&.
545 .SH "COMPATIBILITY"
546 .PP
547 \fBCREATE AGGREGATE\fR
548 is a
549 PostgreSQL
550 language extension\&. The SQL standard does not provide for user\-defined aggregate functions\&.
551 .SH "SEE ALSO"
552 ALTER AGGREGATE (\fBALTER_AGGREGATE\fR(7)), DROP AGGREGATE (\fBDROP_AGGREGATE\fR(7))