From 6bd4b7ae6b725adae16520e53677aaf61997adb7 Mon Sep 17 00:00:00 2001 From: Julien Rouhaud Date: Fri, 19 Jan 2024 08:08:54 +0800 Subject: [PATCH] Remove queryids_lock A normal backend should only write in a dedicated array position at the very beginning of the query execution, and the underlying parallel workers will only read from the same array position during the end of query execution. As a consequence there's no need to protect array access with a lock. Note that this locking approach can lead to severe performance degradation in some light OLTP workload with a high number of clients. --- pg_stat_kcache.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/pg_stat_kcache.c b/pg_stat_kcache.c index fc9d955..bd54cb8 100644 --- a/pg_stat_kcache.c +++ b/pg_stat_kcache.c @@ -144,7 +144,6 @@ typedef struct pgskSharedState { LWLock *lock; /* protects hashtable search/modification */ #if PG_VERSION_NUM >= 90600 - LWLock *queryids_lock; /* protects queryids array */ pgsk_queryid queryids[FLEXIBLE_ARRAY_MEMBER]; /* queryid info for parallel leaders */ #endif @@ -332,7 +331,7 @@ _PG_init(void) #if PG_VERSION_NUM < 150000 RequestAddinShmemSpace(pgsk_memsize()); #if PG_VERSION_NUM >= 90600 - RequestNamedLWLockTranche("pg_stat_kcache", 2); + RequestNamedLWLockTranche("pg_stat_kcache", 1); #else RequestAddinLWLocks(1); #endif /* pg 9.6+ */ @@ -432,9 +431,7 @@ pgsk_set_queryid(pgsk_queryid queryid) /* Only the leader knows the queryid. */ Assert(!IsParallelWorker()); - LWLockAcquire(pgsk->queryids_lock, LW_EXCLUSIVE); pgsk->queryids[MyBackendId] = queryid; - LWLockRelease(pgsk->queryids_lock); } #endif @@ -452,7 +449,7 @@ pgsk_shmem_request(void) prev_shmem_request_hook(); RequestAddinShmemSpace(pgsk_memsize()); - RequestNamedLWLockTranche("pg_stat_kcache", 2); + RequestNamedLWLockTranche("pg_stat_kcache", 1); } #endif @@ -489,9 +486,7 @@ pgsk_shmem_startup(void) { /* First time through ... */ #if PG_VERSION_NUM >= 90600 - LWLockPadded *locks = GetNamedLWLockTranche("pg_stat_kcache"); - pgsk->lock = &(locks[0]).lock; - pgsk->queryids_lock = &(locks[1]).lock; + pgsk->lock = &(GetNamedLWLockTranche("pg_stat_kcache"))->lock; #else pgsk->lock = LWLockAssign(); #endif @@ -1087,11 +1082,7 @@ pgsk_ExecutorEnd (QueryDesc *queryDesc) #if PG_VERSION_NUM >= 90600 if (IsParallelWorker()) - { - LWLockAcquire(pgsk->queryids_lock, LW_SHARED); queryId = pgsk->queryids[ParallelLeaderBackendId]; - LWLockRelease(pgsk->queryids_lock); - } else #endif queryId = queryDesc->plannedstmt->queryId;