Skip to content

Commit 2690a4f

Browse files
committed
Avoid "you don't own a lock of type ExclusiveLock" in GRANT TABLESPACE.
This WARNING appeared because SearchSysCacheLocked1() read cc_relisshared before catcache initialization, when the field is false unconditionally. On the basis of reading false there, it constructed a locktag as though pg_tablespace weren't relisshared. Only shared catalogs could be affected, and only GRANT TABLESPACE was affected in practice. SearchSysCacheLocked1() callers use one other shared-relation syscache, DATABASEOID. DATABASEOID is initialized by the end of CheckMyDatabase(), making the problem unreachable for pg_database. Back-patch to v13 (all supported versions). This has no known impact before v16, where ExecGrant_common() first appeared. Earlier branches avoid trouble by having a separate ExecGrant_Tablespace() that doesn't use LOCKTAG_TUPLE. However, leaving this unfixed in v15 could ensnare a future back-patch of a SearchSysCacheLocked1() call. Reported by Aya Iwata. Discussion: https://postgr.es/m/OS7PR01MB11964507B5548245A7EE54E70EA212@OS7PR01MB11964.jpnprd01.prod.outlook.com
1 parent 2fc0199 commit 2690a4f

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/backend/utils/cache/syscache.c

+10-5
Original file line numberDiff line numberDiff line change
@@ -1196,11 +1196,9 @@ HeapTuple
11961196
SearchSysCacheLocked1(int cacheId,
11971197
Datum key1)
11981198
{
1199+
CatCache *cache = SysCache[cacheId];
11991200
ItemPointerData tid;
12001201
LOCKTAG tag;
1201-
Oid dboid =
1202-
SysCache[cacheId]->cc_relisshared ? InvalidOid : MyDatabaseId;
1203-
Oid reloid = cacheinfo[cacheId].reloid;
12041202

12051203
/*----------
12061204
* Since inplace updates may happen just before our LockTuple(), we must
@@ -1252,8 +1250,15 @@ SearchSysCacheLocked1(int cacheId,
12521250

12531251
tid = tuple->t_self;
12541252
ReleaseSysCache(tuple);
1255-
/* like: LockTuple(rel, &tid, lockmode) */
1256-
SET_LOCKTAG_TUPLE(tag, dboid, reloid,
1253+
1254+
/*
1255+
* Do like LockTuple(rel, &tid, lockmode). While cc_relisshared won't
1256+
* change from one iteration to another, it may have been a temporary
1257+
* "false" until our first SearchSysCache1().
1258+
*/
1259+
SET_LOCKTAG_TUPLE(tag,
1260+
cache->cc_relisshared ? InvalidOid : MyDatabaseId,
1261+
cache->cc_reloid,
12571262
ItemPointerGetBlockNumber(&tid),
12581263
ItemPointerGetOffsetNumber(&tid));
12591264
(void) LockAcquire(&tag, lockmode, false, false);

src/test/regress/input/tablespace.source

+6
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,12 @@ ALTER INDEX testschema.part_a_idx SET TABLESPACE pg_default;
376376
-- Fail, not empty
377377
DROP TABLESPACE regress_tblspace;
378378

379+
-- Adequate cache initialization before GRANT
380+
\c -
381+
BEGIN;
382+
GRANT ALL ON TABLESPACE regress_tblspace TO PUBLIC;
383+
ROLLBACK;
384+
379385
CREATE ROLE regress_tablespace_user1 login;
380386
CREATE ROLE regress_tablespace_user2 login;
381387
GRANT USAGE ON SCHEMA testschema TO regress_tablespace_user2;

src/test/regress/output/tablespace.source

+5
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,11 @@ ALTER INDEX testschema.part_a_idx SET TABLESPACE pg_default;
899899
-- Fail, not empty
900900
DROP TABLESPACE regress_tblspace;
901901
ERROR: tablespace "regress_tblspace" is not empty
902+
-- Adequate cache initialization before GRANT
903+
\c -
904+
BEGIN;
905+
GRANT ALL ON TABLESPACE regress_tblspace TO PUBLIC;
906+
ROLLBACK;
902907
CREATE ROLE regress_tablespace_user1 login;
903908
CREATE ROLE regress_tablespace_user2 login;
904909
GRANT USAGE ON SCHEMA testschema TO regress_tablespace_user2;

0 commit comments

Comments
 (0)