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 1w5sed-003ih6-2j for pgsql-hackers@arkaria.postgresql.org; Thu, 26 Mar 2026 21:54:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w5sec-005tMJ-08 for pgsql-hackers@arkaria.postgresql.org; Thu, 26 Mar 2026 21:54:14 +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 1w5seb-005tMB-2L for pgsql-hackers@lists.postgresql.org; Thu, 26 Mar 2026 21:54:14 +0000 Received: from mail-ua1-x933.google.com ([2607:f8b0:4864:20::933]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w5seZ-00000001MZh-04fL for pgsql-hackers@lists.postgresql.org; Thu, 26 Mar 2026 21:54:13 +0000 Received: by mail-ua1-x933.google.com with SMTP id a1e0cc1a2514c-94abd52b274so413783241.3 for ; Thu, 26 Mar 2026 14:54:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1774562049; cv=none; d=google.com; s=arc-20240605; b=Zcw7bnVq/1It0AdgfJIfB4pN1tzLaUVQLBGLebzdbKF8yLvZ3giQHSk6ZchUwHddqA Z0n9LomAwOQ8UiUzo9Bjb4I/8qJMfCsugy3WlTgA7C4/CTvXqGS3W+tL2OYiw0+ZkWmv 8AY/TUGrhLB+7Ib48ny6DGGoUWu6QqxNJC3AWVYlNopGRGxtGXpdH0W6ic0EMThQ/AIb Dnt9aHK8iVfAelhWbgFZmIYDRtlkozLq25MNwIqb7bVs1T69kX3cD8PuhTo6qHD5HsfN yKbRWdX2XQwsZas3bw1hKlLExEVvMnSStN0LpQBJJW1AwSV7NPtTLTgGZ4Sm0kahLAie pOlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=deonSV52J4j5pfaHtCqZ7yS1UzBn6IQ2O1Y8LNvusKM=; fh=rxy+4R5RAfz27w/g7ZsxrTiDybElymesvHD6KaufTT8=; b=LojVLM7pvp/FNaSXN6xROvaeEPCsG2/Jcdnfz0nmk+RTVAOvp3eRmtcZKUC3hXsrQg KLYMZm/f1x3XtiP70GVUirZy68NPNAwLNDi3KQvV8hYpE/egSJclBfTyQAhR0SJhKeAD n5GeUkaDgZXtg3U/o9eZQDPVByvgnJr4a3lRY98/jDnZwfrfAKYrMH9SMBsK2tXQcuNa R1uQe8JNhOsn+KJExYDY+sxprkrdZVQuCABSNfo1BfESr5CWsMHtZwOXK1AOQKHDd34a ji4dmCSVLCHQ/61rt6VsizfpFdjkeFaNQXaZLFROX2Aki4FWL5LjJQvhRbTWuNLFXaA6 8rYA==; 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=1774562049; x=1775166849; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=deonSV52J4j5pfaHtCqZ7yS1UzBn6IQ2O1Y8LNvusKM=; b=rQFStQTlpl/bXdp39wix3hxDtKHuBzoGz3i5PP0ponhdodYsbmzwbTlaXQJY/mtPl4 2mtjOkj+OvfpDxGG8Zw2yzg1pT0B2QD0nj3JTSea6mddNsrUoNnRnEIOWMKKIIi7fd9t H+N/GUTKY6DVvyouG+reHSBw62DQxQnx1sCVkLoFF+/nXt5fMBIXz4LuWLtyArys9Oq+ gUB9X8pksG/Eq91ivoA8556CvaZL3OvNpq5FyKTiFJ2le94opr5olynfS8gZZwQXFpcL 2sV7taBzi9L530vRDohJDOWcYeZT3nCm/OsYitgopCFqjoiTE791LIFyyUdmVDretWF1 Dvpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774562049; x=1775166849; h=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; bh=deonSV52J4j5pfaHtCqZ7yS1UzBn6IQ2O1Y8LNvusKM=; b=NKLCtpR9j2Zs6NsrQfvYGvLNWhpcKylv8Be4dtojzjpXVNf2qat9izhxM/B/jJtfJA VcYUZ5hQ9e30TYEqVm6fSGidv7YLQxNLtGh1Q+MYwAHq0Boq0MfsaRtU63G3SDtVRuOy E0I5EF+FQGhzQsc54wGTaji98cqwRqXkfmcWqol7OH+BQoX3KYM38Zw9b+nFiSLEigGZ c2IzuvYZzcIAHuEgjtM/miuoBW0lr8NCCpAwB5WaBdkwXi6FeZkLMD2zh9U7FJyeaasX k8L/QMjBPMapHZ3n8bMiUgd8H+SlxGCZB+8H0+hvXSQU1b8xEzOqlQfFeRIZ9eSNFkdj AcNw== X-Gm-Message-State: AOJu0YyPbI5DklYniLae5OyiSFGNfeLybaftQu6uli/YEhptgNo6ME1S wd6UnWbLDl6vlYX+Jr29TLHED+tt3yhLZrsTsy7Tl1AH6up0bL68EeCn/NkubIdzxWPZWgmIJhL yNLTy7KUSyBMAScu5riuRTMv84ffOlFQ= X-Gm-Gg: ATEYQzxIG5+qwLD6McvMsY1DEcUhqwHk717XUJPl2Hn5jdvG0QahzYXS+NUKHLMld8h aueLL6VcF2Vp4iCxxeBRc6eDf7lnOSAo8EIiVmzfu3zhd1X6t+PmBLWwFPja8sH2CH1J+TT29d/ hg6kQZgR9VNryUvNlq9cm1vwWksPUpJ/x9KlX9/FdUOorjsIa8jv2l1jyM7IOSWdpeRmIi9Rnyn /SmvOD79Ir1EeHZUQLj0PgSg6y/5RYYN2tYiDHiYycM2dA9WzpP1mzTNcvNiZ/e3zdMhq2Hal4m UMOQclq4RnvUiiiHYoTR7L6cYvt8JPwcqKCqNeamIylPpzWs5A== X-Received: by 2002:a05:6102:441a:b0:602:b345:6cdd with SMTP id ada2fe7eead31-604f904cae7mr137223137.8.1774562049006; Thu, 26 Mar 2026 14:54:09 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: SATYANARAYANA NARLAPURAM Date: Thu, 26 Mar 2026 14:53:57 -0700 X-Gm-Features: AQROBzBumTiMH9TtzdGRgwFKW6xChf1Vxql8eOxfs_wQBla3SG5hyzqJjqNDvsI Message-ID: Subject: Re: LockHasWaiters() crashes on fast-path locks To: Bharath Rupireddy Cc: PostgreSQL Hackers Content-Type: multipart/mixed; boundary="000000000000903b79064df46cdf" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000903b79064df46cdf Content-Type: multipart/alternative; boundary="000000000000903b78064df46cdd" --000000000000903b78064df46cdd Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Updated the patch with a commit message. On Wed, Mar 25, 2026 at 3:34=E2=80=AFPM SATYANARAYANA NARLAPURAM < satyanarlapuram@gmail.com> wrote: > Hi, > > On Wed, Mar 25, 2026 at 3:06=E2=80=AFPM Bharath Rupireddy < > bharath.rupireddyforpostgres@gmail.com> wrote: > >> Hi, >> >> On Wed, Mar 25, 2026 at 2:15=E2=80=AFPM SATYANARAYANA NARLAPURAM >> wrote: >> > >> > Hi Hackers, >> > >> > LockHasWaiters() assumes that the LOCALLOCK's lock and proclock >> pointers are populated, but this is not the case for locks acquired via = the >> fast-path optimization. Weak locks (< ShareUpdateExclusiveLock) on >> relations may not be stored in the shared lock hash table, and the >> LOCALLOCK entry is left with lock =3D NULL and proclock =3D NULL in such= a case. >> > >> > If LockHasWaiters() is called for such a lock, it dereferences those >> NULL pointers when it reads proclock->holdMask and lock->waitMask, causi= ng >> a segfault. >> > >> > The only existing caller is lazy_truncate_heap() in VACUUM, which >> queries LockHasWaitersRelation(rel, AccessExclusiveLock). Since >> AccessExclusiveLock is the strongest lock level, it is never fast-pathed= , >> so the bug has never been triggered in practice. However, any new caller >> that passes a weak lock mode, for example, checking whether a DDL is >> waiting on an AccessShareLock will crash. The fix is to transfer the loc= k >> to the main lock table before we access them. >> > >> > Attached a patch to address this issue. >> >> Nice find! It would be good to add a test case (perhaps in an existing >> test extension even though we may not commit it; it can act as a >> demo). >> > > Please refer the patches in the thread [2] below for a repro / use case. > > >> >> I see that this type of lock transfer is happening for prepared >> statements (see AtPrepare_Locks [1]). However, I see the proposed >> patch relying on lock =3D=3D NULL for detecting whether the lock was >> acquired using fast-path. Although this looks correct because if the >> lock or proclock pointers are NULL, this identifies that the lock was >> taken using fast-path. But for consistency purposes, can we have the >> same check as that of AtPrepare_Locks? > > > Thank you for the review and code pointer, this is addressed now in v2 > patch, attached. > > [2] > https://www.postgresql.org/message-id/CAHg%2BQDfdoR%3D7iqEAvLW9qtzV0Sx1wp= 2FuALeamqcCdiVEmMF-Q%40mail.gmail.com > > > Thanks, > Satya > --000000000000903b78064df46cdd Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Updated the patch with a commit message.

On Wed, Mar 25, 2026 at 3:34=E2=80=AFPM SATYANARAYANA NARLAPURAM <satyanarlapuram@gmail.com>= ; wrote:
Hi,

On Wed, Mar 25, 2026 at 3:06=E2=80=AFPM Bharath Rupireddy= <bharath.rupireddyforpostgres@gmail.com> wrote:
Hi,

On Wed, Mar 25, 2026 at 2:15=E2=80=AFPM SATYANARAYANA NARLAPURAM
<satyanar= lapuram@gmail.com> wrote:
>
> Hi Hackers,
>
> LockHasWaiters() assumes that the LOCALLOCK's lock and proclock po= inters are populated, but this is not the case for locks acquired via the f= ast-path optimization. Weak locks (< ShareUpdateExclusiveLock) on relati= ons may not be stored in the shared lock hash table, and the LOCALLOCK entr= y is left with lock =3D NULL and proclock =3D NULL in such a case.
>
> If LockHasWaiters() is called for such a lock, it dereferences those N= ULL pointers when it reads proclock->holdMask and lock->waitMask, cau= sing a segfault.
>
> The only existing caller is lazy_truncate_heap() in VACUUM, which quer= ies LockHasWaitersRelation(rel, AccessExclusiveLock). Since AccessExclusive= Lock is the strongest lock level, it is never fast-pathed, so the bug has n= ever been triggered in practice. However, any new caller that passes a weak= lock mode, for example, checking whether a DDL is waiting on an AccessShar= eLock will crash. The fix is to transfer the lock to the main lock table be= fore we access them.
>
> Attached a patch to address this issue.

Nice find! It would be good to add a test case (perhaps in an existing
test extension even though we may not commit it; it can act as a
demo).

Please refer the=C2=A0patches in= the thread [2] below for a repro / use case.
=C2=A0

I see that this type of lock transfer is happening for prepared
statements (see AtPrepare_Locks [1]). However, I see the proposed
patch relying on lock =3D=3D NULL for detecting whether the lock was
acquired using fast-path. Although this looks correct because if the
lock or proclock pointers are NULL, this identifies that the lock was
taken using fast-path. But for consistency purposes, can we have the
same check as that of AtPrepare_Locks?

Than= k=C2=A0you for the review and code pointer, this is addressed now in v2 pat= ch, attached.=C2=A0



Thanks,
Satya
--000000000000903b78064df46cdd-- --000000000000903b79064df46cdf Content-Type: application/octet-stream; name="v3-0001-Fix-LockHasWaiters-crash-for-fast-path-locks.patch" Content-Disposition: attachment; filename="v3-0001-Fix-LockHasWaiters-crash-for-fast-path-locks.patch" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: f_mn80f0kn0 RnJvbSAzNDlmM2ZhNTZhYmRhMWY5NDExNDAwYWU5YTA1ZWZhYmNiMjI3MGU4IE1vbiBTZXAgMTcg MDA6MDA6MDAgMjAwMQpGcm9tOiBTYXR5YSBOYXJsYXB1cmFtIDxzYXR5YW5hcmxhcHVyYW1AZ21h aWwuY29tPgpEYXRlOiBUaHUsIDI2IE1hciAyMDI2IDIxOjQyOjE1ICswMDAwClN1YmplY3Q6IFtQ QVRDSF0gRml4IExvY2tIYXNXYWl0ZXJzKCkgY3Jhc2ggZm9yIGZhc3Q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