1CREATE AGGREGATE(7) SQL Commands CREATE AGGREGATE(7)
2
3
4
6 CREATE AGGREGATE - define a new aggregate function
7
8
10 CREATE AGGREGATE name ( input_data_type [ , ... ] ) (
11 SFUNC = sfunc,
12 STYPE = state_data_type
13 [ , FINALFUNC = ffunc ]
14 [ , INITCOND = initial_condition ]
15 [ , SORTOP = sort_operator ]
16 )
17
18 or the old syntax
19
20 CREATE AGGREGATE name (
21 BASETYPE = base_type,
22 SFUNC = sfunc,
23 STYPE = state_data_type
24 [ , FINALFUNC = ffunc ]
25 [ , INITCOND = initial_condition ]
26 [ , SORTOP = sort_operator ]
27 )
28
29
31 CREATE AGGREGATE defines a new aggregate function. Some basic and com‐
32 monly-used aggregate functions are included with the distribution; they
33 are documented in in the documentation. If one defines new types or
34 needs an aggregate function not already provided, then CREATE AGGREGATE
35 can be used to provide the desired features.
36
37 If a schema name is given (for example, CREATE AGGREGATE myschema.myagg
38 ...) then the aggregate function is created in the specified schema.
39 Otherwise it is created in the current schema.
40
41 An aggregate function is identified by its name and input data type(s).
42 Two aggregates in the same schema can have the same name if they oper‐
43 ate on different input types. The name and input data type(s) of an
44 aggregate must also be distinct from the name and input data type(s) of
45 every ordinary function in the same schema.
46
47 An aggregate function is made from one or two ordinary functions: a
48 state transition function sfunc, and an optional final calculation
49 function ffunc. These are used as follows:
50
51 sfunc( internal-state, next-data-values ) ---> next-internal-state
52 ffunc( internal-state ) ---> aggregate-value
53
54
55 PostgreSQL creates a temporary variable of data type stype to hold the
56 current internal state of the aggregate. At each input row, the aggre‐
57 gate argument value(s) are calculated and the state transition function
58 is invoked with the current state value and the new argument value(s)
59 to calculate a new internal state value. After all the rows have been
60 processed, the final function is invoked once to calculate the aggre‐
61 gate's return value. If there is no final function then the ending
62 state value is returned as-is.
63
64 An aggregate function can provide an initial condition, that is, an
65 initial value for the internal state value. This is specified and
66 stored in the database as a value of type text, but it must be a valid
67 external representation of a constant of the state value data type. If
68 it is not supplied then the state value starts out null.
69
70 If the state transition function is declared ``strict'', then it cannot
71 be called with null inputs. With such a transition function, aggregate
72 execution behaves as follows. Rows with any null input values are
73 ignored (the function is not called and the previous state value is
74 retained). If the initial state value is null, then at the first row
75 with all-nonnull input values, the first argument value replaces the
76 state value, and the transition function is invoked at subsequent rows
77 with all-nonnull input values. This is handy for implementing aggre‐
78 gates like max. Note that this behavior is only available when
79 state_data_type is the same as the first input_data_type. When these
80 types are different, you must supply a nonnull initial condition or use
81 a nonstrict transition function.
82
83 If the state transition function is not strict, then it will be called
84 unconditionally at each input row, and must deal with null inputs and
85 null transition values for itself. This allows the aggregate author to
86 have full control over the aggregate's handling of null values.
87
88 If the final function is declared ``strict'', then it will not be
89 called when the ending state value is null; instead a null result will
90 be returned automatically. (Of course this is just the normal behavior
91 of strict functions.) In any case the final function has the option of
92 returning a null value. For example, the final function for avg returns
93 null when it sees there were zero input rows.
94
95 Aggregates that behave like MIN or MAX can sometimes be optimized by
96 looking into an index instead of scanning every input row. If this
97 aggregate can be so optimized, indicate it by specifying a sort opera‐
98 tor. The basic requirement is that the aggregate must yield the first
99 element in the sort ordering induced by the operator; in other words:
100
101 SELECT agg(col) FROM tab;
102
103 must be equivalent to:
104
105 SELECT col FROM tab ORDER BY col USING sortop LIMIT 1;
106
107 Further assumptions are that the aggregate ignores null inputs, and
108 that it delivers a null result if and only if there were no non-null
109 inputs. Ordinarily, a data type's < operator is the proper sort opera‐
110 tor for MIN, and > is the proper sort operator for MAX. Note that the
111 optimization will never actually take effect unless the specified oper‐
112 ator is the ``less than'' or ``greater than'' strategy member of a B-
113 tree index operator class.
114
116 name The name (optionally schema-qualified) of the aggregate function
117 to create.
118
119 input_data_type
120 An input data type on which this aggregate function operates.
121 To create a zero-argument aggregate function, write * in place
122 of the list of input data types. (An example of such an aggre‐
123 gate is count(*).)
124
125 base_type
126 In the old syntax for CREATE AGGREGATE, the input data type is
127 specified by a basetype parameter rather than being written next
128 to the aggregate name. Note that this syntax allows only one
129 input parameter. To define a zero-argument aggregate function,
130 specify the basetype as "ANY" (not *).
131
132 sfunc The name of the state transition function to be called for each
133 input row. For an N-argument aggregate function, the sfunc must
134 take N+1 arguments, the first being of type state_data_type and
135 the rest matching the declared input data type(s) of the aggre‐
136 gate. The function must return a value of type state_data_type.
137 This function takes the current state value and the current
138 input data value(s), and returns the next state value.
139
140 state_data_type
141 The data type for the aggregate's state value.
142
143 ffunc The name of the final function called to compute the aggregate's
144 result after all input rows have been traversed. The function
145 must take a single argument of type state_data_type. The return
146 data type of the aggregate is defined as the return type of this
147 function. If ffunc is not specified, then the ending state value
148 is used as the aggregate's result, and the return type is
149 state_data_type.
150
151 initial_condition
152 The initial setting for the state value. This must be a string
153 constant in the form accepted for the data type state_data_type.
154 If not specified, the state value starts out null.
155
156 sort_operator
157 The associated sort operator for a MIN- or MAX-like aggregate.
158 This is just an operator name (possibly schema-qualified). The
159 operator is assumed to have the same input data types as the
160 aggregate (which must be a single-argument aggregate).
161
162 The parameters of CREATE AGGREGATE can be written in any order, not
163 just the order illustrated above.
164
166 See in the documentation.
167
169 CREATE AGGREGATE is a PostgreSQL language extension. The SQL standard
170 does not provide for user-defined aggregate functions.
171
173 ALTER AGGREGATE [alter_aggregate(7)], DROP AGGREGATE [drop_aggre‐
174 gate(7)]
175
176
177
178SQL - Language Statements 2014-02-17 CREATE AGGREGATE(7)