pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Eisentraut <peter_e@gmx.net>
To: pgsql-committers@postgresql.org
Subject: pgsql: Fix ALTER SEQUENCE locking
Date: Mon, 15 May 2017 14:33:41 +0000
Message-ID: <E1dAH49-00070q-0h@gemulon.postgresql.org> (raw)
List-Unsubscribe:  <mailto:majordomo@postgresql.org?body=unsub%20pgsql-committers>

Fix ALTER SEQUENCE locking

In 1753b1b027035029c2a2a1649065762fafbf63f3, the pg_sequence system
catalog was introduced.  This made sequence metadata changes
transactional, while the actual sequence values are still behaving
nontransactionally.  This requires some refinement in how ALTER
SEQUENCE, which operates on both, locks the sequence and the catalog.

The main problems were:

- Concurrent ALTER SEQUENCE causes "tuple concurrently updated" error,
  caused by updates to pg_sequence catalog.

- Sequence WAL writes and catalog updates are not protected by same
  lock, which could lead to inconsistent recovery order.

- nextval() disregarding uncommitted ALTER SEQUENCE changes.

To fix, nextval() and friends now lock the sequence using
RowExclusiveLock instead of AccessShareLock.  ALTER SEQUENCE locks the
sequence using ShareRowExclusiveLock.  This means that nextval() and
ALTER SEQUENCE block each other, and ALTER SEQUENCE on the same sequence
blocks itself.  (This was already the case previously for the OWNER TO,
RENAME, and SET SCHEMA variants.)  Also, rearrange some code so that the
entire AlterSequence is protected by the lock on the sequence.

As an exception, use reduced locking for ALTER SEQUENCE ... RESTART.
Since that is basically a setval(), it does not require the full locking
of other ALTER SEQUENCE actions.  So check whether we are only running a
RESTART and run with less locking if so.

Reviewed-by: Michael Paquier <michael.paquier@gmail.com>
Reported-by: Jason Petersen <jason@citusdata.com>
Reported-by: Andres Freund <andres@anarazel.de>

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/f8dc1985fd390774aab4ab0ba71036d6d5e631a9

Modified Files
--------------
doc/src/sgml/ref/alter_sequence.sgml         |  7 +++
src/backend/commands/sequence.c              | 69 +++++++++++++++-------
src/test/isolation/expected/sequence-ddl.out | 85 ++++++++++++++++++++++++++++
src/test/isolation/isolation_schedule        |  1 +
src/test/isolation/specs/sequence-ddl.spec   | 39 +++++++++++++
5 files changed, 180 insertions(+), 21 deletions(-)


-- 
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers


Message-ID: <E1dAH49-00070q-0h@gemulon.postgresql.org>
Permalink:  ../E1dAH49-00070q-0h@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1dAH49-00070q-0h@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: peter_e@gmx.net
  Subject: Re: pgsql: Fix ALTER SEQUENCE locking
  In-Reply-To: <E1dAH49-00070q-0h@gemulon.postgresql.org>
  References: <E1dAH49-00070q-0h@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox