Skip to content

Instantly share code, notes, and snippets.

@zelaznik
Created August 14, 2024 15:00
Show Gist options
  • Save zelaznik/8ada258960e9b4a7f30e9c2bc42e42c8 to your computer and use it in GitHub Desktop.
Save zelaznik/8ada258960e9b4a7f30e9c2bc42e42c8 to your computer and use it in GitHub Desktop.
Custom Agg Functions Notes
CREATE OR REPLACE FUNCTION my_count_state(state integer, value anyelement)
RETURNS integer AS $$
BEGIN
IF value IS NOT NULL THEN
RETURN state + 1;
ELSE
RETURN state;
END IF;
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION my_count_final(state integer)
RETURNS integer AS $$
BEGIN
RETURN state;
END;
$$ LANGUAGE plpgsql;
CREATE AGGREGATE my_count(anyelement) (
SFUNC = my_count_state,
STYPE = integer,
FINALFUNC = my_count_final,
INITCOND = '0'
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment