1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-27 23:21:58 +03:00

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.
This commit is contained in:
Tom Lane
2011-03-23 16:57:41 -04:00
parent 24cdee8606
commit 63fe94d165

View File

@ -70,7 +70,7 @@ SELECT sum(a) FROM test_complex;
expects <function>sum</function> to behave that way. We can do this simply by expects <function>sum</function> to behave that way. We can do this simply by
omitting the <literal>initcond</literal> phrase, so that the initial state omitting the <literal>initcond</literal> phrase, so that the initial state
condition is null. Ordinarily this would mean that the <literal>sfunc</literal> condition is null. Ordinarily this would mean that the <literal>sfunc</literal>
would need to check for a null state-condition input, but for would need to check for a null state-condition input. But for
<function>sum</function> and some other simple aggregates like <function>sum</function> and some other simple aggregates like
<function>max</> and <function>min</>, <function>max</> and <function>min</>,
it is sufficient to insert the first nonnull input value into it is sufficient to insert the first nonnull input value into
@ -95,8 +95,8 @@ SELECT sum(a) FROM test_complex;
It requires It requires
two pieces of running state: the sum of the inputs and the count two pieces of running state: the sum of the inputs and the count
of the number of inputs. The final result is obtained by dividing of the number of inputs. The final result is obtained by dividing
these quantities. Average is typically implemented by using a these quantities. Average is typically implemented by using an
two-element array as the state value. For example, array as the state value. For example,
the built-in implementation of <function>avg(float8)</function> the built-in implementation of <function>avg(float8)</function>
looks like: looks like:
@ -106,9 +106,14 @@ CREATE AGGREGATE avg (float8)
sfunc = float8_accum, sfunc = float8_accum,
stype = float8[], stype = float8[],
finalfunc = float8_avg, finalfunc = float8_avg,
initcond = '{0,0,0}' initcond = '{0,0}'
); );
</programlisting> </programlisting>
(<function>float8_accum</> requires a three-element array, not just
two elements, because it accumulates the sum of squares as well as
the sum and count of the inputs. This is so that it can be used for
some other aggregates besides <function>avg</>.)
</para> </para>
<para> <para>