26
26
#include "utils/resowner.h"
27
27
#include "pgstat.h"
28
28
29
+ #include "compat.h"
29
30
#include "pg_wait_sampling.h"
30
31
31
32
static volatile sig_atomic_t shutdown_requested = false;
@@ -36,7 +37,7 @@ static void handle_sigterm(SIGNAL_ARGS);
36
37
* Register background worker for collecting waits history.
37
38
*/
38
39
void
39
- register_wait_collector (void )
40
+ pgws_register_wait_collector (void )
40
41
{
41
42
BackgroundWorker worker ;
42
43
@@ -47,7 +48,7 @@ register_wait_collector(void)
47
48
worker .bgw_restart_time = 1 ;
48
49
worker .bgw_notify_pid = 0 ;
49
50
snprintf (worker .bgw_library_name , BGW_MAXLEN , "pg_wait_sampling" );
50
- snprintf (worker .bgw_function_name , BGW_MAXLEN , CppAsString (collector_main ));
51
+ snprintf (worker .bgw_function_name , BGW_MAXLEN , CppAsString (pgws_collector_main ));
51
52
snprintf (worker .bgw_name , BGW_MAXLEN , "pg_wait_sampling collector" );
52
53
worker .bgw_main_arg = (Datum ) 0 ;
53
54
RegisterBackgroundWorker (& worker );
@@ -56,7 +57,7 @@ register_wait_collector(void)
56
57
/*
57
58
* Allocate memory for waits history.
58
59
*/
59
- void
60
+ static void
60
61
alloc_history (History * observations , int count )
61
62
{
62
63
observations -> items = (HistoryItem * ) palloc0 (sizeof (HistoryItem ) * count );
@@ -150,7 +151,7 @@ probe_waits(History *observations, HTAB *profile_hash,
150
151
TimestampTz ts = GetCurrentTimestamp ();
151
152
152
153
/* Realloc waits history if needed */
153
- newSize = collector_hdr -> historySize ;
154
+ newSize = pgws_collector_hdr -> historySize ;
154
155
if (observations -> count != newSize )
155
156
realloc_history (observations , newSize );
156
157
@@ -172,8 +173,8 @@ probe_waits(History *observations, HTAB *profile_hash,
172
173
item .pid = proc -> pid ;
173
174
item .wait_event_info = proc -> wait_event_info ;
174
175
175
- if (collector_hdr -> profileQueries )
176
- item .queryId = proc_queryids [i ];
176
+ if (pgws_collector_hdr -> profileQueries )
177
+ item .queryId = pgws_proc_queryids [i ];
177
178
else
178
179
item .queryId = 0 ;
179
180
@@ -291,7 +292,7 @@ make_profile_hash()
291
292
hash_ctl .hash = tag_hash ;
292
293
hash_ctl .hcxt = TopMemoryContext ;
293
294
294
- if (collector_hdr -> profileQueries )
295
+ if (pgws_collector_hdr -> profileQueries )
295
296
hash_ctl .keysize = offsetof(ProfileItem , count );
296
297
else
297
298
hash_ctl .keysize = offsetof(ProfileItem , queryId );
@@ -320,7 +321,7 @@ millisecs_diff(TimestampTz tz1, TimestampTz tz2)
320
321
* Main routine of wait history collector.
321
322
*/
322
323
void
323
- collector_main (Datum main_arg )
324
+ pgws_collector_main (Datum main_arg )
324
325
{
325
326
HTAB * profile_hash = NULL ;
326
327
History observations ;
@@ -350,25 +351,20 @@ collector_main(Datum main_arg)
350
351
pqsignal (SIGTERM , handle_sigterm );
351
352
pqsignal (SIGUSR1 , procsignal_sigusr1_handler );
352
353
BackgroundWorkerUnblockSignals ();
353
-
354
- #if PG_VERSION_NUM >= 110000
355
- InitPostgres (NULL , InvalidOid , NULL , InvalidOid , NULL , false);
356
- #else
357
- InitPostgres (NULL , InvalidOid , NULL , InvalidOid , NULL );
358
- #endif
354
+ InitPostgresCompat (NULL , InvalidOid , NULL , InvalidOid , false, false, NULL );
359
355
SetProcessingMode (NormalProcessing );
360
356
361
357
/* Make pg_wait_sampling recognisable in pg_stat_activity */
362
358
pgstat_report_appname ("pg_wait_sampling collector" );
363
359
364
360
profile_hash = make_profile_hash ();
365
- collector_hdr -> latch = & MyProc -> procLatch ;
361
+ pgws_collector_hdr -> latch = & MyProc -> procLatch ;
366
362
367
363
CurrentResourceOwner = ResourceOwnerCreate (NULL , "pg_wait_sampling collector" );
368
364
collector_context = AllocSetContextCreate (TopMemoryContext ,
369
365
"pg_wait_sampling context" , ALLOCSET_DEFAULT_SIZES );
370
366
old_context = MemoryContextSwitchTo (collector_context );
371
- alloc_history (& observations , collector_hdr -> historySize );
367
+ alloc_history (& observations , pgws_collector_hdr -> historySize );
372
368
MemoryContextSwitchTo (old_context );
373
369
374
370
ereport (LOG , (errmsg ("pg_wait_sampling collector started" )));
@@ -395,16 +391,16 @@ collector_main(Datum main_arg)
395
391
396
392
history_diff = millisecs_diff (history_ts , current_ts );
397
393
profile_diff = millisecs_diff (profile_ts , current_ts );
398
- history_period = collector_hdr -> historyPeriod ;
399
- profile_period = collector_hdr -> profilePeriod ;
394
+ history_period = pgws_collector_hdr -> historyPeriod ;
395
+ profile_period = pgws_collector_hdr -> profilePeriod ;
400
396
401
397
write_history = (history_diff >= (int64 )history_period );
402
398
write_profile = (profile_diff >= (int64 )profile_period );
403
399
404
400
if (write_history || write_profile )
405
401
{
406
402
probe_waits (& observations , profile_hash ,
407
- write_history , write_profile , collector_hdr -> profilePid );
403
+ write_history , write_profile , pgws_collector_hdr -> profilePid );
408
404
409
405
if (write_history )
410
406
{
@@ -443,23 +439,24 @@ collector_main(Datum main_arg)
443
439
ResetLatch (& MyProc -> procLatch );
444
440
445
441
/* Handle request if any */
446
- if (collector_hdr -> request != NO_REQUEST )
442
+ if (pgws_collector_hdr -> request != NO_REQUEST )
447
443
{
448
444
LOCKTAG tag ;
449
- SHMRequest request = collector_hdr -> request ;
445
+ SHMRequest request ;
450
446
451
- init_lock_tag (& tag , PGWS_COLLECTOR_LOCK );
447
+ pgws_init_lock_tag (& tag , PGWS_COLLECTOR_LOCK );
452
448
453
449
LockAcquire (& tag , ExclusiveLock , false, false);
454
- collector_hdr -> request = NO_REQUEST ;
450
+ request = pgws_collector_hdr -> request ;
451
+ pgws_collector_hdr -> request = NO_REQUEST ;
455
452
456
453
if (request == HISTORY_REQUEST || request == PROFILE_REQUEST )
457
454
{
458
455
shm_mq_result mq_result ;
459
456
460
457
/* Send history or profile */
461
- shm_mq_set_sender (collector_mq , MyProc );
462
- mqh = shm_mq_attach (collector_mq , NULL , NULL );
458
+ shm_mq_set_sender (pgws_collector_mq , MyProc );
459
+ mqh = shm_mq_attach (pgws_collector_mq , NULL , NULL );
463
460
mq_result = shm_mq_wait_for_attach (mqh );
464
461
switch (mq_result )
465
462
{
@@ -485,7 +482,7 @@ collector_main(Datum main_arg)
485
482
default :
486
483
AssertState (false);
487
484
}
488
- shm_mq_detach_compat (mqh , collector_mq );
485
+ shm_mq_detach_compat (mqh , pgws_collector_mq );
489
486
}
490
487
else if (request == PROFILE_RESET )
491
488
{
0 commit comments