Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit045ec34

Browse files
committed
pgstat: Acquire lock when reading variable-numbered stats
Somewhere during the development of the patch acquiring a lock during readaccess to variable-numbered stats got lost. The missing lock acquisition won'tcause corruption, but can lead to reading torn values when accessingstats. Add the missing lock acquisitions.Reported-by: Greg Stark <stark@mit.edu>Reviewed-by: "Drouvot, Bertrand" <bdrouvot@amazon.com>Reviewed-by: Andres Freund <andres@anarazel.de>Author: Kyotaro Horiguchi <horikyota.ntt@gmail.com>Discussion:https://postgr.es/m/CAM-w4HMYkM_DkYhWtUGV+qE_rrBxKOzOF0+5faozxO3vXrc9wA@mail.gmail.comBackpatch: 15-
1 parenta2caf18 commit045ec34

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

‎src/backend/utils/activity/pgstat.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,9 +844,12 @@ pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
844844
else
845845
stats_data=MemoryContextAlloc(pgStatLocal.snapshot.context,
846846
kind_info->shared_data_len);
847+
848+
pgstat_lock_entry_shared(entry_ref, false);
847849
memcpy(stats_data,
848850
pgstat_get_entry_data(kind,entry_ref->shared_stats),
849851
kind_info->shared_data_len);
852+
pgstat_unlock_entry(entry_ref);
850853

851854
if (pgstat_fetch_consistency>PGSTAT_FETCH_CONSISTENCY_NONE)
852855
{
@@ -983,9 +986,15 @@ pgstat_build_snapshot(void)
983986

984987
entry->data=MemoryContextAlloc(pgStatLocal.snapshot.context,
985988
kind_info->shared_size);
989+
/*
990+
* Acquire the LWLock directly instead of using
991+
* pg_stat_lock_entry_shared() which requires a reference.
992+
*/
993+
LWLockAcquire(&stats_data->lock,LW_SHARED);
986994
memcpy(entry->data,
987995
pgstat_get_entry_data(kind,stats_data),
988996
kind_info->shared_size);
997+
LWLockRelease(&stats_data->lock);
989998
}
990999
dshash_seq_term(&hstat);
9911000

‎src/backend/utils/activity/pgstat_shmem.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,22 @@ pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait)
579579
return true;
580580
}
581581

582+
/*
583+
* Separate from pgstat_lock_entry() as most callers will need to lock
584+
* exclusively.
585+
*/
586+
bool
587+
pgstat_lock_entry_shared(PgStat_EntryRef*entry_ref,boolnowait)
588+
{
589+
LWLock*lock=&entry_ref->shared_stats->lock;
590+
591+
if (nowait)
592+
returnLWLockConditionalAcquire(lock,LW_SHARED);
593+
594+
LWLockAcquire(lock,LW_SHARED);
595+
return true;
596+
}
597+
582598
void
583599
pgstat_unlock_entry(PgStat_EntryRef*entry_ref)
584600
{

‎src/include/utils/pgstat_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ extern void pgstat_detach_shmem(void);
581581
externPgStat_EntryRef*pgstat_get_entry_ref(PgStat_Kindkind,Oiddboid,Oidobjoid,
582582
boolcreate,bool*found);
583583
externboolpgstat_lock_entry(PgStat_EntryRef*entry_ref,boolnowait);
584+
externboolpgstat_lock_entry_shared(PgStat_EntryRef*entry_ref,boolnowait);
584585
externvoidpgstat_unlock_entry(PgStat_EntryRef*entry_ref);
585586
externboolpgstat_drop_entry(PgStat_Kindkind,Oiddboid,Oidobjoid);
586587
externvoidpgstat_drop_all_entries(void);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp