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 1w5Wo3-003LBm-2A for pgsql-hackers@arkaria.postgresql.org; Wed, 25 Mar 2026 22:34:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w5Wo1-00H6EA-0H for pgsql-hackers@arkaria.postgresql.org; Wed, 25 Mar 2026 22:34:29 +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 1w5Wo0-00H6E2-2S for pgsql-hackers@lists.postgresql.org; Wed, 25 Mar 2026 22:34:29 +0000 Received: from mail-vk1-xa30.google.com ([2607:f8b0:4864:20::a30]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w5Wny-000000019CX-1MaS for pgsql-hackers@lists.postgresql.org; Wed, 25 Mar 2026 22:34:28 +0000 Received: by mail-vk1-xa30.google.com with SMTP id 71dfb90a1353d-56a9a7e762bso463940e0c.3 for ; Wed, 25 Mar 2026 15:34:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1774478065; cv=none; d=google.com; s=arc-20240605; b=eHnuBSZTaRR+djM2++ui0Qv4T2rgxjX46WwQFNxAWVdCPJclisPt5xHDsnT5C+O1B8 rPbfneKE72oi3WK3knqbwn1jgtJIWHa+Gq8lAcZDmFgZ29MJlPecmuWc5vcLKFc4wVsz gZ2Rj7EdbT3gZfFWnkJUSujRigpEH+Y3DN8d0WiocQJ+jpthgrF7NdxntkCYhH0T5Xkl QuTL5AsVybbc/Ux7jBj4MfcAthq50xlD8iu8ZTV0jjbuqCV+aoJ4PB0ZgLQF5DDKTyMd 5Y3z/VcLwBBVirD2Btm+VgaVKuk/8bejE8cW5IqSb1kOZxWliXKx8ZfdDtYbXLEdIJfN TB8A== 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=zMTZ3WCz4+flniSXl0ThK21KroYWoU59OS4OWh2OF0w=; fh=rxy+4R5RAfz27w/g7ZsxrTiDybElymesvHD6KaufTT8=; b=P/sWw+XPhWVFMgPuw5mVJI4VkNAvbnTYvLVGpKEYgrer/xUDuQDgylS0zK/yBicyQD pYQqvOzwh7o9AKLzW5fPyHY6uz5exFkUfGEh5TlZS1T7C4Ejaz1m0DG4m1ak2lPcg3Fr YAU9tHDdXqKVao1XBvgV/iKlArxFL2lKIES1ruVRlmQ8cvAcFymWVMudASU+xyxNN3Y/ kzGLQQ2rr5nbMCPRla6lLWfQNYEAb+SJKjjEgyaviX9HsoZsI+yRJEraAhIqTRB0ilxG xfj8zeWSwVH7zbxpqyk3OB3RiGgtn8rccO0eeNOQFo9Oi4y+RpIJx7Mz52wQ3GSKOwCa OQpA==; 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=1774478065; x=1775082865; 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=zMTZ3WCz4+flniSXl0ThK21KroYWoU59OS4OWh2OF0w=; b=oR4vIQxqU6a6XbFMKUPmTAwgR5E3ZGCSM/nrJxsqp+hWhJVew3N2H4O5rlEOaanfns 3hiPmmNIkm+gkq9TVRHXjg+XcW+VgwPmhKRFum96WBUKakHOHosw67IhwdSqft3VMJOE +ErwL7EN8V7ngLb71e0XZTcpa9fvOey63cbb+IKsdNvDXvzieRHJA8t0lV825qjfPGey y1RtOSms354oYXfQO23//ivyplD2XQnq47WC2nCo7WZeiRheEkAbg3rITwYQljnB+6ZE NkamtbPaCedpu3Pqk15rX8oDpvT3pFQm/CgjgEAURtsFeV637deF4QpBfEVQwDvMpJxi /kTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774478065; x=1775082865; 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=zMTZ3WCz4+flniSXl0ThK21KroYWoU59OS4OWh2OF0w=; b=JjLdhG3POj0FTV5IL1QYf0RoDjENDLp6XXok4AehQ6rZwkHp05RIOIthTPqaZeXq7V O8iJ5paAmterEwOywnEPjtNuvPd/K++hrnUWG8n01B6Po5bjA5jMrjoKz5uQ+Y+pbP0O VSOxuAjCrjJkzKBpTGWgw9tYN0Ag/wJ1BXhvOFPlep0bgrzVMByE7efhfezZzIkmYuII HBx2Rxqbdr0cgWk5R7BfKpcUepgpAa5NiC9odgqS6LUXNlXXJi6I72ImfoBBuz9vbRIb r87cP82l0RXVHNuFvX3wEq5BXP5YgfYAMUTVid8QyB0GcEnmpUTMIrZ6GowIq1E080NL pzBQ== X-Gm-Message-State: AOJu0YwhZn4nLT49qrC34Pmy0CvUZH2N0PVGwW8Kpa6KJdPiqoq+kwoL q2ny/7/5YOrtACfQ5pBDeyhjDCR5Ls8LTC8PE6AX8f6TSvhH2Ual1GQvhLRxsalJq8ZDi2+Rxh/ RBBLaJEUckHT5e14YqZvcCy6H3xPV0hM= X-Gm-Gg: ATEYQzzOqoz9SGkHBqXruBbdmW+w1NcloI3eAJTUzcCRLSjXPiwNipOTcryFjQz0yQX JhSQeYKgccZm6RgH6UIM94cDFS61/TQ4P43EP9vAg8FbZPSKuuCA1M0OfFkRuicLwkO0UlGCmHw VUTLU6vJHVO9vdY0O2Z1lWLmhiCWsxOOL10j+ainf1/VkJt2dd8a54/SgP+G+vis1vbEY5nveP3 ozp02ioAESInaDkKWf8wVFOMMm0ZH8KjjfHTitCD3erqZnVKRoJMmoTLqUVy+o34wUrcdgrP/zD CixEgJQ= X-Received: by 2002:a05:6102:5e98:b0:602:b037:4de8 with SMTP id ada2fe7eead31-6037902e268mr2786550137.4.1774478064853; Wed, 25 Mar 2026 15:34:24 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: SATYANARAYANA NARLAPURAM Date: Wed, 25 Mar 2026 15:34:13 -0700 X-Gm-Features: AQROBzDPtH7ADVOWfkPgeQokFm9qsqALt7U-8DzDerkv2GIJbtgE6oq8kF7_lxU Message-ID: Subject: Re: LockHasWaiters() crashes on fast-path locks To: Bharath Rupireddy Cc: PostgreSQL Hackers Content-Type: multipart/mixed; boundary="000000000000b7c63c064de0deb4" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000b7c63c064de0deb4 Content-Type: multipart/alternative; boundary="000000000000b7c63b064de0deb2" --000000000000b7c63b064de0deb2 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 pointer= s > 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, causin= g > 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 lock > 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%3D7iqEAvLW9qtzV0Sx1wp2F= uALeamqcCdiVEmMF-Q%40mail.gmail.com Thanks, Satya --000000000000b7c63b064de0deb2 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,

On Wed, Mar 25, 2026 at 3:06= =E2=80=AFPM Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com> wrote:
<= /div>
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
--000000000000b7c63b064de0deb2-- --000000000000b7c63c064de0deb4 Content-Type: application/octet-stream; name="v2-0001-lock-has-waiters-fast-path-fix.patch" Content-Disposition: attachment; filename="v2-0001-lock-has-waiters-fast-path-fix.patch" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: f_mn6me8wu0 ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL3N0b3JhZ2UvbG1nci9sb2NrLmMgYi9zcmMvYmFja2Vu ZC9zdG9yYWdlL2xtZ3IvbG9jay5jCmluZGV4IGQ5MzBjNjZjLi4zZjRkMzQzZCAxMDA2NDQKLS0t IGEvc3JjL2JhY2tlbmQvc3RvcmFnZS9sbWdyL2xvY2suYworKysgYi9zcmMvYmFja2VuZC9zdG9y YWdlL2xtZ3IvbG9jay5jCkBAIC03NDMsNiArNzQzLDE3IEBAIExvY2tIYXNXYWl0ZXJzKGNvbnN0 IExPQ0tUQUcgKmxvY2t0YWcsIExPQ0tNT0RFIGxvY2ttb2RlLCBib29sIHNlc3Npb25Mb2NrKQog CSAqLwogCXBhcnRpdGlvbkxvY2sgPSBMb2NrSGFzaFBhcnRpdGlvbkxvY2sobG9jYWxsb2NrLT5o YXNoY29kZSk7CiAKKwkvKgorCSAqIElmIHRoZSBsb2NhbCBsb2NrIHdhcyB0YWtlbiB2aWEgdGhl IGZhc3QtcGF0aCwgd2UgbmVlZCB0byBtb3ZlIGl0CisJICogdG8gdGhlIHByaW1hcnkgbG9jayB0 YWJsZSwgb3IganVzdCBnZXQgYSBwb2ludGVyIHRvIHRoZSBleGlzdGluZworCSAqIHByaW1hcnkg bG9jayB0YWJsZSBlbnRyeSBpZiBieSBjaGFuY2UgaXQncyBhbHJlYWR5IGJlZW4gdHJhbnNmZXJy ZWQuCisJICovCisJaWYgKGxvY2FsbG9jay0+cHJvY2xvY2sgPT0gTlVMTCkKKwl7CisJCWxvY2Fs bG9jay0+cHJvY2xvY2sgPSBGYXN0UGF0aEdldFJlbGF0aW9uTG9ja0VudHJ5KGxvY2FsbG9jayk7 CisJCWxvY2FsbG9jay0+bG9jayA9IGxvY2FsbG9jay0+cHJvY2xvY2stPnRhZy5teUxvY2s7CisJ fQorCiAJTFdMb2NrQWNxdWlyZShwYXJ0aXRpb25Mb2NrLCBMV19TSEFSRUQpOwogCiAJLyoK --000000000000b7c63c064de0deb4--