Re: Create a deferrably-unique index

Lists: pgsql-general
From: Paul Jungwirth <pj(at)illuminatedcomputing(dot)com>
To: pgsql <pgsql-general(at)postgresql(dot)org>
Subject: Create a deferrably-unique index
Date: 2013-08-19 17:45:50
Message-ID: CA+6hpakfbvG4MVWD_UzQPUT8+NjkbqS1bvKRUTENz5pu77Vg3Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

I'm trying to create a unique index where the unique constraint is
`deferrable initially immediate`. But I don't see any way to do this
in the syntax of the `create index` command. It looks like the only
way to do it is via `alter table foo add unique`. Is that right, or
can I do it as part of `create index`?

If I have to use `alter table add unique`, is there any way I can make
sure the implicitly-created index also has a `where` clause? Or is it
impossible to create an index that is unique + deferrable + partial?

Thank you,
Paul

--
_________________________________
Pulchritudo splendor veritatis.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Paul Jungwirth <pj(at)illuminatedcomputing(dot)com>
Cc: pgsql <pgsql-general(at)postgresql(dot)org>
Subject: Re: Create a deferrably-unique index
Date: 2013-08-19 18:34:21
Message-ID: 23968.1376937261@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Paul Jungwirth <pj(at)illuminatedcomputing(dot)com> writes:
> I'm trying to create a unique index where the unique constraint is
> `deferrable initially immediate`. But I don't see any way to do this
> in the syntax of the `create index` command. It looks like the only
> way to do it is via `alter table foo add unique`. Is that right, or
> can I do it as part of `create index`?

Deferrability is a property of a constraint, not an index, so you can
only specify it for indexes that are associated with constraints.
Yes, that limits the kinds of indexes that can be used ...

regards, tom lane


From: Paul Jungwirth <pj(at)illuminatedcomputing(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql <pgsql-general(at)postgresql(dot)org>
Subject: Re: Create a deferrably-unique index
Date: 2013-08-19 18:43:57
Message-ID: CA+6hpa=eoeL=NcTRnJPy5ZDrhm2fpXpmF4LTKq_wawFSkdJayw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

> Deferrability is a property of a constraint, not an index

Yes, but creating a unique constraint implicitly creates an index, and
creating a unique index implicitly creates a constraint. So I'm
wondering whether I can create a pair where the index is partial and
the constraint is deferrable. It sounds like the answer is no? Is
there a workaround where I first create one and then alter the other
one?

Thanks,
Paul

--
_________________________________
Pulchritudo splendor veritatis.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Paul Jungwirth <pj(at)illuminatedcomputing(dot)com>
Cc: pgsql <pgsql-general(at)postgresql(dot)org>
Subject: Re: Create a deferrably-unique index
Date: 2013-08-19 19:01:34
Message-ID: 24779.1376938894@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Paul Jungwirth <pj(at)illuminatedcomputing(dot)com> writes:
>> Deferrability is a property of a constraint, not an index

> Yes, but creating a unique constraint implicitly creates an index, and
> creating a unique index implicitly creates a constraint.

No, it doesn't. I'm using "constraint" in a technical sense here,
that is something that is recorded as a constraint in the system
catalogs.

regression=# select count(*) from pg_constraint;
count
-------
34
(1 row)

regression=# create table foo(f1 int unique);
CREATE TABLE
regression=# select count(*) from pg_constraint;
count
-------
35
(1 row)

regression=# create table bar(f1 int);
CREATE TABLE
regression=# create unique index on bar(f1);
CREATE INDEX
regression=# select count(*) from pg_constraint;
count
-------
35
(1 row)

The index on bar didn't create a constraint.

regards, tom lane