Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d2ssz-0004qy-OU for pgsql-bugs@arkaria.postgresql.org; Tue, 25 Apr 2017 05:19:38 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1d2ssy-0007s6-Pv for pgsql-bugs@arkaria.postgresql.org; Tue, 25 Apr 2017 05:19:36 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1d2ssH-0006fQ-Q4 for pgsql-bugs@postgresql.org; Tue, 25 Apr 2017 05:18:53 +0000 Received: from mail-it0-x235.google.com ([2607:f8b0:4001:c0b::235]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1d2ssD-0007mc-LL for pgsql-bugs@postgresql.org; Tue, 25 Apr 2017 05:18:52 +0000 Received: by mail-it0-x235.google.com with SMTP id 70so17890817ita.0 for ; Mon, 24 Apr 2017 22:18:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citusdata.com; s=google; h=from:mime-version:references:in-reply-to:date:message-id:subject:to :cc; bh=UwekLvAhYqdGQh+ZlM+CG5sURJ7wl1u17Sl7lUcbzE8=; b=kR7KeSeL2IbDW4e5oS1/RBvsi1iw3XI6cbI/6bZtWAlcg6BjrnDP2RJhl2flLNQfzn HtW+bmuP6lXOB/QibR4gZNJ2O+66ihlesfwjnSLrjxTbuRb6CxvcsEUy7p5Opfngr8Ti b0LadTDZFqY/umQvv+UEcXN4+MMsdUcOorabM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:mime-version:references:in-reply-to:date :message-id:subject:to:cc; bh=UwekLvAhYqdGQh+ZlM+CG5sURJ7wl1u17Sl7lUcbzE8=; b=dWBVpwHTnfjqMEVdvXsJoMCyAWyujek8qwImF3wH5QNz++GDJRvqT5YaZj1BgtH07q YPRihhhDLuYgy4AbVrFCaZQWs7mIwn/G4C0PsuUQZEzVeZQyW9ve0U8CBrQfbBVKMOEi EuXb1CZ2XypjK63mbEKthiTmi47TVTnbRLX8GAToCI8lIGzQ9M78iKa7fzZUfuoY7gGU F/QraIcsq91J9MUitqE8vfeH+lA0Gnyd/Ajcg5pT0YEXGJMzsPhQOsCPbI2pzBgjsQtX xABZXQx1w9sXcm7IfonI+Mi7x4EpFPVTv4vU3W7XAzp8rNSvgMoDhmhIJTtfnrQrtF6Y IAOw== X-Gm-Message-State: AN3rC/6uAHbtT/mogzUHwBvNK+HTzBPCFWzVuORPCkAryajQiDas46Gg h99gCFDAD5HgY0cJzU6I3I4pIJ9LIEp5O1I= X-Received: by 10.36.106.134 with SMTP id l128mr2240825itc.23.1493097528735; Mon, 24 Apr 2017 22:18:48 -0700 (PDT) From: Jason Petersen Mime-Version: 1.0 (1.0) References: In-Reply-To: Date: Mon, 24 Apr 2017 23:18:47 -0600 Message-ID: <6291523295125557509@unknownmsgid> Subject: Re: Concurrent ALTER SEQUENCE RESTART Regression To: Michael Paquier Cc: PostgreSQL mailing lists , Peter Eisentraut Content-Type: multipart/alternative; boundary=001a113acfded0caf8054df6dbc8 X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --001a113acfded0caf8054df6dbc8 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable FWIW that was my gut read as well; take a slightly more restrictive lock, possibly blocking other ALTERs (unsure of prior behavior) to avoid ERROR but not at the cost of blocking readers. This seems about right to me. Haven't reported a bug before; what's next? Get a reviewer? -- *Jason Petersen* Software Engineer | Citus Data 303.736.9255 jason@citusdata.com On Apr 24, 2017, at 10:26 PM, Michael Paquier wrote: On Tue, Apr 25, 2017 at 10:53 AM, Michael Paquier wrote: On Tue, Apr 25, 2017 at 4:52 AM, Jason Petersen wrote= : While I understand the above workload is nonsensical, given the non-transactional behavior of ALTER SEQUENCE statements, previous PostgreSQL versions did not produce an error. It is likely applications have been coded with that assumption and will not deal well with the new behavior. Yes, that's a bug. Having poked around the code a bit, I see the functions to access for sequence state have changed; I=E2=80=99m assuming this is an unintended sid= e-effect of that change. I haven=E2=80=99t worked up a patch myself, but I have some hope someone mo= re familiar with the underlying changes could make quick work of this. I am working on it, will send a patch soon. My first intuition is that this is some wild lock issue. I am checking as well other code paths. An open item has been added for the time being. So things are broken for sequences since commit 1753b1b0 (adding Peter in CC) that has changed the way sequence metadata is handled. The failure happens in CatalogTupleUpdate() which uses simple_heap_update() that caller can only use if updates are concurrent safe. But since 1753b1b0 that is not true as the sequence is locked with AccessShareLock. The correct answer is to use a stronger lock, but not something that would block attempts to read next sequence values as it is assumed that ALTER SEQUENCE should be non-disruptive. Hence I think that ShareUpdateExclusiveLock is a good match what what we are looking for here: protect concurrent updates of the sequence metadata for other ALTER SEQUENCE commands but not block other transactions reading this data. Attached is a patch to address the issue. And looking at things deeper... I am seeing at least one other ALTER command that is not concurrent safe... I'll keep that for another thread because it is an older bug. --=20 Michael --001a113acfded0caf8054df6dbc8 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
FWIW that was my gut read as well;= take a slightly more restrictive lock, possibly blocking other ALTERs (uns= ure of prior behavior) to avoid ERROR but not at the cost of blocking reade= rs. This seems about right to me.

<= /div>
Haven't reported a bug before; what= 's next? Get a reviewer?

--
Jason Petersen
Softwar= e Engineer | Citus Data
=

On Apr 24, 2017, at 10:26 PM, Michael Paquier <michael.paquier@gmail.com> = wrote:

On Tue, Apr 25, 20= 17 at 10:53 AM, Michael Paquier
<michael.paquier@gmail.com> wrote:
On Tue, Apr 25, 2017 at 4:52 AM, Jason Peters= en <jason@citusdata.com> w= rote:
While I understand the above workload is nonsensical, given the= non-transactional behavior of ALTER SEQUENCE statements, previous PostgreS= QL versions did not produce an error. It is likely applications have been c= oded with that assumption and will not deal well with the new behavior.
Yes, that's a bug.=

Having poked around= the code a bit, I see the functions to access for sequence state have chan= ged; I=E2=80=99m assuming this is an unintended side-effect of that change.=

I haven=E2=80=99t worked up a pat= ch myself, but I have some hope someone more familiar with the underlying c= hanges could make quick work of this.
<= blockquote type=3D"cite">
I am working on it, will send a patch soon. My first intuition = is that
this is some= wild lock issue. I am checking as well other code paths.
An open item has been added for the t= ime being.

So things are brok= en for sequences since commit 1753b1b0 (adding Peter
in CC)= that has changed the way sequence metadata is handled. The
failure happens in CatalogTupleUpdate() which uses
simple_= heap_update() that caller can only use if updates are
concu= rrent safe. But since 1753b1b0 that is not true as the sequence
<= span>is locked with AccessShareLock. The correct answer is to use a<= br>stronger lock, but not something that would block attempts to read=
next sequence values as it is assumed that ALTER SEQUENCE = should be
non-disruptive. Hence I think that ShareUpdateExc= lusiveLock is a good
match what what we are looking for her= e: protect concurrent updates of
the sequence metadata for = other ALTER SEQUENCE commands but not block
other transacti= ons reading this data.

Attached is a patch= to address the issue.

And looking at thin= gs deeper... I am seeing at least one other ALTER
command t= hat is not concurrent safe... I'll keep that for another
thread because it is an older bug.
--
Mi= chael
<alter= -seq-lock.patch>
--001a113acfded0caf8054df6dbc8--