From 78874531baf99769468dedfff19aa7e2068bc5e5 Mon Sep 17 00:00:00 2001 From: Teodor Sigaev Date: Fri, 24 Mar 2017 13:53:40 +0300 Subject: Fix backup canceling Assert-enabled build crashes but without asserts it works by wrong way: it may not reset forcing full page write and preventing from starting exclusive backup with the same name as cancelled. Patch replaces pair of booleans nonexclusive_backup_running/exclusive_backup_running to single enum to correctly describe backup state. Backpatch to 9.6 where bug was introduced Reported-by: David Steele Authors: Michael Paquier, David Steele Reviewed-by: Anastasia Lubennikova https://commitfest.postgresql.org/13/1068/ --- src/backend/access/transam/xlog.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/backend/access/transam/xlog.c') diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index b99ded5df67..58790e0e96e 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -503,6 +503,12 @@ typedef enum ExclusiveBackupState EXCLUSIVE_BACKUP_STOPPING } ExclusiveBackupState; +/* + * Session status of running backup, used for sanity checks in SQL-callable + * functions to start and stop backups. + */ +static SessionBackupState sessionBackupState = SESSION_BACKUP_NONE; + /* * Shared state data for WAL insertion. */ @@ -10566,13 +10572,17 @@ do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p, /* * Mark that start phase has correctly finished for an exclusive backup. + * Session-level locks are updated as well to reflect that state. */ if (exclusive) { WALInsertLockAcquireExclusive(); XLogCtl->Insert.exclusiveBackupState = EXCLUSIVE_BACKUP_IN_PROGRESS; WALInsertLockRelease(); + sessionBackupState = SESSION_BACKUP_EXCLUSIVE; } + else + sessionBackupState = SESSION_BACKUP_NON_EXCLUSIVE; /* * We're done. As a convenience, return the starting WAL location. @@ -10627,6 +10637,15 @@ pg_stop_backup_callback(int code, Datum arg) WALInsertLockRelease(); } +/* + * Utility routine to fetch the session-level status of a backup running. + */ +SessionBackupState +get_backup_status(void) +{ + return sessionBackupState; +} + /* * do_pg_stop_backup is the workhorse of the user-visible pg_stop_backup() * function. @@ -10794,6 +10813,9 @@ do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p) } WALInsertLockRelease(); + /* Clean up session-level lock */ + sessionBackupState = SESSION_BACKUP_NONE; + /* * Read and parse the START WAL LOCATION line (this code is pretty crude, * but we are not expecting any variability in the file format). -- cgit v1.2.3