Home Explore Blog CI



postgresql

2nd chunk of `doc/src/sgml/ref/create_aggregate.sgml`
dbebe095290fe53c49f9a940d38785d33cea4e94e248c3f40000000100000fb4
    [ , FINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE } ]
    [ , INITCOND = <replaceable class="parameter">initial_condition</replaceable> ]
    [ , PARALLEL = { SAFE | RESTRICTED | UNSAFE } ]
    [ , HYPOTHETICAL ]
)

<phrase>or the old syntax</phrase>

CREATE [ OR REPLACE ] AGGREGATE <replaceable class="parameter">name</replaceable> (
    BASETYPE = <replaceable class="parameter">base_type</replaceable>,
    SFUNC = <replaceable class="parameter">sfunc</replaceable>,
    STYPE = <replaceable class="parameter">state_data_type</replaceable>
    [ , SSPACE = <replaceable class="parameter">state_data_size</replaceable> ]
    [ , FINALFUNC = <replaceable class="parameter">ffunc</replaceable> ]
    [ , FINALFUNC_EXTRA ]
    [ , FINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE } ]
    [ , COMBINEFUNC = <replaceable class="parameter">combinefunc</replaceable> ]
    [ , SERIALFUNC = <replaceable class="parameter">serialfunc</replaceable> ]
    [ , DESERIALFUNC = <replaceable class="parameter">deserialfunc</replaceable> ]
    [ , INITCOND = <replaceable class="parameter">initial_condition</replaceable> ]
    [ , MSFUNC = <replaceable class="parameter">msfunc</replaceable> ]
    [ , MINVFUNC = <replaceable class="parameter">minvfunc</replaceable> ]
    [ , MSTYPE = <replaceable class="parameter">mstate_data_type</replaceable> ]
    [ , MSSPACE = <replaceable class="parameter">mstate_data_size</replaceable> ]
    [ , MFINALFUNC = <replaceable class="parameter">mffunc</replaceable> ]
    [ , MFINALFUNC_EXTRA ]
    [ , MFINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE } ]
    [ , MINITCOND = <replaceable class="parameter">minitial_condition</replaceable> ]
    [ , SORTOP = <replaceable class="parameter">sort_operator</replaceable> ]
)
</synopsis>
 </refsynopsisdiv>

 <refsect1>
  <title>Description</title>

  <para>
   <command>CREATE AGGREGATE</command> defines a new aggregate function.
   <command>CREATE OR REPLACE AGGREGATE</command> will either define a new
   aggregate function or replace an existing definition. Some basic and
   commonly-used aggregate functions are included with the distribution; they
   are documented in <xref linkend="functions-aggregate"/>. If one defines new
   types or needs an aggregate function not already provided, then
   <command>CREATE AGGREGATE</command> can be used to provide the desired
   features.
  </para>

  <para>
   When replacing an existing definition, the argument types, result type,
   and number of direct arguments may not be changed. Also, the new definition
   must be of the same kind (ordinary aggregate, ordered-set aggregate, or
   hypothetical-set aggregate) as the old one.
  </para>

  <para>
   If a schema name is given (for example, <literal>CREATE AGGREGATE
   myschema.myagg ...</literal>) then the aggregate function is created in the
   specified schema.  Otherwise it is created in the current schema.
  </para>

  <para>
   An aggregate function is identified by its name and input data type(s).
   Two aggregates in the same schema can have the same name if they operate on
   different input types.  The
   name and input data type(s) of an aggregate must also be distinct from
   the name and input data type(s) of every ordinary function in the same
   schema.
   This behavior is identical to overloading of ordinary function names
   (see <xref linkend="sql-createfunction"/>).
  </para>

  <para>
   A simple aggregate function is made from one or two ordinary
   functions:
   a state transition function
   <replaceable class="parameter">sfunc</replaceable>,
   and an optional final calculation function
   <replaceable class="parameter">ffunc</replaceable>.
   These are used as follows:
<programlisting>
<replaceable class="parameter">sfunc</replaceable>( internal-state, next-data-values ) ---> next-internal-state
<replaceable class="parameter">ffunc</replaceable>( internal-state ) ---> aggregate-value
</programlisting>
  </para>

  <para>
   <productname>PostgreSQL</productname>

Title: CREATE AGGREGATE Description
Summary
This section describes the CREATE AGGREGATE command, which defines a new aggregate function in PostgreSQL. It covers replacing existing definitions, schema considerations, function identification by name and input data types, and the basic structure of an aggregate function comprising a state transition function (sfunc) and an optional final calculation function (ffunc).