@@ -27,6 +27,12 @@ CREATE AGGREGATE <replaceable class="parameter">name</replaceable> ( [ <replacea
27
27
[ , SSPACE = <replaceable class="PARAMETER">state_data_size</replaceable> ]
28
28
[ , FINALFUNC = <replaceable class="PARAMETER">ffunc</replaceable> ]
29
29
[ , INITCOND = <replaceable class="PARAMETER">initial_condition</replaceable> ]
30
+ [ , MSFUNC = <replaceable class="PARAMETER">msfunc</replaceable> ]
31
+ [ , MINVFUNC = <replaceable class="PARAMETER">minvfunc</replaceable> ]
32
+ [ , MSTYPE = <replaceable class="PARAMETER">mstate_data_type</replaceable> ]
33
+ [ , MSSPACE = <replaceable class="PARAMETER">mstate_data_size</replaceable> ]
34
+ [ , MFINALFUNC = <replaceable class="PARAMETER">mffunc</replaceable> ]
35
+ [ , MINITCOND = <replaceable class="PARAMETER">minitial_condition</replaceable> ]
30
36
[ , SORTOP = <replaceable class="PARAMETER">sort_operator</replaceable> ]
31
37
)
32
38
@@ -49,6 +55,12 @@ CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> (
49
55
[ , SSPACE = <replaceable class="PARAMETER">state_data_size</replaceable> ]
50
56
[ , FINALFUNC = <replaceable class="PARAMETER">ffunc</replaceable> ]
51
57
[ , INITCOND = <replaceable class="PARAMETER">initial_condition</replaceable> ]
58
+ [ , MSFUNC = <replaceable class="PARAMETER">sfunc</replaceable> ]
59
+ [ , MINVFUNC = <replaceable class="PARAMETER">invfunc</replaceable> ]
60
+ [ , MSTYPE = <replaceable class="PARAMETER">state_data_type</replaceable> ]
61
+ [ , MSSPACE = <replaceable class="PARAMETER">state_data_size</replaceable> ]
62
+ [ , MFINALFUNC = <replaceable class="PARAMETER">ffunc</replaceable> ]
63
+ [ , MINITCOND = <replaceable class="PARAMETER">initial_condition</replaceable> ]
52
64
[ , SORTOP = <replaceable class="PARAMETER">sort_operator</replaceable> ]
53
65
)
54
66
</synopsis>
@@ -84,7 +96,7 @@ CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> (
84
96
</para>
85
97
86
98
<para>
87
- An aggregate function is made from one or two ordinary
99
+ A simple aggregate function is made from one or two ordinary
88
100
functions:
89
101
a state transition function
90
102
<replaceable class="PARAMETER">sfunc</replaceable>,
@@ -126,7 +138,7 @@ CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> (
126
138
values are ignored (the function is not called and the previous state value
127
139
is retained). If the initial state value is null, then at the first row
128
140
with all-nonnull input values, the first argument value replaces the state
129
- value, and the transition function is invoked at subsequent rows with
141
+ value, and the transition function is invoked at each subsequent row with
130
142
all-nonnull input values.
131
143
This is handy for implementing aggregates like <function>max</function>.
132
144
Note that this behavior is only available when
@@ -154,6 +166,18 @@ CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> (
154
166
input rows.
155
167
</para>
156
168
169
+ <para>
170
+ An aggregate can optionally support <firstterm>moving-aggregate mode</>,
171
+ as described in <xref linkend="xaggr-moving-aggregates">. This requires
172
+ specifying the <literal>MSFUNC</>, <literal>MINVFUNC</>,
173
+ and <literal>MSTYPE</> parameters, and optionally
174
+ the <literal>MSPACE</>, <literal>MFINALFUNC</>,
175
+ and <literal>MINITCOND</> parameters. Except for <literal>MINVFUNC</>,
176
+ these parameters work like the corresponding simple-aggregate parameters
177
+ without <literal>M</>; they define a separate implementation of the
178
+ aggregate that includes an inverse transition function.
179
+ </para>
180
+
157
181
<para>
158
182
The syntax with <literal>ORDER BY</literal> in the parameter list creates
159
183
a special type of aggregate called an <firstterm>ordered-set
@@ -197,8 +221,8 @@ SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;
197
221
<para>
198
222
To be able to create an aggregate function, you must
199
223
have <literal>USAGE</literal> privilege on the argument types, the state
200
- type, and the return type, as well as <literal>EXECUTE</literal> privilege
201
- on the transition and final functions.
224
+ type(s) , and the return type, as well as <literal>EXECUTE</literal>
225
+ privilege on the transition and final functions.
202
226
</para>
203
227
</refsect1>
204
228
@@ -359,6 +383,79 @@ SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;
359
383
</listitem>
360
384
</varlistentry>
361
385
386
+ <varlistentry>
387
+ <term><replaceable class="PARAMETER">msfunc</replaceable></term>
388
+ <listitem>
389
+ <para>
390
+ The name of the forward state transition function to be called for each
391
+ input row in moving-aggregate mode. This is exactly like the regular
392
+ transition function, except that its first argument and result are of
393
+ type <replaceable>mstate_data_type</>, which might be different
394
+ from <replaceable>state_data_type</>.
395
+ </para>
396
+ </listitem>
397
+ </varlistentry>
398
+
399
+ <varlistentry>
400
+ <term><replaceable class="PARAMETER">minvfunc</replaceable></term>
401
+ <listitem>
402
+ <para>
403
+ The name of the inverse state transition function to be used in
404
+ moving-aggregate mode. This function has the same argument and
405
+ result types as <replaceable>msfunc</>, but it is used to remove
406
+ a value from the current aggregate state, rather than add a value to
407
+ it. The inverse transition function must have the same strictness
408
+ attribute as the forward state transition function.
409
+ </para>
410
+ </listitem>
411
+ </varlistentry>
412
+
413
+ <varlistentry>
414
+ <term><replaceable class="PARAMETER">mstate_data_type</replaceable></term>
415
+ <listitem>
416
+ <para>
417
+ The data type for the aggregate's state value, when using
418
+ moving-aggregate mode.
419
+ </para>
420
+ </listitem>
421
+ </varlistentry>
422
+
423
+ <varlistentry>
424
+ <term><replaceable class="PARAMETER">mstate_data_size</replaceable></term>
425
+ <listitem>
426
+ <para>
427
+ The approximate average size (in bytes) of the aggregate's state
428
+ value, when using moving-aggregate mode. This works the same as
429
+ <replaceable>state_data_size</>.
430
+ </para>
431
+ </listitem>
432
+ </varlistentry>
433
+
434
+ <varlistentry>
435
+ <term><replaceable class="PARAMETER">mffunc</replaceable></term>
436
+ <listitem>
437
+ <para>
438
+ The name of the final function called to compute the aggregate's
439
+ result after all input rows have been traversed, when using
440
+ moving-aggregate mode. This works the same as <replaceable>ffunc</>,
441
+ except that its input type is <replaceable>mstate_data_type</>.
442
+ The aggregate result type determined by <replaceable>mffunc</>
443
+ and <replaceable>mstate_data_type</> must match that determined by the
444
+ aggregate's regular implementation.
445
+ </para>
446
+ </listitem>
447
+ </varlistentry>
448
+
449
+ <varlistentry>
450
+ <term><replaceable class="PARAMETER">minitial_condition</replaceable></term>
451
+ <listitem>
452
+ <para>
453
+ The initial setting for the state value, when using moving-aggregate
454
+ mode. This works the same as <replaceable>initial_condition</>.
455
+ </para>
456
+ </listitem>
457
+ </varlistentry>
458
+
362
459
<varlistentry>
363
460
<term><replaceable class="PARAMETER">sort_operator</replaceable></term>
364
461
<listitem>
@@ -397,6 +494,49 @@ SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;
397
494
<refsect1>
398
495
<title>Notes</title>
399
496
497
+ <para>
498
+ If an aggregate supports moving-aggregate mode, it will improve
499
+ calculation efficiency when the aggregate is used as a window function
500
+ for a window with moving frame start (that is, a frame start mode other
501
+ than <literal>UNBOUNDED PRECEDING</>). Conceptually, the forward
502
+ transition function adds input values to the aggregate's state when
503
+ they enter the window frame from the bottom, and the inverse transition
504
+ function removes them again when they leave the frame at the top. So,
505
+ when values are removed, they are always removed in the same order they
506
+ were added. Whenever the inverse transition function is invoked, it will
507
+ thus receive the earliest added but not yet removed argument value(s).
508
+ The inverse transition function can assume that at least one row will
509
+ remain in the current state after it removes the oldest row. (When this
510
+ would not be the case, the window function mechanism simply starts a
511
+ fresh aggregation, rather than using the inverse transition function.)
512
+ </para>
513
+
514
+ <para>
515
+ The forward transition function for moving-aggregate mode is not
516
+ allowed to return NULL as the new state value. If the inverse
517
+ transition function returns NULL, this is taken as an indication that
518
+ the inverse function cannot reverse the state calculation for this
519
+ particular input, and so the aggregate calculation will be redone from
520
+ scratch for the current frame starting position. This convention
521
+ allows moving-aggregate mode to be used in situations where there are
522
+ some infrequent cases that are impractical to reverse out of the
523
+ running state value.
524
+ </para>
525
+
526
+ <para>
527
+ If no moving-aggregate implementation is supplied,
528
+ the aggregate can still be used with moving frames,
529
+ but <productname>PostgreSQL</productname> will recompute the whole
530
+ aggregation whenever the start of the frame moves.
531
+ Note that whether or not the aggregate supports moving-aggregate
532
+ mode, <productname>PostgreSQL</productname> can handle a moving frame
533
+ end without recalculation; this is done by continuing to add new values
534
+ to the aggregate's state. It is assumed that the final function does
535
+ not damage the aggregate's state value, so that the aggregation can be
536
+ continued even after an aggregate result value has been obtained for
537
+ one set of frame boundaries.
538
+ </para>
539
+
400
540
<para>
401
541
The syntax for ordered-set aggregates allows <literal>VARIADIC</>
402
542
to be specified for both the last direct parameter and the last
@@ -415,6 +555,11 @@ SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;
415
555
ones; any preceding parameters represent additional direct arguments
416
556
that are not constrained to match the aggregated arguments.
417
557
</para>
558
+
559
+ <para>
560
+ Currently, ordered-set aggregates do not need to support
561
+ moving-aggregate mode, since they cannot be used as window functions.
562
+ </para>
418
563
</refsect1>
419
564
420
565
<refsect1>
0 commit comments