Skip to content

Commit bcbc272

Browse files
committed
Fix minor problems with non-exclusive backup cleanup.
The previous coding imagined that it could call before_shmem_exit() when a non-exclusive backup began and then remove the previously-added handler by calling cancel_before_shmem_exit() when that backup ended. However, this only works provided that nothing else in the system has registered a before_shmem_exit() hook in the interim, because cancel_before_shmem_exit() is documented to remove a callback only if it is the latest callback registered. It also only works if nothing can ERROR out between the time that sessionBackupState is reset and the time that cancel_before_shmem_exit(), which doesn't seem to be strictly true. To fix, leave the handler installed for the lifetime of the session, arrange to install it just once, and teach it to quietly do nothing if there isn't a non-exclusive backup in process. This was originally committed to master as 3036401, but I did not back-patch at the time because the consequences were minor. However, now there's been a second report of this causing trouble with a slightly different test case than the one I reported originally, so now I'm back-patching as far as v11 where JIT was introduced. Patch by me, reviewed by Kyotaro Horiguchi, Michael Paquier (who preferred a different approach, but got outvoted), Fujii Masao, and Tom Lane, and with comments by various others. New problem report from Bharath Rupireddy. Discussion: http://postgr.es/m/CA+TgmobMjnyBfNhGTKQEDbqXYE3_rXWpc4CM63fhyerNCes3mA@mail.gmail.com Discussion: http://postgr.es/m/CALj2ACWk7j4F2v2fxxYfrroOF=AdFNPr1WsV+AGtHAFQOqm_pw@mail.gmail.com
1 parent f87f77e commit bcbc272

File tree

4 files changed

+35
-33
lines changed

4 files changed

+35
-33
lines changed

src/backend/access/transam/xlog.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11188,23 +11188,30 @@ do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
1118811188
* system out of backup mode, thus making it a lot more safe to call from
1118911189
* an error handler.
1119011190
*
11191+
* The caller can pass 'arg' as 'true' or 'false' to control whether a warning
11192+
* is emitted.
11193+
*
1119111194
* NB: This is only for aborting a non-exclusive backup that doesn't write
1119211195
* backup_label. A backup started with pg_start_backup() needs to be finished
1119311196
* with pg_stop_backup().
11197+
*
11198+
* NB: This gets used as a before_shmem_exit handler, hence the odd-looking
11199+
* signature.
1119411200
*/
1119511201
void
11196-
do_pg_abort_backup(void)
11202+
do_pg_abort_backup(int code, Datum arg)
1119711203
{
11204+
bool emit_warning = DatumGetBool(arg);
11205+
1119811206
/*
1119911207
* Quick exit if session is not keeping around a non-exclusive backup
1120011208
* already started.
1120111209
*/
11202-
if (sessionBackupState == SESSION_BACKUP_NONE)
11210+
if (sessionBackupState != SESSION_BACKUP_NON_EXCLUSIVE)
1120311211
return;
1120411212

1120511213
WALInsertLockAcquireExclusive();
1120611214
Assert(XLogCtl->Insert.nonExclusiveBackups > 0);
11207-
Assert(sessionBackupState == SESSION_BACKUP_NON_EXCLUSIVE);
1120811215
XLogCtl->Insert.nonExclusiveBackups--;
1120911216

1121011217
if (XLogCtl->Insert.exclusiveBackupState == EXCLUSIVE_BACKUP_NONE &&
@@ -11213,6 +11220,25 @@ do_pg_abort_backup(void)
1121311220
XLogCtl->Insert.forcePageWrites = false;
1121411221
}
1121511222
WALInsertLockRelease();
11223+
11224+
if (emit_warning)
11225+
ereport(WARNING,
11226+
(errmsg("aborting backup due to backend exiting before pg_stop_back up was called")));
11227+
}
11228+
11229+
/*
11230+
* Register a handler that will warn about unterminated backups at end of
11231+
* session, unless this has already been done.
11232+
*/
11233+
void
11234+
register_persistent_abort_backup_handler(void)
11235+
{
11236+
static bool already_done = false;
11237+
11238+
if (already_done)
11239+
return;
11240+
before_shmem_exit(do_pg_abort_backup, DatumGetBool(true));
11241+
already_done = true;
1121611242
}
1121711243

1121811244
/*

src/backend/access/transam/xlogfuncs.c

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,6 @@
4545
static StringInfo label_file;
4646
static StringInfo tblspc_map_file;
4747

48-
/*
49-
* Called when the backend exits with a running non-exclusive base backup,
50-
* to clean up state.
51-
*/
52-
static void
53-
nonexclusive_base_backup_cleanup(int code, Datum arg)
54-
{
55-
do_pg_abort_backup();
56-
ereport(WARNING,
57-
(errmsg("aborting backup due to backend exiting before pg_stop_backup was called")));
58-
}
59-
6048
/*
6149
* pg_start_backup: set up for taking an on-line backup dump
6250
*
@@ -104,10 +92,10 @@ pg_start_backup(PG_FUNCTION_ARGS)
10492
tblspc_map_file = makeStringInfo();
10593
MemoryContextSwitchTo(oldcontext);
10694

95+
register_persistent_abort_backup_handler();
96+
10797
startpoint = do_pg_start_backup(backupidstr, fast, NULL, label_file,
10898
NULL, tblspc_map_file, false, true);
109-
110-
before_shmem_exit(nonexclusive_base_backup_cleanup, (Datum) 0);
11199
}
112100

113101
PG_RETURN_LSN(startpoint);
@@ -249,7 +237,6 @@ pg_stop_backup_v2(PG_FUNCTION_ARGS)
249237
* and tablespace map so they can be written to disk by the caller.
250238
*/
251239
stoppoint = do_pg_stop_backup(label_file->data, waitforarchive, NULL);
252-
cancel_before_shmem_exit(nonexclusive_base_backup_cleanup, (Datum) 0);
253240

254241
values[1] = CStringGetTextDatum(label_file->data);
255242
values[2] = CStringGetTextDatum(tblspc_map_file->data);

src/backend/replication/basebackup.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ static int64 _tarWriteDir(const char *pathbuf, int basepathlen, struct stat *sta
6666
bool sizeonly);
6767
static void send_int8_string(StringInfoData *buf, int64 intval);
6868
static void SendBackupHeader(List *tablespaces);
69-
static void base_backup_cleanup(int code, Datum arg);
7069
static void perform_base_backup(basebackup_options *opt);
7170
static void parse_basebackup_options(List *options, basebackup_options *opt);
7271
static void SendXlogRecPtrResult(XLogRecPtr ptr, TimeLineID tli);
@@ -231,17 +230,6 @@ static const struct exclude_list_item noChecksumFiles[] = {
231230
{NULL, false}
232231
};
233232

234-
235-
/*
236-
* Called when ERROR or FATAL happens in perform_base_backup() after
237-
* we have started the backup - make sure we end it!
238-
*/
239-
static void
240-
base_backup_cleanup(int code, Datum arg)
241-
{
242-
do_pg_abort_backup();
243-
}
244-
245233
/*
246234
* Actually do a base backup for the specified tablespaces.
247235
*
@@ -280,7 +268,7 @@ perform_base_backup(basebackup_options *opt)
280268
* do_pg_stop_backup() should be inside the error cleanup block!
281269
*/
282270

283-
PG_ENSURE_ERROR_CLEANUP(base_backup_cleanup, (Datum) 0);
271+
PG_ENSURE_ERROR_CLEANUP(do_pg_abort_backup, BoolGetDatum(false));
284272
{
285273
ListCell *lc;
286274
tablespaceinfo *ti;
@@ -389,7 +377,7 @@ perform_base_backup(basebackup_options *opt)
389377

390378
endptr = do_pg_stop_backup(labelfile->data, !opt->nowait, &endtli);
391379
}
392-
PG_END_ENSURE_ERROR_CLEANUP(base_backup_cleanup, (Datum) 0);
380+
PG_END_ENSURE_ERROR_CLEANUP(do_pg_abort_backup, BoolGetDatum(false));
393381

394382

395383
if (opt->includewal)

src/include/access/xlog.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ extern XLogRecPtr do_pg_start_backup(const char *backupidstr, bool fast,
359359
bool needtblspcmapfile);
360360
extern XLogRecPtr do_pg_stop_backup(char *labelfile, bool waitforarchive,
361361
TimeLineID *stoptli_p);
362-
extern void do_pg_abort_backup(void);
362+
extern void do_pg_abort_backup(int code, Datum arg);
363+
extern void register_persistent_abort_backup_handler(void);
363364
extern SessionBackupState get_backup_status(void);
364365

365366
/* File path names (all relative to $PGDATA) */

0 commit comments

Comments
 (0)