The FINAL function

The FINAL function converts the internal result to the result type that it returns to the user. For example, for the AVG aggregate, the FINAL function returns the current sum divided by the current row count.

The FINAL function is not required for aggregates that are derived from simple binary operators whose result type is the same as the state type and the column type. If you do not define a FINAL function, the database server simply returns the final state.

In C, the FINAL function can perform cleanup work to release resources that the INIT function allocated. However, it must not free the state itself.