Skip to content

Commit 33394ee

Browse files
committed
Force to send remaining WAL stats to the stats collector at walwriter exit.
In walwriter's main loop, WAL stats message is only sent if enough time has passed since last one was sent to reach PGSTAT_STAT_INTERVAL msecs. This is necessary to avoid overloading to the stats collector. But this can cause recent WAL stats to be unsent when walwriter exits. To ensure that all the WAL stats are sent, this commit makes walwriter force to send remaining WAL stats to the collector when it exits because of shutdown request. Note that those remaining WAL stats can still be unsent when walwriter exits with non-zero exit code (e.g., FATAL error). This is OK because that walwriter exit leads to server crash and subsequent recovery discards all the stats. So there is no need to send remaining stats in that case. Author: Masahiro Ikeda Reviewed-by: Fujii Masao Discussion: https://postgr.es/m/0509ad67b585a5b86a83d445dfa75392@oss.nttdata.com
1 parent 43c6662 commit 33394ee

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/backend/postmaster/walwriter.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ int WalWriterFlushAfter = 128;
7878
#define LOOPS_UNTIL_HIBERNATE 50
7979
#define HIBERNATE_FACTOR 25
8080

81+
/* Prototypes for private functions */
82+
static void HandleWalWriterInterrupts(void);
83+
8184
/*
8285
* Main entry point for walwriter process
8386
*
@@ -242,7 +245,8 @@ WalWriterMain(void)
242245
/* Clear any already-pending wakeups */
243246
ResetLatch(MyLatch);
244247

245-
HandleMainLoopInterrupts();
248+
/* Process any signals received recently */
249+
HandleWalWriterInterrupts();
246250

247251
/*
248252
* Do what we're here for; then, if XLogBackgroundFlush() found useful
@@ -272,3 +276,34 @@ WalWriterMain(void)
272276
WAIT_EVENT_WAL_WRITER_MAIN);
273277
}
274278
}
279+
280+
/*
281+
* Interrupt handler for main loops of WAL writer process.
282+
*/
283+
static void
284+
HandleWalWriterInterrupts(void)
285+
{
286+
if (ProcSignalBarrierPending)
287+
ProcessProcSignalBarrier();
288+
289+
if (ConfigReloadPending)
290+
{
291+
ConfigReloadPending = false;
292+
ProcessConfigFile(PGC_SIGHUP);
293+
}
294+
295+
if (ShutdownRequestPending)
296+
{
297+
/*
298+
* Force to send remaining WAL statistics to the stats collector at
299+
* process exit.
300+
*
301+
* Since pgstat_send_wal is invoked with 'force' is false in main loop
302+
* to avoid overloading to the stats collector, there may exist unsent
303+
* stats counters for the WAL writer.
304+
*/
305+
pgstat_send_wal(true);
306+
307+
proc_exit(0);
308+
}
309+
}

0 commit comments

Comments
 (0)