Fix minor problems with non-exclusive backup cleanup.
authorRobert Haas <[email protected]>
Thu, 6 Aug 2020 17:58:23 +0000 (13:58 -0400)
committerRobert Haas <[email protected]>
Thu, 6 Aug 2020 17:58:23 +0000 (13:58 -0400)
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
303640199d0436c5e7acdf50b837a027b5726594, 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

src/backend/access/transam/xlog.c
src/backend/access/transam/xlogfuncs.c
src/backend/replication/basebackup.c
src/include/access/xlog.h

index 18b4d11bd03f1d1145c0b082dc1e86bc2bbe1bfa..018588187248eab74344633d0c98da83b62a2423 100644 (file)
@@ -11188,23 +11188,30 @@ do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
  * system out of backup mode, thus making it a lot more safe to call from
  * an error handler.
  *
+ * The caller can pass 'arg' as 'true' or 'false' to control whether a warning
+ * is emitted.
+ *
  * NB: This is only for aborting a non-exclusive backup that doesn't write
  * backup_label. A backup started with pg_start_backup() needs to be finished
  * with pg_stop_backup().
+ *
+ * NB: This gets used as a before_shmem_exit handler, hence the odd-looking
+ * signature.
  */
 void
-do_pg_abort_backup(void)
+do_pg_abort_backup(int code, Datum arg)
 {
+   bool    emit_warning = DatumGetBool(arg);
+
    /*
     * Quick exit if session is not keeping around a non-exclusive backup
     * already started.
     */
-   if (sessionBackupState == SESSION_BACKUP_NONE)
+   if (sessionBackupState != SESSION_BACKUP_NON_EXCLUSIVE)
        return;
 
    WALInsertLockAcquireExclusive();
    Assert(XLogCtl->Insert.nonExclusiveBackups > 0);
-   Assert(sessionBackupState == SESSION_BACKUP_NON_EXCLUSIVE);
    XLogCtl->Insert.nonExclusiveBackups--;
 
    if (XLogCtl->Insert.exclusiveBackupState == EXCLUSIVE_BACKUP_NONE &&
@@ -11213,6 +11220,25 @@ do_pg_abort_backup(void)
        XLogCtl->Insert.forcePageWrites = false;
    }
    WALInsertLockRelease();
+
+   if (emit_warning)
+       ereport(WARNING,
+               (errmsg("aborting backup due to backend exiting before pg_stop_back up was called")));
+}
+
+/*
+ * Register a handler that will warn about unterminated backups at end of
+ * session, unless this has already been done.
+ */
+void
+register_persistent_abort_backup_handler(void)
+{
+   static bool already_done = false;
+
+   if (already_done)
+       return;
+   before_shmem_exit(do_pg_abort_backup, DatumGetBool(true));
+   already_done = true;
 }
 
 /*
index 89c98e5562888dcfb3f1099754cf4d7d991c7f64..228f36ce60a1462514dc27a97aa99dcb719371f6 100644 (file)
 static StringInfo label_file;
 static StringInfo tblspc_map_file;
 
-/*
- * Called when the backend exits with a running non-exclusive base backup,
- * to clean up state.
- */
-static void
-nonexclusive_base_backup_cleanup(int code, Datum arg)
-{
-   do_pg_abort_backup();
-   ereport(WARNING,
-           (errmsg("aborting backup due to backend exiting before pg_stop_backup was called")));
-}
-
 /*
  * pg_start_backup: set up for taking an on-line backup dump
  *
@@ -104,10 +92,10 @@ pg_start_backup(PG_FUNCTION_ARGS)
        tblspc_map_file = makeStringInfo();
        MemoryContextSwitchTo(oldcontext);
 
+       register_persistent_abort_backup_handler();
+
        startpoint = do_pg_start_backup(backupidstr, fast, NULL, label_file,
                                        NULL, tblspc_map_file, false, true);
-
-       before_shmem_exit(nonexclusive_base_backup_cleanup, (Datum) 0);
    }
 
    PG_RETURN_LSN(startpoint);
@@ -249,7 +237,6 @@ pg_stop_backup_v2(PG_FUNCTION_ARGS)
         * and tablespace map so they can be written to disk by the caller.
         */
        stoppoint = do_pg_stop_backup(label_file->data, waitforarchive, NULL);
-       cancel_before_shmem_exit(nonexclusive_base_backup_cleanup, (Datum) 0);
 
        values[1] = CStringGetTextDatum(label_file->data);
        values[2] = CStringGetTextDatum(tblspc_map_file->data);
index 3bc26568eb7044fad292739cc5e4a62aa5013c94..efc458d80b48c00915cc317457259dcc6b6461b7 100644 (file)
@@ -66,7 +66,6 @@ static int64 _tarWriteDir(const char *pathbuf, int basepathlen, struct stat *sta
                          bool sizeonly);
 static void send_int8_string(StringInfoData *buf, int64 intval);
 static void SendBackupHeader(List *tablespaces);
-static void base_backup_cleanup(int code, Datum arg);
 static void perform_base_backup(basebackup_options *opt);
 static void parse_basebackup_options(List *options, basebackup_options *opt);
 static void SendXlogRecPtrResult(XLogRecPtr ptr, TimeLineID tli);
@@ -231,17 +230,6 @@ static const struct exclude_list_item noChecksumFiles[] = {
    {NULL, false}
 };
 
-
-/*
- * Called when ERROR or FATAL happens in perform_base_backup() after
- * we have started the backup - make sure we end it!
- */
-static void
-base_backup_cleanup(int code, Datum arg)
-{
-   do_pg_abort_backup();
-}
-
 /*
  * Actually do a base backup for the specified tablespaces.
  *
@@ -280,7 +268,7 @@ perform_base_backup(basebackup_options *opt)
     * do_pg_stop_backup() should be inside the error cleanup block!
     */
 
-   PG_ENSURE_ERROR_CLEANUP(base_backup_cleanup, (Datum) 0);
+   PG_ENSURE_ERROR_CLEANUP(do_pg_abort_backup, BoolGetDatum(false));
    {
        ListCell   *lc;
        tablespaceinfo *ti;
@@ -389,7 +377,7 @@ perform_base_backup(basebackup_options *opt)
 
        endptr = do_pg_stop_backup(labelfile->data, !opt->nowait, &endtli);
    }
-   PG_END_ENSURE_ERROR_CLEANUP(base_backup_cleanup, (Datum) 0);
+   PG_END_ENSURE_ERROR_CLEANUP(do_pg_abort_backup, BoolGetDatum(false));
 
 
    if (opt->includewal)
index d6ab348cb69f162d48c0b2acca818a86a6bc1629..0bed7e337ce69ba05c90ccf5600aaa4206f30018 100644 (file)
@@ -359,7 +359,8 @@ extern XLogRecPtr do_pg_start_backup(const char *backupidstr, bool fast,
                                     bool needtblspcmapfile);
 extern XLogRecPtr do_pg_stop_backup(char *labelfile, bool waitforarchive,
                                    TimeLineID *stoptli_p);
-extern void do_pg_abort_backup(void);
+extern void do_pg_abort_backup(int code, Datum arg);
+extern void register_persistent_abort_backup_handler(void);
 extern SessionBackupState get_backup_status(void);
 
 /* File path names (all relative to $PGDATA) */