Re: Variable constants ?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: stan <stanb(at)panix(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Variable constants ?
Date: 2019-08-15 22:12:49
Message-ID: 31388.1565907169@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

stan <stanb(at)panix(dot)com> writes:
> Failing a better way is there some way I can limit this table to only allow
> one row to exist?

I was recently reminded of a cute trick for that: make a unique index
on a constant.

regression=# create table consts(f1 int, f2 int);
CREATE TABLE
regression=# create unique index consts_only_one on consts((1));
CREATE INDEX
regression=# insert into consts values(1,2);
INSERT 0 1
regression=# insert into consts values(3,4);
ERROR: duplicate key value violates unique constraint "consts_only_one"
DETAIL: Key ((1))=(1) already exists.

Shepard's nearby advice about keeping some history is probably a better
plan though.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Igor Neyman 2019-08-15 23:07:21 RE: Variable constants ?
Previous Message Rich Shepard 2019-08-15 21:27:26 Re: Variable constants ?