Skip to content

Instantly share code, notes, and snippets.

@phillip-haydon
Created December 6, 2016 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillip-haydon/02e1cda346b4900a3e0090cefe85b176 to your computer and use it in GitHub Desktop.
Save phillip-haydon/02e1cda346b4900a3e0090cefe85b176 to your computer and use it in GitHub Desktop.
starting of a postgresql jsonb aggregate function to merge documents together
create or replace function jsonb_or(state jsonb[], current_val jsonb) returns jsonb[] as $$
if (!state) {
state = [];
}
state.push(current_val);
return state;
$$ language plv8;
create or replace function jsonb_or_final(state jsonb[]) returns int as $$
-- todo or the docs together...
return state.length;
$$ language plv8;
create aggregate jsonb_or_agg(jsonb)
(
sfunc = jsonb_or,
stype = jsonb[],
finalfunc = jsonb_or_final
);
select jsonb_or_agg(data) from test_actions group by action_set;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment