diff options
author | Robert Haas <rhaas@postgresql.org> | 2019-12-19 09:06:54 -0500 |
---|---|---|
committer | Robert Haas <rhaas@postgresql.org> | 2019-12-19 09:06:54 -0500 |
commit | 303640199d0436c5e7acdf50b837a027b5726594 (patch) | |
tree | 7e29adeaf572d6bb1ba0d44649b86f64520e109c /src/backend/access/transam/xlog.c | |
parent | e975c1a6026adb9e248a408fe3ca2629bc8c0084 (diff) | |
download | postgresql-303640199d0436c5e7acdf50b837a027b5726594.tar.gz postgresql-303640199d0436c5e7acdf50b837a027b5726594.zip |
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 is a bug, but for now I'm not going to back-patch, because the
consequences are minor. It's possible to cause a spurious warning
to be generated, but that doesn't really matter. It's also possible
to trigger an assertion failure, but production builds shouldn't
have assertions enabled.
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.
Discussion: http://postgr.es/m/CA+TgmobMjnyBfNhGTKQEDbqXYE3_rXWpc4CM63fhyerNCes3mA@mail.gmail.com
Diffstat (limited to 'src/backend/access/transam/xlog.c')
-rw-r--r-- | src/backend/access/transam/xlog.c | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 71b8389ba19..edee0c0f22e 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -11133,23 +11133,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 && @@ -11158,6 +11165,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; } /* |