@@ -55,7 +55,7 @@ static int update_table_sizes_history(void);
5555static void get_stats_for_databases (Oid * databases_oids , int databases_cnt , bool fast );
5656static void run_database_stats_worker (bool fast , Oid db );
5757static int plugin_created (void );
58- static BgwHandleStatus WaitForBackgroundWorkerShutdown (BackgroundWorkerHandle * handle );
58+ static BgwHandleStatus WaitForBackgroundWorkerShutdownSafely (BackgroundWorkerHandle * handle );
5959static int delete_data_in_history (void );
6060static int put_data_into_history (void );
6161void _PG_init (void );
@@ -109,29 +109,24 @@ static void worker_sigterm(SIGNAL_ARGS) {
109109 * error handling to prevent infinite loops in case of hung workers.
110110 * Returns BGWH_STOPPED on success, BGWH_POSTMASTER_DIED on error/timeout.
111111 */
112- static BgwHandleStatus WaitForBackgroundWorkerShutdown (BackgroundWorkerHandle * handle ) {
112+ static BgwHandleStatus WaitForBackgroundWorkerShutdownSafely (BackgroundWorkerHandle * handle ) {
113113 BgwHandleStatus status ;
114114 int rc ;
115- bool save_set_latch_on_sigusr1 ;
116115 int attempts = 0 ;
117116 const int max_attempts = 5 * HOUR_TIME / 100 ; /* maximum 5 hours wait time */
118117
119- save_set_latch_on_sigusr1 = set_latch_on_sigusr1 ;
120- set_latch_on_sigusr1 = true;
121-
122118 PG_TRY ();
123119 {
124120 while (attempts < max_attempts ) {
125121 pid_t pid ;
126122
127123 status = GetBackgroundWorkerPid (handle , & pid );
128124 if (status == BGWH_STOPPED ) {
129- set_latch_on_sigusr1 = save_set_latch_on_sigusr1 ;
130125 return status ;
131126 }
132127
133128 /* Add 100ms timeout instead of infinite wait */
134- rc = WaitLatch (& MyProc -> procLatch , WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH , 100L );
129+ rc = WaitLatch (& MyProc -> procLatch , WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH , 100L , WAIT_EVENT_BGWORKER_SHUTDOWN );
135130
136131 ResetLatch (& MyProc -> procLatch );
137132
@@ -142,7 +137,7 @@ static BgwHandleStatus WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *h
142137
143138 /* Check for interrupts but don't let them break the entire process */
144139 if (QueryCancelPending || ProcDiePending ) {
145- ereport (WARNING , (errmsg ("WaitForBackgroundWorkerShutdown : received interrupt signal, stopping wait" )));
140+ ereport (WARNING , (errmsg ("WaitForBackgroundWorkerShutdownSafely : received interrupt signal, stopping wait" )));
146141 status = BGWH_POSTMASTER_DIED ; /* Return status as if postmaster died */
147142 break ;
148143 }
@@ -152,21 +147,18 @@ static BgwHandleStatus WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *h
152147
153148 /* If maximum attempts reached */
154149 if (attempts >= max_attempts ) {
155- ereport (WARNING , (errmsg ("WaitForBackgroundWorkerShutdown : timeout after %d attempts" , max_attempts )));
150+ ereport (WARNING , (errmsg ("WaitForBackgroundWorkerShutdownSafely : timeout after %d attempts" , max_attempts )));
156151 status = BGWH_POSTMASTER_DIED ; /* Return error status */
157152 }
158153 }
159154 PG_CATCH ();
160155 {
161156 /* Log error but do NOT re-throw exception */
162- ereport (WARNING , (errmsg ("WaitForBackgroundWorkerShutdown: caught exception, returning error status" )));
163- set_latch_on_sigusr1 = save_set_latch_on_sigusr1 ;
157+ ereport (WARNING , (errmsg ("WaitForBackgroundWorkerShutdownSafely: caught exception, returning error status" )));
164158 /* Return error status instead of PG_RE_THROW() */
165159 return BGWH_POSTMASTER_DIED ;
166160 }
167161 PG_END_TRY ();
168-
169- set_latch_on_sigusr1 = save_set_latch_on_sigusr1 ;
170162 return status ;
171163}
172164
@@ -356,7 +348,7 @@ void relsizes_database_stats_job(Datum args) {
356348 pqsignal (SIGTERM , worker_sigterm );
357349 BackgroundWorkerUnblockSignals ();
358350
359- BackgroundWorkerInitializeConnectionByOid (wa .s .db , InvalidOid );
351+ BackgroundWorkerInitializeConnectionByOid (wa .s .db , InvalidOid , 0 );
360352
361353 SetCurrentStatementStartTimestamp ();
362354 StartTransactionCommand ();
@@ -394,7 +386,7 @@ void relsizes_database_stats_job(Datum args) {
394386 /* Remove this condition after decision how to upgrade extensions is made. */
395387 if (SearchSysCacheExists3 (PROCNAMEARGSNSP ,
396388 CStringGetDatum ("get_stats_for_database" ),
397- PointerGetDatum (( & ( oidvector ){ . dim1 = 1 , . values = { INT4OID } } )),
389+ PointerGetDatum (buildoidvector (( Oid []){ INT4OID }, 1 )),
398390 ObjectIdGetDatum (get_namespace_oid ("relsizes_stats_schema" , true))))
399391 {
400392 const char * sql_get_stats =
@@ -495,7 +487,7 @@ static void run_database_stats_worker(bool fast, Oid db) {
495487 ereport (WARNING , (errmsg ("Failed to start background worker [%s], skipping" , database_worker .bgw_name )));
496488 return ;
497489 }
498- status = WaitForBackgroundWorkerShutdown (handle );
490+ status = WaitForBackgroundWorkerShutdownSafely (handle );
499491 if (status != BGWH_STOPPED ) {
500492 ereport (WARNING , (errmsg ("Failure during background worker execution [%s], continuing" , database_worker .bgw_name )));
501493 /* Don't abort execution, just log and continue */
@@ -618,7 +610,7 @@ Datum get_stats_for_database(PG_FUNCTION_ARGS) {
618610 /* Brief pause between file processing to reduce system load */
619611 int retcode = WaitLatch (& MyProc -> procLatch ,
620612 WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH ,
621- worker_file_naptime );
613+ worker_file_naptime , WAIT_EVENT_BUFFER_IO );
622614 ResetLatch (& MyProc -> procLatch );
623615
624616 CHECK_FOR_INTERRUPTS ();
@@ -671,7 +663,7 @@ static void get_stats_for_databases(Oid *databases_oids, int databases_cnt, bool
671663 CHECK_FOR_INTERRUPTS ();
672664 else {
673665 int naptime = (databases_cnt > 0 ) ? (worker_database_naptime / databases_cnt ) : worker_database_naptime ;
674- int retcode = WaitLatch (& MyProc -> procLatch , WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH , naptime );
666+ int retcode = WaitLatch (& MyProc -> procLatch , WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH , naptime , WAIT_EVENT_BGWORKER_STARTUP );
675667 ResetLatch (& MyProc -> procLatch );
676668 CHECK_FOR_INTERRUPTS ();
677669 /* emergency bailout if postmaster has died */
@@ -839,14 +831,14 @@ void relsizes_collect_stats(Datum main_arg) {
839831 optimizer = false;
840832 pqsignal (SIGTERM , worker_sigterm );
841833 BackgroundWorkerUnblockSignals ();
842- BackgroundWorkerInitializeConnection ("postgres" , NULL );
834+ BackgroundWorkerInitializeConnection ("postgres" , NULL , 0 );
843835
844836 while (!got_sigterm ) {
845837 if (enabled )
846838 relsizes_collect_stats_once_internal (true);
847839
848840 int retcode =
849- WaitLatch (& MyProc -> procLatch , WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH , worker_restart_naptime );
841+ WaitLatch (& MyProc -> procLatch , WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH , worker_restart_naptime , WAIT_EVENT_BGWORKER_STARTUP );
850842 ResetLatch (& MyProc -> procLatch );
851843 CHECK_FOR_INTERRUPTS ();
852844 if (retcode & WL_POSTMASTER_DEATH ) {
0 commit comments