Home Explore Blog CI



postgresql

6th chunk of `doc/src/sgml/ref/create_aggregate.sgml`
5181b1e17a36014f861a97e56f8de2a825daf6b26b5a9fe70000000100000fa7
 <literal>VARIADIC</literal>.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><replaceable class="parameter">argname</replaceable></term>

    <listitem>
     <para>
      The name of an argument.  This is currently only useful for
      documentation purposes.  If omitted, the argument has no name.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><replaceable class="parameter">arg_data_type</replaceable></term>
    <listitem>
     <para>
      An input data type on which this aggregate function operates.
      To create a zero-argument aggregate function, write <literal>*</literal>
      in place of the list of argument specifications.  (An example of such an
      aggregate is <function>count(*)</function>.)
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><replaceable class="parameter">base_type</replaceable></term>
    <listitem>
     <para>
      In the old syntax for <command>CREATE AGGREGATE</command>, the input data type
      is specified by a <literal>basetype</literal> parameter rather than being
      written next to the aggregate name.  Note that this syntax allows
      only one input parameter.  To define a zero-argument aggregate function
      with this syntax, specify the <literal>basetype</literal> as
      <literal>"ANY"</literal> (not <literal>*</literal>).
      Ordered-set aggregates cannot be defined with the old syntax.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><replaceable class="parameter">sfunc</replaceable></term>
    <listitem>
     <para>
      The name of the state transition function to be called for each
      input row.  For a normal <replaceable class="parameter">N</replaceable>-argument
      aggregate function, the <replaceable class="parameter">sfunc</replaceable>
      must take <replaceable class="parameter">N</replaceable>+1 arguments,
      the first being of type <replaceable
      class="parameter">state_data_type</replaceable> and the rest
      matching the declared input data type(s) of the aggregate.
      The function must return a value of type <replaceable
      class="parameter">state_data_type</replaceable>.  This function
      takes the current state value and the current input data value(s),
      and returns the next state value.
     </para>

     <para>
      For ordered-set (including hypothetical-set) aggregates, the state
      transition function receives only the current state value and the
      aggregated arguments, not the direct arguments.  Otherwise it is the
      same.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><replaceable class="parameter">state_data_type</replaceable></term>
    <listitem>
     <para>
      The data type for the aggregate's state value.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><replaceable class="parameter">state_data_size</replaceable></term>
    <listitem>
     <para>
      The approximate average size (in bytes) of the aggregate's state value.
      If this parameter is omitted or is zero, a default estimate is used
      based on the <replaceable>state_data_type</replaceable>.
      The planner uses this value to estimate the memory required for a
      grouped aggregate query.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><replaceable class="parameter">ffunc</replaceable></term>
    <listitem>
     <para>
      The name of the final function called to compute the aggregate's
      result after all input rows have been traversed.
      For a normal aggregate, this function
      must take a single argument of type <replaceable
      class="parameter">state_data_type</replaceable>.  The return
      data type of the aggregate is defined as the return type of this
      function.  If <replaceable class="parameter">ffunc</replaceable>
      is not specified, then the ending state value is used as the
      aggregate's result,

Title: CREATE AGGREGATE Parameters: Input Types, State Transition, and Final Function
Summary
This section further defines parameters for the CREATE AGGREGATE command, elaborating on how to specify input data types using both new and old syntax, including handling VARIADIC arguments. It describes the state transition function (sfunc) that processes each input row, its arguments, and return type. It also details the state_data_type and state_data_size, and the final function (ffunc) which computes the final aggregate result from the state value.