How to create a SET-like type on postgresql

Use the HSTORE column type. HSTORE stores key/value pairs. You can use null values if you only care about checking if a key exists.

See https://www.postgresql.org/docs/current/static/hstore.html.

For example, to ask Is ‘x’ in my hstore?, do

CREATE EXTENSION HSTORE;  --create extension only has to be done once
SELECT * FROM 'x=>null,y=>null,z=>null'::HSTORE ? 'x';

I believe this is operation is O(1). In contrast, checking for containment in an ARRAY-type column, is O(n).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)