3 PostgreSQL documentation
6 <refentry id=
"SQL-CREATEAGGREGATE">
8 <refentrytitle id=
"sql-createaggregate-title">CREATE AGGREGATE
</refentrytitle>
9 <manvolnum>7</manvolnum>
10 <refmiscinfo>SQL - Language Statements
</refmiscinfo>
14 <refname>CREATE AGGREGATE
</refname>
15 <refpurpose>define a new aggregate function
</refpurpose>
18 <indexterm zone=
"sql-createaggregate">
19 <primary>CREATE AGGREGATE
</primary>
24 CREATE AGGREGATE
<replaceable class=
"PARAMETER">name
</replaceable> (
<replaceable class=
"PARAMETER">input_data_type
</replaceable> [ , ... ] ) (
25 SFUNC =
<replaceable class=
"PARAMETER">sfunc
</replaceable>,
26 STYPE =
<replaceable class=
"PARAMETER">state_data_type
</replaceable>
27 [ , FINALFUNC =
<replaceable class=
"PARAMETER">ffunc
</replaceable> ]
28 [ , INITCOND =
<replaceable class=
"PARAMETER">initial_condition
</replaceable> ]
29 [ , SORTOP =
<replaceable class=
"PARAMETER">sort_operator
</replaceable> ]
34 CREATE AGGREGATE
<replaceable class=
"PARAMETER">name
</replaceable> (
35 BASETYPE =
<replaceable class=
"PARAMETER">base_type
</replaceable>,
36 SFUNC =
<replaceable class=
"PARAMETER">sfunc
</replaceable>,
37 STYPE =
<replaceable class=
"PARAMETER">state_data_type
</replaceable>
38 [ , FINALFUNC =
<replaceable class=
"PARAMETER">ffunc
</replaceable> ]
39 [ , INITCOND =
<replaceable class=
"PARAMETER">initial_condition
</replaceable> ]
40 [ , SORTOP =
<replaceable class=
"PARAMETER">sort_operator
</replaceable> ]
46 <title>Description
</title>
49 <command>CREATE AGGREGATE
</command> defines a new aggregate
50 function. Some basic and commonly-used aggregate functions are
51 included with the distribution; they are documented in
<xref
52 linkend=
"functions-aggregate">. If one defines new types or needs
53 an aggregate function not already provided, then
<command>CREATE
54 AGGREGATE
</command> can be used to provide the desired features.
58 If a schema name is given (for example,
<literal>CREATE AGGREGATE
59 myschema.myagg ...<
/>) then the aggregate function is created in the
60 specified schema. Otherwise it is created in the current schema.
64 An aggregate function is identified by its name and input data type(s).
65 Two aggregates in the same schema can have the same name if they operate on
66 different input types. The
67 name and input data type(s) of an aggregate must also be distinct from
68 the name and input data type(s) of every ordinary function in the same
73 An aggregate function is made from one or two ordinary
75 a state transition function
76 <replaceable class=
"PARAMETER">sfunc
</replaceable>,
77 and an optional final calculation function
78 <replaceable class=
"PARAMETER">ffunc
</replaceable>.
79 These are used as follows:
81 <replaceable class=
"PARAMETER">sfunc
</replaceable>( internal-state, next-data-values ) ---> next-internal-state
82 <replaceable class=
"PARAMETER">ffunc
</replaceable>( internal-state ) ---> aggregate-value
87 <productname>PostgreSQL
</productname> creates a temporary variable
88 of data type
<replaceable class=
"PARAMETER">stype
</replaceable>
89 to hold the current internal state of the aggregate. At each input row,
90 the aggregate argument value(s) are calculated and
91 the state transition function is invoked with the current state value
92 and the new argument value(s) to calculate a new
93 internal state value. After all the rows have been processed,
94 the final function is invoked once to calculate the aggregate's return
95 value. If there is no final function then the ending state value
100 An aggregate function can provide an initial condition,
101 that is, an initial value for the internal state value.
102 This is specified and stored in the database as a value of type
103 <type>text
</type>, but it must be a valid external representation
104 of a constant of the state value data type. If it is not supplied
105 then the state value starts out null.
109 If the state transition function is declared
<quote>strict
</quote>,
110 then it cannot be called with null inputs. With such a transition
111 function, aggregate execution behaves as follows. Rows with any null input
112 values are ignored (the function is not called and the previous state value
113 is retained). If the initial state value is null, then at the first row
114 with all-nonnull input values, the first argument value replaces the state
115 value, and the transition function is invoked at subsequent rows with
116 all-nonnull input values.
117 This is handy for implementing aggregates like
<function>max
</function>.
118 Note that this behavior is only available when
119 <replaceable class=
"PARAMETER">state_data_type
</replaceable>
120 is the same as the first
121 <replaceable class=
"PARAMETER">input_data_type
</replaceable>.
122 When these types are different, you must supply a nonnull initial
123 condition or use a nonstrict transition function.
127 If the state transition function is not strict, then it will be called
128 unconditionally at each input row, and must deal with null inputs
129 and null transition values for itself. This allows the aggregate
130 author to have full control over the aggregate's handling of null values.
134 If the final function is declared
<quote>strict
</quote>, then it will not
135 be called when the ending state value is null; instead a null result
136 will be returned automatically. (Of course this is just the normal
137 behavior of strict functions.) In any case the final function has
138 the option of returning a null value. For example, the final function for
139 <function>avg
</function> returns null when it sees there were zero
144 Aggregates that behave like
<function>MIN<
/> or
<function>MAX<
/> can
145 sometimes be optimized by looking into an index instead of scanning every
146 input row. If this aggregate can be so optimized, indicate it by
147 specifying a
<firstterm>sort operator<
/>. The basic requirement is that
148 the aggregate must yield the first element in the sort ordering induced by
149 the operator; in other words:
151 SELECT agg(col) FROM tab;
153 must be equivalent to:
155 SELECT col FROM tab ORDER BY col USING sortop LIMIT
1;
157 Further assumptions are that the aggregate ignores null inputs, and that
158 it delivers a null result if and only if there were no non-null inputs.
159 Ordinarily, a data type's
<literal><<
/> operator is the proper sort
160 operator for
<function>MIN<
/>, and
<literal>><
/> is the proper sort
161 operator for
<function>MAX<
/>. Note that the optimization will never
162 actually take effect unless the specified operator is the
<quote>less
163 than
</quote> or
<quote>greater than
</quote> strategy member of a B-tree
164 index operator class.
169 <title>Parameters
</title>
173 <term><replaceable class=
"PARAMETER">name
</replaceable></term>
176 The name (optionally schema-qualified) of the aggregate function
183 <term><replaceable class=
"PARAMETER">input_data_type
</replaceable></term>
186 An input data type on which this aggregate function operates.
187 To create a zero-argument aggregate function, write
<literal>*<
/>
188 in place of the list of input data types. (An example of such an
189 aggregate is
<function>count(*)
</function>.)
195 <term><replaceable class=
"PARAMETER">base_type
</replaceable></term>
198 In the old syntax for
<command>CREATE AGGREGATE<
/>, the input data type
199 is specified by a
<literal>basetype<
/> parameter rather than being
200 written next to the aggregate name. Note that this syntax allows
201 only one input parameter. To define a zero-argument aggregate function,
202 specify the
<literal>basetype<
/> as
203 <literal>"ANY"<
/> (not
<literal>*<
/>).
209 <term><replaceable class=
"PARAMETER">sfunc
</replaceable></term>
212 The name of the state transition function to be called for each
213 input row. For an
<replaceable class=
"PARAMETER">N<
/>-argument
214 aggregate function, the
<replaceable class=
"PARAMETER">sfunc<
/>
215 must take
<replaceable class=
"PARAMETER">N<
/>+
1 arguments,
216 the first being of type
<replaceable
217 class=
"PARAMETER">state_data_type
</replaceable> and the rest
218 matching the declared input data type(s) of the aggregate.
219 The function must return a value of type
<replaceable
220 class=
"PARAMETER">state_data_type
</replaceable>. This function
221 takes the current state value and the current input data value(s),
222 and returns the next state value.
228 <term><replaceable class=
"PARAMETER">state_data_type
</replaceable></term>
231 The data type for the aggregate's state value.
237 <term><replaceable class=
"PARAMETER">ffunc
</replaceable></term>
240 The name of the final function called to compute the aggregate's
241 result after all input rows have been traversed. The function
242 must take a single argument of type
<replaceable
243 class=
"PARAMETER">state_data_type
</replaceable>. The return
244 data type of the aggregate is defined as the return type of this
245 function. If
<replaceable class=
"PARAMETER">ffunc
</replaceable>
246 is not specified, then the ending state value is used as the
247 aggregate's result, and the return type is
<replaceable
248 class=
"PARAMETER">state_data_type
</replaceable>.
254 <term><replaceable class=
"PARAMETER">initial_condition
</replaceable></term>
257 The initial setting for the state value. This must be a string
258 constant in the form accepted for the data type
<replaceable
259 class=
"PARAMETER">state_data_type
</replaceable>. If not
260 specified, the state value starts out null.
266 <term><replaceable class=
"PARAMETER">sort_operator
</replaceable></term>
269 The associated sort operator for a
<function>MIN<
/>- or
270 <function>MAX<
/>-like aggregate.
271 This is just an operator name (possibly schema-qualified).
272 The operator is assumed to have the same input data types as
273 the aggregate (which must be a single-argument aggregate).
280 The parameters of
<command>CREATE AGGREGATE
</command> can be
281 written in any order, not just the order illustrated above.
286 <title>Examples
</title>
289 See
<xref linkend=
"xaggr">.
294 <title>Compatibility
</title>
297 <command>CREATE AGGREGATE
</command> is a
298 <productname>PostgreSQL
</productname> language extension. The SQL
299 standard does not provide for user-defined aggregate functions.
304 <title>See Also
</title>
306 <simplelist type=
"inline">
307 <member><xref linkend=
"sql-alteraggregate" endterm=
"sql-alteraggregate-title"></member>
308 <member><xref linkend=
"sql-dropaggregate" endterm=
"sql-dropaggregate-title"></member>