@@ -70,7 +70,7 @@ SELECT sum(a) FROM test_complex;
70
70
expects <function>sum</function> to behave that way. We can do this simply by
71
71
omitting the <literal>initcond</literal> phrase, so that the initial state
72
72
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
74
74
<function>sum</function> and some other simple aggregates like
75
75
<function>max</> and <function>min</>,
76
76
it is sufficient to insert the first nonnull input value into
@@ -95,8 +95,8 @@ SELECT sum(a) FROM test_complex;
95
95
It requires
96
96
two pieces of running state: the sum of the inputs and the count
97
97
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,
100
100
the built-in implementation of <function>avg(float8)</function>
101
101
looks like:
102
102
@@ -109,6 +109,11 @@ CREATE AGGREGATE avg (float8)
109
109
initcond = '{0,0,0}'
110
110
);
111
111
</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</>.)
112
117
</para>
113
118
114
119
<para>
@@ -174,12 +179,13 @@ if (AggCheckCallContext(fcinfo, NULL))
174
179
One reason for checking this is that when it is true for a transition
175
180
function, the first input
176
181
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</>
178
185
case where it is safe for a function to modify a pass-by-reference input.
179
186
In particular, aggregate final functions should not modify their inputs in
180
187
any case, because in some cases they will be re-executed on the same
181
188
final transition value.)
182
- See <literal>int8inc()</> for an example.
183
189
</para>
184
190
185
191
<para>
0 commit comments