Optimization of FK creation - Mailing list pgsql-general

From Robert Ayrapetyan
Subject Optimization of FK creation
Date
Msg-id AANLkTikcUfX4+wja_Guo+UtVCMzjwEbSEhU6gpQTBoEK@mail.gmail.com
Whole thread Raw
Responses Re: Optimization of FK creation  (Tom Lane <tgl@sss.pgh.pa.us>)
List 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

pgsql-general by date:

Previous
From: tv@fuzzy.cz
Date:
Subject: Re: LOG: unexpected EOF on client connection
Next
From: Tom Lane
Date:
Subject: Re: Optimization of FK creation