Skip to content

Commit e1013f9

Browse files
akorotkovAlexander Korotkov
authored andcommitted
Allow parameters online change.
1 parent 8b63d50 commit e1013f9

File tree

3 files changed

+99
-49
lines changed

3 files changed

+99
-49
lines changed

contrib/pg_stat_wait/collector.c

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "miscadmin.h"
77
#include "postmaster/bgworker.h"
88
#include "storage/ipc.h"
9+
#include "storage/pg_shmem.h"
910
#include "storage/procarray.h"
1011
#include "storage/procsignal.h"
1112
#include "storage/s_lock.h"
@@ -27,10 +28,6 @@ shm_toc *toc;
2728
shm_mq *mq;
2829
static volatile sig_atomic_t shutdown_requested = false;
2930

30-
int historySize;
31-
int historyPeriod;
32-
bool historySkipLatch;
33-
3431
static void handle_sigterm(SIGNAL_ARGS);
3532
static void collector_main(Datum main_arg);
3633

@@ -52,6 +49,22 @@ CollectorShmemSize(void)
5249
return size;
5350
}
5451

52+
static bool
53+
shmem_int_guc_check_hook(int *newval, void **extra, GucSource source)
54+
{
55+
if (UsedShmemSegAddr == NULL)
56+
return false;
57+
return true;
58+
}
59+
60+
static bool
61+
shmem_bool_guc_check_hook(bool *newval, void **extra, GucSource source)
62+
{
63+
if (UsedShmemSegAddr == NULL)
64+
return false;
65+
return true;
66+
}
67+
5568
CollectorShmqHeader *
5669
GetCollectorMem(bool init)
5770
{
@@ -75,6 +88,21 @@ GetCollectorMem(bool init)
7588

7689
mq_mem = shm_toc_allocate(toc, COLLECTOR_QUEUE_SIZE);
7790
shm_toc_insert(toc, 1, mq_mem);
91+
92+
DefineCustomIntVariable("pg_stat_wait.history_size",
93+
"Sets size of waits history.", NULL,
94+
&hdr->historySize, 5000, 100, INT_MAX,
95+
PGC_SUSET, 0, shmem_int_guc_check_hook, NULL, NULL);
96+
97+
DefineCustomIntVariable("pg_stat_wait.history_period",
98+
"Sets period of waits history sampling.", NULL,
99+
&hdr->historyPeriod, 10, 1, INT_MAX,
100+
PGC_SUSET, 0, shmem_int_guc_check_hook, NULL, NULL);
101+
102+
DefineCustomBoolVariable("pg_stat_wait.history_skip_latch",
103+
"Skip latch events in waits history", NULL,
104+
&hdr->historySkipLatch, false,
105+
PGC_SUSET, 0, shmem_bool_guc_check_hook, NULL, NULL);
78106
}
79107
else
80108
{
@@ -110,6 +138,39 @@ AllocHistory(History *observations, int count)
110138
observations->wraparound = false;
111139
}
112140

141+
static void
142+
ReallocHistory(History *observations, int count)
143+
{
144+
HistoryItem *newitems;
145+
int copyCount;
146+
int i, j;
147+
148+
newitems = (HistoryItem *) palloc0(sizeof(HistoryItem) * count);
149+
150+
if (observations->wraparound)
151+
copyCount = Min(observations->count, count);
152+
else
153+
copyCount = observations->index;
154+
155+
i = 0;
156+
j = observations->index;
157+
while (i < copyCount)
158+
{
159+
j--;
160+
if (j < 0)
161+
j = observations->count - 1;
162+
memcpy(&newitems[i], &observations->items[j], sizeof(HistoryItem));
163+
i++;
164+
}
165+
166+
pfree(observations->items);
167+
observations->items = newitems;
168+
169+
observations->index = copyCount;
170+
observations->count = count;
171+
observations->wraparound = false;
172+
}
173+
113174
/* Read current wait information from proc, if readCurrent is true,
114175
* then it reads from currently going wait, and can be inconsistent
115176
*/
@@ -167,7 +228,11 @@ get_next_observation(History *observations)
167228
static void
168229
write_waits_history(History *observations, TimestampTz current_ts)
169230
{
170-
int i;
231+
int i, newSize;
232+
233+
newSize = hdr->historySize;
234+
if (observations->count != newSize)
235+
ReallocHistory(observations, newSize);
171236

172237
LWLockAcquire(ProcArrayLock, LW_SHARED);
173238
for (i = 0; i < ProcGlobal->allProcCount; ++i)
@@ -181,7 +246,7 @@ write_waits_history(History *observations, TimestampTz current_ts)
181246

182247
if (stateOk)
183248
{
184-
if (historySkipLatch && item.classId == WAIT_LATCH)
249+
if (hdr->historySkipLatch && item.classId == WAIT_LATCH)
185250
continue;
186251

187252
item.ts = current_ts;
@@ -237,7 +302,7 @@ collector_main(Datum main_arg)
237302
ALLOCSET_DEFAULT_INITSIZE,
238303
ALLOCSET_DEFAULT_MAXSIZE);
239304
old_context = MemoryContextSwitchTo(collector_context);
240-
AllocHistory(&observations, historySize);
305+
AllocHistory(&observations, hdr->historySize);
241306
MemoryContextSwitchTo(old_context);
242307

243308
while (1)
@@ -254,7 +319,7 @@ collector_main(Datum main_arg)
254319

255320
rc = WaitLatch(&MyProc->procLatch,
256321
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
257-
historyPeriod);
322+
hdr->historyPeriod);
258323

