1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
5 >User-Defined Aggregates</TITLE
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
10 HREF="mailto:pgsql-docs@postgresql.org"><LINK
12 TITLE="PostgreSQL 7.4.1 Documentation"
13 HREF="index.html"><LINK
16 HREF="extend.html"><LINK
18 TITLE="Function Overloading"
19 HREF="xfunc-overload.html"><LINK
21 TITLE="User-Defined Types"
22 HREF="xtypes.html"><LINK
25 HREF="stylesheet.css"><META
27 CONTENT="2003-12-22T03:48:47"></HEAD
33 SUMMARY="Header navigation table"
43 >PostgreSQL 7.4.1 Documentation</TH
51 HREF="xfunc-overload.html"
67 >Chapter 33. Extending <ACRONYM
99 >33.9. User-Defined Aggregates</A
105 > Aggregate functions in <SPAN
115 >state transition functions</I
117 That is, an aggregate can be
118 defined in terms of state that is modified whenever an
119 input item is processed. To define a new aggregate
120 function, one selects a data type for the state value,
121 an initial value for the state, and a state transition
122 function. The state transition function is just an
123 ordinary function that could also be used outside the
124 context of the aggregate. A <I
128 can also be specified, in case the desired result of the aggregate
129 is different from the data that needs to be kept in the running
133 > Thus, in addition to the argument and result data types seen by a user
134 of the aggregate, there is an internal state-value data type that
135 may be different from both the argument and result types.
138 > If we define an aggregate that does not use a final function,
139 we have an aggregate that computes a running function of
140 the column values from each row. <CODE
144 example of this kind of aggregate. <CODE
148 zero and always adds the current row's value to
149 its running total. For example, if we want to make a <CODE
153 aggregate to work on a data type for complex numbers,
154 we only need the addition function for that data type.
155 The aggregate definition would be:
159 >CREATE AGGREGATE complex_sum (
166 SELECT complex_sum(a) FROM test_complex;
173 (In practice, we'd just name the aggregate <CODE
180 > to figure out which kind
181 of sum to apply to a column of type <TT
187 > The above definition of <CODE
190 > will return zero (the initial
191 state condition) if there are no nonnull input values.
192 Perhaps we want to return null in that case instead --- the SQL standard
196 > to behave that way. We can do this simply by
200 > phrase, so that the initial state
201 condition is null. Ordinarily this would mean that the <TT
205 would need to check for a null state-condition input, but for
209 > and some other simple aggregates like
217 it is sufficient to insert the first nonnull input value into
218 the state variable and then start applying the transition function
219 at the second nonnull input value. <SPAN
223 will do that automatically if the initial condition is null and
224 the transition function is marked <SPAN
227 > (i.e., not to be called
231 > Another bit of default behavior for a <SPAN
234 > transition function
235 is that the previous state value is retained unchanged whenever a
236 null input value is encountered. Thus, null values are ignored. If you
237 need some other behavior for null inputs, just do not define your transition
238 function as strict, and code it to test for null inputs and do
245 > (average) is a more complex example of an aggregate. It requires
246 two pieces of running state: the sum of the inputs and the count
247 of the number of inputs. The final result is obtained by dividing
248 these quantities. Average is typically implemented by using a
249 two-element array as the state value. For example,
250 the built-in implementation of <CODE
257 CLASS="PROGRAMLISTING"
258 >CREATE AGGREGATE avg (
259 sfunc = float8_accum,
262 finalfunc = float8_avg,
268 > Aggregate functions may use polymorphic
269 state transition functions or final functions, so that the same functions
270 can be used to implement multiple aggregates.
272 HREF="extend-type-system.html#EXTEND-TYPES-POLYMORPHIC"
275 for an explanation of polymorphic functions.
276 Going a step further, the aggregate function itself may be specified
277 with a polymorphic base type and state type, allowing a single
278 aggregate definition to serve for multiple input data types.
279 Here is an example of a polymorphic aggregate:
282 CLASS="PROGRAMLISTING"
283 >CREATE AGGREGATE array_accum (
284 sfunc = array_append,
285 basetype = anyelement,
291 Here, the actual state type for any aggregate call is the array type
292 having the actual input type as elements.
295 > Here's the output using two different actual data types as arguments:
298 CLASS="PROGRAMLISTING"
299 >SELECT attrelid::regclass, array_accum(attname)
301 WHERE attnum > 0 AND attrelid = 'pg_user'::regclass
304 attrelid | array_accum
305 ----------+-----------------------------------------------------------------------------
306 pg_user | {usename,usesysid,usecreatedb,usesuper,usecatupd,passwd,valuntil,useconfig}
309 SELECT attrelid::regclass, array_accum(atttypid)
311 WHERE attnum > 0 AND attrelid = 'pg_user'::regclass
314 attrelid | array_accum
315 ----------+------------------------------
316 pg_user | {19,23,16,16,16,25,702,1009}
321 > For further details see the
323 HREF="sql-createaggregate.html"
336 SUMMARY="Footer navigation table"
347 HREF="xfunc-overload.html"
375 >Function Overloading</TD
389 >User-Defined Types</TD