create_aggregate.sgml 9.91 KB
Newer Older
1
<!--
2
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_aggregate.sgml,v 1.13 2001/01/20 20:59:29 petere Exp $
3 4 5
Postgres documentation
-->

6 7
<refentry id="SQL-CREATEAGGREGATE">
 <refmeta>
8
  <refentrytitle id="sql-createaggregate-title">
9
   CREATE AGGREGATE
10 11 12
  </refentrytitle>
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>
13

14 15
 <refnamediv>
  <refname>
16
   CREATE AGGREGATE
17 18
  </refname>
  <refpurpose>
19
   Defines a new aggregate function
20
  </refpurpose>
21
 </refnamediv>
22 23
 <refsynopsisdiv>
  <refsynopsisdivinfo>
24
   <date>2000-07-16</date>
25 26
  </refsynopsisdivinfo>
  <synopsis>
27 28
CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> ( BASETYPE = <replaceable class="PARAMETER">input_data_type</replaceable>,
    SFUNC = <replaceable class="PARAMETER">sfunc</replaceable>, STYPE = <replaceable class="PARAMETER">state_type</replaceable>
29
    [ , FINALFUNC = <replaceable class="PARAMETER">ffunc</replaceable> ]
30
    [ , INITCOND = <replaceable class="PARAMETER">initial_condition</replaceable> ] )
31
  </synopsis>
32

33 34
  <refsect2 id="R2-SQL-CREATEAGGREGATE-1">
   <refsect2info>
35
    <date>2000-07-16</date>
36 37
   </refsect2info>
   <title>
38
    Inputs
39 40 41 42 43 44 45 46 47 48 49
   </title>
   <para>
    <variablelist>
     <varlistentry>
      <term><replaceable class="PARAMETER">name</replaceable></term>
      <listitem>
       <para>
	The name of an aggregate function to create.
       </para>
      </listitem>
     </varlistentry>
50

51
     <varlistentry>
52
      <term><replaceable class="PARAMETER">input_data_type</replaceable></term>
53 54
      <listitem>
       <para>
55
	The input data type on which this aggregate function operates.
56 57 58
	This can be specified as ANY for an aggregate that does not
	examine its input values
	(an example is <function>count(*)</function>).
59 60 61
       </para>
      </listitem>
     </varlistentry>
62

63
     <varlistentry>
64
      <term><replaceable class="PARAMETER">sfunc</replaceable></term>
65 66
      <listitem>
       <para>
67 68 69 70
	The name of the state transition function
	to be called for each input data value.
	This is normally a function of two arguments, the first being of
	type <replaceable class="PARAMETER">state_type</replaceable>
71 72
	and the second of
	type <replaceable class="PARAMETER">input_data_type</replaceable>.
73 74 75 76 77 78 79
	Alternatively, for an aggregate that does not examine its input
	values, the function takes just one argument of
	type <replaceable class="PARAMETER">state_type</replaceable>.
	In either case the function must return a value of
	type <replaceable class="PARAMETER">state_type</replaceable>.
	This function takes the current state value and the current
	input data item, and returns the next state value.
80 81 82
       </para>
      </listitem>
     </varlistentry>
83

84
     <varlistentry>
85
      <term><replaceable class="PARAMETER">state_type</replaceable></term>
86 87
      <listitem>
       <para>
88
	The data type for the aggregate's state value.
89 90 91
       </para>
      </listitem>
     </varlistentry>
92

93 94 95 96
     <varlistentry>
      <term><replaceable class="PARAMETER">ffunc</replaceable></term>
      <listitem>
       <para>
97 98 99 100
	The name of the final function called to compute the aggregate's
	result after all input data has been traversed.  The function
	must take a single argument of type
	<replaceable class="PARAMETER">state_type</replaceable>.
Bruce Momjian's avatar
Bruce Momjian committed
101
	The output data type of the aggregate is defined as the return
102
	type of this function.
103 104 105 106
	If <replaceable class="PARAMETER">ffunc</replaceable>
	is not specified, then the ending state value is used as the
	aggregate's result, and the output type is
	<replaceable class="PARAMETER">state_type</replaceable>.
107 108 109
       </para>
      </listitem>
     </varlistentry>
110

111
     <varlistentry>
112
      <term><replaceable class="PARAMETER">initial_condition</replaceable></term>
113 114
      <listitem>
       <para>
115
	The initial setting for the state value.  This must be a literal
Bruce Momjian's avatar
Bruce Momjian committed
116
	constant in the form accepted for the data type
117 118
	<replaceable class="PARAMETER">state_type</replaceable>.
	If not specified, the state value starts out NULL.
119 120
       </para>
      </listitem>
121 122 123
     </varlistentry>
    </variablelist>
   </para>
124 125 126 127 128 129 130
  </refsect2>

  <refsect2 id="R2-SQL-CREATEAGGREGATE-2">
   <refsect2info>
    <date>1998-09-09</date>
   </refsect2info>
   <title>
131
    Outputs
132 133 134 135 136 137 138 139 140 141
   </title>
   <para>

    <variablelist>
     <varlistentry>
      <term><computeroutput>
CREATE
       </computeroutput></term>
      <listitem>
       <para>
142 143 144 145
	Message returned if the command completes successfully.
       </para>
      </listitem>
     </varlistentry>
146
    </variablelist>
147
   </para>
148 149 150 151 152
  </refsect2>
 </refsynopsisdiv>

 <refsect1 id="R1-SQL-CREATEAGGREGATE-1">
  <refsect1info>
