How do you use the CREATE AGGREGATE statement to create a new user-defined aggregate function?
Posted by LeoRobs
Last Updated: June 01, 2024
The CREATE AGGREGATE statement in PostgreSQL allows you to define a user-defined aggregate function. An aggregate function computes a single result from a set of input values, and the CREATE AGGREGATE statement provides the flexibility to define custom behavior for how these values are aggregated. Here's a step-by-step guide on how to create a user-defined aggregate function using the CREATE AGGREGATE statement:
Steps to Create a User-Defined Aggregate Function
1. Define Transition Functions: Transition functions are responsible for combining two values into an intermediate state. You will need to create one or more transition functions before creating the aggregate. 2. Create the Aggregate: Use the CREATE AGGREGATE statement to define the aggregate function. You will need to specify the data type of the input values, the intermediate state, and the transition function.
Syntax
CREATE AGGREGATE name (
    SFUNC = transition_function,
    STYPE = intermediate_type,
    INITCOND = initial_condition
);
- name: The name of the aggregate function you're creating. - SFUNC: The transition function that takes the current state and a new input value and produces a new state. - STYPE: The data type of the intermediate state. - INITCOND: (Optional) The initial condition for the aggregate, typically the initial value of your aggregate.
Example
Suppose we want to create a user-defined aggregate function to compute the average of a set of integers. 1. Create the Transition Function: We'll first create a simple transition function that maintains the sum and count.
CREATE FUNCTION avg_int_state(state integer[], value integer) RETURNS integer[] AS $$
BEGIN
    state[1] := state[1] + value;           -- sum
    state[2] := state[2] + 1;               -- count
    RETURN state;
END;
$$ LANGUAGE plpgsql;
2. Create the Aggregate: Now, we can create the aggregate function using the transition function.
CREATE AGGREGATE avg_int (
    SFUNC = avg_int_state,
    STYPE = integer[],
    INITCOND = '{0,0}'  -- Initial state: {sum, count}
);
3. Final Calculation Function (optional): Lastly, we might want a function to finalize the average calculation from the accumulated state.
CREATE FUNCTION finalize_avg(state integer[]) RETURNS float AS $$
BEGIN
    IF state[2] = 0 THEN  -- Avoid division by zero
        RETURN NULL;
    ELSE
        RETURN state[1]::float / state[2];  -- Calculate average
    END IF;
END;
$$ LANGUAGE plpgsql;
4. Finalizing the Aggregate (update the aggregate to include a final function):
CREATE AGGREGATE avg_int (
    SFUNC = avg_int_state,
    STYPE = integer[],
    INITCOND = '{0,0}',
    FINALFUNC = finalize_avg  -- Add the finalization step
);
Usage
Once the user-defined aggregate avg_int is defined, you can use it in a query like any standard aggregate function:
SELECT avg_int(column_name) FROM your_table;
Conclusion
Creating user-defined aggregate functions in PostgreSQL allows for custom aggregations tailored to specific application needs. By following the above steps and understanding the functionality of transition functions, you can effectively define how your aggregate should behave.
Related Content