Thread: ADD FOREIGN KEY locking
Hello all,
James Sewell,
Solutions Architect
______________________________________
![](http://www.lisasoft.com/sites/lisasoft/files/u1/logo1.jpg)
When I add a FK with a statement like this:
ALTER TABLE a ADD FOREIGN KEY (id) REFERENCES b(id);
I see a lock on table b:
select locktype,mode,granted from pg_locks, pg_stat_activity where relation::regclass::text = 'b' AND pg_locks.pid = pg_stat_activity.pid;locktype | relationmode | AccessShareLockgranted | tquery | SOME LONG RUNNING QUERY WHICH SELECTS FROM blocktype | relationmode | AccessExclusiveLockgranted | fquery | ALTER TABLE a ADD FOREIGN KEY (id) REFERENCES b(id);
This means that my add key won't complete until my long running query does. That seems a bit odd to me? In this database there are lots of datawarehouse type queries running, which makes it a bit hard for me to schedule this operation.
Is this just a manifestation of adding the key being in an ALTER TABLE, which always needs an AccessExclusiveLock? Or am I missing some edge case when this lock would be required in this circumstance?
No real urgency on this question, I just found it a bit strange and thought someone might be able to shed some light.
James Sewell,
Solutions Architect
______________________________________
![](http://www.lisasoft.com/sites/lisasoft/files/u1/logo1.jpg)
The contents of this email are confidential and may be subject to legal or professional privilege and copyright. No representation is made that this email is free of viruses or other defects. If you have received this communication in error, you may not copy or distribute any part of it or otherwise disclose its contents to anyone. Please advise the sender of your incorrect receipt of this correspondence.
Oh,
James Sewell,
Solutions Architect
______________________________________
![](http://www.lisasoft.com/sites/lisasoft/files/u1/logo1.jpg)
I've just noticed something in the Commit fest post
- Reducing lock strength of trigger and foreign key DDL
Perhaps I just need to be more patient.
Cheers,
James Sewell,
Solutions Architect
______________________________________
![](http://www.lisasoft.com/sites/lisasoft/files/u1/logo1.jpg)
On Wed, Feb 18, 2015 at 10:57 AM, James Sewell <james.sewell@lisasoft.com> wrote:
Hello all,When I add a FK with a statement like this:ALTER TABLE a ADD FOREIGN KEY (id) REFERENCES b(id);I see a lock on table b:select locktype,mode,granted from pg_locks, pg_stat_activity where relation::regclass::text = 'b' AND pg_locks.pid = pg_stat_activity.pid;locktype | relationmode | AccessShareLockgranted | tquery | SOME LONG RUNNING QUERY WHICH SELECTS FROM blocktype | relationmode | AccessExclusiveLockgranted | fquery | ALTER TABLE a ADD FOREIGN KEY (id) REFERENCES b(id);This means that my add key won't complete until my long running query does. That seems a bit odd to me? In this database there are lots of datawarehouse type queries running, which makes it a bit hard for me to schedule this operation.Is this just a manifestation of adding the key being in an ALTER TABLE, which always needs an AccessExclusiveLock? Or am I missing some edge case when this lock would be required in this circumstance?No real urgency on this question, I just found it a bit strange and thought someone might be able to shed some light.
James Sewell,
Solutions Architect
______________________________________
The contents of this email are confidential and may be subject to legal or professional privilege and copyright. No representation is made that this email is free of viruses or other defects. If you have received this communication in error, you may not copy or distribute any part of it or otherwise disclose its contents to anyone. Please advise the sender of your incorrect receipt of this correspondence.
On Wed, Feb 18, 2015 at 9:06 AM, James Sewell <james.sewell@lisasoft.com> wrote:
Yup.
I've just noticed something in the Commit fest post- Reducing lock strength of trigger and foreign key DDL
This reduces the lock taken for ADD FOREIGN KEY to ShareRowExclusiveLock, authorizing SELECT and SELECT FOR [SHARE | UPDATE ... ].
Perhaps I just need to be more patient.
Yup.
--
Michael