Process waiting for ExclusiveLock on INSERT

From: Jehan-Guillaume de Rorthais <jgdr(at)dalibo(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Process waiting for ExclusiveLock on INSERT
Date: 2012-11-13 22:27:10
Message-ID: 50A2C93E.9070607@dalibo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello,

I had a surprising question today from a customer about exclusive
locks on INSERT. His log file shows something like:

======================>
LOG: process 1881 still waiting for ExclusiveLock on extension of
relation 168318652 of database 58429185 after 1000.065 ms
STATEMENT: INSERT INTO xxxx (...cols...) VALUES (...values...)
<======================

While asking on #postgresql and investigating in the code, I think I
hit a theory. When the system is I/O bound and backends are doing
shared buffer cleanups, at some point they have to extend a relation
with new pages, requiring for an exclusive lock on the relation to
forbid anyone else to extend it in the same time. At this time, if
multiple backends try to extend the relation, one win, all other wait
for the lock, leading to messages in the log file when log_lock_waits
is enabled.

This lock would comes from src/backend/access/heap/hio.c:432:
======================>
/*
* Have to extend the relation.
*
* We have to use a lock to ensure no one else is extending the rel
at the
* same time, else we will both try to initialize the same new page. We
* can skip locking for new or temp relations, however, since no one
else
* could be accessing them.
*/
needLock = !RELATION_IS_LOCAL(relation);

if (needLock)
LockRelationForExtension(relation, ExclusiveLock);
<======================

Is this theory correct or this issue should be discussed a bit more on
this list ?

PFA a small bash script with a small PostgreSQL configuration in
comments that reproduce this behavior very often on my laptop, eg:

======================>
2012-11-13 23:15:51 CET [23137]: [1-1] user=postgres,db=test LOG:
process 23137 still waiting for ExclusiveLock on extension of relation
28118 of database 28115 after 100.086 ms
2012-11-13 23:15:51 CET [23137]: [2-1] user=postgres,db=test
STATEMENT: insert into test select i, md5(i::text) from
generate_series(1,1000) AS i
<======================

Regards,
- --
Jehan-Guillaume de Rorthais
http://www.dalibo.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAlCiyT4ACgkQXu9L1HbaT6KbdgCgslQiKjP5bovr/eN5gi1TJB6i
9pcAoI9BpfD/4306xSUZTPUcQTLYHJS3
=HgzB
-----END PGP SIGNATURE-----

Attachment Content-Type Size
test_exclusivelock_on_insert.sh application/x-shellscript 1012 bytes

Browse pgsql-hackers by date

  From Date Subject
Next Message Dimitri Fontaine 2012-11-13 22:43:12 Re: Proof of concept: standalone backend with full FE/BE protocol
Previous Message Alvaro Herrera 2012-11-13 22:07:30 Re: foreign key locks