Skip to content

Commit 0912691

Browse files
nathan-bossartCommitfest Bot
authored andcommitted
replace p_s_s entry spinlocks with lwlocks
1 parent eab9e4e commit 0912691

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ typedef struct pgssEntry
240240
int encoding; /* query text encoding */
241241
TimestampTz stats_since; /* timestamp of entry allocation */
242242
TimestampTz minmax_stats_since; /* timestamp of last min/max values reset */
243-
slock_t mutex; /* protects the counters only */
243+
LWLock mutex; /* protects the counters only */
244244
} pgssEntry;
245245

246246
/*
@@ -256,6 +256,7 @@ typedef struct pgssSharedState
256256
int n_writers; /* number of active writers to query file */
257257
int gc_count; /* query file garbage collection cycle count */
258258
pgssGlobalStats stats; /* global statistics for pgss */
259+
int tranche;
259260
} pgssSharedState;
260261

261262
/*---- Local variables ----*/
@@ -554,6 +555,7 @@ pgss_shmem_startup(void)
554555
pgss->gc_count = 0;
555556
pgss->stats.dealloc = 0;
556557
pgss->stats.stats_reset = GetCurrentTimestamp();
558+
pgss->tranche = LWLockNewTrancheId();
557559
}
558560

559561
info.keysize = sizeof(pgssHashKey);
@@ -565,6 +567,8 @@ pgss_shmem_startup(void)
565567

566568
LWLockRelease(AddinShmemInitLock);
567569

570+
LWLockRegisterTranche(pgss->tranche, "pg_stat_statements counters");
571+
568572
/*
569573
* If we're in the postmaster (or a standalone backend...), set up a shmem
570574
* exit hook to dump the statistics to disk.
@@ -1411,7 +1415,7 @@ pgss_store(const char *query, int64 queryId,
14111415
* Grab the spinlock while updating the counters (see comment about
14121416
* locking rules at the head of the file)
14131417
*/
1414-
SpinLockAcquire(&entry->mutex);
1418+
LWLockAcquire(&entry->mutex, LW_EXCLUSIVE);
14151419

14161420
/* "Unstick" entry if it was previously sticky */
14171421
if (IS_STICKY(entry->counters))
@@ -1511,7 +1515,7 @@ pgss_store(const char *query, int64 queryId,
15111515
else if (planOrigin == PLAN_STMT_CACHE_CUSTOM)
15121516
entry->counters.custom_plan_calls++;
15131517

1514-
SpinLockRelease(&entry->mutex);
1518+
LWLockRelease(&entry->mutex);
15151519
}
15161520

15171521
done:
@@ -1901,9 +1905,9 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
19011905
}
19021906

19031907
/* copy counters to a local variable to keep locking time short */
1904-
SpinLockAcquire(&entry->mutex);
1908+
LWLockAcquire(&entry->mutex, LW_SHARED);
19051909
tmp = entry->counters;
1906-
SpinLockRelease(&entry->mutex);
1910+
LWLockRelease(&entry->mutex);
19071911

19081912
/*
19091913
* The spinlock is not required when reading these two as they are
@@ -2134,7 +2138,7 @@ entry_alloc(pgssHashKey *key, Size query_offset, int query_len, int encoding,
21342138
/* set the appropriate initial usage count */
21352139
entry->counters.usage = sticky ? pgss->cur_median_usage : USAGE_INIT;
21362140
/* re-initialize the mutex each time ... we assume no one using it */
2137-
SpinLockInit(&entry->mutex);
2141+
LWLockInitialize(&entry->mutex, pgss->tranche);
21382142
/* ... and don't forget the query text metadata */
21392143
Assert(query_len >= 0);
21402144
entry->query_offset = query_offset;

0 commit comments

Comments
 (0)