mirror of
https://github.com/postgres/postgres.git
synced 2025-07-11 10:01:57 +03:00
Add memory barriers for PgBackendStatus.st_changecount protocol.
st_changecount protocol needs the memory barriers to ensure that the apparent order of execution is as it desires. Otherwise, for example, the CPU might rearrange the code so that st_changecount is incremented twice before the modification on a machine with weak memory ordering. This surprising result can lead to bugs. This commit introduces the macros to load and store st_changecount with the memory barriers. These are called before and after PgBackendStatus entries are modified or copied into private memory, in order to prevent CPU from reordering PgBackendStatus access. Per discussion on pgsql-hackers, we decided not to back-patch this to 9.4 or before until we get an actual bug report about this. Patch by me. Review by Robert Haas.
This commit is contained in:
@ -16,6 +16,7 @@
|
||||
#include "libpq/pqcomm.h"
|
||||
#include "portability/instr_time.h"
|
||||
#include "postmaster/pgarch.h"
|
||||
#include "storage/barrier.h"
|
||||
#include "utils/hsearch.h"
|
||||
#include "utils/relcache.h"
|
||||
|
||||
@ -714,6 +715,12 @@ typedef struct PgBackendStatus
|
||||
* st_changecount again. If the value hasn't changed, and if it's even,
|
||||
* the copy is valid; otherwise start over. This makes updates cheap
|
||||
* while reads are potentially expensive, but that's the tradeoff we want.
|
||||
*
|
||||
* The above protocol needs the memory barriers to ensure that
|
||||
* the apparent order of execution is as it desires. Otherwise,
|
||||
* for example, the CPU might rearrange the code so that st_changecount
|
||||
* is incremented twice before the modification on a machine with
|
||||
* weak memory ordering. This surprising result can lead to bugs.
|
||||
*/
|
||||
int st_changecount;
|
||||
|
||||
@ -745,6 +752,43 @@ typedef struct PgBackendStatus
|
||||
char *st_activity;
|
||||
} PgBackendStatus;
|
||||
|
||||
/*
|
||||
* Macros to load and store st_changecount with the memory barriers.
|
||||
*
|
||||
* pgstat_increment_changecount_before() and
|
||||
* pgstat_increment_changecount_after() need to be called before and after
|
||||
* PgBackendStatus entries are modified, respectively. This makes sure that
|
||||
* st_changecount is incremented around the modification.
|
||||
*
|
||||
* Also pgstat_save_changecount_before() and pgstat_save_changecount_after()
|
||||
* need to be called before and after PgBackendStatus entries are copied into
|
||||
* private memory, respectively.
|
||||
*/
|
||||
#define pgstat_increment_changecount_before(beentry) \
|
||||
do { \
|
||||
beentry->st_changecount++; \
|
||||
pg_write_barrier(); \
|
||||
} while (0)
|
||||
|
||||
#define pgstat_increment_changecount_after(beentry) \
|
||||
do { \
|
||||
pg_write_barrier(); \
|
||||
beentry->st_changecount++; \
|
||||
Assert((beentry->st_changecount & 1) == 0); \
|
||||
} while (0)
|
||||
|
||||
#define pgstat_save_changecount_before(beentry, save_changecount) \
|
||||
do { \
|
||||
save_changecount = beentry->st_changecount; \
|
||||
pg_read_barrier(); \
|
||||
} while (0)
|
||||
|
||||
#define pgstat_save_changecount_after(beentry, save_changecount) \
|
||||
do { \
|
||||
pg_read_barrier(); \
|
||||
save_changecount = beentry->st_changecount; \
|
||||
} while (0)
|
||||
|
||||
/* ----------
|
||||
* LocalPgBackendStatus
|
||||
*
|
||||
|
Reference in New Issue
Block a user