Re: Trying to create array of enum to array of text for exclusion constraint - Mailing list pgsql-general

From Emre Hasegeli
Subject Re: Trying to create array of enum to array of text for exclusion constraint
Date
Msg-id CAE2gYzzoaojoA0A8MVd0K0GMnGo9d_Vo=TsjGEPSfQ59RZhGqg@mail.gmail.com
Whole thread Raw
In response to Trying to create array of enum to array of text for exclusion constraint  (Steven Lembark <lembark@wrkhors.com>)
List 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}).


pgsql-general by date:

Previous
From: hubert depesz lubaczewski
Date:
Subject: Re: swarm of processes in BIND state?
Next
From: Martín Marqués
Date:
Subject: Re: BDR to ignore table exists error