From: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
To: Alexandre Felipe <o.alexandre.felipe@gmail.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Subject: Re: FIX: BUG #19687: ALTER SEQUENCE missing lock
Date: Tue, 15 Sep 2026 13:30:36 +0530
Message-ID: <CAJTYsWWpDFe_KKvHQby-R_M9uLLYXbL-iYDP4H_rmAeTBu9dBQ@mail.gmail.com> (raw)
In-Reply-To: <CAE8JnxNPC6MjO4xN2k_r+jxG38PLmKa7CtWRMnJxvL==ZoFdWw@mail.gmail.com>
References: <CAE8JnxNPC6MjO4xN2k_r+jxG38PLmKa7CtWRMnJxvL==ZoFdWw@mail.gmail.com>
Hi,
On Tue, 15 Sept 2026 at 12:19, Alexandre Felipe <
o.alexandre.felipe@gmail.com> wrote:
>
> Trying to do something different today.
>
> Hunting bugs.
>
> Addressing an issue in `ALTER SEQUENCE` commands [1], by adding
> a missing lock when calling RelationSetNewRelfilenumber and being
> more accurate about the type of lock required in that function's
doc-comment.
>
> The change is sound, even if it is not the complete fix yet.
I posted about the same bug on pgsql-bugs yesterday [2] (quoted below),
so maybe it is better to keep this in one thread.
I tried locking at the RelationSetNewRelfilenumber() call first, but it
deadlocks. ALTER still holds ShareRowExclusiveLock while it waits for the
upgrade, and that conflicts with the RowExclusiveLock a scanning
transaction needs for nextval():
s2: BEGIN; SELECT * FROM seq1; -- holds AccessShareLock
s1: ALTER SEQUENCE seq1 AS int; -- holds ShareRowExclusiveLock,
-- waits for AccessExclusiveLock
s2: SELECT nextval('seq1'); -- waits for s1
An isolation permutation for that gives me:
step s1altertype: ALTER SEQUENCE seq1 AS int; <waiting ...>
step s2nvone: SELECT nextval('seq1');
ERROR: deadlock detected
Taking AccessExclusiveLock at the initial lookup instead avoids this, since
ALTER then holds nothing on the sequence while it waits, and nextval() can
still proceed. That is the one-line change in [2].
The doc-comment correction in your patch looks right either way.
[1]
https://www.postgresql.org/message-id/19687-dd094472b0c48afb%40postgresql.org
[2]
https://www.postgresql.org/message-id/CAJTYsWWH0N-jJUviz3eviLa_ehGVsmumOmpTGufbRAsuDD2Uiw%40mail.gma...
Regards,
Ayush
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-hackers@postgresql.org
Cc: ayushtiwari.slg01@gmail.com, o.alexandre.felipe@gmail.com
Subject: Re: FIX: BUG #19687: ALTER SEQUENCE missing lock
In-Reply-To: <CAJTYsWWpDFe_KKvHQby-R_M9uLLYXbL-iYDP4H_rmAeTBu9dBQ@mail.gmail.com>
References: <CAE8JnxNPC6MjO4xN2k_r+jxG38PLmKa7CtWRMnJxvL==ZoFdWw@mail.gmail.com> <CAJTYsWWpDFe_KKvHQby-R_M9uLLYXbL-iYDP4H_rmAeTBu9dBQ@mail.gmail.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