Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id BD1CE1337B84 for ; Wed, 2 Feb 2011 01:26:04 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 65770-09 for ; Wed, 2 Feb 2011 05:25:57 +0000 (UTC) X-Greylist: delayed 00:05:45.320794 by SQLgrey-1.7.6 Received: from mail-qy0-f181.google.com (mail-qy0-f181.google.com [209.85.216.181]) by mail.postgresql.org (Postfix) with ESMTP id 625A71337B82 for ; Wed, 2 Feb 2011 01:25:57 -0400 (AST) Received: by qyk12 with SMTP id 12so8377001qyk.19 for ; Tue, 01 Feb 2011 21:25:57 -0800 (PST) Received: by 10.224.28.66 with SMTP id l2mr8416174qac.143.1296624009228; Tue, 01 Feb 2011 21:20:09 -0800 (PST) Received: from tornado.gateway.2wire.net (adsl-242-235-209.rmo.bellsouth.net [74.242.235.209]) by mx.google.com with ESMTPS id l12sm16271416qcu.19.2011.02.01.21.20.06 (version=SSLv3 cipher=RC4-MD5); Tue, 01 Feb 2011 21:20:07 -0800 (PST) Date: Wed, 2 Feb 2011 00:20:04 -0500 From: Noah Misch To: Nikolas Everett Cc: pgsql-performance@postgresql.org Subject: Re: Exhaustive list of what takes what locks Message-ID: <20110202052004.GA26098@tornado.gateway.2wire.net> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.18 (2008-05-17) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.6 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7 X-Spam-Level: X-Archive-Number: 201102/127 X-Sequence-Number: 42265 On Tue, Feb 01, 2011 at 02:18:37PM -0500, Nikolas Everett wrote: > Is there an exhaustive list of what takes what locks and how long they last? This documents which commands take each lock type, but it is not exhaustive: http://www.postgresql.org/docs/current/interactive/explicit-locking.html All locks on user-created database objects last until the transaction ends. This does not apply to advisory locks. Also, many commands internally take locks on system catalogs and release those locks as soon as possible. > CREATE TABLE account (account_id SERIAL PRIMARY KEY, name CHARACTER VARYING > NOT NULL); > CREATE TABLE foo (account_id INTEGER NOT NULL REFERENCES account > (account_id), stuff CHARACTER VARYING); > DROP TABLE foo; > That query shows that the DROP takes an AccessExclusiveLock on account. > This isn't totally unexpected but it is unfortunate because it means we > have to wait for a downtime window to maintain constraints even if they are > not really in use. PostgreSQL 9.1 will contain changes to make similar operations, though not that one, take ShareRowExclusiveLock instead of AccessExclusiveLock. Offhand, the same optimization probably could be arranged for it with minimal fuss. If "account" is heavily queried but seldom changed, that might be enough for you. The internal implementation of a FOREIGN KEY constraint takes the form of triggers on both tables. Each INSERT or UPDATE needs to know definitively whether to fire a given trigger, so adding or removing an arbitrary trigger will continue to require at least ShareRowExclusiveLock. In the abstract, the special case of a FOREIGN KEY constraint could be looser still, but that would be tricky to implement. nm