Skip to content

Commit 6fb7c5d

Browse files
committed
Centralize timestamp computation of control file on updates
This commit moves the timestamp computation of the control file within the routine of src/common/ in charge of updating the backend's control file, which is shared by multiple frontend tools (pg_rewind, pg_checksums and pg_resetwal) and the backend itself. This change has as direct effect to update the control file's timestamp when writing the control file in pg_rewind and pg_checksums, something that is helpful to keep track of control file updates for those operations, something also tracked by the backend at startup within its logs. This part is arguably a bug, as ControlFileData->time should be updated each time a new version of the control file is written, but this is a behavior change so no backpatch is done. Author: Amul Sul Reviewed-by: Nathan Bossart, Michael Paquier, Bharath Rupireddy Discussion: https://postgr.es/m/CAAJ_b97nd_ghRpyFV9Djf9RLXkoTbOUqnocq11WGq9TisX09Fw@mail.gmail.com
1 parent 3804539 commit 6fb7c5d

File tree

3 files changed

+5
-8
lines changed

3 files changed

+5
-8
lines changed

src/backend/access/transam/xlog.c

+1-7
Original file line numberDiff line numberDiff line change
@@ -7339,7 +7339,7 @@ StartupXLOG(void)
73397339
ControlFile->backupEndPoint = ControlFile->minRecoveryPoint;
73407340
}
73417341
}
7342-
ControlFile->time = (pg_time_t) time(NULL);
7342+
73437343
/* No need to hold ControlFileLock yet, we aren't up far enough */
73447344
UpdateControlFile();
73457345

@@ -8199,7 +8199,6 @@ StartupXLOG(void)
81998199
*/
82008200
LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
82018201
ControlFile->state = DB_IN_PRODUCTION;
8202-
ControlFile->time = (pg_time_t) time(NULL);
82038202

82048203
SpinLockAcquire(&XLogCtl->info_lck);
82058204
XLogCtl->SharedRecoveryState = RECOVERY_STATE_DONE;
@@ -9142,7 +9141,6 @@ CreateCheckPoint(int flags)
91429141
{
91439142
LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
91449143
ControlFile->state = DB_SHUTDOWNING;
9145-
ControlFile->time = (pg_time_t) time(NULL);
91469144
UpdateControlFile();
91479145
LWLockRelease(ControlFileLock);
91489146
}
@@ -9412,7 +9410,6 @@ CreateCheckPoint(int flags)
94129410
ControlFile->state = DB_SHUTDOWNED;
94139411
ControlFile->checkPoint = ProcLastRecPtr;
94149412
ControlFile->checkPointCopy = checkPoint;
9415-
ControlFile->time = (pg_time_t) time(NULL);
94169413
/* crash recovery should always recover to the end of WAL */
94179414
ControlFile->minRecoveryPoint = InvalidXLogRecPtr;
94189415
ControlFile->minRecoveryPointTLI = 0;
@@ -9539,7 +9536,6 @@ CreateEndOfRecoveryRecord(void)
95399536
* changes to this point.
95409537
*/
95419538
LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
9542-
ControlFile->time = (pg_time_t) time(NULL);
95439539
ControlFile->minRecoveryPoint = recptr;
95449540
ControlFile->minRecoveryPointTLI = xlrec.ThisTimeLineID;
95459541
UpdateControlFile();
@@ -9740,7 +9736,6 @@ CreateRestartPoint(int flags)
97409736
{
97419737
LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
97429738
ControlFile->state = DB_SHUTDOWNED_IN_RECOVERY;
9743-
ControlFile->time = (pg_time_t) time(NULL);
97449739
UpdateControlFile();
97459740
LWLockRelease(ControlFileLock);
97469741
}
@@ -9801,7 +9796,6 @@ CreateRestartPoint(int flags)
98019796
{
98029797
ControlFile->checkPoint = lastCheckPointRecPtr;
98039798
ControlFile->checkPointCopy = lastCheckPoint;
9804-
ControlFile->time = (pg_time_t) time(NULL);
98059799

98069800
/*
98079801
* Ensure minRecoveryPoint is past the checkpoint record. Normally,

src/bin/pg_resetwal/pg_resetwal.c

-1
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,6 @@ RewriteControlFile(void)
911911
ControlFile.checkPointCopy.time = (pg_time_t) time(NULL);
912912

913913
ControlFile.state = DB_SHUTDOWNED;
914-
ControlFile.time = (pg_time_t) time(NULL);
915914
ControlFile.checkPoint = ControlFile.checkPointCopy.redo;
916915
ControlFile.minRecoveryPoint = 0;
917916
ControlFile.minRecoveryPointTLI = 0;

src/common/controldata_utils.c

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <unistd.h>
2424
#include <sys/stat.h>
2525
#include <fcntl.h>
26+
#include <time.h>
2627

2728
#include "access/xlog_internal.h"
2829
#include "catalog/pg_control.h"
@@ -168,6 +169,9 @@ update_controlfile(const char *DataDir,
168169
StaticAssertStmt(sizeof(ControlFileData) <= PG_CONTROL_FILE_SIZE,
169170
"sizeof(ControlFileData) exceeds PG_CONTROL_FILE_SIZE");
170171

172+
/* Update timestamp */
173+
ControlFile->time = (pg_time_t) time(NULL);
174+
171175
/* Recalculate CRC of control file */
172176
INIT_CRC32C(ControlFile->crc);
173177
COMP_CRC32C(ControlFile->crc,

0 commit comments

Comments
 (0)