Re: Trying to create array of enum to array of text for exclusion constraint

From: Emre Hasegeli <emre(at)hasegeli(dot)com>
To: Steven Lembark <lembark(at)wrkhors(dot)com>
Cc: PostgreSQL General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Trying to create array of enum to array of text for exclusion constraint
Date: 2016-05-28 09:55:33
Message-ID: CAE2gYzzoaojoA0A8MVd0K0GMnGo9d_Vo=TsjGEPSfQ59RZhGqg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> or is there something built in that I have missed?

The intarray extension in the contrib provides a GiST operator class
for int[]. That can be used with exclusion constraints:

> hasegeli=# create extension intarray;
> CREATE EXTENSION
>
> hasegeli=# create type e as enum ('a', 'b');
> CREATE TYPE
>
> hasegeli=# create table t (es e[]);
> CREATE TABLE
>
> hasegeli=# create function es_to_int (e[]) returns int[] language sql immutable as $$
> select array_agg(oid::int) from pg_enum
> where enumtypid = (select oid from pg_type where typname = 'e')
> and enumlabel = any($1::text[])$$;
> CREATE FUNCTION
>
> hasegeli=# alter table t add exclude using gist (es_to_int(es) with &&);
> ALTER TABLE
>
> hasegeli=# insert into t values ('{a,b}');
> INSERT 0 1
>
> hasegeli=# insert into t values ('{a}');
> ERROR: conflicting key value violates exclusion constraint "t_es_to_int_excl"
> DETAIL: Key (es_to_int(es))=({114830}) conflicts with existing key (es_to_int(es))=({114830,114832}).

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Martín Marqués 2016-05-28 11:26:04 Re: BDR to ignore table exists error
Previous Message hubert depesz lubaczewski 2016-05-28 06:03:51 Re: swarm of processes in BIND state?