Home Explore Blog CI



postgresql

3rd chunk of `doc/src/sgml/ref/create_aggregate.sgml`
4c8efd6dccfeaca72ebd4d3df05296b1bcd4aa728aa6afd90000000100000fa1
   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> creates a temporary variable
   of data type <replaceable class="parameter">stype</replaceable>
   to hold the current internal state of the aggregate.  At each input row,
   the aggregate argument value(s) are calculated and
   the state transition function is invoked with the current state value
   and the new argument value(s) to calculate a new
   internal state value.  After all the rows have been processed,
   the final function is invoked once to calculate the aggregate's return
   value.  If there is no final function then the ending state value
   is returned as-is.
  </para>

  <para>
   An aggregate function can provide an initial condition,
   that is, an initial value for the internal state value.
   This is specified and stored in the database as a value of type
   <type>text</type>, but it must be a valid external representation
   of a constant of the state value data type.  If it is not supplied
   then the state value starts out null.
  </para>

  <para>
   If the state transition function is declared <quote>strict</quote>,
   then it cannot be called with null inputs.  With such a transition
   function, aggregate execution behaves as follows.  Rows with any 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 at the first row
   with all-nonnull input values, the first argument value replaces the state
   value, and the transition function is invoked at each subsequent row with
   all-nonnull input values.
   This is handy for implementing aggregates like <function>max</function>.
   Note that this behavior is only available when
   <replaceable class="parameter">state_data_type</replaceable>
   is the same as the first
   <replaceable class="parameter">arg_data_type</replaceable>.
   When these types are different, you must supply a nonnull initial
   condition or use a nonstrict transition function.
  </para>

  <para>
   If the state transition function is not strict, then it will be called
   unconditionally at each input row, and must deal with null inputs
   and null state values for itself.  This allows the aggregate
   author to have full control over the aggregate's handling of null values.
  </para>

  <para>
   If the final function is declared <quote>strict</quote>, then it will not
   be called when the ending state value is null; instead a null result
   will be returned automatically.  (Of course this is just the normal
   behavior of strict functions.)  In any case the final function has
   the option of returning a null value.  For example, the final function for
   <function>avg</function> returns null when it sees there were zero
   input rows.
  </para>

  <para>
   Sometimes it is useful to declare the final function as taking not just
   the state value, but extra parameters corresponding to the aggregate's
   input values.  The main reason for doing this is if the final function
   is polymorphic and the state

Title: Aggregate Function Mechanics
Summary
This section details how aggregate functions operate in PostgreSQL. It explains the use of state transition functions (sfunc) and final calculation functions (ffunc) to process input rows and derive the aggregate result. It covers the creation and handling of an internal state variable, the significance of initial conditions, and the behavior of strict vs. non-strict transition and final functions in managing null inputs and state values.