From fe5f249a7fdb6f0999092a611ed39f1b4076a3e7 Mon Sep 17 00:00:00 2001 From: Amul Sul Date: Wed, 16 Jun 2021 09:02:24 -0400 Subject: [PATCH v30 2/5] Refactor: add function to set database state in control file --- src/backend/access/transam/xlog.c | 29 ++++++++++++++--------------- src/include/access/xlog.h | 2 ++ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 445f13b28a3..ac50d567be9 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -38,7 +38,6 @@ #include "access/xlogreader.h" #include "access/xlogutils.h" #include "catalog/catversion.h" -#include "catalog/pg_control.h" #include "catalog/pg_database.h" #include "commands/progress.h" #include "commands/tablespace.h" @@ -8135,6 +8134,17 @@ XLogAcceptWrites(XLogReaderState *xlogreader, XLogRecPtr EndOfLog, return promoted; } +/* Set ControlFile's database state */ +void +SetControlFileDBState(DBState state) +{ + LWLockAcquire(ControlFileLock, LW_EXCLUSIVE); + ControlFile->state = state; + ControlFile->time = (pg_time_t) time(NULL); + UpdateControlFile(); + LWLockRelease(ControlFileLock); +} + /* * Checks if recovery has reached a consistent state. When consistency is * reached and we have a valid starting standby snapshot, tell postmaster @@ -8976,13 +8986,7 @@ CreateCheckPoint(int flags) START_CRIT_SECTION(); if (shutdown) - { - LWLockAcquire(ControlFileLock, LW_EXCLUSIVE); - ControlFile->state = DB_SHUTDOWNING; - ControlFile->time = (pg_time_t) time(NULL); - UpdateControlFile(); - LWLockRelease(ControlFileLock); - } + SetControlFileDBState(DB_SHUTDOWNING); /* * Let smgr prepare for checkpoint; this has to happen before we determine @@ -9523,13 +9527,8 @@ CreateRestartPoint(int flags) UpdateMinRecoveryPoint(InvalidXLogRecPtr, true); if (flags & CHECKPOINT_IS_SHUTDOWN) - { - LWLockAcquire(ControlFileLock, LW_EXCLUSIVE); - ControlFile->state = DB_SHUTDOWNED_IN_RECOVERY; - ControlFile->time = (pg_time_t) time(NULL); - UpdateControlFile(); - LWLockRelease(ControlFileLock); - } + SetControlFileDBState(DB_SHUTDOWNED_IN_RECOVERY); + return false; } diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h index 77187c12beb..e730572b168 100644 --- a/src/include/access/xlog.h +++ b/src/include/access/xlog.h @@ -15,6 +15,7 @@ #include "access/xlogdefs.h" #include "access/xloginsert.h" #include "access/xlogreader.h" +#include "catalog/pg_control.h" #include "datatype/timestamp.h" #include "lib/stringinfo.h" #include "nodes/pg_list.h" @@ -334,6 +335,7 @@ extern void XLOGShmemInit(void); extern void BootStrapXLOG(void); extern void LocalProcessControlFile(bool reset); extern void StartupXLOG(void); +extern void SetControlFileDBState(DBState state); extern void ShutdownXLOG(int code, Datum arg); extern void InitXLOGAccess(void); extern void CreateCheckPoint(int flags); -- 2.18.0