Home Explore Blog CI



postgresql

7th chunk of `doc/src/sgml/ref/create_aggregate.sgml`
d4d4921115b0d86c7be4bd700a0281cb2cc588a95e860b070000000100000fa1
 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, and the return type is <replaceable
      class="parameter">state_data_type</replaceable>.
     </para>

     <para>
      For ordered-set (including hypothetical-set) aggregates, the
      final function receives not only the final state value,
      but also the values of all the direct arguments.
     </para>

     <para>
      If <literal>FINALFUNC_EXTRA</literal> is specified, then in addition to the
      final state value and any direct arguments, the final function
      receives extra NULL values corresponding to the aggregate's regular
      (aggregated) arguments.  This is mainly useful to allow correct
      resolution of the aggregate result type when a polymorphic aggregate
      is being defined.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><literal>FINALFUNC_MODIFY</literal> = { <literal>READ_ONLY</literal> | <literal>SHAREABLE</literal> | <literal>READ_WRITE</literal> }</term>
    <listitem>
     <para>
      This option specifies whether the final function is a pure function
      that does not modify its arguments.  <literal>READ_ONLY</literal> indicates
      it does not; the other two values indicate that it may change the
      transition state value.  See <xref linkend="sql-createaggregate-notes"/>
      below for more detail.  The
      default is <literal>READ_ONLY</literal>, except for ordered-set aggregates,
      for which the default is <literal>READ_WRITE</literal>.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><replaceable class="parameter">combinefunc</replaceable></term>
    <listitem>
     <para>
      The <replaceable class="parameter">combinefunc</replaceable> function
      may optionally be specified to allow the aggregate function to support
      partial aggregation.  If provided,
      the <replaceable class="parameter">combinefunc</replaceable> must
      combine two <replaceable class="parameter">state_data_type</replaceable>
      values, each containing the result of aggregation over some subset of
      the input values, to produce a
      new <replaceable class="parameter">state_data_type</replaceable> that
      represents the result of aggregating over both sets of inputs.  This
      function can be thought of as
      an <replaceable class="parameter">sfunc</replaceable>, where instead of
      acting upon an individual input row and adding it to the running
      aggregate state, it adds another aggregate state to the running state.
     </para>

     <para>
      The <replaceable class="parameter">combinefunc</replaceable> must be
      declared as taking two arguments of
      the <replaceable class="parameter">state_data_type</replaceable> and
      returning a value of
      the <replaceable class="parameter">state_data_type</replaceable>.
      Optionally this function may be <quote>strict</quote>. In this case the
      function will not be called when either of the input states are null;
      the other state will be taken as

Title: CREATE AGGREGATE Parameters: Final Function, Modification, and Combine Function
Summary
This section describes the 'ffunc' parameter which is the final function called to compute the aggregate result. It details the arguments it receives (state value, direct arguments, and potentially extra NULL values), how the return type is determined, and the FINALFUNC_MODIFY option which indicates whether the final function modifies its arguments. It also introduces the 'combinefunc' which supports partial aggregation by combining two aggregate states into a single state.