Storing bitsets in PostgreSQL?
Can you use the "bit" type instead of "bytea", and use your own user-defined aggregate?

postgres=&gt; CREATE AGGREGATE bitset_or ("bit")
postgres-&gt; ( sfunc    = bitor,
postgres-&gt;   stype    = "bit",
postgres-&gt;   initcond =   '0'  );


Now bitset_or should work with your query as written (again, assuming bytea -> bit).