February 19th, 2011 by depesz | Tags: , , , , , , , | 4 comments »
Did it help? If yes - maybe you can help me?

On 25th of January, Tom Lane committed patch:

Implement ALTER TABLE ADD UNIQUE/PRIMARY KEY USING INDEX.
 
This feature allows a unique or pkey constraint to be created using an
already-existing unique index.  While the constraint isn't very
functionally different from the bare index, it's nice to be able to do that
for documentation purposes.  The main advantage over just issuing a plain
ALTER TABLE ADD UNIQUE/PRIMARY KEY is that the index can be created with
CREATE INDEX CONCURRENTLY, so that there is not a long interval where the
table is locked against updates.
 
On the way, refactor some of the code in DefineIndex() and index_create()
so that we don't have to pass through those functions in order to create
the index constraint's catalog entries.  Also, in parse_utilcmd.c, pass
around the ParseState pointer in struct CreateStmtContext to save on
notation, and add error location pointers to some error reports that didn't
have one before.
 
Gurjeet Singh, reviewed by Steve Singer and Tom Lane

I, personally, am extremely happy about it.

First, let's see the syntax:

$ create table test (
    i int4 not null,
    z text
    );
CREATE TABLE
 
$ create unique index test_idx on test (i);
CREATE INDEX

Above creates simple table, with unique index, but which is not primary key or unique constraint. But now I can:

$ alter table test add primary key using index test_idx;
ALTER TABLE
 
$ \d test
     Table "public.test"
 Column |  Type   | Modifiers 
--------+---------+-----------
 i      | integer | not null
 z      | text    | 
Indexes:
    "test_idx" PRIMARY KEY, btree (i)

or:

$ alter table test add unique using index test_idx;
ALTER TABLE
 
$ \d test
     Table "public.test"
 Column |  Type   | Modifiers 
--------+---------+-----------
 i      | integer | not null
 z      | text    | 
Indexes:
    "test_idx" UNIQUE CONSTRAINT, btree (i)

Why is that so cool?

Indexes do accumulate bloat. It happens. And, while you can do “REINDEX" – it's locking operation.

But – you can create new index concurrently, with the same specification as bloated indexes, but under different name and then drop old index. This creation can be done CONCURRENTLY which makes it non-locking.

Which is great, but you can't really do it with PRIMARY KEYS or UNIQUE CONSTRAINTS, as you can't drop index (old, bloated) while having constraint. The best that we could do so far, when we had bloated primary key index, was to create new unique index, concurrently, and drop primary key.

This approach leads to problems with tools which use PRIMARY KEYs.

Luckily now – you can add primary key back to table, using recreated, bloat-free index. Or even – add primary key to table “on the fly", with very short lock only.

Sweet. Doesn't solve all problems – for example foreign keys pointing to primary key, but it's a good start.

  1. 4 comments

  2. # Marko Tiikkaja
    Feb 20, 2011

    The new NOT VALID option for FOREIGN KEYs can help there if you quickly want to point existing foreign keys to the new primary key.

  3. Feb 20, 2011

    @Marko
    I know. Kind of. I will be testing and writing about it later (perhaps today, not sure yet).

  4. # Arun
    Jan 11, 2013

    When I am using unique index to enforce primary key, its taking very long time. like index got created in 2 min through parallel 8 but primary key enableing taking 25 min. any help?

  5. Jan 11, 2013

    @Arun:
    Sorry, but I literally don’t understand your problem/question.

Sorry, comments for this post are disabled.