Skip to content

Commit 472671e

Browse files
committed
Improve user-defined-aggregates documentation.
On closer inspection, that two-element initcond value seems to have been a little white lie to avoid explaining the full behavior of float8_accum. But if people are going to expect the examples to be exactly correct, I suppose we'd better explain. Per comment from Thom Brown.
1 parent b5f2f2a commit 472671e

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

doc/src/sgml/xaggr.sgml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ SELECT sum(a) FROM test_complex;
7070
expects <function>sum</function> to behave that way. We can do this simply by
7171
omitting the <literal>initcond</literal> phrase, so that the initial state
7272
condition is null. Ordinarily this would mean that the <literal>sfunc</literal>
73-
would need to check for a null state-condition input, but for
73+
would need to check for a null state-condition input. But for
7474
<function>sum</function> and some other simple aggregates like
7575
<function>max</> and <function>min</>,
7676
it is sufficient to insert the first nonnull input value into
@@ -95,8 +95,8 @@ SELECT sum(a) FROM test_complex;
9595
It requires
9696
two pieces of running state: the sum of the inputs and the count
9797
of the number of inputs. The final result is obtained by dividing
98-
these quantities. Average is typically implemented by using a
99-
two-element array as the state value. For example,
98+
these quantities. Average is typically implemented by using an
99+
array as the state value. For example,
100100
the built-in implementation of <function>avg(float8)</function>
101101
looks like:
102102

@@ -109,6 +109,11 @@ CREATE AGGREGATE avg (float8)
109109
initcond = '{0,0,0}'
110110
);
111111
</programlisting>
112+
113+
(<function>float8_accum</> requires a three-element array, not just
114+
two elements, because it accumulates the sum of squares as well as
115+
the sum and count of the inputs. This is so that it can be used for
116+
some other aggregates besides <function>avg</>.)
112117
</para>
113118

114119
<para>
@@ -174,12 +179,13 @@ if (AggCheckCallContext(fcinfo, NULL))
174179
One reason for checking this is that when it is true for a transition
175180
function, the first input
176181
must be a temporary transition value and can therefore safely be modified
177-
in-place rather than allocating a new copy. (This is the <emphasis>only</>
182+
in-place rather than allocating a new copy.
183+
See <literal>int8inc()</> for an example.
184+
(This is the <emphasis>only</>
178185
case where it is safe for a function to modify a pass-by-reference input.
179186
In particular, aggregate final functions should not modify their inputs in
180187
any case, because in some cases they will be re-executed on the same
181188
final transition value.)
182-
See <literal>int8inc()</> for an example.
183189
</para>
184190

185191
<para>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy