Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 688423d

Browse files
committed
Exit from base backups when shutdown is requested
When the exit waits until the whole backup completes, it may take a very long time. In passing, add back an error check in the main loop so we detect clients that disconnect much earlier if the backup is large.
1 parent 5294816 commit 688423d

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

src/backend/replication/basebackup.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,15 @@ sendDir(char *path, int basepathlen, bool sizeonly)
308308
strlen(PG_TEMP_FILE_PREFIX)) == 0)
309309
continue;
310310

311+
/*
312+
* Check if the postmaster has signaled us to exit, and abort
313+
* with an error in that case. The error handler further up
314+
* will call do_pg_abort_backup() for us.
315+
*/
316+
if (walsender_shutdown_requested || walsender_ready_to_stop)
317+
ereport(ERROR,
318+
(errmsg("shutdown requested, aborting active base backup")));
319+
311320
snprintf(pathbuf, MAXPGPATH, "%s/%s", path, de->d_name);
312321

313322
/* Skip postmaster.pid in the data directory */
@@ -462,7 +471,10 @@ sendFile(char *filename, int basepathlen, struct stat * statbuf)
462471
while ((cnt = fread(buf, 1, Min(sizeof(buf), statbuf->st_size - len), fp)) > 0)
463472
{
464473
/* Send the chunk as a CopyData message */
465-
pq_putmessage('d', buf, cnt);
474+
if (pq_putmessage('d', buf, cnt))
475+
ereport(ERROR,
476+
(errmsg("base backup could not send data, aborting backup")));
477+
466478
len += cnt;
467479

468480
if (len >= statbuf->st_size)

src/backend/replication/walsender.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ static XLogRecPtr sentPtr = {0, 0};
8888

8989
/* Flags set by signal handlers for later service in main loop */
9090
static volatile sig_atomic_t got_SIGHUP = false;
91-
static volatile sig_atomic_t shutdown_requested = false;
92-
static volatile sig_atomic_t ready_to_stop = false;
91+
volatile sig_atomic_t walsender_shutdown_requested = false;
92+
volatile sig_atomic_t walsender_ready_to_stop = false;
9393

9494
/* Signal handlers */
9595
static void WalSndSigHupHandler(SIGNAL_ARGS);
@@ -426,16 +426,16 @@ WalSndLoop(void)
426426
* When SIGUSR2 arrives, we send all outstanding logs up to the
427427
* shutdown checkpoint record (i.e., the latest record) and exit.
428428
*/
429-
if (ready_to_stop)
429+
if (walsender_ready_to_stop)
430430
{
431431
if (!XLogSend(output_message, &caughtup))
432432
break;
433433
if (caughtup)
434-
shutdown_requested = true;
434+
walsender_shutdown_requested = true;
435435
}
436436

437437
/* Normal exit from the walsender is here */
438-
if (shutdown_requested)
438+
if (walsender_shutdown_requested)
439439
{
440440
/* Inform the standby that XLOG streaming was done */
441441
pq_puttextmessage('C', "COPY 0");
@@ -461,7 +461,7 @@ WalSndLoop(void)
461461

462462
if (!XLogSend(output_message, &caughtup))
463463
break;
464-
if (caughtup && !got_SIGHUP && !ready_to_stop && !shutdown_requested)
464+
if (caughtup && !got_SIGHUP && !walsender_ready_to_stop && !walsender_shutdown_requested)
465465
{
466466
/*
467467
* XXX: We don't really need the periodic wakeups anymore,
@@ -841,7 +841,7 @@ WalSndSigHupHandler(SIGNAL_ARGS)
841841
static void
842842
WalSndShutdownHandler(SIGNAL_ARGS)
843843
{
844-
shutdown_requested = true;
844+
walsender_shutdown_requested = true;
845845
if (MyWalSnd)
846846
SetLatch(&MyWalSnd->latch);
847847
}
@@ -889,7 +889,7 @@ WalSndXLogSendHandler(SIGNAL_ARGS)
889889
static void
890890
WalSndLastCycleHandler(SIGNAL_ARGS)
891891
{
892-
ready_to_stop = true;
892+
walsender_ready_to_stop = true;
893893
if (MyWalSnd)
894894
SetLatch(&MyWalSnd->latch);
895895
}

src/include/replication/walsender.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ extern WalSndCtlData *WalSndCtl;
5353

5454
/* global state */
5555
extern bool am_walsender;
56+
extern volatile sig_atomic_t walsender_shutdown_requested;
57+
extern volatile sig_atomic_t walsender_ready_to_stop;
5658

5759
/* user-settable parameters */
5860
extern int WalSndDelay;

0 commit comments

Comments
 (0)