153
   <date>2000-07-16</date>
154 155
  </refsect1info>
  <title>
156
   Description
157
  </title>
158 159 160 161
  <para>
   <command>CREATE AGGREGATE</command> 
   allows a user or programmer to extend <productname>Postgres</productname>
   functionality by defining new aggregate functions. Some aggregate functions
162 163
   for base types such as <function>min(integer)</function>
   and <function>avg(double precision)</function> are already provided in the base
164
   distribution. If one defines new types or needs an aggregate function not
165
   already provided, then <command>CREATE AGGREGATE</command>
166 167
   can be used to provide the desired features.
  </para>
168
  <para>
169 170 171 172 173 174
   An  aggregate  function is identified by its name and input data type.
   Two aggregates can have the same name if they operate on different
   input types.  To avoid confusion, do not make an ordinary function
   of the same name and input data type as an aggregate.
  </para>
  <para>
175
   An  aggregate function is made from one or two ordinary
176
   functions:
177 178 179
   a state transition function
   <replaceable class="PARAMETER">sfunc</replaceable>,
   and an optional final calculation function
180 181
   <replaceable class="PARAMETER">ffunc</replaceable>.
   These are used as follows:
182
   <programlisting>
183 184
<replaceable class="PARAMETER">sfunc</replaceable>( internal-state, next-data-item ) ---> next-internal-state
<replaceable class="PARAMETER">ffunc</replaceable>( internal-state ) ---> aggregate-value
185 186 187
   </programlisting>
  </para>
  <para>
188 189 190 191 192 193
   <productname>Postgres</productname> creates a temporary variable
   of data type <replaceable class="PARAMETER">stype</replaceable>
   to hold the current internal state of the aggregate.  At each input
   data item,
   the state transition function is invoked to calculate a new
   internal state value.  After all the data has been processed,
194
   the final function is invoked once to calculate the aggregate's output
195 196
   value.  If there is no final function then the ending state value
   is returned as-is.
197
  </para>
198
  
199
  <para>
200 201 202 203
   An aggregate function may provide an initial condition,
   that is, an initial value for the internal state value.
   This is specified and stored in the database as a field of type
   <type>text</type>, but it must be a valid external representation
Bruce Momjian's avatar
Bruce Momjian committed
204
   of a constant of the state value data type.  If it is not supplied
205 206
   then the state value starts out NULL.
  </para>
207
  
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  <para>
   If the state transition function is declared "strict" in pg_proc,
   then it cannot be called with NULL inputs.  With such a transition
   function, aggregate execution behaves as follows.  NULL input values
   are ignored (the function is not called and the previous state value
   is retained).  If the initial state value is NULL, then the first
   non-NULL input value replaces the state value, and the transition
   function is invoked beginning with the second non-NULL input value.
   This is handy for implementing aggregates like <function>max</function>.
   Note that this behavior is only available when
   <replaceable class="PARAMETER">state_type</replaceable>
   is the same as
   <replaceable class="PARAMETER">input_data_type</replaceable>.
   When these types are different, you must supply a non-NULL initial
   condition or use a non-strict transition function.
  </para>
  
  <para>
   If the state transition function is not strict, then it will be called
   unconditionally at each input value, and must deal with NULL inputs
   and NULL transition values for itself.  This allows the aggregate
   author to have full control over the aggregate's handling of NULLs.
  </para>
  
  <para>
   If the final function is declared "strict", then it will not
   be called when the ending state value is NULL; instead a NULL result
   will be output automatically.  (Of course this is just the normal
   behavior of strict functions.)  In any case the final function has
   the option of returning NULL.  For example, the final function for
   <function>avg</function> returns NULL when it sees there were zero
   input tuples.
240 241
  </para>
  
242 243
  <refsect2 id="R2-SQL-CREATEAGGREGATE-3">
   <refsect2info>
244
    <date>2000-07-16</date>
245 246
   </refsect2info>
   <title>
247
    Notes
248
   </title>
249
   <para>
250
    Use <command>DROP AGGREGATE</command>
251
    to drop aggregate functions.
252
   </para>
253

254 255 256 257 258
   <para>
    The parameters of <command>CREATE AGGREGATE</command> can be written
    in any order, not just the order illustrated above.
   </para>

259
  </refsect2>
260 261
 </refsect1>

262 263
 <refsect1 id="R1-SQL-CREATEAGGREGATE-2">
  <title>
264
   Usage
265 266
  </title>
  <para>
267
   Refer to the chapter on aggregate functions
268
   in the <citetitle>PostgreSQL Programmer's Guide</citetitle> for
269
   complete examples of usage.
270
  </para>
271 272 273 274
 </refsect1>

 <refsect1 id="R1-SQL-CREATEAGGREGATE-3">
  <title>
275
   Compatibility
276 277 278 279 280 281 282
  </title>

  <refsect2 id="R2-SQL-CREATEAGGREGATE-4">
   <refsect2info>
    <date>1998-09-09</date>
   </refsect2info>
   <title>
283
    SQL92
284 285
   </title>
   <para>
286
    <command>CREATE AGGREGATE</command> 
287
    is a <productname>Postgres</productname> language extension.
288
    There is no <command>CREATE AGGREGATE</command> in SQL92.
289
   </para>
290 291
  </refsect2>
 </refsect1>
292
</refentry>
293 294 295 296

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
297
sgml-omittag:nil
298 299 300 301 302 303 304 305 306 307 308 309
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"../reference.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:"/usr/lib/sgml/catalog"
sgml-local-ecat-files:nil
End:
-->