pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
To: Andres Freund <andres@anarazel.de>
To: pgsql-hackers@postgresql.org
Cc: Noah Misch <noah@leadboat.com>
Cc: Jason Petersen <jason@citusdata.com>
Cc: Michael Paquier <michael.paquier@gmail.com>
Cc: PostgreSQL mailing lists <pgsql-bugs@postgresql.org>
Subject: Re: Concurrent ALTER SEQUENCE RESTART Regression
Date: Wed, 10 May 2017 01:09:37 -0400
Message-ID: <4521f870-bb1f-1b29-3b3f-42419696efe7@2ndquadrant.com> (raw)
In-Reply-To: <20170507234334.yimkp6aq2o43wtt2@alap3.anarazel.de>
References: <99a009ea-1797-5b82-f3d3-eb4a5a8eb5dc@2ndquadrant.com>
	<20170427011258.2tpv55b6leqe2uva@alap3.anarazel.de>
	<1e5bb916-fd58-3995-851b-6875c6dd73eb@2ndquadrant.com>
	<20170427055806.uviqrl57lkgcmmmy@alap3.anarazel.de>
	<20170427062304.gxh3rczhh6vblrwh@alap3.anarazel.de>
	<CAB7nPqTYQ+GZ9jP7FNHbTa+J5Z1k3ZmBrxvAhtOvpkH3k9j42g@mail.gmail.com>
	<E77D2275-DA45-4F43-B00D-A9B113DFA9FB@anarazel.de>
	<CAB7nPqQ=yHXxiWU2FuaC13L_5y7UG7R_OkJhWQbXYMJPXCDtNg@mail.gmail.com>
	<20170430080517.GA811193@rfd.leadboat.com>
	<59aeb834-b113-6769-bfe4-65f2f8c875e3@2ndquadrant.com>
	<20170507234334.yimkp6aq2o43wtt2@alap3.anarazel.de>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

On 5/7/17 19:43, Andres Freund wrote:
> 3. Keep the catalog, make ALTER properly transactional, blocking
>    concurrent nextval()s. This resolves the issue that nextval() can't
>    see the changed definition of the sequence.

This was the intended choice.

I think I have more clarity about the different, overlapping issues:

1. Concurrent ALTER SEQUENCE causes "tuple concurrently updated"
   error, caused by updates to pg_sequence catalog.  This can be fixed
   by taking a self-exclusive lock somewhere.

   A typical answer for other catalogs is to use
   ShareRowExclusiveLock.  But in another context it was argued that
   is undesirable for other reasons, and it's better to stick with
   RowExclusiveLock and deal with the occasional consequences.  But
   this question is obsoleted by #2.

2. There is some inconsistency or disagreement about what to lock
   when modifying relation metadata.  When you alter a non-relation
   object, you just have the respective catalog to lock, and you have
   to make the trade-offs described in #1.  When you alter a relation
   object, you can lock the catalog or the actual relation, or both.
   I had gone with locking the catalog, but existing practice in
   tablecmds.c is to lock the relation with the most appropriate lock
   level, and use RowExclusiveLock for the catalog.  We can make
   sequences do that, too.

3. Sequence WAL writes and catalog updates are not protected by same
   lock.  We can fix that by holding a lock longer.  If per #2 we make
   the lock on the sequence the "main" one, then we just hold that one
   across the catalog update.

4. Some concerns about in AlterSequence() opening the pg_sequence
   catalog while holding the sequence buffer lock.  Move some things
   around to fix that.

5. nextval() disregarding uncommitted ALTER SEQUENCE changes.  In
   <PG10, it would read the uncommitted metadata and observe it.
   Currently, it goes ahead even if there is an uncommitted ALTER
   SEQUENCE pending that would prohibit what it's about to do (e.g.,
   MAXVALUE change).

   I think the correct fix is to have nextval() and ALTER SEQUENCE use
   sensible lock levels so that they block each other.  Since
   nextval() currently uses AccessShareLock, the suggestion was for
   ALTER SEQUENCE to therefore use AccessExclusiveLock.  But I think a
   better idea would be for nextval() to use RowExclusiveLock
   (analogous to UPDATE) and ALTER SEQUENCE to use
   ShareRowExclusiveLock, which would also satisfy issue #1.

Attached patches:

0001 Adds isolation tests for sequence behavior, in particular regarding
point #5.  The expected files in 0001 show how it behaves in 9.6, and if
you apply it to 10 without the subsequent fixes, it will show some problems.

0002 Locking changes discussed above, with test changes.

0003 (optional) Reduce locking if only RESTART is specified (because
that does not do a catalog update, so it can run concurrently with nextval).

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachments:

  0001-Add-isolation-tests-for-sequences.patch (invalid/octet-stream, 0B) download

view thread (84+ messages)  latest in thread

Message-ID: <4521f870-bb1f-1b29-3b3f-42419696efe7@2ndquadrant.com>
Permalink:  ../4521f870-bb1f-1b29-3b3f-42419696efe7@2ndquadrant.com/
Also on:    postgresql.org/message-id/4521f870-bb1f-1b29-3b3f-42419696efe7@2ndquadrant.com

 ·  · 

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-bugs@postgresql.org
  Cc: peter.eisentraut@2ndquadrant.com, andres@anarazel.de, pgsql-hackers@postgresql.org, noah@leadboat.com, jason@citusdata.com, michael.paquier@gmail.com
  Subject: Re: Concurrent ALTER SEQUENCE RESTART Regression
  In-Reply-To: <4521f870-bb1f-1b29-3b3f-42419696efe7@2ndquadrant.com>
  References: <99a009ea-1797-5b82-f3d3-eb4a5a8eb5dc@2ndquadrant.com> <20170427011258.2tpv55b6leqe2uva@alap3.anarazel.de> <1e5bb916-fd58-3995-851b-6875c6dd73eb@2ndquadrant.com> <20170427055806.uviqrl57lkgcmmmy@alap3.anarazel.de> <20170427062304.gxh3rczhh6vblrwh@alap3.anarazel.de> <CAB7nPqTYQ+GZ9jP7FNHbTa+J5Z1k3ZmBrxvAhtOvpkH3k9j42g@mail.gmail.com> <E77D2275-DA45-4F43-B00D-A9B113DFA9FB@anarazel.de> <CAB7nPqQ=yHXxiWU2FuaC13L_5y7UG7R_OkJhWQbXYMJPXCDtNg@mail.gmail.com> <20170430080517.GA811193@rfd.leadboat.com> <59aeb834-b113-6769-bfe4-65f2f8c875e3@2ndquadrant.com> <20170507234334.yimkp6aq2o43wtt2@alap3.anarazel.de> <4521f870-bb1f-1b29-3b3f-42419696efe7@2ndquadrant.com>

* 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