Optimization of FK creation

From: Robert Ayrapetyan <robert(dot)ayrapetyan(at)comodo(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Optimization of FK creation
Date: 2010-10-08 14:33:08
Message-ID: AANLkTikcUfX4+wja_Guo+UtVCMzjwEbSEhU6gpQTBoEK@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello.

It's often neccessary to create empty table and FK to some existing
table from this newly created table.
However, even newly created (empty) table requires lock on parent
table it wants refer to.
On large production DBs with high load on parent table it's almost
neverending operation.

For example,

CREATE TABLE "table_foo" (
...
);

ALTER TABLE "table_foo"
ADD CONSTRAINT "FK_table_foo_REF_table_parent"
FOREIGN KEY ("ColName") REFERENCES "table_parent" ("ColName");

hangs forever because requires lock on "ColName" of table_parent with
billions of records,
which for empty table seems senseless.

I think it's very easy to check if "table_foo" is empty and therefore
not requires any locks on
"parent_table" and generate FK immediately without acquiring the lock
on "parent" table.

Similar problem raised here:
http://postgresql.1045698.n5.nabble.com/probelm-with-alter-table-add-constraint-td2072411.html

Please fix this issue asap.

regards,
--
Ayrapetyan Robert

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2010-10-08 14:40:25 Re: Optimization of FK creation
Previous Message tv 2010-10-08 14:11:24 Re: LOG: unexpected EOF on client connection