259324
if (rc & WL_POSTMASTER_DEATH)
260325
exit(1);

contrib/pg_stat_wait/pg_stat_wait.c

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,12 @@ _PG_init(void)
5050
DefineCustomBoolVariable("pg_stat_wait.history", "Collect waits history",
5151
NULL, &WaitsHistoryOn, false, PGC_POSTMASTER, 0, NULL, NULL, NULL);
5252

53-
DefineCustomIntVariable("pg_stat_wait.history_size",
54-
"Sets size of waits history.", NULL,
55-
&historySize, 5000, 100, INT_MAX,
56-
PGC_POSTMASTER, 0, NULL, NULL, NULL);
57-
58-
DefineCustomIntVariable("pg_stat_wait.history_period",
59-
"Sets period of waits history sampling.", NULL,
60-
&historyPeriod, 10, 1, INT_MAX,
61-
PGC_POSTMASTER, 0, NULL, NULL, NULL);
62-
63-
DefineCustomBoolVariable("pg_stat_wait.history_skip_latch",
64-
"Skip latch events in waits history", NULL,
65-
&historySkipLatch, false, PGC_POSTMASTER, 0, NULL, NULL, NULL);
66-
6753
if (WaitsHistoryOn)
6854
{
6955
/*
7056
* Request additional shared resources. (These are no-ops if we're not in
7157
* the postmaster process.) We'll allocate or attach to the shared
72-
* resources in pgss_shmem_startup().
58+
* resources in pgsw_shmem_startup().
7359
*/
7460
RequestAddinShmemSpace(CollectorShmemSize());
7561
RegisterWaitsCollector();

contrib/pg_stat_wait/pg_stat_wait.h

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,44 @@
1212

1313
typedef struct
1414
{
15-
uint32 backendPid;
16-
bool reset;
17-
int backendIdx;
18-
int classIdx;
19-
int eventIdx;
15+
uint32 backendPid;
16+
bool reset;
17+
int backendIdx;
18+
int classIdx;
19+
int eventIdx;
2020
} WaitProfileContext;
2121

2222
typedef struct
2323
{
24-
int class_cnt;
25-
int event_cnt;
24+
int class_cnt;
25+
int event_cnt;
2626
} WaitEventContext;
2727

2828
typedef struct
2929
{
30-
int classId;
31-
int eventId;
32-
int params[WAIT_PARAMS_COUNT];
33-
int backendPid;
34-
uint64 waitTime;
30+
int classId;
31+
int eventId;
32+
int params[WAIT_PARAMS_COUNT];
33+
int backendPid;
34+
uint64 waitTime;
3535

3636
TimestampTz ts;
3737
} HistoryItem;
3838

3939
typedef struct
4040
{
41-
int idx;
42-
HistoryItem *state;
43-
bool done;
44-
TimestampTz ts;
41+
int idx;
42+
HistoryItem *state;
43+
bool done;
44+
TimestampTz ts;
4545
} WaitCurrentContext;
4646

4747
typedef struct
4848
{
49-
bool wraparound;
50-
int index;
51-
int count;
52-
HistoryItem *items;
49+
bool wraparound;
50+
int index;
51+
int count;
52+
HistoryItem *items;
5353
} History;
5454

5555
typedef enum
@@ -60,19 +60,18 @@ typedef enum
6060

6161
typedef struct
6262
{
63-
Latch *latch;
64-
SHMRequest request;
63+
Latch *latch;
64+
SHMRequest request;
65+
int historySize;
66+
int historyPeriod;
67+
bool historySkipLatch;
6568
} CollectorShmqHeader;
6669

6770
extern PGDLLIMPORT char *WAIT_LOCK_NAMES[];
6871
extern PGDLLIMPORT char *WAIT_LWLOCK_NAMES[];
6972
extern PGDLLIMPORT char *WAIT_IO_NAMES[];
7073
extern PGDLLIMPORT char *WAIT_NETWORK_NAMES[];
7174
extern PGDLLIMPORT const int WAIT_OFFSETS[];
72-
extern PGDLLIMPORT int historySize;
73-
extern PGDLLIMPORT int historyPeriod;
74-
extern PGDLLIMPORT bool historySkipLatch;
75-
7675

7776
Size CollectorShmemSize(void);
7877
CollectorShmqHeader *GetCollectorMem(bool init);

0 commit comments

Comments
 (0)