Skip to content

Instantly share code, notes, and snippets.

@ryandotsmith
Created January 23, 2013 05:19
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ryandotsmith/4602274 to your computer and use it in GitHub Desktop.
Save ryandotsmith/4602274 to your computer and use it in GitHub Desktop.
Postgres array concatenation aggregate function.
CREATE AGGREGATE array_accum (anyarray)
(
sfunc = array_cat,
stype = anyarray,
initcond = '{}'
);
@billiegoose
Copy link

This is awesome man!

@tkalfigo
Copy link

Useful and works like a charm. Perhaps consider proposing on the pg-dev list to include it in future version.

@richddr
Copy link

richddr commented Nov 20, 2019

This is pretty sweet 👍

@andcri
Copy link

andcri commented Feb 24, 2020

Great!

@Medosopher
Copy link

Great Job. Thanks.

@jbdesbas
Copy link

jbdesbas commented Feb 1, 2021

Simple and useful ! Thanks !

@bf
Copy link

bf commented Jul 13, 2022

had to change it for pg 14:

CREATE AGGREGATE array_accum (anycompatiblearray)
(
    sfunc = array_cat,
    stype = anycompatiblearray,
    initcond = '{}'
);  

@WhereRtheInterwebs
Copy link

WhereRtheInterwebs commented Apr 22, 2023

This is great! Is it possible to adapt this so that you can make the array items distinct?
Example of what I'm thinking:
array_accum(distinct "duplicateList")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment