]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix locking bugs that could corrupt pg_control.
authorThomas Munro <tmunro@postgresql.org>
Mon, 8 Jun 2020 01:57:24 +0000 (13:57 +1200)
committerThomas Munro <tmunro@postgresql.org>
Mon, 8 Jun 2020 01:58:35 +0000 (13:58 +1200)
The redo routines for XLOG_CHECKPOINT_{ONLINE,SHUTDOWN} must acquire
ControlFileLock before modifying ControlFile->checkPointCopy, or the
checkpointer could write out a control file with a bad checksum.

Likewise, XLogReportParameters() must acquire ControlFileLock before
modifying ControlFile and calling UpdateControlFile().

Back-patch to all supported releases.

Author: Nathan Bossart <bossartn@amazon.com>
Author: Fujii Masao <masao.fujii@oss.nttdata.com>
Reviewed-by: Fujii Masao <masao.fujii@oss.nttdata.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Thomas Munro <thomas.munro@gmail.com>
Discussion: https://postgr.es/m/70BF24D6-DC51-443F-B55A-95735803842A%40amazon.com

src/backend/access/transam/xlog.c

index 8bb8a6a940b999395501434203132ec3fcef50cc..e285ae96c1e96980d517458f81ef0addc9614507 100644 (file)
@@ -9539,6 +9539,8 @@ XLogReportParameters(void)
                        XLogFlush(recptr);
                }
 
+               LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
+
                ControlFile->MaxConnections = MaxConnections;
                ControlFile->max_worker_processes = max_worker_processes;
                ControlFile->max_wal_senders = max_wal_senders;
@@ -9548,6 +9550,8 @@ XLogReportParameters(void)
                ControlFile->wal_log_hints = wal_log_hints;
                ControlFile->track_commit_timestamp = track_commit_timestamp;
                UpdateControlFile();
+
+               LWLockRelease(ControlFileLock);
        }
 }
 
@@ -9772,7 +9776,9 @@ xlog_redo(XLogReaderState *record)
                }
 
                /* ControlFile->checkPointCopy always tracks the latest ckpt XID */
+               LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
                ControlFile->checkPointCopy.nextFullXid = checkPoint.nextFullXid;
+               LWLockRelease(ControlFileLock);
 
                /* Update shared-memory copy of checkpoint XID/epoch */
                SpinLockAcquire(&XLogCtl->info_lck);
@@ -9829,7 +9835,9 @@ xlog_redo(XLogReaderState *record)
                        SetTransactionIdLimit(checkPoint.oldestXid,
                                                                  checkPoint.oldestXidDB);
                /* ControlFile->checkPointCopy always tracks the latest ckpt XID */
+               LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
                ControlFile->checkPointCopy.nextFullXid = checkPoint.nextFullXid;
+               LWLockRelease(ControlFileLock);
 
                /* Update shared-memory copy of checkpoint XID/epoch */
                SpinLockAcquire(&XLogCtl->info_lck);