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.94.2) (envelope-from ) id 1t35wc-00AGPV-EN for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Oct 2024 03:52:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1t35wZ-00DmhA-9n for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Oct 2024 03:52:27 +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.94.2) (envelope-from ) id 1t35wY-00DmfG-Ml for pgsql-hackers@lists.postgresql.org; Tue, 22 Oct 2024 03:52:27 +0000 Received: from mail-oi1-x22d.google.com ([2607:f8b0:4864:20::22d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1t35wV-002KLd-LN for pgsql-hackers@postgresql.org; Tue, 22 Oct 2024 03:52:26 +0000 Received: by mail-oi1-x22d.google.com with SMTP id 5614622812f47-3e5f9712991so2542308b6e.2 for ; Mon, 21 Oct 2024 20:52:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1729569141; x=1730173941; darn=postgresql.org; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=bURPdY4zvSZQ/b/4bLKEkH5YdTJnfi9pN/qGsbg76d8=; b=PURMuPFMVb0I6fFLb/J4zMwVkQOvCHP4To6qk7h8GVkuRPdelxWehbSFS1vW8uSB1k IHw+GX/swiQ/xaw0rgJ0q9dzXIHkJebNGqSHp7c0zgvU9gcWfhxBdQOIByJ4dOUh1O5B uKzHV3EJWg/j2FijjFi1/xA7cwfy7qhNnvfp8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729569141; x=1730173941; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=bURPdY4zvSZQ/b/4bLKEkH5YdTJnfi9pN/qGsbg76d8=; b=auj08uhqI4Zd2xZk/8HWrxUBhVZmRESIolF8qi/xvG2uHaqaq1XuTmSRq8iQwpQsyF 0a+s/6ok+o36N1yQ6UZufpQMfkEzPADZQqe5HlDvvoz6qIP4jkjFdk9IHiXzzbwhJ6ei 0RE14lsESGlTgNwJjeMbLs1k8hncQsV5ooyfmDyyVtZhSmBJBxGRBEh2zfV7h1xjbkOH BIPmLYsJkiz2y2ugwR1emfp+UJ4gLyG7hNsRWJm/gjBnn4TGRNpuuOCuPl3cC4HhTcIo dGoJoY9InXh37DBdEI2n0aoCuwnuBmGaq8fyHfGCkfYExQlviDcT2ErbWEbr9tLnKUC2 9FNg== X-Forwarded-Encrypted: i=1; AJvYcCXNMYxn/vAUcKpcpXBgBkVl4cWPCKIVc06Th31vLWzA2jMde+KU8/LHtDOlD6W2kMExavMANMjv4wLLFBac@postgresql.org X-Gm-Message-State: AOJu0YyY801PhX4hr77KZA4Ua+y/jN/pekfffN+5EcYrRKUjLGkYAjbZ vzI2ut7C0ukWAA/v5h3/If7c4XsDW4kaQSx43cKReSn/9PcWOKTHCRB6DCLGQQ== X-Google-Smtp-Source: AGHT+IFINLYkL68cGgBg6RxQQHWYWqIVw0rzI83Nz74zUZ2maI0inyWo/Ecn61ZwBI8688QrkN1tNw== X-Received: by 2002:a05:6808:1311:b0:3e6:147:8bd9 with SMTP id 5614622812f47-3e602c88299mr9089067b6e.18.1729569139329; Mon, 21 Oct 2024 20:52:19 -0700 (PDT) Received: from google.com ([2600:1702:a20:5750::48]) by smtp.gmail.com with ESMTPSA id 5614622812f47-3e6102b64d5sm1140144b6e.8.2024.10.21.20.52.16 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Oct 2024 20:52:17 -0700 (PDT) Date: Mon, 21 Oct 2024 20:52:15 -0700 From: Noah Misch To: Alexander Lakhin Cc: Nitin Motiani , Heikki Linnakangas , Robert Haas , pgsql-hackers@postgresql.org, Tom Lane , Smolkin Grigory , Michael Paquier Subject: Re: race condition in pg_class Message-ID: <20241022035215.3e.nmisch@google.com> References: <20240904195720.cf.nmisch@google.com> <20240905220434.80.nmisch@google.com> <20240906185531.48.nmisch@google.com> <20240919213346.0a.nmisch@google.com> <20240924224352.93.nmisch@google.com> <6c92f59b-f5bc-e58c-9bdd-d1f21c17c786@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="noA2FbL88cejiwum" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <6c92f59b-f5bc-e58c-9bdd-d1f21c17c786@gmail.com> User-Agent: Mutt/2.2.12 (2023-09-09) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --noA2FbL88cejiwum Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit On Mon, Oct 21, 2024 at 10:00:00PM +0300, Alexander Lakhin wrote: > Please look at an anomaly introduced with a07e03fd8. > With the attached modification for intra-grant-inplace.spec, running this > test triggers a Valgrind-detected error for me: > ==00:00:00:09.624 319033== Conditional jump or move depends on uninitialised value(s) > ==00:00:00:09.624 319033==    at 0x25D120: DoesMultiXactIdConflict (heapam.c:7373) > ==00:00:00:09.624 319033==    by 0x25B45A: heap_inplace_lock (heapam.c:6265) > ==00:00:00:09.624 319033==    by 0x27D8CB: systable_inplace_update_begin (genam.c:867) > ==00:00:00:09.624 319033==    by 0x33F717: index_update_stats (index.c:2856) > ==00:00:00:09.624 319033==    by 0x33FEE2: index_build (index.c:3106) > ==00:00:00:09.625 319033==    by 0x33C7D3: index_create (index.c:1276) > ==00:00:00:09.625 319033==    by 0x451000: DefineIndex (indexcmds.c:1216) > ==00:00:00:09.625 319033==    by 0x48D091: ATExecAddIndex (tablecmds.c:9156) > ==00:00:00:09.625 319033==    by 0x483F8E: ATExecCmd (tablecmds.c:5302) > ==00:00:00:09.625 319033==    by 0x483877: ATRewriteCatalogs (tablecmds.c:5186) > ==00:00:00:09.625 319033==    by 0x482B9A: ATController (tablecmds.c:4741) > ==00:00:00:09.625 319033==    by 0x4827A1: AlterTable (tablecmds.c:4387) > ==00:00:00:09.625 319033== Thanks. > Perhaps current_is_member in heap_inplace_lock() should be initialized > before the DoesMultiXactIdConflict() call as in other places... heap_inplace_lock() ignores current_is_member after computing it, so let's just pass NULL, as attached. --noA2FbL88cejiwum Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="inplace129-DoesMultiXactIdConflict-v1.patch" Author: Noah Misch Commit: Noah Misch Stop reading uninitialized memory in heap_inplace_lock(). Stop computing a never-used value. This removes the read; the read had no functional implications. Back-patch to v12, like commit a07e03fd8fa7daf4d1356f7cb501ffe784ea6257. Reported by Alexander Lakhin. Reviewed by FIXME. Discussion: https://postgr.es/m/6c92f59b-f5bc-e58c-9bdd-d1f21c17c786@gmail.com diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index da5e656..82a0492 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -6260,10 +6260,9 @@ heap_inplace_lock(Relation relation, LockTupleMode lockmode = LockTupleNoKeyExclusive; MultiXactStatus mxact_status = MultiXactStatusNoKeyUpdate; int remain; - bool current_is_member; if (DoesMultiXactIdConflict((MultiXactId) xwait, infomask, - lockmode, ¤t_is_member)) + lockmode, NULL)) { LockBuffer(buffer, BUFFER_LOCK_UNLOCK); ret = false; diff --git a/src/test/isolation/expected/intra-grant-inplace.out b/src/test/isolation/expected/intra-grant-inplace.out index b5fe8b0..4e9695a 100644 --- a/src/test/isolation/expected/intra-grant-inplace.out +++ b/src/test/isolation/expected/intra-grant-inplace.out @@ -63,6 +63,30 @@ step addk2: ALTER TABLE intra_grant_inplace ADD PRIMARY KEY (c); step r3: ROLLBACK; step addk2: <... completed> +starting permutation: b3 sfnku3 keyshr5 addk2 r3 +step b3: BEGIN ISOLATION LEVEL READ COMMITTED; +step sfnku3: + SELECT relhasindex FROM pg_class + WHERE oid = 'intra_grant_inplace'::regclass FOR NO KEY UPDATE; + +relhasindex +----------- +f +(1 row) + +step keyshr5: + SELECT relhasindex FROM pg_class + WHERE oid = 'intra_grant_inplace'::regclass FOR KEY SHARE; + +relhasindex +----------- +f +(1 row) + +step addk2: ALTER TABLE intra_grant_inplace ADD PRIMARY KEY (c); +step r3: ROLLBACK; +step addk2: <... completed> + starting permutation: b2 sfnku2 addk2 c2 step b2: BEGIN; step sfnku2: diff --git a/src/test/isolation/specs/intra-grant-inplace.spec b/src/test/isolation/specs/intra-grant-inplace.spec index 2992c85..9936d38 100644 --- a/src/test/isolation/specs/intra-grant-inplace.spec +++ b/src/test/isolation/specs/intra-grant-inplace.spec @@ -96,6 +96,14 @@ permutation addk2(r3) r3 +# reproduce bug in DoesMultiXactIdConflict() call +permutation + b3 + sfnku3 + keyshr5 + addk2(r3) + r3 + # same-xact rowmark permutation b2 --noA2FbL88cejiwum--