Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a9d9acb

Browse files
committed
Create infrastructure for moving-aggregate optimization.
Until now, when executing an aggregate function as a window function within a window with moving frame start (that is, any frame start mode except UNBOUNDED PRECEDING), we had to recalculate the aggregate from scratch each time the frame head moved. This patch allows an aggregate definition to include an alternate "moving aggregate" implementation that includes an inverse transition function for removing rows from the aggregate's running state. As long as this can be done successfully, runtime is proportional to the total number of input rows, rather than to the number of input rows times the average frame length. This commit includes the core infrastructure, documentation, and regression tests using user-defined aggregates. Follow-on commits will update some of the built-in aggregates to use this feature. David Rowley and Florian Pflug, reviewed by Dean Rasheed; additional hacking by me
1 parent 3c41b81 commit a9d9acb

File tree

20 files changed

+2228
-290
lines changed

20 files changed

+2228
-290
lines changed

doc/src/sgml/catalogs.sgml

+43
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,24 @@
386386
<entry><literal><link linkend="catalog-pg-proc"><structname>pg_proc</structname></link>.oid</literal></entry>
387387
<entry>Final function (zero if none)</entry>
388388
</row>
389+
<row>
390+
<entry><structfield>aggmtransfn</structfield></entry>
391+
<entry><type>regproc</type></entry>
392+
<entry><literal><link linkend="catalog-pg-proc"><structname>pg_proc</structname></link>.oid</literal></entry>
393+
<entry>Forward transition function for moving-aggregate mode (zero if none)</entry>
394+
</row>
395+
<row>
396+
<entry><structfield>aggminvtransfn</structfield></entry>
397+
<entry><type>regproc</type></entry>
398+
<entry><literal><link linkend="catalog-pg-proc"><structname>pg_proc</structname></link>.oid</literal></entry>
399+
<entry>Inverse transition function for moving-aggregate mode (zero if none)</entry>
400+
</row>
401+
<row>
402+
<entry><structfield>aggmfinalfn</structfield></entry>
403+
<entry><type>regproc</type></entry>
404+
<entry><literal><link linkend="catalog-pg-proc"><structname>pg_proc</structname></link>.oid</literal></entry>
405+
<entry>Final function for moving-aggregate mode (zero if none)</entry>
406+
</row>
389407
<row>
390408
<entry><structfield>aggsortop</structfield></entry>
391409
<entry><type>oid</type></entry>
@@ -405,6 +423,20 @@
405423
<entry>Approximate average size (in bytes) of the transition state
406424
data, or zero to use a default estimate</entry>
407425
</row>
426+
<row>
427+
<entry><structfield>aggmtranstype</structfield></entry>
428+
<entry><type>oid</type></entry>
429+
<entry><literal><link linkend="catalog-pg-type"><structname>pg_type</structname></link>.oid</literal></entry>
430+
<entry>Data type of the aggregate function's internal transition (state)
431+
data for moving-aggregate mode (zero if none)</entry>
432+
</row>
433+
<row>
434+
<entry><structfield>aggmtransspace</structfield></entry>
435+
<entry><type>int4</type></entry>
436+
<entry></entry>
437+
<entry>Approximate average size (in bytes) of the transition state data
438+
for moving-aggregate mode, or zero to use a default estimate</entry>
439+
</row>
408440
<row>
409441
<entry><structfield>agginitval</structfield></entry>
410442
<entry><type>text</type></entry>
@@ -416,6 +448,17 @@
416448
value starts out null.
417449
</entry>
418450
</row>
451+
<row>
452+
<entry><structfield>aggminitval</structfield></entry>
453+
<entry><type>text</type></entry>
454+
<entry></entry>
455+
<entry>
456+
The initial value of the transition state for moving-aggregate mode.
457+
This is a text field containing the initial value in its external
458+
string representation. If this field is null, the transition state
459+
value starts out null.
460+
</entry>
461+
</row>
419462
</tbody>
420463
</tgroup>
421464
</table>

doc/src/sgml/ref/create_aggregate.sgml

+149-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ CREATE AGGREGATE <replaceable class="parameter">name</replaceable> ( [ <replacea
2727
[ , SSPACE = <replaceable class="PARAMETER">state_data_size</replaceable> ]
2828
[ , FINALFUNC = <replaceable class="PARAMETER">ffunc</replaceable> ]
2929
[ , 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> ]
3036
[ , SORTOP = <replaceable class="PARAMETER">sort_operator</replaceable> ]
3137
)
3238

@@ -49,6 +55,12 @@ CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> (
4955
[ , SSPACE = <replaceable class="PARAMETER">state_data_size</replaceable> ]
5056
[ , FINALFUNC = <replaceable class="PARAMETER">ffunc</replaceable> ]
5157
[ , 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> ]
5264
[ , SORTOP = <replaceable class="PARAMETER">sort_operator</replaceable> ]
5365
)
5466
</synopsis>
@@ -84,7 +96,7 @@ CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> (
8496
</para>
8597

8698
<para>
87-
An aggregate function is made from one or two ordinary
99+
A simple aggregate function is made from one or two ordinary
88100
functions:
89101
a state transition function
90102
<replaceable class="PARAMETER">sfunc</replaceable>,
@@ -126,7 +138,7 @@ CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> (
126138
values are ignored (the function is not called and the previous state value
127139
is retained). If the initial state value is null, then at the first row
128140
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
130142
all-nonnull input values.
131143
This is handy for implementing aggregates like <function>max</function>.
132144
Note that this behavior is only available when
@@ -154,6 +166,18 @@ CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> (
154166
input rows.
155167
</para>
156168

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+
157181
<para>
158182
The syntax with <literal>ORDER BY</literal> in the parameter list creates
159183
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;
197221
<para>
198222
To be able to create an aggregate function, you must
199223
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.
202226
</para>
203227
</refsect1>
204228

@@ -359,6 +383,79 @@ SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;
359383
</listitem>
360384
</varlistentry>
361385

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+
362459
<varlistentry>
363460
<term><replaceable class="PARAMETER">sort_operator</replaceable></term>
364461
<listitem>
@@ -397,6 +494,49 @@ SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;
397494
<refsect1>
398495
<title>Notes</title>
399496

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+
400540
<para>
401541
The syntax for ordered-set aggregates allows <literal>VARIADIC</>
402542
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;
415555
ones; any preceding parameters represent additional direct arguments
416556
that are not constrained to match the aggregated arguments.
417557
</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>
418563
</refsect1>
419564

420565
<refsect1>

0 commit comments

Comments
 (0)