Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x6O62-000CWq-0J for pgsql-hackers@arkaria.postgresql.org; Tue, 15 Sep 2026 08:00:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x6O61-000KnZ-1f for pgsql-hackers@arkaria.postgresql.org; Tue, 15 Sep 2026 08:00:53 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x6O61-000KnR-0d for pgsql-hackers@lists.postgresql.org; Tue, 15 Sep 2026 08:00:53 +0000 Received: from mail-yx2-x11.google.com ([2607:f8b0:4864:41::11]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1x6O5x-00000000X8k-1gxQ for pgsql-hackers@lists.postgresql.org; Tue, 15 Sep 2026 08:00:52 +0000 Received: by mail-yx2-x11.google.com with SMTP id 00721157ae682-85d46e4cdcdso25051407b3.2 for ; Tue, 15 Sep 2026 01:00:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1789459248; cv=none; d=google.com; s=arc-20260327; b=nALpCPpqpByLCQ4ggSyxYkxns8ixX2uSjPARBpBXkYo+j9MoPl42euyHMFhh4LQhew dG1nwdWOm2kMHf4zVNnT+5ALIjcruiFeF12Vpj996tNJGQZEIsxt1rlYxGKu/2WD16Sp 9ng9WTnJi8xqXjvK/FJdiKzDuRwbY4BC55J9loPVVgUxKljTq4p53NLEp6/gg44bLzDI mRy7Zcmo5VpUO5NgKcVdOsU0FpxNM8yamDIF6H50K1quFmrqqMGrBJr0a9yszsOyIRmT 5nd8Vuh/czjf7vcjvFKTFtOYTJa6uyWGb3DifhK+zjPuc/Gvw4ACz7pWXI0kJnEs8Enm jCZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=BKeNmkrZD3NTyc/xffRYxpLJwm6RaCL3mete7yKLi3s=; fh=j8VJQxZVIP4JJ7uYY9rDRPjdWtp4cNqpNTlcqaQTH7c=; b=MoPkdKM6vpRREcgjsIEenxDqhXvdHCqtET8LZOdLiuOe1jxKyCBA7tYpH1M1ElzeTx USzUJnnRvFKExAamD6AZG5q2fPpTUN7jk2KMy/ybUWm1COKireOsL1nQZcW1UVRSAVIl SkJ8jKYpTg9Dt6cz7JgZf1ayDOSqTeIVV+ebaeiUNpRBhNiK0NJ9rrOuu7AWIBQfR7Be KrggzpvzV+vzlW5EaL4QKNgiNx/YDQtomejYQZRxQG3iXRrlnpo0B8pChvO6Rrd/CvIP SUzIbbZGlN2R6t1QiYg8yjlpshmt/dQplBkVbCB7Lm3HtR5L9U6/T3am7EyfuKUuiCIz 37Pg==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1789459248; x=1790064048; darn=lists.postgresql.org; h=content-type:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:from:to:cc:subject:date:message-id:reply-to :content-type; bh=BKeNmkrZD3NTyc/xffRYxpLJwm6RaCL3mete7yKLi3s=; b=iUFP6s2OidEAXanrd3GL9o97J5mZ48CCb2QuHIX7I1cZJQX3LGlMgfA2RsrEnh74S6 E9FptX+6qe9kLVSi4c1PaGc30IMrjOAYSvxABa6nK3uCAgQdY8ZhXBS/zPmqzRk/Wq5z MFQjPZW5ex1V2ghxHMh6v0xJWGyiXx3n2IVt/1xueixZ8AVvWYIMVrvS+wJ4KM7S8g9D 5eUXtve9L3cQ0py1dKYkwtnje3Z6XoCxcTKqZ8KYlhSDb1/uc5dg8qCHCWD8juKx8Xkx rBuJX7eqH+RbFC5YztH/XDf7s6alacIKuzU8Cx7Dq5AKxFa7g2rRy9h8ltClOJ+i8r1Z +5kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789459248; x=1790064048; h=content-type:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to:content-type; bh=BKeNmkrZD3NTyc/xffRYxpLJwm6RaCL3mete7yKLi3s=; b=dlDiBSqRcfvSLIHHFzVtmaTuNyhW7QC+VKVC24aopCaNz2jf5tGi/y3U4EpyVzwK6y NKVIPO1qGAh40VRzKjrc4SQKvNEH+XjpOLlIbS51nGtR81G+TGWoQ1wLeh6k7Y8lvOmc T2K4+K+CR+F/DiLPMTcREOWA+vn6fVLf7LQgMrxsf8k+FQdB95A2cEoEIfE5eQiwIeXH O2o/RQfbSQaa9AkxqIQv+ubHNzLFZh1g+11j/zdyR7P4HKAbnlKX88XpciDZYr1kxFw8 XBZ6Gcenfo5Nzpge+CVr0jsoJcmeVitQZBnj+aR6AUIuHjrBp/O12iFnVHRkYRbASjtk uYGQ== X-Gm-Message-State: AFuF++kINAuMhHOEOQWYCsJ8oXFNOY6vz/lcgdBsWXm+4+S+dxaQCYks adttF7p0UdIgiBkzGnc64YJpdzBHW9XMCiVfjVxY2k7BdfWpPgz/6Tim99YihFE+VS7lUCRzgho 0X6veRFJBPEOKaWKfKxNkfdTm73L7gNCBSz+KTV0= X-Gm-Gg: AYBFou1TKvTUN4AdioBaC9sZjmFfU1AqO3i6wpZBClnZ6JMrMxQQRHPbfjG41OgdRhz E7b1lO8cqrNGRTl6upASXtJopiMTLTjrHuXPycqqlowo3gK+B7eW9pvxasn6/r4psziEorfBe9F RIfAljKymQq4asNuYjVccHNkIm4p1M287+FFk4JTzbh4FkNi+K3ynioRKV/NWraoxON0YzqqoKm Tk0MrtvQOvm3QOMKd4H/V3zswoEqo4hMISInoEj0J8KIBRihvlxlpC1NG9VlQACepPUZHKBjSdO OhokiVXSvaj8AukXvXQcTeqBhfbZmBOrJcrVolmQkjz+qBOoJJ+Fk/0b5MFfn4+xO+dgfdwD9wF 6vD8jXe1fxhQ= X-Received: by 2002:a05:690c:4dc7:b0:881:392b:23db with SMTP id 00721157ae682-88d1ffc55ddmr23172387b3.11.1789459247765; Tue, 15 Sep 2026 01:00:47 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Ayush Tiwari Date: Tue, 15 Sep 2026 13:30:36 +0530 X-Gm-Features: AcwNN1W18M1RbkOkexT2jl-HYMN0KT8PQdZP1E-Jn9P9NNh_HNTTXq8hPZq7LlA Message-ID: Subject: Re: FIX: BUG #19687: ALTER SEQUENCE missing lock To: Alexandre Felipe Cc: PostgreSQL Hackers Content-Type: multipart/alternative; boundary="000000000000cd9f57065b80f22e" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000cd9f57065b80f22e Content-Type: text/plain; charset="UTF-8" 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; 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.gmail.com Regards, Ayush --000000000000cd9f57065b80f22e Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,

On Tue, 15 Sept 2026 at 12:19, Alexandre Felipe= <o.alexandre.felipe@gma= il.com> wrote:
>
> Trying to do something different toda= y.
>
> Hunting bugs.
>
> Addressing an issue in `AL= TER SEQUENCE` commands [1], by adding
> a missing lock when calling R= elationSetNewRelfilenumber and being
> more accurate about the type o= f lock required in that function's doc-comment.
>
> The cha= nge 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 RelationS= etNewRelfilenumber() call first, but it
deadlocks.=C2=A0 ALTER still hol= ds ShareRowExclusiveLock while it waits for the
upgrade, and that confli= cts with the RowExclusiveLock a scanning
transaction needs for nextval()= :

=C2=A0 s2: BEGIN; SELECT * FROM seq1; =C2=A0 =C2=A0-- holds Access= ShareLock
=C2=A0 s1: ALTER SEQUENCE seq1 AS int; =C2=A0 -- holds ShareRo= wExclusiveLock,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 -- wa= its for AccessExclusiveLock
=C2=A0 s2: SELECT nextval('seq1'); = =C2=A0 =C2=A0 =C2=A0 -- waits for s1

An isolation permutation for th= at gives me:

=C2=A0 step s1altertype: ALTER SEQUENCE seq1 AS int; &l= t;waiting ...>
=C2=A0 step s2nvone: SELECT nextval('seq1');=C2=A0 ERROR: =C2=A0deadlock detected

Taking AccessExclusiveLock a= t the initial lookup instead avoids this, since
ALTER then holds nothing= on the sequence while it waits, and nextval() can
still proceed.=C2=A0 = That is the one-line change in [2].

The doc-comment correction in yo= ur patch looks right either way.

[1] https://www.post= gresql.org/message-id/19687-dd094472b0c48afb%40postgresql.org
[2] https://www.postgresql.org/messag= e-id/CAJTYsWWH0N-jJUviz3eviLa_ehGVsmumOmpTGufbRAsuDD2Uiw%40mail.gmail.com

Regards,
Ayush
--000000000000cd9f57065b80f22e--