Skip to content

Store GUC variables in local process memory to avoid IPC shenanigans #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ If `pg_wait_sampling.sample_cpu` is set to true then processes that are not
waiting on anything are also sampled. The wait event columns for such processes
will be NULL.

These GUCs are allowed to be changed by superuser. Also, they are placed into
shared memory. Thus, they could be changed from any backend and affects worker
runtime.
Values of these GUC variables can be changed only in config file or with ALTER SYSTEM.
Then you need to reload server's configuration (such as with pg_reload_conf function)
for changes to take effect.

See
[PostgreSQL documentation](http://www.postgresql.org/docs/devel/static/monitoring-stats.html#WAIT-EVENT-TABLE)
Expand Down
30 changes: 17 additions & 13 deletions collector.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "funcapi.h"
#include "miscadmin.h"
#include "postmaster/bgworker.h"
#include "postmaster/interrupt.h"
#include "storage/ipc.h"
#include "storage/procarray.h"
#include "storage/procsignal.h"
Expand Down Expand Up @@ -151,7 +152,7 @@ probe_waits(History *observations, HTAB *profile_hash,
TimestampTz ts = GetCurrentTimestamp();

/* Realloc waits history if needed */
newSize = pgws_collector_hdr->historySize;
newSize = pgws_historySize;
if (observations->count != newSize)
realloc_history(observations, newSize);

Expand All @@ -170,7 +171,7 @@ probe_waits(History *observations, HTAB *profile_hash,
item.pid = proc->pid;
item.wait_event_info = proc->wait_event_info;

if (pgws_collector_hdr->profileQueries)
if (pgws_profileQueries)
item.queryId = pgws_proc_queryids[i];
else
item.queryId = 0;
Expand Down Expand Up @@ -289,7 +290,7 @@ make_profile_hash()
hash_ctl.hash = tag_hash;
hash_ctl.hcxt = TopMemoryContext;

if (pgws_collector_hdr->profileQueries)
if (pgws_profileQueries)
hash_ctl.keysize = offsetof(ProfileItem, count);
else
hash_ctl.keysize = offsetof(ProfileItem, queryId);
Expand Down Expand Up @@ -346,6 +347,7 @@ pgws_collector_main(Datum main_arg)
* partitipate to the ProcSignal infrastructure.
*/
pqsignal(SIGTERM, handle_sigterm);
pqsignal(SIGHUP, SignalHandlerForConfigReload);
pqsignal(SIGUSR1, procsignal_sigusr1_handler);
BackgroundWorkerUnblockSignals();
InitPostgresCompat(NULL, InvalidOid, NULL, InvalidOid, 0, NULL);
Expand All @@ -361,7 +363,7 @@ pgws_collector_main(Datum main_arg)
collector_context = AllocSetContextCreate(TopMemoryContext,
"pg_wait_sampling context", ALLOCSET_DEFAULT_SIZES);
old_context = MemoryContextSwitchTo(collector_context);
alloc_history(&observations, pgws_collector_hdr->historySize);
alloc_history(&observations, pgws_historySize);
MemoryContextSwitchTo(old_context);

ereport(LOG, (errmsg("pg_wait_sampling collector started")));
Expand All @@ -375,29 +377,31 @@ pgws_collector_main(Datum main_arg)
shm_mq_handle *mqh;
int64 history_diff,
profile_diff;
int history_period,
profile_period;
bool write_history,
write_profile;

/* We need an explicit call for at least ProcSignal notifications. */
CHECK_FOR_INTERRUPTS();

if (ConfigReloadPending)
{
ConfigReloadPending = false;
ProcessConfigFile(PGC_SIGHUP);
}

/* Wait calculate time to next sample for history or profile */
current_ts = GetCurrentTimestamp();

history_diff = millisecs_diff(history_ts, current_ts);
profile_diff = millisecs_diff(profile_ts, current_ts);
history_period = pgws_collector_hdr->historyPeriod;
profile_period = pgws_collector_hdr->profilePeriod;

write_history = (history_diff >= (int64)history_period);
write_profile = (profile_diff >= (int64)profile_period);
write_history = (history_diff >= (int64)pgws_historyPeriod);
write_profile = (profile_diff >= (int64)pgws_profilePeriod);

if (write_history || write_profile)
{
probe_waits(&observations, profile_hash,
write_history, write_profile, pgws_collector_hdr->profilePid);
write_history, write_profile, pgws_profilePid);

if (write_history)
{
Expand All @@ -421,8 +425,8 @@ pgws_collector_main(Datum main_arg)
* shared memory.
*/
rc = WaitLatch(&MyProc->procLatch, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
Min(history_period - (int)history_diff,
profile_period - (int)profile_diff), PG_WAIT_EXTENSION);
Min(pgws_historyPeriod - (int)history_diff,
pgws_historyPeriod - (int)profile_diff), PG_WAIT_EXTENSION);

if (rc & WL_POSTMASTER_DEATH)
proc_exit(1);
Expand Down
14 changes: 0 additions & 14 deletions compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,4 @@ InitPostgresCompat(const char *in_dbname, Oid dboid,
#endif
}

static inline void
get_guc_variables_compat(struct config_generic ***vars, int *num_vars)
{
Assert(vars != NULL);
Assert(num_vars != NULL);

#if PG_VERSION_NUM >= 160000
*vars = get_guc_variables(num_vars);
#else
*vars = get_guc_variables();
*num_vars = GetNumConfigOptions();
#endif
}

#endif
Loading