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 1tDviz-0048pM-3P for pgsql-bugs@arkaria.postgresql.org; Thu, 21 Nov 2024 01:11:13 +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 1tDvix-00Ajci-Hc for pgsql-bugs@arkaria.postgresql.org; Thu, 21 Nov 2024 01:11:11 +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 1tDviw-00Ajca-WA for pgsql-bugs@lists.postgresql.org; Thu, 21 Nov 2024 01:11:11 +0000 Received: from mail-oa1-x2c.google.com ([2001:4860:4864:20::2c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tDvit-0031VW-0C for pgsql-bugs@lists.postgresql.org; Thu, 21 Nov 2024 01:11:09 +0000 Received: by mail-oa1-x2c.google.com with SMTP id 586e51a60fabf-2962feec93bso308256fac.1 for ; Wed, 20 Nov 2024 17:11:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1732151464; x=1732756264; darn=lists.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=EN4dmce1EnjZjg8qYO3M8K7AkyVSpymJrs0N4WHyoz4=; b=FwdZ9PKgAUnOkJ6/PimIBtL/3/RS//skOQD9GBxKmz7kfkvUY6Q1bTYeaEOyQQEDNC c9u9eaf6cY8dj1xUxr6PgadMr/Uj9h5JoH8BgGzHDqEip4cxKloPdmuUIWNUg1sn9h6E yu+UTRciVz9HyqrfJAioMnpxt7HNL28swlAUM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732151464; x=1732756264; 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=EN4dmce1EnjZjg8qYO3M8K7AkyVSpymJrs0N4WHyoz4=; b=kIOXa4pzG52hjNjgCoGXdsEQozY2mZOiULkT4PJDKWEvhmsFhjUvk7DsvPWwtxGdED YLnzJ5O83ioRRBNZiAFZPmP1oMR0LDw5xhLrGq5Dx0TA6HVgbEHlCoAeV38v/nfbxulx hyxiyZBMMH18+nCAuT1LnoBXzx5U1kyek9ak0yOELsGT6U26KsewjDon2RiVXK58rv6x 0PaoK+DHw3SWoKIQApITEFd0whzGfxTalq6z1mFTgRDDeBbzhb6gYN+jF4rfKJBkCnn6 GPfeMcrMG4faSNhm7YCZQbBYngU7cBF/Ii6l4Sdhwx3X+VSYoJz/rmkkVRSnTsvMXA/S L5Ww== X-Gm-Message-State: AOJu0YyB6Tu4W2+En45PHee/AEBPgCzNKEQ4SbI/BciHiNdUqIwzULla OhJBuBbabdBkBgcqb/yivjGWQUc7VdZcbs/rFy7IwGK4aTh/WgTmSP3xbIcbxDyrvBSpsCHOLX8 = X-Gm-Gg: ASbGncsOgSS0A1qpHOTJMSZCNGeJyULXDxNikP4ZGokPsYCwu5K0gdS20eNCOxPOjuF 8yCQcP7UYlLVtbG+TQ8xQ9geItKlbYIM5Ikbls4+XkdX/++yFcPzADTnt4svre/FT8BtptBdNEP Guhg9BgqUAkbf9NlFPBvVe1ry9eqdlvqfcpvw7MZLo0aSDpMlOE9GztqKarunMkBUqUzY0Ead1b BoT/H9P7javtlijkP0kwcAwdQDkqDZbuVzgqwTV1GaL X-Google-Smtp-Source: AGHT+IEyZyxfRGEk4+R5XsLknjRQZXXQWBzJ6N2zLzxhBI3Tl/QNvJsh2pRTy7iURsea0HEUmVgdlg== X-Received: by 2002:a05:6870:14cc:b0:295:ed0a:8061 with SMTP id 586e51a60fabf-296d9ed19cemr4752073fac.39.1732151464168; Wed, 20 Nov 2024 17:11:04 -0800 (PST) Received: from google.com ([2600:1702:a20:5750::48]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-296518be6bdsm4552708fac.15.2024.11.20.17.11.02 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 20 Nov 2024 17:11:03 -0800 (PST) Date: Wed, 20 Nov 2024 17:11:00 -0800 From: Noah Misch To: "Aya Iwata (Fujitsu)" Cc: "pgsql-bugs@lists.postgresql.org" Subject: Re: 'WARNING: you don't own a lock of type ExclusiveLock' is printed during the GRANT command on PostgreSQL16.5 Message-ID: <20241121011100.c3.nmisch@google.com> References: <20241120062742.b0.nmisch@google.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="BDIroXaFzUAhfEr3" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20241120062742.b0.nmisch@google.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 --BDIroXaFzUAhfEr3 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit On Tue, Nov 19, 2024 at 10:27:42PM -0800, Noah Misch wrote: > On Wed, Nov 20, 2024 at 05:11:07AM +0000, Aya Iwata (Fujitsu) wrote: > > During our tests on November release, > > we discovered a "WARNING: you don't own a lock of type ExclusiveLock" that occurred at a strange time in GRANT command. > > This is only possible when psql executes each command individually. > > $ psql -d postgres -c "CREATE USER nmluser1;" > > CREATE ROLE > > $ psql -d postgres -c "CREATE TABLESPACE test_tblspace LOCATION '/xxx /xxx/pgsql16.5/tbs';" > > CREATE TABLESPACE > > $ psql -d postgres -c "CREATE DATABASE enc_testdb;" > > CREATE DATABASE > > $ psql -d postgres -c "GRANT ALL PRIVILEGES ON DATABASE postgres TO nmluser1;" > > GRANT > > $ psql -d postgres -c "GRANT ALL PRIVILEGES ON DATABASE enc_testdb TO nmluser1;" > > GRANT > > $ psql -d postgres -c "GRANT CREATE ON TABLESPACE test_tblspace TO nmluser1;" > > WARNING:  you don't own a lock of type ExclusiveLock > > GRANT Did you ever see the WARNING with a DATABASE command, or just with a TABLESPACE command? > Thanks for the report. This seems reasonably likely to come from a defect in > commit 51ff46d. I will look into it. Yes, that commit had us reading cc_relisshared before that field got initialized. I can reproduce the WARNING in v16+, with GRANT TABLESPACE. Before a cache's initialization cc_relisshared==false, so SearchSysCacheLocked1() used the wrong locktag. The attached patch is about right. At a minimum, I still need to work on the details in its log message. --BDIroXaFzUAhfEr3 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="inplace250-catcache-init-SearchSysCacheLocked1-v1.patch" Author: Noah Misch Commit: Noah Misch Avoid "you don't own a lock of type ExclusiveLock" in GRANT TABLESPACE. FIXME look for other user-visible implications: - other commands that can reach this warning - commands that actually need the protection of the LOCKTAG_TUPLE and don't get it as a result of setting the wrong dboid in the locktag. These would be commands involving pg_database. This happened because SearchSysCacheLocked1() read cc_relisshared before cache initialization, so the locktag for a shared catalog incorrectly contained dboid==MyDatabaseId. Only shared catalogs were affected. Back-patch to v13 (all supported versions). This has no known impact before v16, where ExecGrant_common() first appeared. Earlier branches have a separate ExecGrant_Tablespace() that doesn't use LOCKTAG_TUPLE, and ExecGrant_Database() seems to get a correctly-initialized cache FIXMEconfirm. Even so it's not very future-proof to assume v15 won't get impact in the future. Reported by Aya Iwata. Reviewed by FIXME. Discussion: https://postgr.es/m/OS7PR01MB11964507B5548245A7EE54E70EA212@OS7PR01MB11964.jpnprd01.prod.outlook.com diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c index f41b1c2..f7f4f56 100644 --- a/src/backend/utils/cache/syscache.c +++ b/src/backend/utils/cache/syscache.c @@ -287,11 +287,9 @@ HeapTuple SearchSysCacheLocked1(int cacheId, Datum key1) { + CatCache *cache = SysCache[cacheId]; ItemPointerData tid; LOCKTAG tag; - Oid dboid = - SysCache[cacheId]->cc_relisshared ? InvalidOid : MyDatabaseId; - Oid reloid = cacheinfo[cacheId].reloid; /*---------- * Since inplace updates may happen just before our LockTuple(), we must @@ -343,8 +341,15 @@ SearchSysCacheLocked1(int cacheId, tid = tuple->t_self; ReleaseSysCache(tuple); - /* like: LockTuple(rel, &tid, lockmode) */ - SET_LOCKTAG_TUPLE(tag, dboid, reloid, + + /* + * Do like LockTuple(rel, &tid, lockmode). While cc_relisshared won't + * change from one iteration to another, it may have been a temporary + * "false" until our first SearchSysCache1(). + */ + SET_LOCKTAG_TUPLE(tag, + cache->cc_relisshared ? InvalidOid : MyDatabaseId, + cache->cc_reloid, ItemPointerGetBlockNumber(&tid), ItemPointerGetOffsetNumber(&tid)); (void) LockAcquire(&tag, lockmode, false, false); diff --git a/src/test/regress/expected/tablespace.out b/src/test/regress/expected/tablespace.out index dd535d4..a90e39e 100644 --- a/src/test/regress/expected/tablespace.out +++ b/src/test/regress/expected/tablespace.out @@ -927,6 +927,11 @@ ALTER INDEX testschema.part_a_idx SET TABLESPACE pg_default; -- Fail, not empty DROP TABLESPACE regress_tblspace; ERROR: tablespace "regress_tblspace" is not empty +-- Adequate cache initialization before GRANT +\c - +BEGIN; +GRANT ALL ON TABLESPACE regress_tblspace TO PUBLIC; +ROLLBACK; CREATE ROLE regress_tablespace_user1 login; CREATE ROLE regress_tablespace_user2 login; GRANT USAGE ON SCHEMA testschema TO regress_tablespace_user2; diff --git a/src/test/regress/sql/tablespace.sql b/src/test/regress/sql/tablespace.sql index c8b8378..dfe3db0 100644 --- a/src/test/regress/sql/tablespace.sql +++ b/src/test/regress/sql/tablespace.sql @@ -396,6 +396,12 @@ ALTER INDEX testschema.part_a_idx SET TABLESPACE pg_default; -- Fail, not empty DROP TABLESPACE regress_tblspace; +-- Adequate cache initialization before GRANT +\c - +BEGIN; +GRANT ALL ON TABLESPACE regress_tblspace TO PUBLIC; +ROLLBACK; + CREATE ROLE regress_tablespace_user1 login; CREATE ROLE regress_tablespace_user2 login; GRANT USAGE ON SCHEMA testschema TO regress_tablespace_user2; --BDIroXaFzUAhfEr3--