Skip to content

Commit e6cd530

Browse files
author
Alexander Korotkov
committed
Fix work with GUCs in shared memory and background worker restart.
1 parent c758a97 commit e6cd530

File tree

2 files changed

+82
-15
lines changed

2 files changed

+82
-15
lines changed

collector.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ register_wait_collector(void)
4141
/* Set up background worker parameters */
4242
worker.bgw_flags = BGWORKER_SHMEM_ACCESS;
4343
worker.bgw_start_time = BgWorkerStart_ConsistentState;
44-
worker.bgw_restart_time = BGW_NEVER_RESTART;
44+
worker.bgw_restart_time = 0;
4545
worker.bgw_main = collector_main;
4646
worker.bgw_notify_pid = 0;
4747
snprintf(worker.bgw_name, BGW_MAXLEN, "pg_wait_sampling collector");

pg_wait_sampling.c

+81-14
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,80 @@ shmem_int_guc_check_hook(int *newval, void **extra, GucSource source)
7474
return true;
7575
}
7676

77+
/*
78+
* This union allows us to mix the numerous different types of structs
79+
* that we are organizing.
80+
*/
81+
typedef union
82+
{
83+
struct config_generic generic;
84+
struct config_bool _bool;
85+
struct config_real real;
86+
struct config_int integer;
87+
struct config_string string;
88+
struct config_enum _enum;
89+
} mixedStruct;
90+
91+
/*
92+
* Setup new GUCs or modify existsing.
93+
*/
94+
static void
95+
setup_gucs()
96+
{
97+
struct config_generic **guc_vars;
98+
int numOpts,
99+
i;
100+
bool history_size_found = false,
101+
history_period_found = false,
102+
profile_period_found = false;
103+
104+
/* Initialize the guc_variables[] array */
105+
build_guc_variables();
106+
107+
guc_vars = get_guc_variables();
108+
numOpts = GetNumConfigOptions();
109+
110+
for (i = 0; i < numOpts; i++)
111+
{
112+
mixedStruct *var = (mixedStruct *) guc_vars[i];
113+
const char *name = var->generic.name;
114+
115+
if (!strcmp(name, "pg_wait_sampling.history_size"))
116+
{
117+
history_size_found = true;
118+
var->integer.variable = &collector_hdr->historySize;
119+
}
120+
else if (!strcmp(name, "pg_wait_sampling.history_period"))
121+
{
122+
history_period_found = true;
123+
var->integer.variable = &collector_hdr->historyPeriod;
124+
}
125+
else if (!strcmp(name, "pg_wait_sampling.profile_period"))
126+
{
127+
history_skip_latch_found = true;
128+
var->integer.variable = &collector_hdr->profilePeriod;
129+
}
130+
}
131+
132+
if (!history_size_found)
133+
DefineCustomIntVariable("pg_wait_sampling.history_size",
134+
"Sets size of waits history.", NULL,
135+
&collector_hdr->historySize, 5000, 100, INT_MAX,
136+
PGC_SUSET, 0, shmem_int_guc_check_hook, NULL, NULL);
137+
138+
if (!history_period_found)
139+
DefineCustomIntVariable("pg_wait_sampling.history_period",
140+
"Sets period of waits history sampling.", NULL,
141+
&collector_hdr->historyPeriod, 10, 1, INT_MAX,
142+
PGC_SUSET, 0, shmem_int_guc_check_hook, NULL, NULL);
143+
144+
if (!profile_period_found)
145+
DefineCustomIntVariable("pg_wait_sampling.profile_period",
146+
"Sets period of waits profile sampling.", NULL,
147+
&collector_hdr->profilePeriod, 10, 1, INT_MAX,
148+
PGC_SUSET, 0, shmem_int_guc_check_hook, NULL, NULL);
149+
}
150+
77151
/*
78152
* Distribute shared memory.
79153
*/
@@ -94,6 +168,9 @@ pgws_shmem_startup(void)
94168
shm_toc_insert(toc, 0, collector_hdr);
95169
collector_mq = shm_toc_allocate(toc, COLLECTOR_QUEUE_SIZE);
96170
shm_toc_insert(toc, 1, collector_mq);
171+
172+
/* Initialize GUC variables in shared memory */
173+
setup_gucs();
97174
}
98175
else
99176
{
@@ -103,20 +180,6 @@ pgws_shmem_startup(void)
103180
collector_mq = shm_toc_lookup(toc, 1);
104181
}
105182

106-
/* Initialize GUC variables in shared memory */
107-
DefineCustomIntVariable("pg_wait_sampling.history_size",
108-
"Sets size of waits history.", NULL,
109-
&collector_hdr->historySize, 5000, 100, INT_MAX,
110-
PGC_SUSET, 0, shmem_int_guc_check_hook, NULL, NULL);
111-
DefineCustomIntVariable("pg_wait_sampling.history_period",
112-
"Sets period of waits history sampling.", NULL,
113-
&collector_hdr->historyPeriod, 10, 1, INT_MAX,
114-
PGC_SUSET, 0, shmem_int_guc_check_hook, NULL, NULL);
115-
DefineCustomIntVariable("pg_wait_sampling.profile_period",
116-
"Sets period of waits profile sampling.", NULL,
117-
&collector_hdr->profilePeriod, 10, 1, INT_MAX,
118-
PGC_SUSET, 0, shmem_int_guc_check_hook, NULL, NULL);
119-
120183
shmem_initialized = true;
121184

122185
if (prev_shmem_startup_hook)
@@ -354,6 +417,10 @@ receive_array(SHMRequest request, Size item_size, Size *count)
354417
mq = shm_mq_create(collector_mq, COLLECTOR_QUEUE_SIZE);
355418
collector_hdr->request = request;
356419

420+
if (!collector_hdr->latch)
421+
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR),
422+
errmsg("pg_wait_sampling collector wasn't started")));
423+
357424
SetLatch(collector_hdr->latch);
358425

359426
shm_mq_set_receiver(mq, MyProc);

0 commit comments

Comments
 (0)