Re: unique constraint - Mailing list pgsql-novice

From Tom Lane
Subject Re: unique constraint
Date
Msg-id 13949.1334690333@sss.pgh.pa.us
Whole thread Raw
In response to unique constraint  ("Roberto Caravani" <JFanatiker@gmx.at>)
List pgsql-novice
"Roberto Caravani" <JFanatiker@gmx.at> writes:
> I have a little problem with a unique constraint:

> create table meta_data (
>     id                serial        primary key,
>     type            int            not null,
>     data            bytea        not null,
>     unique (type, data)
> );

> data can be quite large (a few hundred kilo bytes). The unique constraint automatically creates a btree index as I
learnedfrom the documentation. The problem is that I get an error when inserting data, that the index is to large. 

You mean that an index entry is too large, because you have a wide
"data" value?

The usual hack for that is to put a unique index on some hash of the
wide column, trusting that you won't get hash collisions.  So for
instance

create unique index meta_data_unique on meta_data (type, md5(data));

            regards, tom lane

pgsql-novice by date:

Previous
From: "ktm@rice.edu"
Date:
Subject: Re: unique constraint
Next
From: Willy-Bas Loos
Date:
Subject: Re: Escaping literal strings in pg_8.4