Skip to content

Commit

Permalink
Remove queryids_lock
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rjuju committed Jan 19, 2024
1 parent 6717537 commit 6bd4b7a
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions pg_stat_kcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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+ */
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 6bd4b7a

Please sign in to comment.