Add circular WAL decoding buffer, take II.
[pgsql.git] / src / backend / access / transam / xlog.c
blob4ac3871c74f1195a5ec1e22b2e8a6c7971b2ee92
1 /*-------------------------------------------------------------------------
3 * xlog.c
4 * PostgreSQL write-ahead log manager
6 * The Write-Ahead Log (WAL) functionality is split into several source
7 * files, in addition to this one:
9 * xloginsert.c - Functions for constructing WAL records
10 * xlogrecovery.c - WAL recovery and standby code
11 * xlogreader.c - Facility for reading WAL files and parsing WAL records
12 * xlogutils.c - Helper functions for WAL redo routines
14 * This file contains functions for coordinating database startup and
15 * checkpointing, and managing the write-ahead log buffers when the
16 * system is running.
18 * StartupXLOG() is the main entry point of the startup process. It
19 * coordinates database startup, performing WAL recovery, and the
20 * transition from WAL recovery into normal operations.
22 * XLogInsertRecord() inserts a WAL record into the WAL buffers. Most
23 * callers should not call this directly, but use the functions in
24 * xloginsert.c to construct the WAL record. XLogFlush() can be used
25 * to force the WAL to disk.
27 * In addition to those, there are many other functions for interrogating
28 * the current system state, and for starting/stopping backups.
31 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
32 * Portions Copyright (c) 1994, Regents of the University of California
34 * src/backend/access/transam/xlog.c
36 *-------------------------------------------------------------------------
39 #include "postgres.h"
41 #include <ctype.h>
42 #include <math.h>
43 #include <time.h>
44 #include <fcntl.h>
45 #include <sys/stat.h>
46 #include <sys/time.h>
47 #include <unistd.h>
49 #include "access/clog.h"
50 #include "access/commit_ts.h"
51 #include "access/heaptoast.h"
52 #include "access/multixact.h"
53 #include "access/rewriteheap.h"
54 #include "access/subtrans.h"
55 #include "access/timeline.h"
56 #include "access/transam.h"
57 #include "access/twophase.h"
58 #include "access/xact.h"
59 #include "access/xlog_internal.h"
60 #include "access/xlogarchive.h"
61 #include "access/xloginsert.h"
62 #include "access/xlogreader.h"
63 #include "access/xlogrecovery.h"
64 #include "access/xlogutils.h"
65 #include "catalog/catversion.h"
66 #include "catalog/pg_control.h"
67 #include "catalog/pg_database.h"
68 #include "common/controldata_utils.h"
69 #include "common/file_utils.h"
70 #include "executor/instrument.h"
71 #include "miscadmin.h"
72 #include "pg_trace.h"
73 #include "pgstat.h"
74 #include "port/atomics.h"
75 #include "port/pg_iovec.h"
76 #include "postmaster/bgwriter.h"
77 #include "postmaster/startup.h"
78 #include "postmaster/walwriter.h"
79 #include "replication/basebackup.h"
80 #include "replication/logical.h"
81 #include "replication/origin.h"
82 #include "replication/slot.h"
83 #include "replication/snapbuild.h"
84 #include "replication/walreceiver.h"
85 #include "replication/walsender.h"
86 #include "storage/bufmgr.h"
87 #include "storage/fd.h"
88 #include "storage/ipc.h"
89 #include "storage/large_object.h"
90 #include "storage/latch.h"
91 #include "storage/pmsignal.h"
92 #include "storage/predicate.h"
93 #include "storage/proc.h"
94 #include "storage/procarray.h"
95 #include "storage/reinit.h"
96 #include "storage/smgr.h"
97 #include "storage/spin.h"
98 #include "storage/sync.h"
99 #include "utils/guc.h"
100 #include "utils/memutils.h"
101 #include "utils/ps_status.h"
102 #include "utils/relmapper.h"
103 #include "utils/pg_rusage.h"
104 #include "utils/snapmgr.h"
105 #include "utils/timeout.h"
106 #include "utils/timestamp.h"
108 extern uint32 bootstrap_data_checksum_version;
110 /* timeline ID to be used when bootstrapping */
111 #define BootstrapTimeLineID 1
113 /* User-settable parameters */
114 int max_wal_size_mb = 1024; /* 1 GB */
115 int min_wal_size_mb = 80; /* 80 MB */
116 int wal_keep_size_mb = 0;
117 int XLOGbuffers = -1;
118 int XLogArchiveTimeout = 0;
119 int XLogArchiveMode = ARCHIVE_MODE_OFF;
120 char *XLogArchiveCommand = NULL;
121 bool EnableHotStandby = false;
122 bool fullPageWrites = true;
123 bool wal_log_hints = false;
124 int wal_compression = WAL_COMPRESSION_NONE;
125 char *wal_consistency_checking_string = NULL;
126 bool *wal_consistency_checking = NULL;
127 bool wal_init_zero = true;
128 bool wal_recycle = true;
129 bool log_checkpoints = true;
130 int sync_method = DEFAULT_SYNC_METHOD;
131 int wal_level = WAL_LEVEL_MINIMAL;
132 int CommitDelay = 0; /* precommit delay in microseconds */
133 int CommitSiblings = 5; /* # concurrent xacts needed to sleep */
134 int wal_retrieve_retry_interval = 5000;
135 int max_slot_wal_keep_size_mb = -1;
136 bool track_wal_io_timing = false;
138 #ifdef WAL_DEBUG
139 bool XLOG_DEBUG = false;
140 #endif
142 int wal_segment_size = DEFAULT_XLOG_SEG_SIZE;
145 * Number of WAL insertion locks to use. A higher value allows more insertions
146 * to happen concurrently, but adds some CPU overhead to flushing the WAL,
147 * which needs to iterate all the locks.
149 #define NUM_XLOGINSERT_LOCKS 8
152 * Max distance from last checkpoint, before triggering a new xlog-based
153 * checkpoint.
155 int CheckPointSegments;
157 /* Estimated distance between checkpoints, in bytes */
158 static double CheckPointDistanceEstimate = 0;
159 static double PrevCheckPointDistance = 0;
162 * GUC support
164 const struct config_enum_entry sync_method_options[] = {
165 {"fsync", SYNC_METHOD_FSYNC, false},
166 #ifdef HAVE_FSYNC_WRITETHROUGH
167 {"fsync_writethrough", SYNC_METHOD_FSYNC_WRITETHROUGH, false},
168 #endif
169 #ifdef HAVE_FDATASYNC
170 {"fdatasync", SYNC_METHOD_FDATASYNC, false},
171 #endif
172 #ifdef OPEN_SYNC_FLAG
173 {"open_sync", SYNC_METHOD_OPEN, false},
174 #endif
175 #ifdef OPEN_DATASYNC_FLAG
176 {"open_datasync", SYNC_METHOD_OPEN_DSYNC, false},
177 #endif
178 {NULL, 0, false}
183 * Although only "on", "off", and "always" are documented,
184 * we accept all the likely variants of "on" and "off".
186 const struct config_enum_entry archive_mode_options[] = {
187 {"always", ARCHIVE_MODE_ALWAYS, false},
188 {"on", ARCHIVE_MODE_ON, false},
189 {"off", ARCHIVE_MODE_OFF, false},
190 {"true", ARCHIVE_MODE_ON, true},
191 {"false", ARCHIVE_MODE_OFF, true},
192 {"yes", ARCHIVE_MODE_ON, true},
193 {"no", ARCHIVE_MODE_OFF, true},
194 {"1", ARCHIVE_MODE_ON, true},
195 {"0", ARCHIVE_MODE_OFF, true},
196 {NULL, 0, false}
200 * Statistics for current checkpoint are collected in this global struct.
201 * Because only the checkpointer or a stand-alone backend can perform
202 * checkpoints, this will be unused in normal backends.
204 CheckpointStatsData CheckpointStats;
207 * During recovery, lastFullPageWrites keeps track of full_page_writes that
208 * the replayed WAL records indicate. It's initialized with full_page_writes
209 * that the recovery starting checkpoint record indicates, and then updated
210 * each time XLOG_FPW_CHANGE record is replayed.
212 static bool lastFullPageWrites;
215 * Local copy of the state tracked by SharedRecoveryState in shared memory,
216 * It is false if SharedRecoveryState is RECOVERY_STATE_DONE. True actually
217 * means "not known, need to check the shared state".
219 static bool LocalRecoveryInProgress = true;
222 * Local state for XLogInsertAllowed():
223 * 1: unconditionally allowed to insert XLOG
224 * 0: unconditionally not allowed to insert XLOG
225 * -1: must check RecoveryInProgress(); disallow until it is false
226 * Most processes start with -1 and transition to 1 after seeing that recovery
227 * is not in progress. But we can also force the value for special cases.
228 * The coding in XLogInsertAllowed() depends on the first two of these states
229 * being numerically the same as bool true and false.
231 static int LocalXLogInsertAllowed = -1;
234 * ProcLastRecPtr points to the start of the last XLOG record inserted by the
235 * current backend. It is updated for all inserts. XactLastRecEnd points to
236 * end+1 of the last record, and is reset when we end a top-level transaction,
237 * or start a new one; so it can be used to tell if the current transaction has
238 * created any XLOG records.
240 * While in parallel mode, this may not be fully up to date. When committing,
241 * a transaction can assume this covers all xlog records written either by the
242 * user backend or by any parallel worker which was present at any point during
243 * the transaction. But when aborting, or when still in parallel mode, other
244 * parallel backends may have written WAL records at later LSNs than the value
245 * stored here. The parallel leader advances its own copy, when necessary,
246 * in WaitForParallelWorkersToFinish.
248 XLogRecPtr ProcLastRecPtr = InvalidXLogRecPtr;
249 XLogRecPtr XactLastRecEnd = InvalidXLogRecPtr;
250 XLogRecPtr XactLastCommitEnd = InvalidXLogRecPtr;
253 * RedoRecPtr is this backend's local copy of the REDO record pointer
254 * (which is almost but not quite the same as a pointer to the most recent
255 * CHECKPOINT record). We update this from the shared-memory copy,
256 * XLogCtl->Insert.RedoRecPtr, whenever we can safely do so (ie, when we
257 * hold an insertion lock). See XLogInsertRecord for details. We are also
258 * allowed to update from XLogCtl->RedoRecPtr if we hold the info_lck;
259 * see GetRedoRecPtr.
261 * NB: Code that uses this variable must be prepared not only for the
262 * possibility that it may be arbitrarily out of date, but also for the
263 * possibility that it might be set to InvalidXLogRecPtr. We used to
264 * initialize it as a side effect of the first call to RecoveryInProgress(),
265 * which meant that most code that might use it could assume that it had a
266 * real if perhaps stale value. That's no longer the case.
268 static XLogRecPtr RedoRecPtr;
271 * doPageWrites is this backend's local copy of (forcePageWrites ||
272 * fullPageWrites). It is used together with RedoRecPtr to decide whether
273 * a full-page image of a page need to be taken.
275 * NB: Initially this is false, and there's no guarantee that it will be
276 * initialized to any other value before it is first used. Any code that
277 * makes use of it must recheck the value after obtaining a WALInsertLock,
278 * and respond appropriately if it turns out that the previous value wasn't
279 * accurate.
281 static bool doPageWrites;
283 /*----------
284 * Shared-memory data structures for XLOG control
286 * LogwrtRqst indicates a byte position that we need to write and/or fsync
287 * the log up to (all records before that point must be written or fsynced).
288 * LogwrtResult indicates the byte positions we have already written/fsynced.
289 * These structs are identical but are declared separately to indicate their
290 * slightly different functions.
292 * To read XLogCtl->LogwrtResult, you must hold either info_lck or
293 * WALWriteLock. To update it, you need to hold both locks. The point of
294 * this arrangement is that the value can be examined by code that already
295 * holds WALWriteLock without needing to grab info_lck as well. In addition
296 * to the shared variable, each backend has a private copy of LogwrtResult,
297 * which is updated when convenient.
299 * The request bookkeeping is simpler: there is a shared XLogCtl->LogwrtRqst
300 * (protected by info_lck), but we don't need to cache any copies of it.
302 * info_lck is only held long enough to read/update the protected variables,
303 * so it's a plain spinlock. The other locks are held longer (potentially
304 * over I/O operations), so we use LWLocks for them. These locks are:
306 * WALBufMappingLock: must be held to replace a page in the WAL buffer cache.
307 * It is only held while initializing and changing the mapping. If the
308 * contents of the buffer being replaced haven't been written yet, the mapping
309 * lock is released while the write is done, and reacquired afterwards.
311 * WALWriteLock: must be held to write WAL buffers to disk (XLogWrite or
312 * XLogFlush).
314 * ControlFileLock: must be held to read/update control file or create
315 * new log file.
317 *----------
320 typedef struct XLogwrtRqst
322 XLogRecPtr Write; /* last byte + 1 to write out */
323 XLogRecPtr Flush; /* last byte + 1 to flush */
324 } XLogwrtRqst;
326 typedef struct XLogwrtResult
328 XLogRecPtr Write; /* last byte + 1 written out */
329 XLogRecPtr Flush; /* last byte + 1 flushed */
330 } XLogwrtResult;
333 * Inserting to WAL is protected by a small fixed number of WAL insertion
334 * locks. To insert to the WAL, you must hold one of the locks - it doesn't
335 * matter which one. To lock out other concurrent insertions, you must hold
336 * of them. Each WAL insertion lock consists of a lightweight lock, plus an
337 * indicator of how far the insertion has progressed (insertingAt).
339 * The insertingAt values are read when a process wants to flush WAL from
340 * the in-memory buffers to disk, to check that all the insertions to the
341 * region the process is about to write out have finished. You could simply
342 * wait for all currently in-progress insertions to finish, but the
343 * insertingAt indicator allows you to ignore insertions to later in the WAL,
344 * so that you only wait for the insertions that are modifying the buffers
345 * you're about to write out.
347 * This isn't just an optimization. If all the WAL buffers are dirty, an
348 * inserter that's holding a WAL insert lock might need to evict an old WAL
349 * buffer, which requires flushing the WAL. If it's possible for an inserter
350 * to block on another inserter unnecessarily, deadlock can arise when two
351 * inserters holding a WAL insert lock wait for each other to finish their
352 * insertion.
354 * Small WAL records that don't cross a page boundary never update the value,
355 * the WAL record is just copied to the page and the lock is released. But
356 * to avoid the deadlock-scenario explained above, the indicator is always
357 * updated before sleeping while holding an insertion lock.
359 * lastImportantAt contains the LSN of the last important WAL record inserted
360 * using a given lock. This value is used to detect if there has been
361 * important WAL activity since the last time some action, like a checkpoint,
362 * was performed - allowing to not repeat the action if not. The LSN is
363 * updated for all insertions, unless the XLOG_MARK_UNIMPORTANT flag was
364 * set. lastImportantAt is never cleared, only overwritten by the LSN of newer
365 * records. Tracking the WAL activity directly in WALInsertLock has the
366 * advantage of not needing any additional locks to update the value.
368 typedef struct
370 LWLock lock;
371 XLogRecPtr insertingAt;
372 XLogRecPtr lastImportantAt;
373 } WALInsertLock;
376 * All the WAL insertion locks are allocated as an array in shared memory. We
377 * force the array stride to be a power of 2, which saves a few cycles in
378 * indexing, but more importantly also ensures that individual slots don't
379 * cross cache line boundaries. (Of course, we have to also ensure that the
380 * array start address is suitably aligned.)
382 typedef union WALInsertLockPadded
384 WALInsertLock l;
385 char pad[PG_CACHE_LINE_SIZE];
386 } WALInsertLockPadded;
389 * State of an exclusive backup, necessary to control concurrent activities
390 * across sessions when working on exclusive backups.
392 * EXCLUSIVE_BACKUP_NONE means that there is no exclusive backup actually
393 * running, to be more precise pg_start_backup() is not being executed for
394 * an exclusive backup and there is no exclusive backup in progress.
395 * EXCLUSIVE_BACKUP_STARTING means that pg_start_backup() is starting an
396 * exclusive backup.
397 * EXCLUSIVE_BACKUP_IN_PROGRESS means that pg_start_backup() has finished
398 * running and an exclusive backup is in progress. pg_stop_backup() is
399 * needed to finish it.
400 * EXCLUSIVE_BACKUP_STOPPING means that pg_stop_backup() is stopping an
401 * exclusive backup.
403 typedef enum ExclusiveBackupState
405 EXCLUSIVE_BACKUP_NONE = 0,
406 EXCLUSIVE_BACKUP_STARTING,
407 EXCLUSIVE_BACKUP_IN_PROGRESS,
408 EXCLUSIVE_BACKUP_STOPPING
409 } ExclusiveBackupState;
412 * Session status of running backup, used for sanity checks in SQL-callable
413 * functions to start and stop backups.
415 static SessionBackupState sessionBackupState = SESSION_BACKUP_NONE;
418 * Shared state data for WAL insertion.
420 typedef struct XLogCtlInsert
422 slock_t insertpos_lck; /* protects CurrBytePos and PrevBytePos */
425 * CurrBytePos is the end of reserved WAL. The next record will be
426 * inserted at that position. PrevBytePos is the start position of the
427 * previously inserted (or rather, reserved) record - it is copied to the
428 * prev-link of the next record. These are stored as "usable byte
429 * positions" rather than XLogRecPtrs (see XLogBytePosToRecPtr()).
431 uint64 CurrBytePos;
432 uint64 PrevBytePos;
435 * Make sure the above heavily-contended spinlock and byte positions are
436 * on their own cache line. In particular, the RedoRecPtr and full page
437 * write variables below should be on a different cache line. They are
438 * read on every WAL insertion, but updated rarely, and we don't want
439 * those reads to steal the cache line containing Curr/PrevBytePos.
441 char pad[PG_CACHE_LINE_SIZE];
444 * fullPageWrites is the authoritative value used by all backends to
445 * determine whether to write full-page image to WAL. This shared value,
446 * instead of the process-local fullPageWrites, is required because, when
447 * full_page_writes is changed by SIGHUP, we must WAL-log it before it
448 * actually affects WAL-logging by backends. Checkpointer sets at startup
449 * or after SIGHUP.
451 * To read these fields, you must hold an insertion lock. To modify them,
452 * you must hold ALL the locks.
454 XLogRecPtr RedoRecPtr; /* current redo point for insertions */
455 bool forcePageWrites; /* forcing full-page writes for PITR? */
456 bool fullPageWrites;
459 * exclusiveBackupState indicates the state of an exclusive backup (see
460 * comments of ExclusiveBackupState for more details). nonExclusiveBackups
461 * is a counter indicating the number of streaming base backups currently
462 * in progress. forcePageWrites is set to true when either of these is
463 * non-zero. lastBackupStart is the latest checkpoint redo location used
464 * as a starting point for an online backup.
466 ExclusiveBackupState exclusiveBackupState;
467 int nonExclusiveBackups;
468 XLogRecPtr lastBackupStart;
471 * WAL insertion locks.
473 WALInsertLockPadded *WALInsertLocks;
474 } XLogCtlInsert;
477 * Total shared-memory state for XLOG.
479 typedef struct XLogCtlData
481 XLogCtlInsert Insert;
483 /* Protected by info_lck: */
484 XLogwrtRqst LogwrtRqst;
485 XLogRecPtr RedoRecPtr; /* a recent copy of Insert->RedoRecPtr */
486 FullTransactionId ckptFullXid; /* nextXid of latest checkpoint */
487 XLogRecPtr asyncXactLSN; /* LSN of newest async commit/abort */
488 XLogRecPtr replicationSlotMinLSN; /* oldest LSN needed by any slot */
490 XLogSegNo lastRemovedSegNo; /* latest removed/recycled XLOG segment */
492 /* Fake LSN counter, for unlogged relations. Protected by ulsn_lck. */
493 XLogRecPtr unloggedLSN;
494 slock_t ulsn_lck;
496 /* Time and LSN of last xlog segment switch. Protected by WALWriteLock. */
497 pg_time_t lastSegSwitchTime;
498 XLogRecPtr lastSegSwitchLSN;
501 * Protected by info_lck and WALWriteLock (you must hold either lock to
502 * read it, but both to update)
504 XLogwrtResult LogwrtResult;
507 * Latest initialized page in the cache (last byte position + 1).
509 * To change the identity of a buffer (and InitializedUpTo), you need to
510 * hold WALBufMappingLock. To change the identity of a buffer that's
511 * still dirty, the old page needs to be written out first, and for that
512 * you need WALWriteLock, and you need to ensure that there are no
513 * in-progress insertions to the page by calling
514 * WaitXLogInsertionsToFinish().
516 XLogRecPtr InitializedUpTo;
519 * These values do not change after startup, although the pointed-to pages
520 * and xlblocks values certainly do. xlblocks values are protected by
521 * WALBufMappingLock.
523 char *pages; /* buffers for unwritten XLOG pages */
524 XLogRecPtr *xlblocks; /* 1st byte ptr-s + XLOG_BLCKSZ */
525 int XLogCacheBlck; /* highest allocated xlog buffer index */
528 * InsertTimeLineID is the timeline into which new WAL is being inserted
529 * and flushed. It is zero during recovery, and does not change once set.
531 * If we create a new timeline when the system was started up,
532 * PrevTimeLineID is the old timeline's ID that we forked off from.
533 * Otherwise it's equal to InsertTimeLineID.
535 TimeLineID InsertTimeLineID;
536 TimeLineID PrevTimeLineID;
539 * SharedRecoveryState indicates if we're still in crash or archive
540 * recovery. Protected by info_lck.
542 RecoveryState SharedRecoveryState;
545 * InstallXLogFileSegmentActive indicates whether the checkpointer should
546 * arrange for future segments by recycling and/or PreallocXlogFiles().
547 * Protected by ControlFileLock. Only the startup process changes it. If
548 * true, anyone can use InstallXLogFileSegment(). If false, the startup
549 * process owns the exclusive right to install segments, by reading from
550 * the archive and possibly replacing existing files.
552 bool InstallXLogFileSegmentActive;
555 * WalWriterSleeping indicates whether the WAL writer is currently in
556 * low-power mode (and hence should be nudged if an async commit occurs).
557 * Protected by info_lck.
559 bool WalWriterSleeping;
562 * During recovery, we keep a copy of the latest checkpoint record here.
563 * lastCheckPointRecPtr points to start of checkpoint record and
564 * lastCheckPointEndPtr points to end+1 of checkpoint record. Used by the
565 * checkpointer when it wants to create a restartpoint.
567 * Protected by info_lck.
569 XLogRecPtr lastCheckPointRecPtr;
570 XLogRecPtr lastCheckPointEndPtr;
571 CheckPoint lastCheckPoint;
574 * lastFpwDisableRecPtr points to the start of the last replayed
575 * XLOG_FPW_CHANGE record that instructs full_page_writes is disabled.
577 XLogRecPtr lastFpwDisableRecPtr;
579 slock_t info_lck; /* locks shared variables shown above */
580 } XLogCtlData;
582 static XLogCtlData *XLogCtl = NULL;
584 /* a private copy of XLogCtl->Insert.WALInsertLocks, for convenience */
585 static WALInsertLockPadded *WALInsertLocks = NULL;
588 * We maintain an image of pg_control in shared memory.
590 static ControlFileData *ControlFile = NULL;
593 * Calculate the amount of space left on the page after 'endptr'. Beware
594 * multiple evaluation!
596 #define INSERT_FREESPACE(endptr) \
597 (((endptr) % XLOG_BLCKSZ == 0) ? 0 : (XLOG_BLCKSZ - (endptr) % XLOG_BLCKSZ))
599 /* Macro to advance to next buffer index. */
600 #define NextBufIdx(idx) \
601 (((idx) == XLogCtl->XLogCacheBlck) ? 0 : ((idx) + 1))
604 * XLogRecPtrToBufIdx returns the index of the WAL buffer that holds, or
605 * would hold if it was in cache, the page containing 'recptr'.
607 #define XLogRecPtrToBufIdx(recptr) \
608 (((recptr) / XLOG_BLCKSZ) % (XLogCtl->XLogCacheBlck + 1))
611 * These are the number of bytes in a WAL page usable for WAL data.
613 #define UsableBytesInPage (XLOG_BLCKSZ - SizeOfXLogShortPHD)
616 * Convert values of GUCs measured in megabytes to equiv. segment count.
617 * Rounds down.
619 #define ConvertToXSegs(x, segsize) XLogMBVarToSegs((x), (segsize))
621 /* The number of bytes in a WAL segment usable for WAL data. */
622 static int UsableBytesInSegment;
625 * Private, possibly out-of-date copy of shared LogwrtResult.
626 * See discussion above.
628 static XLogwrtResult LogwrtResult = {0, 0};
631 * openLogFile is -1 or a kernel FD for an open log file segment.
632 * openLogSegNo identifies the segment, and openLogTLI the corresponding TLI.
633 * These variables are only used to write the XLOG, and so will normally refer
634 * to the active segment.
636 * Note: call Reserve/ReleaseExternalFD to track consumption of this FD.
638 static int openLogFile = -1;
639 static XLogSegNo openLogSegNo = 0;
640 static TimeLineID openLogTLI = 0;
643 * Local copies of equivalent fields in the control file. When running
644 * crash recovery, LocalMinRecoveryPoint is set to InvalidXLogRecPtr as we
645 * expect to replay all the WAL available, and updateMinRecoveryPoint is
646 * switched to false to prevent any updates while replaying records.
647 * Those values are kept consistent as long as crash recovery runs.
649 static XLogRecPtr LocalMinRecoveryPoint;
650 static TimeLineID LocalMinRecoveryPointTLI;
651 static bool updateMinRecoveryPoint = true;
653 /* For WALInsertLockAcquire/Release functions */
654 static int MyLockNo = 0;
655 static bool holdingAllLocks = false;
657 #ifdef WAL_DEBUG
658 static MemoryContext walDebugCxt = NULL;
659 #endif
661 static void CleanupAfterArchiveRecovery(TimeLineID EndOfLogTLI,
662 XLogRecPtr EndOfLog,
663 TimeLineID newTLI);
664 static void CheckRequiredParameterValues(void);
665 static void XLogReportParameters(void);
666 static int LocalSetXLogInsertAllowed(void);
667 static void CreateEndOfRecoveryRecord(void);
668 static XLogRecPtr CreateOverwriteContrecordRecord(XLogRecPtr aborted_lsn,
669 XLogRecPtr missingContrecPtr,
670 TimeLineID newTLI);
671 static void CheckPointGuts(XLogRecPtr checkPointRedo, int flags);
672 static void KeepLogSeg(XLogRecPtr recptr, XLogSegNo *logSegNo);
673 static XLogRecPtr XLogGetReplicationSlotMinimumLSN(void);
675 static void AdvanceXLInsertBuffer(XLogRecPtr upto, TimeLineID tli,
676 bool opportunistic);
677 static void XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible);
678 static bool InstallXLogFileSegment(XLogSegNo *segno, char *tmppath,
679 bool find_free, XLogSegNo max_segno,
680 TimeLineID tli);
681 static void XLogFileClose(void);
682 static void PreallocXlogFiles(XLogRecPtr endptr, TimeLineID tli);
683 static void RemoveTempXlogFiles(void);
684 static void RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr lastredoptr,
685 XLogRecPtr endptr, TimeLineID insertTLI);
686 static void RemoveXlogFile(const char *segname, XLogSegNo recycleSegNo,
687 XLogSegNo *endlogSegNo, TimeLineID insertTLI);
688 static void UpdateLastRemovedPtr(char *filename);
689 static void ValidateXLOGDirectoryStructure(void);
690 static void CleanupBackupHistory(void);
691 static void UpdateMinRecoveryPoint(XLogRecPtr lsn, bool force);
692 static bool PerformRecoveryXLogAction(void);
693 static void InitControlFile(uint64 sysidentifier);
694 static void WriteControlFile(void);
695 static void ReadControlFile(void);
696 static void UpdateControlFile(void);
697 static char *str_time(pg_time_t tnow);
699 static void pg_start_backup_callback(int code, Datum arg);
700 static void pg_stop_backup_callback(int code, Datum arg);
702 static int get_sync_bit(int method);
704 static void CopyXLogRecordToWAL(int write_len, bool isLogSwitch,
705 XLogRecData *rdata,
706 XLogRecPtr StartPos, XLogRecPtr EndPos,
707 TimeLineID tli);
708 static void ReserveXLogInsertLocation(int size, XLogRecPtr *StartPos,
709 XLogRecPtr *EndPos, XLogRecPtr *PrevPtr);
710 static bool ReserveXLogSwitch(XLogRecPtr *StartPos, XLogRecPtr *EndPos,
711 XLogRecPtr *PrevPtr);
712 static XLogRecPtr WaitXLogInsertionsToFinish(XLogRecPtr upto);
713 static char *GetXLogBuffer(XLogRecPtr ptr, TimeLineID tli);
714 static XLogRecPtr XLogBytePosToRecPtr(uint64 bytepos);
715 static XLogRecPtr XLogBytePosToEndRecPtr(uint64 bytepos);
716 static uint64 XLogRecPtrToBytePos(XLogRecPtr ptr);
718 static void WALInsertLockAcquire(void);
719 static void WALInsertLockAcquireExclusive(void);
720 static void WALInsertLockRelease(void);
721 static void WALInsertLockUpdateInsertingAt(XLogRecPtr insertingAt);
724 * Insert an XLOG record represented by an already-constructed chain of data
725 * chunks. This is a low-level routine; to construct the WAL record header
726 * and data, use the higher-level routines in xloginsert.c.
728 * If 'fpw_lsn' is valid, it is the oldest LSN among the pages that this
729 * WAL record applies to, that were not included in the record as full page
730 * images. If fpw_lsn <= RedoRecPtr, the function does not perform the
731 * insertion and returns InvalidXLogRecPtr. The caller can then recalculate
732 * which pages need a full-page image, and retry. If fpw_lsn is invalid, the
733 * record is always inserted.
735 * 'flags' gives more in-depth control on the record being inserted. See
736 * XLogSetRecordFlags() for details.
738 * 'topxid_included' tells whether the top-transaction id is logged along with
739 * current subtransaction. See XLogRecordAssemble().
741 * The first XLogRecData in the chain must be for the record header, and its
742 * data must be MAXALIGNed. XLogInsertRecord fills in the xl_prev and
743 * xl_crc fields in the header, the rest of the header must already be filled
744 * by the caller.
746 * Returns XLOG pointer to end of record (beginning of next record).
747 * This can be used as LSN for data pages affected by the logged action.
748 * (LSN is the XLOG point up to which the XLOG must be flushed to disk
749 * before the data page can be written out. This implements the basic
750 * WAL rule "write the log before the data".)
752 XLogRecPtr
753 XLogInsertRecord(XLogRecData *rdata,
754 XLogRecPtr fpw_lsn,
755 uint8 flags,
756 int num_fpi,
757 bool topxid_included)
759 XLogCtlInsert *Insert = &XLogCtl->Insert;
760 pg_crc32c rdata_crc;
761 bool inserted;
762 XLogRecord *rechdr = (XLogRecord *) rdata->data;
763 uint8 info = rechdr->xl_info & ~XLR_INFO_MASK;
764 bool isLogSwitch = (rechdr->xl_rmid == RM_XLOG_ID &&
765 info == XLOG_SWITCH);
766 XLogRecPtr StartPos;
767 XLogRecPtr EndPos;
768 bool prevDoPageWrites = doPageWrites;
769 TimeLineID insertTLI;
771 /* we assume that all of the record header is in the first chunk */
772 Assert(rdata->len >= SizeOfXLogRecord);
774 /* cross-check on whether we should be here or not */
775 if (!XLogInsertAllowed())
776 elog(ERROR, "cannot make new WAL entries during recovery");
779 * Given that we're not in recovery, InsertTimeLineID is set and can't
780 * change, so we can read it without a lock.
782 insertTLI = XLogCtl->InsertTimeLineID;
784 /*----------
786 * We have now done all the preparatory work we can without holding a
787 * lock or modifying shared state. From here on, inserting the new WAL
788 * record to the shared WAL buffer cache is a two-step process:
790 * 1. Reserve the right amount of space from the WAL. The current head of
791 * reserved space is kept in Insert->CurrBytePos, and is protected by
792 * insertpos_lck.
794 * 2. Copy the record to the reserved WAL space. This involves finding the
795 * correct WAL buffer containing the reserved space, and copying the
796 * record in place. This can be done concurrently in multiple processes.
798 * To keep track of which insertions are still in-progress, each concurrent
799 * inserter acquires an insertion lock. In addition to just indicating that
800 * an insertion is in progress, the lock tells others how far the inserter
801 * has progressed. There is a small fixed number of insertion locks,
802 * determined by NUM_XLOGINSERT_LOCKS. When an inserter crosses a page
803 * boundary, it updates the value stored in the lock to the how far it has
804 * inserted, to allow the previous buffer to be flushed.
806 * Holding onto an insertion lock also protects RedoRecPtr and
807 * fullPageWrites from changing until the insertion is finished.
809 * Step 2 can usually be done completely in parallel. If the required WAL
810 * page is not initialized yet, you have to grab WALBufMappingLock to
811 * initialize it, but the WAL writer tries to do that ahead of insertions
812 * to avoid that from happening in the critical path.
814 *----------
816 START_CRIT_SECTION();
817 if (isLogSwitch)
818 WALInsertLockAcquireExclusive();
819 else
820 WALInsertLockAcquire();
823 * Check to see if my copy of RedoRecPtr is out of date. If so, may have
824 * to go back and have the caller recompute everything. This can only
825 * happen just after a checkpoint, so it's better to be slow in this case
826 * and fast otherwise.
828 * Also check to see if fullPageWrites or forcePageWrites was just turned
829 * on; if we weren't already doing full-page writes then go back and
830 * recompute.
832 * If we aren't doing full-page writes then RedoRecPtr doesn't actually
833 * affect the contents of the XLOG record, so we'll update our local copy
834 * but not force a recomputation. (If doPageWrites was just turned off,
835 * we could recompute the record without full pages, but we choose not to
836 * bother.)
838 if (RedoRecPtr != Insert->RedoRecPtr)
840 Assert(RedoRecPtr < Insert->RedoRecPtr);
841 RedoRecPtr = Insert->RedoRecPtr;
843 doPageWrites = (Insert->fullPageWrites || Insert->forcePageWrites);
845 if (doPageWrites &&
846 (!prevDoPageWrites ||
847 (fpw_lsn != InvalidXLogRecPtr && fpw_lsn <= RedoRecPtr)))
850 * Oops, some buffer now needs to be backed up that the caller didn't
851 * back up. Start over.
853 WALInsertLockRelease();
854 END_CRIT_SECTION();
855 return InvalidXLogRecPtr;
859 * Reserve space for the record in the WAL. This also sets the xl_prev
860 * pointer.
862 if (isLogSwitch)
863 inserted = ReserveXLogSwitch(&StartPos, &EndPos, &rechdr->xl_prev);
864 else
866 ReserveXLogInsertLocation(rechdr->xl_tot_len, &StartPos, &EndPos,
867 &rechdr->xl_prev);
868 inserted = true;
871 if (inserted)
874 * Now that xl_prev has been filled in, calculate CRC of the record
875 * header.
877 rdata_crc = rechdr->xl_crc;
878 COMP_CRC32C(rdata_crc, rechdr, offsetof(XLogRecord, xl_crc));
879 FIN_CRC32C(rdata_crc);
880 rechdr->xl_crc = rdata_crc;
883 * All the record data, including the header, is now ready to be
884 * inserted. Copy the record in the space reserved.
886 CopyXLogRecordToWAL(rechdr->xl_tot_len, isLogSwitch, rdata,
887 StartPos, EndPos, insertTLI);
890 * Unless record is flagged as not important, update LSN of last
891 * important record in the current slot. When holding all locks, just
892 * update the first one.
894 if ((flags & XLOG_MARK_UNIMPORTANT) == 0)
896 int lockno = holdingAllLocks ? 0 : MyLockNo;
898 WALInsertLocks[lockno].l.lastImportantAt = StartPos;
901 else
904 * This was an xlog-switch record, but the current insert location was
905 * already exactly at the beginning of a segment, so there was no need
906 * to do anything.
911 * Done! Let others know that we're finished.
913 WALInsertLockRelease();
915 END_CRIT_SECTION();
917 MarkCurrentTransactionIdLoggedIfAny();
920 * Mark top transaction id is logged (if needed) so that we should not try
921 * to log it again with the next WAL record in the current subtransaction.
923 if (topxid_included)
924 MarkSubxactTopXidLogged();
927 * Update shared LogwrtRqst.Write, if we crossed page boundary.
929 if (StartPos / XLOG_BLCKSZ != EndPos / XLOG_BLCKSZ)
931 SpinLockAcquire(&XLogCtl->info_lck);
932 /* advance global request to include new block(s) */
933 if (XLogCtl->LogwrtRqst.Write < EndPos)
934 XLogCtl->LogwrtRqst.Write = EndPos;
935 /* update local result copy while I have the chance */
936 LogwrtResult = XLogCtl->LogwrtResult;
937 SpinLockRelease(&XLogCtl->info_lck);
941 * If this was an XLOG_SWITCH record, flush the record and the empty
942 * padding space that fills the rest of the segment, and perform
943 * end-of-segment actions (eg, notifying archiver).
945 if (isLogSwitch)
947 TRACE_POSTGRESQL_WAL_SWITCH();
948 XLogFlush(EndPos);
951 * Even though we reserved the rest of the segment for us, which is
952 * reflected in EndPos, we return a pointer to just the end of the
953 * xlog-switch record.
955 if (inserted)
957 EndPos = StartPos + SizeOfXLogRecord;
958 if (StartPos / XLOG_BLCKSZ != EndPos / XLOG_BLCKSZ)
960 uint64 offset = XLogSegmentOffset(EndPos, wal_segment_size);
962 if (offset == EndPos % XLOG_BLCKSZ)
963 EndPos += SizeOfXLogLongPHD;
964 else
965 EndPos += SizeOfXLogShortPHD;
970 #ifdef WAL_DEBUG
971 if (XLOG_DEBUG)
973 static XLogReaderState *debug_reader = NULL;
974 XLogRecord *record;
975 DecodedXLogRecord *decoded;
976 StringInfoData buf;
977 StringInfoData recordBuf;
978 char *errormsg = NULL;
979 MemoryContext oldCxt;
981 oldCxt = MemoryContextSwitchTo(walDebugCxt);
983 initStringInfo(&buf);
984 appendStringInfo(&buf, "INSERT @ %X/%X: ", LSN_FORMAT_ARGS(EndPos));
987 * We have to piece together the WAL record data from the XLogRecData
988 * entries, so that we can pass it to the rm_desc function as one
989 * contiguous chunk.
991 initStringInfo(&recordBuf);
992 for (; rdata != NULL; rdata = rdata->next)
993 appendBinaryStringInfo(&recordBuf, rdata->data, rdata->len);
995 /* We also need temporary space to decode the record. */
996 record = (XLogRecord *) recordBuf.data;
997 decoded = (DecodedXLogRecord *)
998 palloc(DecodeXLogRecordRequiredSpace(record->xl_tot_len));
1000 if (!debug_reader)
1001 debug_reader = XLogReaderAllocate(wal_segment_size, NULL,
1002 XL_ROUTINE(), NULL);
1004 if (!debug_reader)
1006 appendStringInfoString(&buf, "error decoding record: out of memory while allocating a WAL reading processor");
1008 else if (!DecodeXLogRecord(debug_reader,
1009 decoded,
1010 record,
1011 EndPos,
1012 &errormsg))
1014 appendStringInfo(&buf, "error decoding record: %s",
1015 errormsg ? errormsg : "no error message");
1017 else
1019 appendStringInfoString(&buf, " - ");
1021 debug_reader->record = decoded;
1022 xlog_outdesc(&buf, debug_reader);
1023 debug_reader->record = NULL;
1025 elog(LOG, "%s", buf.data);
1027 pfree(decoded);
1028 pfree(buf.data);
1029 pfree(recordBuf.data);
1030 MemoryContextSwitchTo(oldCxt);
1032 #endif
1035 * Update our global variables
1037 ProcLastRecPtr = StartPos;
1038 XactLastRecEnd = EndPos;
1040 /* Report WAL traffic to the instrumentation. */
1041 if (inserted)
1043 pgWalUsage.wal_bytes += rechdr->xl_tot_len;
1044 pgWalUsage.wal_records++;
1045 pgWalUsage.wal_fpi += num_fpi;
1048 return EndPos;
1052 * Reserves the right amount of space for a record of given size from the WAL.
1053 * *StartPos is set to the beginning of the reserved section, *EndPos to
1054 * its end+1. *PrevPtr is set to the beginning of the previous record; it is
1055 * used to set the xl_prev of this record.
1057 * This is the performance critical part of XLogInsert that must be serialized
1058 * across backends. The rest can happen mostly in parallel. Try to keep this
1059 * section as short as possible, insertpos_lck can be heavily contended on a
1060 * busy system.
1062 * NB: The space calculation here must match the code in CopyXLogRecordToWAL,
1063 * where we actually copy the record to the reserved space.
1065 static void
1066 ReserveXLogInsertLocation(int size, XLogRecPtr *StartPos, XLogRecPtr *EndPos,
1067 XLogRecPtr *PrevPtr)
1069 XLogCtlInsert *Insert = &XLogCtl->Insert;
1070 uint64 startbytepos;
1071 uint64 endbytepos;
1072 uint64 prevbytepos;
1074 size = MAXALIGN(size);
1076 /* All (non xlog-switch) records should contain data. */
1077 Assert(size > SizeOfXLogRecord);
1080 * The duration the spinlock needs to be held is minimized by minimizing
1081 * the calculations that have to be done while holding the lock. The
1082 * current tip of reserved WAL is kept in CurrBytePos, as a byte position
1083 * that only counts "usable" bytes in WAL, that is, it excludes all WAL
1084 * page headers. The mapping between "usable" byte positions and physical
1085 * positions (XLogRecPtrs) can be done outside the locked region, and
1086 * because the usable byte position doesn't include any headers, reserving
1087 * X bytes from WAL is almost as simple as "CurrBytePos += X".
1089 SpinLockAcquire(&Insert->insertpos_lck);
1091 startbytepos = Insert->CurrBytePos;
1092 endbytepos = startbytepos + size;
1093 prevbytepos = Insert->PrevBytePos;
1094 Insert->CurrBytePos = endbytepos;
1095 Insert->PrevBytePos = startbytepos;
1097 SpinLockRelease(&Insert->insertpos_lck);
1099 *StartPos = XLogBytePosToRecPtr(startbytepos);
1100 *EndPos = XLogBytePosToEndRecPtr(endbytepos);
1101 *PrevPtr = XLogBytePosToRecPtr(prevbytepos);
1104 * Check that the conversions between "usable byte positions" and
1105 * XLogRecPtrs work consistently in both directions.
1107 Assert(XLogRecPtrToBytePos(*StartPos) == startbytepos);
1108 Assert(XLogRecPtrToBytePos(*EndPos) == endbytepos);
1109 Assert(XLogRecPtrToBytePos(*PrevPtr) == prevbytepos);
1113 * Like ReserveXLogInsertLocation(), but for an xlog-switch record.
1115 * A log-switch record is handled slightly differently. The rest of the
1116 * segment will be reserved for this insertion, as indicated by the returned
1117 * *EndPos value. However, if we are already at the beginning of the current
1118 * segment, *StartPos and *EndPos are set to the current location without
1119 * reserving any space, and the function returns false.
1121 static bool
1122 ReserveXLogSwitch(XLogRecPtr *StartPos, XLogRecPtr *EndPos, XLogRecPtr *PrevPtr)
1124 XLogCtlInsert *Insert = &XLogCtl->Insert;
1125 uint64 startbytepos;
1126 uint64 endbytepos;
1127 uint64 prevbytepos;
1128 uint32 size = MAXALIGN(SizeOfXLogRecord);
1129 XLogRecPtr ptr;
1130 uint32 segleft;
1133 * These calculations are a bit heavy-weight to be done while holding a
1134 * spinlock, but since we're holding all the WAL insertion locks, there
1135 * are no other inserters competing for it. GetXLogInsertRecPtr() does
1136 * compete for it, but that's not called very frequently.
1138 SpinLockAcquire(&Insert->insertpos_lck);
1140 startbytepos = Insert->CurrBytePos;
1142 ptr = XLogBytePosToEndRecPtr(startbytepos);
1143 if (XLogSegmentOffset(ptr, wal_segment_size) == 0)
1145 SpinLockRelease(&Insert->insertpos_lck);
1146 *EndPos = *StartPos = ptr;
1147 return false;
1150 endbytepos = startbytepos + size;
1151 prevbytepos = Insert->PrevBytePos;
1153 *StartPos = XLogBytePosToRecPtr(startbytepos);
1154 *EndPos = XLogBytePosToEndRecPtr(endbytepos);
1156 segleft = wal_segment_size - XLogSegmentOffset(*EndPos, wal_segment_size);
1157 if (segleft != wal_segment_size)
1159 /* consume the rest of the segment */
1160 *EndPos += segleft;
1161 endbytepos = XLogRecPtrToBytePos(*EndPos);
1163 Insert->CurrBytePos = endbytepos;
1164 Insert->PrevBytePos = startbytepos;
1166 SpinLockRelease(&Insert->insertpos_lck);
1168 *PrevPtr = XLogBytePosToRecPtr(prevbytepos);
1170 Assert(XLogSegmentOffset(*EndPos, wal_segment_size) == 0);
1171 Assert(XLogRecPtrToBytePos(*EndPos) == endbytepos);
1172 Assert(XLogRecPtrToBytePos(*StartPos) == startbytepos);
1173 Assert(XLogRecPtrToBytePos(*PrevPtr) == prevbytepos);
1175 return true;
1179 * Subroutine of XLogInsertRecord. Copies a WAL record to an already-reserved
1180 * area in the WAL.
1182 static void
1183 CopyXLogRecordToWAL(int write_len, bool isLogSwitch, XLogRecData *rdata,
1184 XLogRecPtr StartPos, XLogRecPtr EndPos, TimeLineID tli)
1186 char *currpos;
1187 int freespace;
1188 int written;
1189 XLogRecPtr CurrPos;
1190 XLogPageHeader pagehdr;
1193 * Get a pointer to the right place in the right WAL buffer to start
1194 * inserting to.
1196 CurrPos = StartPos;
1197 currpos = GetXLogBuffer(CurrPos, tli);
1198 freespace = INSERT_FREESPACE(CurrPos);
1201 * there should be enough space for at least the first field (xl_tot_len)
1202 * on this page.
1204 Assert(freespace >= sizeof(uint32));
1206 /* Copy record data */
1207 written = 0;
1208 while (rdata != NULL)
1210 char *rdata_data = rdata->data;
1211 int rdata_len = rdata->len;
1213 while (rdata_len > freespace)
1216 * Write what fits on this page, and continue on the next page.
1218 Assert(CurrPos % XLOG_BLCKSZ >= SizeOfXLogShortPHD || freespace == 0);
1219 memcpy(currpos, rdata_data, freespace);
1220 rdata_data += freespace;
1221 rdata_len -= freespace;
1222 written += freespace;
1223 CurrPos += freespace;
1226 * Get pointer to beginning of next page, and set the xlp_rem_len
1227 * in the page header. Set XLP_FIRST_IS_CONTRECORD.
1229 * It's safe to set the contrecord flag and xlp_rem_len without a
1230 * lock on the page. All the other flags were already set when the
1231 * page was initialized, in AdvanceXLInsertBuffer, and we're the
1232 * only backend that needs to set the contrecord flag.
1234 currpos = GetXLogBuffer(CurrPos, tli);
1235 pagehdr = (XLogPageHeader) currpos;
1236 pagehdr->xlp_rem_len = write_len - written;
1237 pagehdr->xlp_info |= XLP_FIRST_IS_CONTRECORD;
1239 /* skip over the page header */
1240 if (XLogSegmentOffset(CurrPos, wal_segment_size) == 0)
1242 CurrPos += SizeOfXLogLongPHD;
1243 currpos += SizeOfXLogLongPHD;
1245 else
1247 CurrPos += SizeOfXLogShortPHD;
1248 currpos += SizeOfXLogShortPHD;
1250 freespace = INSERT_FREESPACE(CurrPos);
1253 Assert(CurrPos % XLOG_BLCKSZ >= SizeOfXLogShortPHD || rdata_len == 0);
1254 memcpy(currpos, rdata_data, rdata_len);
1255 currpos += rdata_len;
1256 CurrPos += rdata_len;
1257 freespace -= rdata_len;
1258 written += rdata_len;
1260 rdata = rdata->next;
1262 Assert(written == write_len);
1265 * If this was an xlog-switch, it's not enough to write the switch record,
1266 * we also have to consume all the remaining space in the WAL segment. We
1267 * have already reserved that space, but we need to actually fill it.
1269 if (isLogSwitch && XLogSegmentOffset(CurrPos, wal_segment_size) != 0)
1271 /* An xlog-switch record doesn't contain any data besides the header */
1272 Assert(write_len == SizeOfXLogRecord);
1274 /* Assert that we did reserve the right amount of space */
1275 Assert(XLogSegmentOffset(EndPos, wal_segment_size) == 0);
1277 /* Use up all the remaining space on the current page */
1278 CurrPos += freespace;
1281 * Cause all remaining pages in the segment to be flushed, leaving the
1282 * XLog position where it should be, at the start of the next segment.
1283 * We do this one page at a time, to make sure we don't deadlock
1284 * against ourselves if wal_buffers < wal_segment_size.
1286 while (CurrPos < EndPos)
1289 * The minimal action to flush the page would be to call
1290 * WALInsertLockUpdateInsertingAt(CurrPos) followed by
1291 * AdvanceXLInsertBuffer(...). The page would be left initialized
1292 * mostly to zeros, except for the page header (always the short
1293 * variant, as this is never a segment's first page).
1295 * The large vistas of zeros are good for compressibility, but the
1296 * headers interrupting them every XLOG_BLCKSZ (with values that
1297 * differ from page to page) are not. The effect varies with
1298 * compression tool, but bzip2 for instance compresses about an
1299 * order of magnitude worse if those headers are left in place.
1301 * Rather than complicating AdvanceXLInsertBuffer itself (which is
1302 * called in heavily-loaded circumstances as well as this lightly-
1303 * loaded one) with variant behavior, we just use GetXLogBuffer
1304 * (which itself calls the two methods we need) to get the pointer
1305 * and zero most of the page. Then we just zero the page header.
1307 currpos = GetXLogBuffer(CurrPos, tli);
1308 MemSet(currpos, 0, SizeOfXLogShortPHD);
1310 CurrPos += XLOG_BLCKSZ;
1313 else
1315 /* Align the end position, so that the next record starts aligned */
1316 CurrPos = MAXALIGN64(CurrPos);
1319 if (CurrPos != EndPos)
1320 elog(PANIC, "space reserved for WAL record does not match what was written");
1324 * Acquire a WAL insertion lock, for inserting to WAL.
1326 static void
1327 WALInsertLockAcquire(void)
1329 bool immed;
1332 * It doesn't matter which of the WAL insertion locks we acquire, so try
1333 * the one we used last time. If the system isn't particularly busy, it's
1334 * a good bet that it's still available, and it's good to have some
1335 * affinity to a particular lock so that you don't unnecessarily bounce
1336 * cache lines between processes when there's no contention.
1338 * If this is the first time through in this backend, pick a lock
1339 * (semi-)randomly. This allows the locks to be used evenly if you have a
1340 * lot of very short connections.
1342 static int lockToTry = -1;
1344 if (lockToTry == -1)
1345 lockToTry = MyProc->pgprocno % NUM_XLOGINSERT_LOCKS;
1346 MyLockNo = lockToTry;
1349 * The insertingAt value is initially set to 0, as we don't know our
1350 * insert location yet.
1352 immed = LWLockAcquire(&WALInsertLocks[MyLockNo].l.lock, LW_EXCLUSIVE);
1353 if (!immed)
1356 * If we couldn't get the lock immediately, try another lock next
1357 * time. On a system with more insertion locks than concurrent
1358 * inserters, this causes all the inserters to eventually migrate to a
1359 * lock that no-one else is using. On a system with more inserters
1360 * than locks, it still helps to distribute the inserters evenly
1361 * across the locks.
1363 lockToTry = (lockToTry + 1) % NUM_XLOGINSERT_LOCKS;
1368 * Acquire all WAL insertion locks, to prevent other backends from inserting
1369 * to WAL.
1371 static void
1372 WALInsertLockAcquireExclusive(void)
1374 int i;
1377 * When holding all the locks, all but the last lock's insertingAt
1378 * indicator is set to 0xFFFFFFFFFFFFFFFF, which is higher than any real
1379 * XLogRecPtr value, to make sure that no-one blocks waiting on those.
1381 for (i = 0; i < NUM_XLOGINSERT_LOCKS - 1; i++)
1383 LWLockAcquire(&WALInsertLocks[i].l.lock, LW_EXCLUSIVE);
1384 LWLockUpdateVar(&WALInsertLocks[i].l.lock,
1385 &WALInsertLocks[i].l.insertingAt,
1386 PG_UINT64_MAX);
1388 /* Variable value reset to 0 at release */
1389 LWLockAcquire(&WALInsertLocks[i].l.lock, LW_EXCLUSIVE);
1391 holdingAllLocks = true;
1395 * Release our insertion lock (or locks, if we're holding them all).
1397 * NB: Reset all variables to 0, so they cause LWLockWaitForVar to block the
1398 * next time the lock is acquired.
1400 static void
1401 WALInsertLockRelease(void)
1403 if (holdingAllLocks)
1405 int i;
1407 for (i = 0; i < NUM_XLOGINSERT_LOCKS; i++)
1408 LWLockReleaseClearVar(&WALInsertLocks[i].l.lock,
1409 &WALInsertLocks[i].l.insertingAt,
1412 holdingAllLocks = false;
1414 else
1416 LWLockReleaseClearVar(&WALInsertLocks[MyLockNo].l.lock,
1417 &WALInsertLocks[MyLockNo].l.insertingAt,
1423 * Update our insertingAt value, to let others know that we've finished
1424 * inserting up to that point.
1426 static void
1427 WALInsertLockUpdateInsertingAt(XLogRecPtr insertingAt)
1429 if (holdingAllLocks)
1432 * We use the last lock to mark our actual position, see comments in
1433 * WALInsertLockAcquireExclusive.
1435 LWLockUpdateVar(&WALInsertLocks[NUM_XLOGINSERT_LOCKS - 1].l.lock,
1436 &WALInsertLocks[NUM_XLOGINSERT_LOCKS - 1].l.insertingAt,
1437 insertingAt);
1439 else
1440 LWLockUpdateVar(&WALInsertLocks[MyLockNo].l.lock,
1441 &WALInsertLocks[MyLockNo].l.insertingAt,
1442 insertingAt);
1446 * Wait for any WAL insertions < upto to finish.
1448 * Returns the location of the oldest insertion that is still in-progress.
1449 * Any WAL prior to that point has been fully copied into WAL buffers, and
1450 * can be flushed out to disk. Because this waits for any insertions older
1451 * than 'upto' to finish, the return value is always >= 'upto'.
1453 * Note: When you are about to write out WAL, you must call this function
1454 * *before* acquiring WALWriteLock, to avoid deadlocks. This function might
1455 * need to wait for an insertion to finish (or at least advance to next
1456 * uninitialized page), and the inserter might need to evict an old WAL buffer
1457 * to make room for a new one, which in turn requires WALWriteLock.
1459 static XLogRecPtr
1460 WaitXLogInsertionsToFinish(XLogRecPtr upto)
1462 uint64 bytepos;
1463 XLogRecPtr reservedUpto;
1464 XLogRecPtr finishedUpto;
1465 XLogCtlInsert *Insert = &XLogCtl->Insert;
1466 int i;
1468 if (MyProc == NULL)
1469 elog(PANIC, "cannot wait without a PGPROC structure");
1471 /* Read the current insert position */
1472 SpinLockAcquire(&Insert->insertpos_lck);
1473 bytepos = Insert->CurrBytePos;
1474 SpinLockRelease(&Insert->insertpos_lck);
1475 reservedUpto = XLogBytePosToEndRecPtr(bytepos);
1478 * No-one should request to flush a piece of WAL that hasn't even been
1479 * reserved yet. However, it can happen if there is a block with a bogus
1480 * LSN on disk, for example. XLogFlush checks for that situation and
1481 * complains, but only after the flush. Here we just assume that to mean
1482 * that all WAL that has been reserved needs to be finished. In this
1483 * corner-case, the return value can be smaller than 'upto' argument.
1485 if (upto > reservedUpto)
1487 ereport(LOG,
1488 (errmsg("request to flush past end of generated WAL; request %X/%X, current position %X/%X",
1489 LSN_FORMAT_ARGS(upto), LSN_FORMAT_ARGS(reservedUpto))));
1490 upto = reservedUpto;
1494 * Loop through all the locks, sleeping on any in-progress insert older
1495 * than 'upto'.
1497 * finishedUpto is our return value, indicating the point upto which all
1498 * the WAL insertions have been finished. Initialize it to the head of
1499 * reserved WAL, and as we iterate through the insertion locks, back it
1500 * out for any insertion that's still in progress.
1502 finishedUpto = reservedUpto;
1503 for (i = 0; i < NUM_XLOGINSERT_LOCKS; i++)
1505 XLogRecPtr insertingat = InvalidXLogRecPtr;
1510 * See if this insertion is in progress. LWLockWaitForVar will
1511 * wait for the lock to be released, or for the 'value' to be set
1512 * by a LWLockUpdateVar call. When a lock is initially acquired,
1513 * its value is 0 (InvalidXLogRecPtr), which means that we don't
1514 * know where it's inserting yet. We will have to wait for it. If
1515 * it's a small insertion, the record will most likely fit on the
1516 * same page and the inserter will release the lock without ever
1517 * calling LWLockUpdateVar. But if it has to sleep, it will
1518 * advertise the insertion point with LWLockUpdateVar before
1519 * sleeping.
1521 if (LWLockWaitForVar(&WALInsertLocks[i].l.lock,
1522 &WALInsertLocks[i].l.insertingAt,
1523 insertingat, &insertingat))
1525 /* the lock was free, so no insertion in progress */
1526 insertingat = InvalidXLogRecPtr;
1527 break;
1531 * This insertion is still in progress. Have to wait, unless the
1532 * inserter has proceeded past 'upto'.
1534 } while (insertingat < upto);
1536 if (insertingat != InvalidXLogRecPtr && insertingat < finishedUpto)
1537 finishedUpto = insertingat;
1539 return finishedUpto;
1543 * Get a pointer to the right location in the WAL buffer containing the
1544 * given XLogRecPtr.
1546 * If the page is not initialized yet, it is initialized. That might require
1547 * evicting an old dirty buffer from the buffer cache, which means I/O.
1549 * The caller must ensure that the page containing the requested location
1550 * isn't evicted yet, and won't be evicted. The way to ensure that is to
1551 * hold onto a WAL insertion lock with the insertingAt position set to
1552 * something <= ptr. GetXLogBuffer() will update insertingAt if it needs
1553 * to evict an old page from the buffer. (This means that once you call
1554 * GetXLogBuffer() with a given 'ptr', you must not access anything before
1555 * that point anymore, and must not call GetXLogBuffer() with an older 'ptr'
1556 * later, because older buffers might be recycled already)
1558 static char *
1559 GetXLogBuffer(XLogRecPtr ptr, TimeLineID tli)
1561 int idx;
1562 XLogRecPtr endptr;
1563 static uint64 cachedPage = 0;
1564 static char *cachedPos = NULL;
1565 XLogRecPtr expectedEndPtr;
1568 * Fast path for the common case that we need to access again the same
1569 * page as last time.
1571 if (ptr / XLOG_BLCKSZ == cachedPage)
1573 Assert(((XLogPageHeader) cachedPos)->xlp_magic == XLOG_PAGE_MAGIC);
1574 Assert(((XLogPageHeader) cachedPos)->xlp_pageaddr == ptr - (ptr % XLOG_BLCKSZ));
1575 return cachedPos + ptr % XLOG_BLCKSZ;
1579 * The XLog buffer cache is organized so that a page is always loaded to a
1580 * particular buffer. That way we can easily calculate the buffer a given
1581 * page must be loaded into, from the XLogRecPtr alone.
1583 idx = XLogRecPtrToBufIdx(ptr);
1586 * See what page is loaded in the buffer at the moment. It could be the
1587 * page we're looking for, or something older. It can't be anything newer
1588 * - that would imply the page we're looking for has already been written
1589 * out to disk and evicted, and the caller is responsible for making sure
1590 * that doesn't happen.
1592 * However, we don't hold a lock while we read the value. If someone has
1593 * just initialized the page, it's possible that we get a "torn read" of
1594 * the XLogRecPtr if 64-bit fetches are not atomic on this platform. In
1595 * that case we will see a bogus value. That's ok, we'll grab the mapping
1596 * lock (in AdvanceXLInsertBuffer) and retry if we see anything else than
1597 * the page we're looking for. But it means that when we do this unlocked
1598 * read, we might see a value that appears to be ahead of the page we're
1599 * looking for. Don't PANIC on that, until we've verified the value while
1600 * holding the lock.
1602 expectedEndPtr = ptr;
1603 expectedEndPtr += XLOG_BLCKSZ - ptr % XLOG_BLCKSZ;
1605 endptr = XLogCtl->xlblocks[idx];
1606 if (expectedEndPtr != endptr)
1608 XLogRecPtr initializedUpto;
1611 * Before calling AdvanceXLInsertBuffer(), which can block, let others
1612 * know how far we're finished with inserting the record.
1614 * NB: If 'ptr' points to just after the page header, advertise a
1615 * position at the beginning of the page rather than 'ptr' itself. If
1616 * there are no other insertions running, someone might try to flush
1617 * up to our advertised location. If we advertised a position after
1618 * the page header, someone might try to flush the page header, even
1619 * though page might actually not be initialized yet. As the first
1620 * inserter on the page, we are effectively responsible for making
1621 * sure that it's initialized, before we let insertingAt to move past
1622 * the page header.
1624 if (ptr % XLOG_BLCKSZ == SizeOfXLogShortPHD &&
1625 XLogSegmentOffset(ptr, wal_segment_size) > XLOG_BLCKSZ)
1626 initializedUpto = ptr - SizeOfXLogShortPHD;
1627 else if (ptr % XLOG_BLCKSZ == SizeOfXLogLongPHD &&
1628 XLogSegmentOffset(ptr, wal_segment_size) < XLOG_BLCKSZ)
1629 initializedUpto = ptr - SizeOfXLogLongPHD;
1630 else
1631 initializedUpto = ptr;
1633 WALInsertLockUpdateInsertingAt(initializedUpto);
1635 AdvanceXLInsertBuffer(ptr, tli, false);
1636 endptr = XLogCtl->xlblocks[idx];
1638 if (expectedEndPtr != endptr)
1639 elog(PANIC, "could not find WAL buffer for %X/%X",
1640 LSN_FORMAT_ARGS(ptr));
1642 else
1645 * Make sure the initialization of the page is visible to us, and
1646 * won't arrive later to overwrite the WAL data we write on the page.
1648 pg_memory_barrier();
1652 * Found the buffer holding this page. Return a pointer to the right
1653 * offset within the page.
1655 cachedPage = ptr / XLOG_BLCKSZ;
1656 cachedPos = XLogCtl->pages + idx * (Size) XLOG_BLCKSZ;
1658 Assert(((XLogPageHeader) cachedPos)->xlp_magic == XLOG_PAGE_MAGIC);
1659 Assert(((XLogPageHeader) cachedPos)->xlp_pageaddr == ptr - (ptr % XLOG_BLCKSZ));
1661 return cachedPos + ptr % XLOG_BLCKSZ;
1665 * Converts a "usable byte position" to XLogRecPtr. A usable byte position
1666 * is the position starting from the beginning of WAL, excluding all WAL
1667 * page headers.
1669 static XLogRecPtr
1670 XLogBytePosToRecPtr(uint64 bytepos)
1672 uint64 fullsegs;
1673 uint64 fullpages;
1674 uint64 bytesleft;
1675 uint32 seg_offset;
1676 XLogRecPtr result;
1678 fullsegs = bytepos / UsableBytesInSegment;
1679 bytesleft = bytepos % UsableBytesInSegment;
1681 if (bytesleft < XLOG_BLCKSZ - SizeOfXLogLongPHD)
1683 /* fits on first page of segment */
1684 seg_offset = bytesleft + SizeOfXLogLongPHD;
1686 else
1688 /* account for the first page on segment with long header */
1689 seg_offset = XLOG_BLCKSZ;
1690 bytesleft -= XLOG_BLCKSZ - SizeOfXLogLongPHD;
1692 fullpages = bytesleft / UsableBytesInPage;
1693 bytesleft = bytesleft % UsableBytesInPage;
1695 seg_offset += fullpages * XLOG_BLCKSZ + bytesleft + SizeOfXLogShortPHD;
1698 XLogSegNoOffsetToRecPtr(fullsegs, seg_offset, wal_segment_size, result);
1700 return result;
1704 * Like XLogBytePosToRecPtr, but if the position is at a page boundary,
1705 * returns a pointer to the beginning of the page (ie. before page header),
1706 * not to where the first xlog record on that page would go to. This is used
1707 * when converting a pointer to the end of a record.
1709 static XLogRecPtr
1710 XLogBytePosToEndRecPtr(uint64 bytepos)
1712 uint64 fullsegs;
1713 uint64 fullpages;
1714 uint64 bytesleft;
1715 uint32 seg_offset;
1716 XLogRecPtr result;
1718 fullsegs = bytepos / UsableBytesInSegment;
1719 bytesleft = bytepos % UsableBytesInSegment;
1721 if (bytesleft < XLOG_BLCKSZ - SizeOfXLogLongPHD)
1723 /* fits on first page of segment */
1724 if (bytesleft == 0)
1725 seg_offset = 0;
1726 else
1727 seg_offset = bytesleft + SizeOfXLogLongPHD;
1729 else
1731 /* account for the first page on segment with long header */
1732 seg_offset = XLOG_BLCKSZ;
1733 bytesleft -= XLOG_BLCKSZ - SizeOfXLogLongPHD;
1735 fullpages = bytesleft / UsableBytesInPage;
1736 bytesleft = bytesleft % UsableBytesInPage;
1738 if (bytesleft == 0)
1739 seg_offset += fullpages * XLOG_BLCKSZ + bytesleft;
1740 else
1741 seg_offset += fullpages * XLOG_BLCKSZ + bytesleft + SizeOfXLogShortPHD;
1744 XLogSegNoOffsetToRecPtr(fullsegs, seg_offset, wal_segment_size, result);
1746 return result;
1750 * Convert an XLogRecPtr to a "usable byte position".
1752 static uint64
1753 XLogRecPtrToBytePos(XLogRecPtr ptr)
1755 uint64 fullsegs;
1756 uint32 fullpages;
1757 uint32 offset;
1758 uint64 result;
1760 XLByteToSeg(ptr, fullsegs, wal_segment_size);
1762 fullpages = (XLogSegmentOffset(ptr, wal_segment_size)) / XLOG_BLCKSZ;
1763 offset = ptr % XLOG_BLCKSZ;
1765 if (fullpages == 0)
1767 result = fullsegs * UsableBytesInSegment;
1768 if (offset > 0)
1770 Assert(offset >= SizeOfXLogLongPHD);
1771 result += offset - SizeOfXLogLongPHD;
1774 else
1776 result = fullsegs * UsableBytesInSegment +
1777 (XLOG_BLCKSZ - SizeOfXLogLongPHD) + /* account for first page */
1778 (fullpages - 1) * UsableBytesInPage; /* full pages */
1779 if (offset > 0)
1781 Assert(offset >= SizeOfXLogShortPHD);
1782 result += offset - SizeOfXLogShortPHD;
1786 return result;
1790 * Initialize XLOG buffers, writing out old buffers if they still contain
1791 * unwritten data, upto the page containing 'upto'. Or if 'opportunistic' is
1792 * true, initialize as many pages as we can without having to write out
1793 * unwritten data. Any new pages are initialized to zeros, with pages headers
1794 * initialized properly.
1796 static void
1797 AdvanceXLInsertBuffer(XLogRecPtr upto, TimeLineID tli, bool opportunistic)
1799 XLogCtlInsert *Insert = &XLogCtl->Insert;
1800 int nextidx;
1801 XLogRecPtr OldPageRqstPtr;
1802 XLogwrtRqst WriteRqst;
1803 XLogRecPtr NewPageEndPtr = InvalidXLogRecPtr;
1804 XLogRecPtr NewPageBeginPtr;
1805 XLogPageHeader NewPage;
1806 int npages = 0;
1808 LWLockAcquire(WALBufMappingLock, LW_EXCLUSIVE);
1811 * Now that we have the lock, check if someone initialized the page
1812 * already.
1814 while (upto >= XLogCtl->InitializedUpTo || opportunistic)
1816 nextidx = XLogRecPtrToBufIdx(XLogCtl->InitializedUpTo);
1819 * Get ending-offset of the buffer page we need to replace (this may
1820 * be zero if the buffer hasn't been used yet). Fall through if it's
1821 * already written out.
1823 OldPageRqstPtr = XLogCtl->xlblocks[nextidx];
1824 if (LogwrtResult.Write < OldPageRqstPtr)
1827 * Nope, got work to do. If we just want to pre-initialize as much
1828 * as we can without flushing, give up now.
1830 if (opportunistic)
1831 break;
1833 /* Before waiting, get info_lck and update LogwrtResult */
1834 SpinLockAcquire(&XLogCtl->info_lck);
1835 if (XLogCtl->LogwrtRqst.Write < OldPageRqstPtr)
1836 XLogCtl->LogwrtRqst.Write = OldPageRqstPtr;
1837 LogwrtResult = XLogCtl->LogwrtResult;
1838 SpinLockRelease(&XLogCtl->info_lck);
1841 * Now that we have an up-to-date LogwrtResult value, see if we
1842 * still need to write it or if someone else already did.
1844 if (LogwrtResult.Write < OldPageRqstPtr)
1847 * Must acquire write lock. Release WALBufMappingLock first,
1848 * to make sure that all insertions that we need to wait for
1849 * can finish (up to this same position). Otherwise we risk
1850 * deadlock.
1852 LWLockRelease(WALBufMappingLock);
1854 WaitXLogInsertionsToFinish(OldPageRqstPtr);
1856 LWLockAcquire(WALWriteLock, LW_EXCLUSIVE);
1858 LogwrtResult = XLogCtl->LogwrtResult;
1859 if (LogwrtResult.Write >= OldPageRqstPtr)
1861 /* OK, someone wrote it already */
1862 LWLockRelease(WALWriteLock);
1864 else
1866 /* Have to write it ourselves */
1867 TRACE_POSTGRESQL_WAL_BUFFER_WRITE_DIRTY_START();
1868 WriteRqst.Write = OldPageRqstPtr;
1869 WriteRqst.Flush = 0;
1870 XLogWrite(WriteRqst, tli, false);
1871 LWLockRelease(WALWriteLock);
1872 WalStats.m_wal_buffers_full++;
1873 TRACE_POSTGRESQL_WAL_BUFFER_WRITE_DIRTY_DONE();
1875 /* Re-acquire WALBufMappingLock and retry */
1876 LWLockAcquire(WALBufMappingLock, LW_EXCLUSIVE);
1877 continue;
1882 * Now the next buffer slot is free and we can set it up to be the
1883 * next output page.
1885 NewPageBeginPtr = XLogCtl->InitializedUpTo;
1886 NewPageEndPtr = NewPageBeginPtr + XLOG_BLCKSZ;
1888 Assert(XLogRecPtrToBufIdx(NewPageBeginPtr) == nextidx);
1890 NewPage = (XLogPageHeader) (XLogCtl->pages + nextidx * (Size) XLOG_BLCKSZ);
1893 * Be sure to re-zero the buffer so that bytes beyond what we've
1894 * written will look like zeroes and not valid XLOG records...
1896 MemSet((char *) NewPage, 0, XLOG_BLCKSZ);
1899 * Fill the new page's header
1901 NewPage->xlp_magic = XLOG_PAGE_MAGIC;
1903 /* NewPage->xlp_info = 0; */ /* done by memset */
1904 NewPage->xlp_tli = tli;
1905 NewPage->xlp_pageaddr = NewPageBeginPtr;
1907 /* NewPage->xlp_rem_len = 0; */ /* done by memset */
1910 * If online backup is not in progress, mark the header to indicate
1911 * that WAL records beginning in this page have removable backup
1912 * blocks. This allows the WAL archiver to know whether it is safe to
1913 * compress archived WAL data by transforming full-block records into
1914 * the non-full-block format. It is sufficient to record this at the
1915 * page level because we force a page switch (in fact a segment
1916 * switch) when starting a backup, so the flag will be off before any
1917 * records can be written during the backup. At the end of a backup,
1918 * the last page will be marked as all unsafe when perhaps only part
1919 * is unsafe, but at worst the archiver would miss the opportunity to
1920 * compress a few records.
1922 if (!Insert->forcePageWrites)
1923 NewPage->xlp_info |= XLP_BKP_REMOVABLE;
1926 * If first page of an XLOG segment file, make it a long header.
1928 if ((XLogSegmentOffset(NewPage->xlp_pageaddr, wal_segment_size)) == 0)
1930 XLogLongPageHeader NewLongPage = (XLogLongPageHeader) NewPage;
1932 NewLongPage->xlp_sysid = ControlFile->system_identifier;
1933 NewLongPage->xlp_seg_size = wal_segment_size;
1934 NewLongPage->xlp_xlog_blcksz = XLOG_BLCKSZ;
1935 NewPage->xlp_info |= XLP_LONG_HEADER;
1939 * Make sure the initialization of the page becomes visible to others
1940 * before the xlblocks update. GetXLogBuffer() reads xlblocks without
1941 * holding a lock.
1943 pg_write_barrier();
1945 *((volatile XLogRecPtr *) &XLogCtl->xlblocks[nextidx]) = NewPageEndPtr;
1947 XLogCtl->InitializedUpTo = NewPageEndPtr;
1949 npages++;
1951 LWLockRelease(WALBufMappingLock);
1953 #ifdef WAL_DEBUG
1954 if (XLOG_DEBUG && npages > 0)
1956 elog(DEBUG1, "initialized %d pages, up to %X/%X",
1957 npages, LSN_FORMAT_ARGS(NewPageEndPtr));
1959 #endif
1963 * Calculate CheckPointSegments based on max_wal_size_mb and
1964 * checkpoint_completion_target.
1966 static void
1967 CalculateCheckpointSegments(void)
1969 double target;
1971 /*-------
1972 * Calculate the distance at which to trigger a checkpoint, to avoid
1973 * exceeding max_wal_size_mb. This is based on two assumptions:
1975 * a) we keep WAL for only one checkpoint cycle (prior to PG11 we kept
1976 * WAL for two checkpoint cycles to allow us to recover from the
1977 * secondary checkpoint if the first checkpoint failed, though we
1978 * only did this on the primary anyway, not on standby. Keeping just
1979 * one checkpoint simplifies processing and reduces disk space in
1980 * many smaller databases.)
1981 * b) during checkpoint, we consume checkpoint_completion_target *
1982 * number of segments consumed between checkpoints.
1983 *-------
1985 target = (double) ConvertToXSegs(max_wal_size_mb, wal_segment_size) /
1986 (1.0 + CheckPointCompletionTarget);
1988 /* round down */
1989 CheckPointSegments = (int) target;
1991 if (CheckPointSegments < 1)
1992 CheckPointSegments = 1;
1995 void
1996 assign_max_wal_size(int newval, void *extra)
1998 max_wal_size_mb = newval;
1999 CalculateCheckpointSegments();
2002 void
2003 assign_checkpoint_completion_target(double newval, void *extra)
2005 CheckPointCompletionTarget = newval;
2006 CalculateCheckpointSegments();
2010 * At a checkpoint, how many WAL segments to recycle as preallocated future
2011 * XLOG segments? Returns the highest segment that should be preallocated.
2013 static XLogSegNo
2014 XLOGfileslop(XLogRecPtr lastredoptr)
2016 XLogSegNo minSegNo;
2017 XLogSegNo maxSegNo;
2018 double distance;
2019 XLogSegNo recycleSegNo;
2022 * Calculate the segment numbers that min_wal_size_mb and max_wal_size_mb
2023 * correspond to. Always recycle enough segments to meet the minimum, and
2024 * remove enough segments to stay below the maximum.
2026 minSegNo = lastredoptr / wal_segment_size +
2027 ConvertToXSegs(min_wal_size_mb, wal_segment_size) - 1;
2028 maxSegNo = lastredoptr / wal_segment_size +
2029 ConvertToXSegs(max_wal_size_mb, wal_segment_size) - 1;
2032 * Between those limits, recycle enough segments to get us through to the
2033 * estimated end of next checkpoint.
2035 * To estimate where the next checkpoint will finish, assume that the
2036 * system runs steadily consuming CheckPointDistanceEstimate bytes between
2037 * every checkpoint.
2039 distance = (1.0 + CheckPointCompletionTarget) * CheckPointDistanceEstimate;
2040 /* add 10% for good measure. */
2041 distance *= 1.10;
2043 recycleSegNo = (XLogSegNo) ceil(((double) lastredoptr + distance) /
2044 wal_segment_size);
2046 if (recycleSegNo < minSegNo)
2047 recycleSegNo = minSegNo;
2048 if (recycleSegNo > maxSegNo)
2049 recycleSegNo = maxSegNo;
2051 return recycleSegNo;
2055 * Check whether we've consumed enough xlog space that a checkpoint is needed.
2057 * new_segno indicates a log file that has just been filled up (or read
2058 * during recovery). We measure the distance from RedoRecPtr to new_segno
2059 * and see if that exceeds CheckPointSegments.
2061 * Note: it is caller's responsibility that RedoRecPtr is up-to-date.
2063 bool
2064 XLogCheckpointNeeded(XLogSegNo new_segno)
2066 XLogSegNo old_segno;
2068 XLByteToSeg(RedoRecPtr, old_segno, wal_segment_size);
2070 if (new_segno >= old_segno + (uint64) (CheckPointSegments - 1))
2071 return true;
2072 return false;
2076 * Write and/or fsync the log at least as far as WriteRqst indicates.
2078 * If flexible == true, we don't have to write as far as WriteRqst, but
2079 * may stop at any convenient boundary (such as a cache or logfile boundary).
2080 * This option allows us to avoid uselessly issuing multiple writes when a
2081 * single one would do.
2083 * Must be called with WALWriteLock held. WaitXLogInsertionsToFinish(WriteRqst)
2084 * must be called before grabbing the lock, to make sure the data is ready to
2085 * write.
2087 static void
2088 XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
2090 bool ispartialpage;
2091 bool last_iteration;
2092 bool finishing_seg;
2093 int curridx;
2094 int npages;
2095 int startidx;
2096 uint32 startoffset;
2098 /* We should always be inside a critical section here */
2099 Assert(CritSectionCount > 0);
2102 * Update local LogwrtResult (caller probably did this already, but...)
2104 LogwrtResult = XLogCtl->LogwrtResult;
2107 * Since successive pages in the xlog cache are consecutively allocated,
2108 * we can usually gather multiple pages together and issue just one
2109 * write() call. npages is the number of pages we have determined can be
2110 * written together; startidx is the cache block index of the first one,
2111 * and startoffset is the file offset at which it should go. The latter
2112 * two variables are only valid when npages > 0, but we must initialize
2113 * all of them to keep the compiler quiet.
2115 npages = 0;
2116 startidx = 0;
2117 startoffset = 0;
2120 * Within the loop, curridx is the cache block index of the page to
2121 * consider writing. Begin at the buffer containing the next unwritten
2122 * page, or last partially written page.
2124 curridx = XLogRecPtrToBufIdx(LogwrtResult.Write);
2126 while (LogwrtResult.Write < WriteRqst.Write)
2129 * Make sure we're not ahead of the insert process. This could happen
2130 * if we're passed a bogus WriteRqst.Write that is past the end of the
2131 * last page that's been initialized by AdvanceXLInsertBuffer.
2133 XLogRecPtr EndPtr = XLogCtl->xlblocks[curridx];
2135 if (LogwrtResult.Write >= EndPtr)
2136 elog(PANIC, "xlog write request %X/%X is past end of log %X/%X",
2137 LSN_FORMAT_ARGS(LogwrtResult.Write),
2138 LSN_FORMAT_ARGS(EndPtr));
2140 /* Advance LogwrtResult.Write to end of current buffer page */
2141 LogwrtResult.Write = EndPtr;
2142 ispartialpage = WriteRqst.Write < LogwrtResult.Write;
2144 if (!XLByteInPrevSeg(LogwrtResult.Write, openLogSegNo,
2145 wal_segment_size))
2148 * Switch to new logfile segment. We cannot have any pending
2149 * pages here (since we dump what we have at segment end).
2151 Assert(npages == 0);
2152 if (openLogFile >= 0)
2153 XLogFileClose();
2154 XLByteToPrevSeg(LogwrtResult.Write, openLogSegNo,
2155 wal_segment_size);
2156 openLogTLI = tli;
2158 /* create/use new log file */
2159 openLogFile = XLogFileInit(openLogSegNo, tli);
2160 ReserveExternalFD();
2163 /* Make sure we have the current logfile open */
2164 if (openLogFile < 0)
2166 XLByteToPrevSeg(LogwrtResult.Write, openLogSegNo,
2167 wal_segment_size);
2168 openLogTLI = tli;
2169 openLogFile = XLogFileOpen(openLogSegNo, tli);
2170 ReserveExternalFD();
2173 /* Add current page to the set of pending pages-to-dump */
2174 if (npages == 0)
2176 /* first of group */
2177 startidx = curridx;
2178 startoffset = XLogSegmentOffset(LogwrtResult.Write - XLOG_BLCKSZ,
2179 wal_segment_size);
2181 npages++;
2184 * Dump the set if this will be the last loop iteration, or if we are
2185 * at the last page of the cache area (since the next page won't be
2186 * contiguous in memory), or if we are at the end of the logfile
2187 * segment.
2189 last_iteration = WriteRqst.Write <= LogwrtResult.Write;
2191 finishing_seg = !ispartialpage &&
2192 (startoffset + npages * XLOG_BLCKSZ) >= wal_segment_size;
2194 if (last_iteration ||
2195 curridx == XLogCtl->XLogCacheBlck ||
2196 finishing_seg)
2198 char *from;
2199 Size nbytes;
2200 Size nleft;
2201 int written;
2202 instr_time start;
2204 /* OK to write the page(s) */
2205 from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
2206 nbytes = npages * (Size) XLOG_BLCKSZ;
2207 nleft = nbytes;
2210 errno = 0;
2212 /* Measure I/O timing to write WAL data */
2213 if (track_wal_io_timing)
2214 INSTR_TIME_SET_CURRENT(start);
2216 pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
2217 written = pg_pwrite(openLogFile, from, nleft, startoffset);
2218 pgstat_report_wait_end();
2221 * Increment the I/O timing and the number of times WAL data
2222 * were written out to disk.
2224 if (track_wal_io_timing)
2226 instr_time duration;
2228 INSTR_TIME_SET_CURRENT(duration);
2229 INSTR_TIME_SUBTRACT(duration, start);
2230 WalStats.m_wal_write_time += INSTR_TIME_GET_MICROSEC(duration);
2233 WalStats.m_wal_write++;
2235 if (written <= 0)
2237 char xlogfname[MAXFNAMELEN];
2238 int save_errno;
2240 if (errno == EINTR)
2241 continue;
2243 save_errno = errno;
2244 XLogFileName(xlogfname, tli, openLogSegNo,
2245 wal_segment_size);
2246 errno = save_errno;
2247 ereport(PANIC,
2248 (errcode_for_file_access(),
2249 errmsg("could not write to log file %s "
2250 "at offset %u, length %zu: %m",
2251 xlogfname, startoffset, nleft)));
2253 nleft -= written;
2254 from += written;
2255 startoffset += written;
2256 } while (nleft > 0);
2258 npages = 0;
2261 * If we just wrote the whole last page of a logfile segment,
2262 * fsync the segment immediately. This avoids having to go back
2263 * and re-open prior segments when an fsync request comes along
2264 * later. Doing it here ensures that one and only one backend will
2265 * perform this fsync.
2267 * This is also the right place to notify the Archiver that the
2268 * segment is ready to copy to archival storage, and to update the
2269 * timer for archive_timeout, and to signal for a checkpoint if
2270 * too many logfile segments have been used since the last
2271 * checkpoint.
2273 if (finishing_seg)
2275 issue_xlog_fsync(openLogFile, openLogSegNo, tli);
2277 /* signal that we need to wakeup walsenders later */
2278 WalSndWakeupRequest();
2280 LogwrtResult.Flush = LogwrtResult.Write; /* end of page */
2282 if (XLogArchivingActive())
2283 XLogArchiveNotifySeg(openLogSegNo, tli);
2285 XLogCtl->lastSegSwitchTime = (pg_time_t) time(NULL);
2286 XLogCtl->lastSegSwitchLSN = LogwrtResult.Flush;
2289 * Request a checkpoint if we've consumed too much xlog since
2290 * the last one. For speed, we first check using the local
2291 * copy of RedoRecPtr, which might be out of date; if it looks
2292 * like a checkpoint is needed, forcibly update RedoRecPtr and
2293 * recheck.
2295 if (IsUnderPostmaster && XLogCheckpointNeeded(openLogSegNo))
2297 (void) GetRedoRecPtr();
2298 if (XLogCheckpointNeeded(openLogSegNo))
2299 RequestCheckpoint(CHECKPOINT_CAUSE_XLOG);
2304 if (ispartialpage)
2306 /* Only asked to write a partial page */
2307 LogwrtResult.Write = WriteRqst.Write;
2308 break;
2310 curridx = NextBufIdx(curridx);
2312 /* If flexible, break out of loop as soon as we wrote something */
2313 if (flexible && npages == 0)
2314 break;
2317 Assert(npages == 0);
2320 * If asked to flush, do so
2322 if (LogwrtResult.Flush < WriteRqst.Flush &&
2323 LogwrtResult.Flush < LogwrtResult.Write)
2326 * Could get here without iterating above loop, in which case we might
2327 * have no open file or the wrong one. However, we do not need to
2328 * fsync more than one file.
2330 if (sync_method != SYNC_METHOD_OPEN &&
2331 sync_method != SYNC_METHOD_OPEN_DSYNC)
2333 if (openLogFile >= 0 &&
2334 !XLByteInPrevSeg(LogwrtResult.Write, openLogSegNo,
2335 wal_segment_size))
2336 XLogFileClose();
2337 if (openLogFile < 0)
2339 XLByteToPrevSeg(LogwrtResult.Write, openLogSegNo,
2340 wal_segment_size);
2341 openLogTLI = tli;
2342 openLogFile = XLogFileOpen(openLogSegNo, tli);
2343 ReserveExternalFD();
2346 issue_xlog_fsync(openLogFile, openLogSegNo, tli);
2349 /* signal that we need to wakeup walsenders later */
2350 WalSndWakeupRequest();
2352 LogwrtResult.Flush = LogwrtResult.Write;
2356 * Update shared-memory status
2358 * We make sure that the shared 'request' values do not fall behind the
2359 * 'result' values. This is not absolutely essential, but it saves some
2360 * code in a couple of places.
2363 SpinLockAcquire(&XLogCtl->info_lck);
2364 XLogCtl->LogwrtResult = LogwrtResult;
2365 if (XLogCtl->LogwrtRqst.Write < LogwrtResult.Write)
2366 XLogCtl->LogwrtRqst.Write = LogwrtResult.Write;
2367 if (XLogCtl->LogwrtRqst.Flush < LogwrtResult.Flush)
2368 XLogCtl->LogwrtRqst.Flush = LogwrtResult.Flush;
2369 SpinLockRelease(&XLogCtl->info_lck);
2374 * Record the LSN for an asynchronous transaction commit/abort
2375 * and nudge the WALWriter if there is work for it to do.
2376 * (This should not be called for synchronous commits.)
2378 void
2379 XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
2381 XLogRecPtr WriteRqstPtr = asyncXactLSN;
2382 bool sleeping;
2384 SpinLockAcquire(&XLogCtl->info_lck);
2385 LogwrtResult = XLogCtl->LogwrtResult;
2386 sleeping = XLogCtl->WalWriterSleeping;
2387 if (XLogCtl->asyncXactLSN < asyncXactLSN)
2388 XLogCtl->asyncXactLSN = asyncXactLSN;
2389 SpinLockRelease(&XLogCtl->info_lck);
2392 * If the WALWriter is sleeping, we should kick it to make it come out of
2393 * low-power mode. Otherwise, determine whether there's a full page of
2394 * WAL available to write.
2396 if (!sleeping)
2398 /* back off to last completed page boundary */
2399 WriteRqstPtr -= WriteRqstPtr % XLOG_BLCKSZ;
2401 /* if we have already flushed that far, we're done */
2402 if (WriteRqstPtr <= LogwrtResult.Flush)
2403 return;
2407 * Nudge the WALWriter: it has a full page of WAL to write, or we want it
2408 * to come out of low-power mode so that this async commit will reach disk
2409 * within the expected amount of time.
2411 if (ProcGlobal->walwriterLatch)
2412 SetLatch(ProcGlobal->walwriterLatch);
2416 * Record the LSN up to which we can remove WAL because it's not required by
2417 * any replication slot.
2419 void
2420 XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn)
2422 SpinLockAcquire(&XLogCtl->info_lck);
2423 XLogCtl->replicationSlotMinLSN = lsn;
2424 SpinLockRelease(&XLogCtl->info_lck);
2429 * Return the oldest LSN we must retain to satisfy the needs of some
2430 * replication slot.
2432 static XLogRecPtr
2433 XLogGetReplicationSlotMinimumLSN(void)
2435 XLogRecPtr retval;
2437 SpinLockAcquire(&XLogCtl->info_lck);
2438 retval = XLogCtl->replicationSlotMinLSN;
2439 SpinLockRelease(&XLogCtl->info_lck);
2441 return retval;
2445 * Advance minRecoveryPoint in control file.
2447 * If we crash during recovery, we must reach this point again before the
2448 * database is consistent.
2450 * If 'force' is true, 'lsn' argument is ignored. Otherwise, minRecoveryPoint
2451 * is only updated if it's not already greater than or equal to 'lsn'.
2453 static void
2454 UpdateMinRecoveryPoint(XLogRecPtr lsn, bool force)
2456 /* Quick check using our local copy of the variable */
2457 if (!updateMinRecoveryPoint || (!force && lsn <= LocalMinRecoveryPoint))
2458 return;
2461 * An invalid minRecoveryPoint means that we need to recover all the WAL,
2462 * i.e., we're doing crash recovery. We never modify the control file's
2463 * value in that case, so we can short-circuit future checks here too. The
2464 * local values of minRecoveryPoint and minRecoveryPointTLI should not be
2465 * updated until crash recovery finishes. We only do this for the startup
2466 * process as it should not update its own reference of minRecoveryPoint
2467 * until it has finished crash recovery to make sure that all WAL
2468 * available is replayed in this case. This also saves from extra locks
2469 * taken on the control file from the startup process.
2471 if (XLogRecPtrIsInvalid(LocalMinRecoveryPoint) && InRecovery)
2473 updateMinRecoveryPoint = false;
2474 return;
2477 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
2479 /* update local copy */
2480 LocalMinRecoveryPoint = ControlFile->minRecoveryPoint;
2481 LocalMinRecoveryPointTLI = ControlFile->minRecoveryPointTLI;
2483 if (XLogRecPtrIsInvalid(LocalMinRecoveryPoint))
2484 updateMinRecoveryPoint = false;
2485 else if (force || LocalMinRecoveryPoint < lsn)
2487 XLogRecPtr newMinRecoveryPoint;
2488 TimeLineID newMinRecoveryPointTLI;
2491 * To avoid having to update the control file too often, we update it
2492 * all the way to the last record being replayed, even though 'lsn'
2493 * would suffice for correctness. This also allows the 'force' case
2494 * to not need a valid 'lsn' value.
2496 * Another important reason for doing it this way is that the passed
2497 * 'lsn' value could be bogus, i.e., past the end of available WAL, if
2498 * the caller got it from a corrupted heap page. Accepting such a
2499 * value as the min recovery point would prevent us from coming up at
2500 * all. Instead, we just log a warning and continue with recovery.
2501 * (See also the comments about corrupt LSNs in XLogFlush.)
2503 newMinRecoveryPoint = GetCurrentReplayRecPtr(&newMinRecoveryPointTLI);
2504 if (!force && newMinRecoveryPoint < lsn)
2505 elog(WARNING,
2506 "xlog min recovery request %X/%X is past current point %X/%X",
2507 LSN_FORMAT_ARGS(lsn), LSN_FORMAT_ARGS(newMinRecoveryPoint));
2509 /* update control file */
2510 if (ControlFile->minRecoveryPoint < newMinRecoveryPoint)
2512 ControlFile->minRecoveryPoint = newMinRecoveryPoint;
2513 ControlFile->minRecoveryPointTLI = newMinRecoveryPointTLI;
2514 UpdateControlFile();
2515 LocalMinRecoveryPoint = newMinRecoveryPoint;
2516 LocalMinRecoveryPointTLI = newMinRecoveryPointTLI;
2518 ereport(DEBUG2,
2519 (errmsg_internal("updated min recovery point to %X/%X on timeline %u",
2520 LSN_FORMAT_ARGS(newMinRecoveryPoint),
2521 newMinRecoveryPointTLI)));
2524 LWLockRelease(ControlFileLock);
2528 * Ensure that all XLOG data through the given position is flushed to disk.
2530 * NOTE: this differs from XLogWrite mainly in that the WALWriteLock is not
2531 * already held, and we try to avoid acquiring it if possible.
2533 void
2534 XLogFlush(XLogRecPtr record)
2536 XLogRecPtr WriteRqstPtr;
2537 XLogwrtRqst WriteRqst;
2538 TimeLineID insertTLI = XLogCtl->InsertTimeLineID;
2541 * During REDO, we are reading not writing WAL. Therefore, instead of
2542 * trying to flush the WAL, we should update minRecoveryPoint instead. We
2543 * test XLogInsertAllowed(), not InRecovery, because we need checkpointer
2544 * to act this way too, and because when it tries to write the
2545 * end-of-recovery checkpoint, it should indeed flush.
2547 if (!XLogInsertAllowed())
2549 UpdateMinRecoveryPoint(record, false);
2550 return;
2553 /* Quick exit if already known flushed */
2554 if (record <= LogwrtResult.Flush)
2555 return;
2557 #ifdef WAL_DEBUG
2558 if (XLOG_DEBUG)
2559 elog(LOG, "xlog flush request %X/%X; write %X/%X; flush %X/%X",
2560 LSN_FORMAT_ARGS(record),
2561 LSN_FORMAT_ARGS(LogwrtResult.Write),
2562 LSN_FORMAT_ARGS(LogwrtResult.Flush));
2563 #endif
2565 START_CRIT_SECTION();
2568 * Since fsync is usually a horribly expensive operation, we try to
2569 * piggyback as much data as we can on each fsync: if we see any more data
2570 * entered into the xlog buffer, we'll write and fsync that too, so that
2571 * the final value of LogwrtResult.Flush is as large as possible. This
2572 * gives us some chance of avoiding another fsync immediately after.
2575 /* initialize to given target; may increase below */
2576 WriteRqstPtr = record;
2579 * Now wait until we get the write lock, or someone else does the flush
2580 * for us.
2582 for (;;)
2584 XLogRecPtr insertpos;
2586 /* read LogwrtResult and update local state */
2587 SpinLockAcquire(&XLogCtl->info_lck);
2588 if (WriteRqstPtr < XLogCtl->LogwrtRqst.Write)
2589 WriteRqstPtr = XLogCtl->LogwrtRqst.Write;
2590 LogwrtResult = XLogCtl->LogwrtResult;
2591 SpinLockRelease(&XLogCtl->info_lck);
2593 /* done already? */
2594 if (record <= LogwrtResult.Flush)
2595 break;
2598 * Before actually performing the write, wait for all in-flight
2599 * insertions to the pages we're about to write to finish.
2601 insertpos = WaitXLogInsertionsToFinish(WriteRqstPtr);
2604 * Try to get the write lock. If we can't get it immediately, wait
2605 * until it's released, and recheck if we still need to do the flush
2606 * or if the backend that held the lock did it for us already. This
2607 * helps to maintain a good rate of group committing when the system
2608 * is bottlenecked by the speed of fsyncing.
2610 if (!LWLockAcquireOrWait(WALWriteLock, LW_EXCLUSIVE))
2613 * The lock is now free, but we didn't acquire it yet. Before we
2614 * do, loop back to check if someone else flushed the record for
2615 * us already.
2617 continue;
2620 /* Got the lock; recheck whether request is satisfied */
2621 LogwrtResult = XLogCtl->LogwrtResult;
2622 if (record <= LogwrtResult.Flush)
2624 LWLockRelease(WALWriteLock);
2625 break;
2629 * Sleep before flush! By adding a delay here, we may give further
2630 * backends the opportunity to join the backlog of group commit
2631 * followers; this can significantly improve transaction throughput,
2632 * at the risk of increasing transaction latency.
2634 * We do not sleep if enableFsync is not turned on, nor if there are
2635 * fewer than CommitSiblings other backends with active transactions.
2637 if (CommitDelay > 0 && enableFsync &&
2638 MinimumActiveBackends(CommitSiblings))
2640 pg_usleep(CommitDelay);
2643 * Re-check how far we can now flush the WAL. It's generally not
2644 * safe to call WaitXLogInsertionsToFinish while holding
2645 * WALWriteLock, because an in-progress insertion might need to
2646 * also grab WALWriteLock to make progress. But we know that all
2647 * the insertions up to insertpos have already finished, because
2648 * that's what the earlier WaitXLogInsertionsToFinish() returned.
2649 * We're only calling it again to allow insertpos to be moved
2650 * further forward, not to actually wait for anyone.
2652 insertpos = WaitXLogInsertionsToFinish(insertpos);
2655 /* try to write/flush later additions to XLOG as well */
2656 WriteRqst.Write = insertpos;
2657 WriteRqst.Flush = insertpos;
2659 XLogWrite(WriteRqst, insertTLI, false);
2661 LWLockRelease(WALWriteLock);
2662 /* done */
2663 break;
2666 END_CRIT_SECTION();
2668 /* wake up walsenders now that we've released heavily contended locks */
2669 WalSndWakeupProcessRequests();
2672 * If we still haven't flushed to the request point then we have a
2673 * problem; most likely, the requested flush point is past end of XLOG.
2674 * This has been seen to occur when a disk page has a corrupted LSN.
2676 * Formerly we treated this as a PANIC condition, but that hurts the
2677 * system's robustness rather than helping it: we do not want to take down
2678 * the whole system due to corruption on one data page. In particular, if
2679 * the bad page is encountered again during recovery then we would be
2680 * unable to restart the database at all! (This scenario actually
2681 * happened in the field several times with 7.1 releases.) As of 8.4, bad
2682 * LSNs encountered during recovery are UpdateMinRecoveryPoint's problem;
2683 * the only time we can reach here during recovery is while flushing the
2684 * end-of-recovery checkpoint record, and we don't expect that to have a
2685 * bad LSN.
2687 * Note that for calls from xact.c, the ERROR will be promoted to PANIC
2688 * since xact.c calls this routine inside a critical section. However,
2689 * calls from bufmgr.c are not within critical sections and so we will not
2690 * force a restart for a bad LSN on a data page.
2692 if (LogwrtResult.Flush < record)
2693 elog(ERROR,
2694 "xlog flush request %X/%X is not satisfied --- flushed only to %X/%X",
2695 LSN_FORMAT_ARGS(record),
2696 LSN_FORMAT_ARGS(LogwrtResult.Flush));
2700 * Write & flush xlog, but without specifying exactly where to.
2702 * We normally write only completed blocks; but if there is nothing to do on
2703 * that basis, we check for unwritten async commits in the current incomplete
2704 * block, and write through the latest one of those. Thus, if async commits
2705 * are not being used, we will write complete blocks only.
2707 * If, based on the above, there's anything to write we do so immediately. But
2708 * to avoid calling fsync, fdatasync et. al. at a rate that'd impact
2709 * concurrent IO, we only flush WAL every wal_writer_delay ms, or if there's
2710 * more than wal_writer_flush_after unflushed blocks.
2712 * We can guarantee that async commits reach disk after at most three
2713 * wal_writer_delay cycles. (When flushing complete blocks, we allow XLogWrite
2714 * to write "flexibly", meaning it can stop at the end of the buffer ring;
2715 * this makes a difference only with very high load or long wal_writer_delay,
2716 * but imposes one extra cycle for the worst case for async commits.)
2718 * This routine is invoked periodically by the background walwriter process.
2720 * Returns true if there was any work to do, even if we skipped flushing due
2721 * to wal_writer_delay/wal_writer_flush_after.
2723 bool
2724 XLogBackgroundFlush(void)
2726 XLogwrtRqst WriteRqst;
2727 bool flexible = true;
2728 static TimestampTz lastflush;
2729 TimestampTz now;
2730 int flushbytes;
2731 TimeLineID insertTLI;
2733 /* XLOG doesn't need flushing during recovery */
2734 if (RecoveryInProgress())
2735 return false;
2738 * Since we're not in recovery, InsertTimeLineID is set and can't change,
2739 * so we can read it without a lock.
2741 insertTLI = XLogCtl->InsertTimeLineID;
2743 /* read LogwrtResult and update local state */
2744 SpinLockAcquire(&XLogCtl->info_lck);
2745 LogwrtResult = XLogCtl->LogwrtResult;
2746 WriteRqst = XLogCtl->LogwrtRqst;
2747 SpinLockRelease(&XLogCtl->info_lck);
2749 /* back off to last completed page boundary */
2750 WriteRqst.Write -= WriteRqst.Write % XLOG_BLCKSZ;
2752 /* if we have already flushed that far, consider async commit records */
2753 if (WriteRqst.Write <= LogwrtResult.Flush)
2755 SpinLockAcquire(&XLogCtl->info_lck);
2756 WriteRqst.Write = XLogCtl->asyncXactLSN;
2757 SpinLockRelease(&XLogCtl->info_lck);
2758 flexible = false; /* ensure it all gets written */
2762 * If already known flushed, we're done. Just need to check if we are
2763 * holding an open file handle to a logfile that's no longer in use,
2764 * preventing the file from being deleted.
2766 if (WriteRqst.Write <= LogwrtResult.Flush)
2768 if (openLogFile >= 0)
2770 if (!XLByteInPrevSeg(LogwrtResult.Write, openLogSegNo,
2771 wal_segment_size))
2773 XLogFileClose();
2776 return false;
2780 * Determine how far to flush WAL, based on the wal_writer_delay and
2781 * wal_writer_flush_after GUCs.
2783 now = GetCurrentTimestamp();
2784 flushbytes =
2785 WriteRqst.Write / XLOG_BLCKSZ - LogwrtResult.Flush / XLOG_BLCKSZ;
2787 if (WalWriterFlushAfter == 0 || lastflush == 0)
2789 /* first call, or block based limits disabled */
2790 WriteRqst.Flush = WriteRqst.Write;
2791 lastflush = now;
2793 else if (TimestampDifferenceExceeds(lastflush, now, WalWriterDelay))
2796 * Flush the writes at least every WalWriterDelay ms. This is
2797 * important to bound the amount of time it takes for an asynchronous
2798 * commit to hit disk.
2800 WriteRqst.Flush = WriteRqst.Write;
2801 lastflush = now;
2803 else if (flushbytes >= WalWriterFlushAfter)
2805 /* exceeded wal_writer_flush_after blocks, flush */
2806 WriteRqst.Flush = WriteRqst.Write;
2807 lastflush = now;
2809 else
2811 /* no flushing, this time round */
2812 WriteRqst.Flush = 0;
2815 #ifdef WAL_DEBUG
2816 if (XLOG_DEBUG)
2817 elog(LOG, "xlog bg flush request write %X/%X; flush: %X/%X, current is write %X/%X; flush %X/%X",
2818 LSN_FORMAT_ARGS(WriteRqst.Write),
2819 LSN_FORMAT_ARGS(WriteRqst.Flush),
2820 LSN_FORMAT_ARGS(LogwrtResult.Write),
2821 LSN_FORMAT_ARGS(LogwrtResult.Flush));
2822 #endif
2824 START_CRIT_SECTION();
2826 /* now wait for any in-progress insertions to finish and get write lock */
2827 WaitXLogInsertionsToFinish(WriteRqst.Write);
2828 LWLockAcquire(WALWriteLock, LW_EXCLUSIVE);
2829 LogwrtResult = XLogCtl->LogwrtResult;
2830 if (WriteRqst.Write > LogwrtResult.Write ||
2831 WriteRqst.Flush > LogwrtResult.Flush)
2833 XLogWrite(WriteRqst, insertTLI, flexible);
2835 LWLockRelease(WALWriteLock);
2837 END_CRIT_SECTION();
2839 /* wake up walsenders now that we've released heavily contended locks */
2840 WalSndWakeupProcessRequests();
2843 * Great, done. To take some work off the critical path, try to initialize
2844 * as many of the no-longer-needed WAL buffers for future use as we can.
2846 AdvanceXLInsertBuffer(InvalidXLogRecPtr, insertTLI, true);
2849 * If we determined that we need to write data, but somebody else
2850 * wrote/flushed already, it should be considered as being active, to
2851 * avoid hibernating too early.
2853 return true;
2857 * Test whether XLOG data has been flushed up to (at least) the given position.
2859 * Returns true if a flush is still needed. (It may be that someone else
2860 * is already in process of flushing that far, however.)
2862 bool
2863 XLogNeedsFlush(XLogRecPtr record)
2866 * During recovery, we don't flush WAL but update minRecoveryPoint
2867 * instead. So "needs flush" is taken to mean whether minRecoveryPoint
2868 * would need to be updated.
2870 if (RecoveryInProgress())
2873 * An invalid minRecoveryPoint means that we need to recover all the
2874 * WAL, i.e., we're doing crash recovery. We never modify the control
2875 * file's value in that case, so we can short-circuit future checks
2876 * here too. This triggers a quick exit path for the startup process,
2877 * which cannot update its local copy of minRecoveryPoint as long as
2878 * it has not replayed all WAL available when doing crash recovery.
2880 if (XLogRecPtrIsInvalid(LocalMinRecoveryPoint) && InRecovery)
2881 updateMinRecoveryPoint = false;
2883 /* Quick exit if already known to be updated or cannot be updated */
2884 if (record <= LocalMinRecoveryPoint || !updateMinRecoveryPoint)
2885 return false;
2888 * Update local copy of minRecoveryPoint. But if the lock is busy,
2889 * just return a conservative guess.
2891 if (!LWLockConditionalAcquire(ControlFileLock, LW_SHARED))
2892 return true;
2893 LocalMinRecoveryPoint = ControlFile->minRecoveryPoint;
2894 LocalMinRecoveryPointTLI = ControlFile->minRecoveryPointTLI;
2895 LWLockRelease(ControlFileLock);
2898 * Check minRecoveryPoint for any other process than the startup
2899 * process doing crash recovery, which should not update the control
2900 * file value if crash recovery is still running.
2902 if (XLogRecPtrIsInvalid(LocalMinRecoveryPoint))
2903 updateMinRecoveryPoint = false;
2905 /* check again */
2906 if (record <= LocalMinRecoveryPoint || !updateMinRecoveryPoint)
2907 return false;
2908 else
2909 return true;
2912 /* Quick exit if already known flushed */
2913 if (record <= LogwrtResult.Flush)
2914 return false;
2916 /* read LogwrtResult and update local state */
2917 SpinLockAcquire(&XLogCtl->info_lck);
2918 LogwrtResult = XLogCtl->LogwrtResult;
2919 SpinLockRelease(&XLogCtl->info_lck);
2921 /* check again */
2922 if (record <= LogwrtResult.Flush)
2923 return false;
2925 return true;
2929 * Try to make a given XLOG file segment exist.
2931 * logsegno: identify segment.
2933 * *added: on return, true if this call raised the number of extant segments.
2935 * path: on return, this char[MAXPGPATH] has the path to the logsegno file.
2937 * Returns -1 or FD of opened file. A -1 here is not an error; a caller
2938 * wanting an open segment should attempt to open "path", which usually will
2939 * succeed. (This is weird, but it's efficient for the callers.)
2941 static int
2942 XLogFileInitInternal(XLogSegNo logsegno, TimeLineID logtli,
2943 bool *added, char *path)
2945 char tmppath[MAXPGPATH];
2946 PGAlignedXLogBlock zbuffer;
2947 XLogSegNo installed_segno;
2948 XLogSegNo max_segno;
2949 int fd;
2950 int save_errno;
2952 Assert(logtli != 0);
2954 XLogFilePath(path, logtli, logsegno, wal_segment_size);
2957 * Try to use existent file (checkpoint maker may have created it already)
2959 *added = false;
2960 fd = BasicOpenFile(path, O_RDWR | PG_BINARY | get_sync_bit(sync_method));
2961 if (fd < 0)
2963 if (errno != ENOENT)
2964 ereport(ERROR,
2965 (errcode_for_file_access(),
2966 errmsg("could not open file \"%s\": %m", path)));
2968 else
2969 return fd;
2972 * Initialize an empty (all zeroes) segment. NOTE: it is possible that
2973 * another process is doing the same thing. If so, we will end up
2974 * pre-creating an extra log segment. That seems OK, and better than
2975 * holding the lock throughout this lengthy process.
2977 elog(DEBUG2, "creating and filling new WAL file");
2979 snprintf(tmppath, MAXPGPATH, XLOGDIR "/xlogtemp.%d", (int) getpid());
2981 unlink(tmppath);
2983 /* do not use get_sync_bit() here --- want to fsync only at end of fill */
2984 fd = BasicOpenFile(tmppath, O_RDWR | O_CREAT | O_EXCL | PG_BINARY);
2985 if (fd < 0)
2986 ereport(ERROR,
2987 (errcode_for_file_access(),
2988 errmsg("could not create file \"%s\": %m", tmppath)));
2990 memset(zbuffer.data, 0, XLOG_BLCKSZ);
2992 pgstat_report_wait_start(WAIT_EVENT_WAL_INIT_WRITE);
2993 save_errno = 0;
2994 if (wal_init_zero)
2996 struct iovec iov[PG_IOV_MAX];
2997 int blocks;
3000 * Zero-fill the file. With this setting, we do this the hard way to
3001 * ensure that all the file space has really been allocated. On
3002 * platforms that allow "holes" in files, just seeking to the end
3003 * doesn't allocate intermediate space. This way, we know that we
3004 * have all the space and (after the fsync below) that all the
3005 * indirect blocks are down on disk. Therefore, fdatasync(2) or
3006 * O_DSYNC will be sufficient to sync future writes to the log file.
3009 /* Prepare to write out a lot of copies of our zero buffer at once. */
3010 for (int i = 0; i < lengthof(iov); ++i)
3012 iov[i].iov_base = zbuffer.data;
3013 iov[i].iov_len = XLOG_BLCKSZ;
3016 /* Loop, writing as many blocks as we can for each system call. */
3017 blocks = wal_segment_size / XLOG_BLCKSZ;
3018 for (int i = 0; i < blocks;)
3020 int iovcnt = Min(blocks - i, lengthof(iov));
3021 off_t offset = i * XLOG_BLCKSZ;
3023 if (pg_pwritev_with_retry(fd, iov, iovcnt, offset) < 0)
3025 save_errno = errno;
3026 break;
3029 i += iovcnt;
3032 else
3035 * Otherwise, seeking to the end and writing a solitary byte is
3036 * enough.
3038 errno = 0;
3039 if (pg_pwrite(fd, zbuffer.data, 1, wal_segment_size - 1) != 1)
3041 /* if write didn't set errno, assume no disk space */
3042 save_errno = errno ? errno : ENOSPC;
3045 pgstat_report_wait_end();
3047 if (save_errno)
3050 * If we fail to make the file, delete it to release disk space
3052 unlink(tmppath);
3054 close(fd);
3056 errno = save_errno;
3058 ereport(ERROR,
3059 (errcode_for_file_access(),
3060 errmsg("could not write to file \"%s\": %m", tmppath)));
3063 pgstat_report_wait_start(WAIT_EVENT_WAL_INIT_SYNC);
3064 if (pg_fsync(fd) != 0)
3066 int save_errno = errno;
3068 close(fd);
3069 errno = save_errno;
3070 ereport(ERROR,
3071 (errcode_for_file_access(),
3072 errmsg("could not fsync file \"%s\": %m", tmppath)));
3074 pgstat_report_wait_end();
3076 if (close(fd) != 0)
3077 ereport(ERROR,
3078 (errcode_for_file_access(),
3079 errmsg("could not close file \"%s\": %m", tmppath)));
3082 * Now move the segment into place with its final name. Cope with
3083 * possibility that someone else has created the file while we were
3084 * filling ours: if so, use ours to pre-create a future log segment.
3086 installed_segno = logsegno;
3089 * XXX: What should we use as max_segno? We used to use XLOGfileslop when
3090 * that was a constant, but that was always a bit dubious: normally, at a
3091 * checkpoint, XLOGfileslop was the offset from the checkpoint record, but
3092 * here, it was the offset from the insert location. We can't do the
3093 * normal XLOGfileslop calculation here because we don't have access to
3094 * the prior checkpoint's redo location. So somewhat arbitrarily, just use
3095 * CheckPointSegments.
3097 max_segno = logsegno + CheckPointSegments;
3098 if (InstallXLogFileSegment(&installed_segno, tmppath, true, max_segno,
3099 logtli))
3101 *added = true;
3102 elog(DEBUG2, "done creating and filling new WAL file");
3104 else
3107 * No need for any more future segments, or InstallXLogFileSegment()
3108 * failed to rename the file into place. If the rename failed, a
3109 * caller opening the file may fail.
3111 unlink(tmppath);
3112 elog(DEBUG2, "abandoned new WAL file");
3115 return -1;
3119 * Create a new XLOG file segment, or open a pre-existing one.
3121 * logsegno: identify segment to be created/opened.
3123 * Returns FD of opened file.
3125 * Note: errors here are ERROR not PANIC because we might or might not be
3126 * inside a critical section (eg, during checkpoint there is no reason to
3127 * take down the system on failure). They will promote to PANIC if we are
3128 * in a critical section.
3131 XLogFileInit(XLogSegNo logsegno, TimeLineID logtli)
3133 bool ignore_added;
3134 char path[MAXPGPATH];
3135 int fd;
3137 Assert(logtli != 0);
3139 fd = XLogFileInitInternal(logsegno, logtli, &ignore_added, path);
3140 if (fd >= 0)
3141 return fd;
3143 /* Now open original target segment (might not be file I just made) */
3144 fd = BasicOpenFile(path, O_RDWR | PG_BINARY | get_sync_bit(sync_method));
3145 if (fd < 0)
3146 ereport(ERROR,
3147 (errcode_for_file_access(),
3148 errmsg("could not open file \"%s\": %m", path)));
3149 return fd;
3153 * Create a new XLOG file segment by copying a pre-existing one.
3155 * destsegno: identify segment to be created.
3157 * srcTLI, srcsegno: identify segment to be copied (could be from
3158 * a different timeline)
3160 * upto: how much of the source file to copy (the rest is filled with
3161 * zeros)
3163 * Currently this is only used during recovery, and so there are no locking
3164 * considerations. But we should be just as tense as XLogFileInit to avoid
3165 * emplacing a bogus file.
3167 static void
3168 XLogFileCopy(TimeLineID destTLI, XLogSegNo destsegno,
3169 TimeLineID srcTLI, XLogSegNo srcsegno,
3170 int upto)
3172 char path[MAXPGPATH];
3173 char tmppath[MAXPGPATH];
3174 PGAlignedXLogBlock buffer;
3175 int srcfd;
3176 int fd;
3177 int nbytes;
3180 * Open the source file
3182 XLogFilePath(path, srcTLI, srcsegno, wal_segment_size);
3183 srcfd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
3184 if (srcfd < 0)
3185 ereport(ERROR,
3186 (errcode_for_file_access(),
3187 errmsg("could not open file \"%s\": %m", path)));
3190 * Copy into a temp file name.
3192 snprintf(tmppath, MAXPGPATH, XLOGDIR "/xlogtemp.%d", (int) getpid());
3194 unlink(tmppath);
3196 /* do not use get_sync_bit() here --- want to fsync only at end of fill */
3197 fd = OpenTransientFile(tmppath, O_RDWR | O_CREAT | O_EXCL | PG_BINARY);
3198 if (fd < 0)
3199 ereport(ERROR,
3200 (errcode_for_file_access(),
3201 errmsg("could not create file \"%s\": %m", tmppath)));
3204 * Do the data copying.
3206 for (nbytes = 0; nbytes < wal_segment_size; nbytes += sizeof(buffer))
3208 int nread;
3210 nread = upto - nbytes;
3213 * The part that is not read from the source file is filled with
3214 * zeros.
3216 if (nread < sizeof(buffer))
3217 memset(buffer.data, 0, sizeof(buffer));
3219 if (nread > 0)
3221 int r;
3223 if (nread > sizeof(buffer))
3224 nread = sizeof(buffer);
3225 pgstat_report_wait_start(WAIT_EVENT_WAL_COPY_READ);
3226 r = read(srcfd, buffer.data, nread);
3227 if (r != nread)
3229 if (r < 0)
3230 ereport(ERROR,
3231 (errcode_for_file_access(),
3232 errmsg("could not read file \"%s\": %m",
3233 path)));
3234 else
3235 ereport(ERROR,
3236 (errcode(ERRCODE_DATA_CORRUPTED),
3237 errmsg("could not read file \"%s\": read %d of %zu",
3238 path, r, (Size) nread)));
3240 pgstat_report_wait_end();
3242 errno = 0;
3243 pgstat_report_wait_start(WAIT_EVENT_WAL_COPY_WRITE);
3244 if ((int) write(fd, buffer.data, sizeof(buffer)) != (int) sizeof(buffer))
3246 int save_errno = errno;
3249 * If we fail to make the file, delete it to release disk space
3251 unlink(tmppath);
3252 /* if write didn't set errno, assume problem is no disk space */
3253 errno = save_errno ? save_errno : ENOSPC;
3255 ereport(ERROR,
3256 (errcode_for_file_access(),
3257 errmsg("could not write to file \"%s\": %m", tmppath)));
3259 pgstat_report_wait_end();
3262 pgstat_report_wait_start(WAIT_EVENT_WAL_COPY_SYNC);
3263 if (pg_fsync(fd) != 0)
3264 ereport(data_sync_elevel(ERROR),
3265 (errcode_for_file_access(),
3266 errmsg("could not fsync file \"%s\": %m", tmppath)));
3267 pgstat_report_wait_end();
3269 if (CloseTransientFile(fd) != 0)
3270 ereport(ERROR,
3271 (errcode_for_file_access(),
3272 errmsg("could not close file \"%s\": %m", tmppath)));
3274 if (CloseTransientFile(srcfd) != 0)
3275 ereport(ERROR,
3276 (errcode_for_file_access(),
3277 errmsg("could not close file \"%s\": %m", path)));
3280 * Now move the segment into place with its final name.
3282 if (!InstallXLogFileSegment(&destsegno, tmppath, false, 0, destTLI))
3283 elog(ERROR, "InstallXLogFileSegment should not have failed");
3287 * Install a new XLOG segment file as a current or future log segment.
3289 * This is used both to install a newly-created segment (which has a temp
3290 * filename while it's being created) and to recycle an old segment.
3292 * *segno: identify segment to install as (or first possible target).
3293 * When find_free is true, this is modified on return to indicate the
3294 * actual installation location or last segment searched.
3296 * tmppath: initial name of file to install. It will be renamed into place.
3298 * find_free: if true, install the new segment at the first empty segno
3299 * number at or after the passed numbers. If false, install the new segment
3300 * exactly where specified, deleting any existing segment file there.
3302 * max_segno: maximum segment number to install the new file as. Fail if no
3303 * free slot is found between *segno and max_segno. (Ignored when find_free
3304 * is false.)
3306 * tli: The timeline on which the new segment should be installed.
3308 * Returns true if the file was installed successfully. false indicates that
3309 * max_segno limit was exceeded, the startup process has disabled this
3310 * function for now, or an error occurred while renaming the file into place.
3312 static bool
3313 InstallXLogFileSegment(XLogSegNo *segno, char *tmppath,
3314 bool find_free, XLogSegNo max_segno, TimeLineID tli)
3316 char path[MAXPGPATH];
3317 struct stat stat_buf;
3319 Assert(tli != 0);
3321 XLogFilePath(path, tli, *segno, wal_segment_size);
3323 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
3324 if (!XLogCtl->InstallXLogFileSegmentActive)
3326 LWLockRelease(ControlFileLock);
3327 return false;
3330 if (!find_free)
3332 /* Force installation: get rid of any pre-existing segment file */
3333 durable_unlink(path, DEBUG1);
3335 else
3337 /* Find a free slot to put it in */
3338 while (stat(path, &stat_buf) == 0)
3340 if ((*segno) >= max_segno)
3342 /* Failed to find a free slot within specified range */
3343 LWLockRelease(ControlFileLock);
3344 return false;
3346 (*segno)++;
3347 XLogFilePath(path, tli, *segno, wal_segment_size);
3352 * Perform the rename using link if available, paranoidly trying to avoid
3353 * overwriting an existing file (there shouldn't be one).
3355 if (durable_rename_excl(tmppath, path, LOG) != 0)
3357 LWLockRelease(ControlFileLock);
3358 /* durable_rename_excl already emitted log message */
3359 return false;
3362 LWLockRelease(ControlFileLock);
3364 return true;
3368 * Open a pre-existing logfile segment for writing.
3371 XLogFileOpen(XLogSegNo segno, TimeLineID tli)
3373 char path[MAXPGPATH];
3374 int fd;
3376 XLogFilePath(path, tli, segno, wal_segment_size);
3378 fd = BasicOpenFile(path, O_RDWR | PG_BINARY | get_sync_bit(sync_method));
3379 if (fd < 0)
3380 ereport(PANIC,
3381 (errcode_for_file_access(),
3382 errmsg("could not open file \"%s\": %m", path)));
3384 return fd;
3388 * Close the current logfile segment for writing.
3390 static void
3391 XLogFileClose(void)
3393 Assert(openLogFile >= 0);
3396 * WAL segment files will not be re-read in normal operation, so we advise
3397 * the OS to release any cached pages. But do not do so if WAL archiving
3398 * or streaming is active, because archiver and walsender process could
3399 * use the cache to read the WAL segment.
3401 #if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED)
3402 if (!XLogIsNeeded())
3403 (void) posix_fadvise(openLogFile, 0, 0, POSIX_FADV_DONTNEED);
3404 #endif
3406 if (close(openLogFile) != 0)
3408 char xlogfname[MAXFNAMELEN];
3409 int save_errno = errno;
3411 XLogFileName(xlogfname, openLogTLI, openLogSegNo, wal_segment_size);
3412 errno = save_errno;
3413 ereport(PANIC,
3414 (errcode_for_file_access(),
3415 errmsg("could not close file \"%s\": %m", xlogfname)));
3418 openLogFile = -1;
3419 ReleaseExternalFD();
3423 * Preallocate log files beyond the specified log endpoint.
3425 * XXX this is currently extremely conservative, since it forces only one
3426 * future log segment to exist, and even that only if we are 75% done with
3427 * the current one. This is only appropriate for very low-WAL-volume systems.
3428 * High-volume systems will be OK once they've built up a sufficient set of
3429 * recycled log segments, but the startup transient is likely to include
3430 * a lot of segment creations by foreground processes, which is not so good.
3432 * XLogFileInitInternal() can ereport(ERROR). All known causes indicate big
3433 * trouble; for example, a full filesystem is one cause. The checkpoint WAL
3434 * and/or ControlFile updates already completed. If a RequestCheckpoint()
3435 * initiated the present checkpoint and an ERROR ends this function, the
3436 * command that called RequestCheckpoint() fails. That's not ideal, but it's
3437 * not worth contorting more functions to use caller-specified elevel values.
3438 * (With or without RequestCheckpoint(), an ERROR forestalls some inessential
3439 * reporting and resource reclamation.)
3441 static void
3442 PreallocXlogFiles(XLogRecPtr endptr, TimeLineID tli)
3444 XLogSegNo _logSegNo;
3445 int lf;
3446 bool added;
3447 char path[MAXPGPATH];
3448 uint64 offset;
3450 if (!XLogCtl->InstallXLogFileSegmentActive)
3451 return; /* unlocked check says no */
3453 XLByteToPrevSeg(endptr, _logSegNo, wal_segment_size);
3454 offset = XLogSegmentOffset(endptr - 1, wal_segment_size);
3455 if (offset >= (uint32) (0.75 * wal_segment_size))
3457 _logSegNo++;
3458 lf = XLogFileInitInternal(_logSegNo, tli, &added, path);
3459 if (lf >= 0)
3460 close(lf);
3461 if (added)
3462 CheckpointStats.ckpt_segs_added++;
3467 * Throws an error if the given log segment has already been removed or
3468 * recycled. The caller should only pass a segment that it knows to have
3469 * existed while the server has been running, as this function always
3470 * succeeds if no WAL segments have been removed since startup.
3471 * 'tli' is only used in the error message.
3473 * Note: this function guarantees to keep errno unchanged on return.
3474 * This supports callers that use this to possibly deliver a better
3475 * error message about a missing file, while still being able to throw
3476 * a normal file-access error afterwards, if this does return.
3478 void
3479 CheckXLogRemoved(XLogSegNo segno, TimeLineID tli)
3481 int save_errno = errno;
3482 XLogSegNo lastRemovedSegNo;
3484 SpinLockAcquire(&XLogCtl->info_lck);
3485 lastRemovedSegNo = XLogCtl->lastRemovedSegNo;
3486 SpinLockRelease(&XLogCtl->info_lck);
3488 if (segno <= lastRemovedSegNo)
3490 char filename[MAXFNAMELEN];
3492 XLogFileName(filename, tli, segno, wal_segment_size);
3493 errno = save_errno;
3494 ereport(ERROR,
3495 (errcode_for_file_access(),
3496 errmsg("requested WAL segment %s has already been removed",
3497 filename)));
3499 errno = save_errno;
3503 * Return the last WAL segment removed, or 0 if no segment has been removed
3504 * since startup.
3506 * NB: the result can be out of date arbitrarily fast, the caller has to deal
3507 * with that.
3509 XLogSegNo
3510 XLogGetLastRemovedSegno(void)
3512 XLogSegNo lastRemovedSegNo;
3514 SpinLockAcquire(&XLogCtl->info_lck);
3515 lastRemovedSegNo = XLogCtl->lastRemovedSegNo;
3516 SpinLockRelease(&XLogCtl->info_lck);
3518 return lastRemovedSegNo;
3523 * Update the last removed segno pointer in shared memory, to reflect that the
3524 * given XLOG file has been removed.
3526 static void
3527 UpdateLastRemovedPtr(char *filename)
3529 uint32 tli;
3530 XLogSegNo segno;
3532 XLogFromFileName(filename, &tli, &segno, wal_segment_size);
3534 SpinLockAcquire(&XLogCtl->info_lck);
3535 if (segno > XLogCtl->lastRemovedSegNo)
3536 XLogCtl->lastRemovedSegNo = segno;
3537 SpinLockRelease(&XLogCtl->info_lck);
3541 * Remove all temporary log files in pg_wal
3543 * This is called at the beginning of recovery after a previous crash,
3544 * at a point where no other processes write fresh WAL data.
3546 static void
3547 RemoveTempXlogFiles(void)
3549 DIR *xldir;
3550 struct dirent *xlde;
3552 elog(DEBUG2, "removing all temporary WAL segments");
3554 xldir = AllocateDir(XLOGDIR);
3555 while ((xlde = ReadDir(xldir, XLOGDIR)) != NULL)
3557 char path[MAXPGPATH];
3559 if (strncmp(xlde->d_name, "xlogtemp.", 9) != 0)
3560 continue;
3562 snprintf(path, MAXPGPATH, XLOGDIR "/%s", xlde->d_name);
3563 unlink(path);
3564 elog(DEBUG2, "removed temporary WAL segment \"%s\"", path);
3566 FreeDir(xldir);
3570 * Recycle or remove all log files older or equal to passed segno.
3572 * endptr is current (or recent) end of xlog, and lastredoptr is the
3573 * redo pointer of the last checkpoint. These are used to determine
3574 * whether we want to recycle rather than delete no-longer-wanted log files.
3576 * insertTLI is the current timeline for XLOG insertion. Any recycled
3577 * segments should be reused for this timeline.
3579 static void
3580 RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr lastredoptr, XLogRecPtr endptr,
3581 TimeLineID insertTLI)
3583 DIR *xldir;
3584 struct dirent *xlde;
3585 char lastoff[MAXFNAMELEN];
3586 XLogSegNo endlogSegNo;
3587 XLogSegNo recycleSegNo;
3589 /* Initialize info about where to try to recycle to */
3590 XLByteToSeg(endptr, endlogSegNo, wal_segment_size);
3591 recycleSegNo = XLOGfileslop(lastredoptr);
3594 * Construct a filename of the last segment to be kept. The timeline ID
3595 * doesn't matter, we ignore that in the comparison. (During recovery,
3596 * InsertTimeLineID isn't set, so we can't use that.)
3598 XLogFileName(lastoff, 0, segno, wal_segment_size);
3600 elog(DEBUG2, "attempting to remove WAL segments older than log file %s",
3601 lastoff);
3603 xldir = AllocateDir(XLOGDIR);
3605 while ((xlde = ReadDir(xldir, XLOGDIR)) != NULL)
3607 /* Ignore files that are not XLOG segments */
3608 if (!IsXLogFileName(xlde->d_name) &&
3609 !IsPartialXLogFileName(xlde->d_name))
3610 continue;
3613 * We ignore the timeline part of the XLOG segment identifiers in
3614 * deciding whether a segment is still needed. This ensures that we
3615 * won't prematurely remove a segment from a parent timeline. We could
3616 * probably be a little more proactive about removing segments of
3617 * non-parent timelines, but that would be a whole lot more
3618 * complicated.
3620 * We use the alphanumeric sorting property of the filenames to decide
3621 * which ones are earlier than the lastoff segment.
3623 if (strcmp(xlde->d_name + 8, lastoff + 8) <= 0)
3625 if (XLogArchiveCheckDone(xlde->d_name))
3627 /* Update the last removed location in shared memory first */
3628 UpdateLastRemovedPtr(xlde->d_name);
3630 RemoveXlogFile(xlde->d_name, recycleSegNo, &endlogSegNo,
3631 insertTLI);
3636 FreeDir(xldir);
3640 * Remove WAL files that are not part of the given timeline's history.
3642 * This is called during recovery, whenever we switch to follow a new
3643 * timeline, and at the end of recovery when we create a new timeline. We
3644 * wouldn't otherwise care about extra WAL files lying in pg_wal, but they
3645 * might be leftover pre-allocated or recycled WAL segments on the old timeline
3646 * that we haven't used yet, and contain garbage. If we just leave them in
3647 * pg_wal, they will eventually be archived, and we can't let that happen.
3648 * Files that belong to our timeline history are valid, because we have
3649 * successfully replayed them, but from others we can't be sure.
3651 * 'switchpoint' is the current point in WAL where we switch to new timeline,
3652 * and 'newTLI' is the new timeline we switch to.
3654 void
3655 RemoveNonParentXlogFiles(XLogRecPtr switchpoint, TimeLineID newTLI)
3657 DIR *xldir;
3658 struct dirent *xlde;
3659 char switchseg[MAXFNAMELEN];
3660 XLogSegNo endLogSegNo;
3661 XLogSegNo switchLogSegNo;
3662 XLogSegNo recycleSegNo;
3665 * Initialize info about where to begin the work. This will recycle,
3666 * somewhat arbitrarily, 10 future segments.
3668 XLByteToPrevSeg(switchpoint, switchLogSegNo, wal_segment_size);
3669 XLByteToSeg(switchpoint, endLogSegNo, wal_segment_size);
3670 recycleSegNo = endLogSegNo + 10;
3673 * Construct a filename of the last segment to be kept.
3675 XLogFileName(switchseg, newTLI, switchLogSegNo, wal_segment_size);
3677 elog(DEBUG2, "attempting to remove WAL segments newer than log file %s",
3678 switchseg);
3680 xldir = AllocateDir(XLOGDIR);
3682 while ((xlde = ReadDir(xldir, XLOGDIR)) != NULL)
3684 /* Ignore files that are not XLOG segments */
3685 if (!IsXLogFileName(xlde->d_name))
3686 continue;
3689 * Remove files that are on a timeline older than the new one we're
3690 * switching to, but with a segment number >= the first segment on the
3691 * new timeline.
3693 if (strncmp(xlde->d_name, switchseg, 8) < 0 &&
3694 strcmp(xlde->d_name + 8, switchseg + 8) > 0)
3697 * If the file has already been marked as .ready, however, don't
3698 * remove it yet. It should be OK to remove it - files that are
3699 * not part of our timeline history are not required for recovery
3700 * - but seems safer to let them be archived and removed later.
3702 if (!XLogArchiveIsReady(xlde->d_name))
3703 RemoveXlogFile(xlde->d_name, recycleSegNo, &endLogSegNo,
3704 newTLI);
3708 FreeDir(xldir);
3712 * Recycle or remove a log file that's no longer needed.
3714 * segname is the name of the segment to recycle or remove. recycleSegNo
3715 * is the segment number to recycle up to. endlogSegNo is the segment
3716 * number of the current (or recent) end of WAL.
3718 * endlogSegNo gets incremented if the segment is recycled so as it is not
3719 * checked again with future callers of this function.
3721 * insertTLI is the current timeline for XLOG insertion. Any recycled segments
3722 * should be used for this timeline.
3724 static void
3725 RemoveXlogFile(const char *segname, XLogSegNo recycleSegNo,
3726 XLogSegNo *endlogSegNo, TimeLineID insertTLI)
3728 char path[MAXPGPATH];
3729 #ifdef WIN32
3730 char newpath[MAXPGPATH];
3731 #endif
3732 struct stat statbuf;
3734 snprintf(path, MAXPGPATH, XLOGDIR "/%s", segname);
3737 * Before deleting the file, see if it can be recycled as a future log
3738 * segment. Only recycle normal files, because we don't want to recycle
3739 * symbolic links pointing to a separate archive directory.
3741 if (wal_recycle &&
3742 *endlogSegNo <= recycleSegNo &&
3743 XLogCtl->InstallXLogFileSegmentActive && /* callee rechecks this */
3744 lstat(path, &statbuf) == 0 && S_ISREG(statbuf.st_mode) &&
3745 InstallXLogFileSegment(endlogSegNo, path,
3746 true, recycleSegNo, insertTLI))
3748 ereport(DEBUG2,
3749 (errmsg_internal("recycled write-ahead log file \"%s\"",
3750 segname)));
3751 CheckpointStats.ckpt_segs_recycled++;
3752 /* Needn't recheck that slot on future iterations */
3753 (*endlogSegNo)++;
3755 else
3757 /* No need for any more future segments, or recycling failed ... */
3758 int rc;
3760 ereport(DEBUG2,
3761 (errmsg_internal("removing write-ahead log file \"%s\"",
3762 segname)));
3764 #ifdef WIN32
3767 * On Windows, if another process (e.g another backend) holds the file
3768 * open in FILE_SHARE_DELETE mode, unlink will succeed, but the file
3769 * will still show up in directory listing until the last handle is
3770 * closed. To avoid confusing the lingering deleted file for a live
3771 * WAL file that needs to be archived, rename it before deleting it.
3773 * If another process holds the file open without FILE_SHARE_DELETE
3774 * flag, rename will fail. We'll try again at the next checkpoint.
3776 snprintf(newpath, MAXPGPATH, "%s.deleted", path);
3777 if (rename(path, newpath) != 0)
3779 ereport(LOG,
3780 (errcode_for_file_access(),
3781 errmsg("could not rename file \"%s\": %m",
3782 path)));
3783 return;
3785 rc = durable_unlink(newpath, LOG);
3786 #else
3787 rc = durable_unlink(path, LOG);
3788 #endif
3789 if (rc != 0)
3791 /* Message already logged by durable_unlink() */
3792 return;
3794 CheckpointStats.ckpt_segs_removed++;
3797 XLogArchiveCleanup(segname);
3801 * Verify whether pg_wal and pg_wal/archive_status exist.
3802 * If the latter does not exist, recreate it.
3804 * It is not the goal of this function to verify the contents of these
3805 * directories, but to help in cases where someone has performed a cluster
3806 * copy for PITR purposes but omitted pg_wal from the copy.
3808 * We could also recreate pg_wal if it doesn't exist, but a deliberate
3809 * policy decision was made not to. It is fairly common for pg_wal to be
3810 * a symlink, and if that was the DBA's intent then automatically making a
3811 * plain directory would result in degraded performance with no notice.
3813 static void
3814 ValidateXLOGDirectoryStructure(void)
3816 char path[MAXPGPATH];
3817 struct stat stat_buf;
3819 /* Check for pg_wal; if it doesn't exist, error out */
3820 if (stat(XLOGDIR, &stat_buf) != 0 ||
3821 !S_ISDIR(stat_buf.st_mode))
3822 ereport(FATAL,
3823 (errmsg("required WAL directory \"%s\" does not exist",
3824 XLOGDIR)));
3826 /* Check for archive_status */
3827 snprintf(path, MAXPGPATH, XLOGDIR "/archive_status");
3828 if (stat(path, &stat_buf) == 0)
3830 /* Check for weird cases where it exists but isn't a directory */
3831 if (!S_ISDIR(stat_buf.st_mode))
3832 ereport(FATAL,
3833 (errmsg("required WAL directory \"%s\" does not exist",
3834 path)));
3836 else
3838 ereport(LOG,
3839 (errmsg("creating missing WAL directory \"%s\"", path)));
3840 if (MakePGDirectory(path) < 0)
3841 ereport(FATAL,
3842 (errmsg("could not create missing directory \"%s\": %m",
3843 path)));
3848 * Remove previous backup history files. This also retries creation of
3849 * .ready files for any backup history files for which XLogArchiveNotify
3850 * failed earlier.
3852 static void
3853 CleanupBackupHistory(void)
3855 DIR *xldir;
3856 struct dirent *xlde;
3857 char path[MAXPGPATH + sizeof(XLOGDIR)];
3859 xldir = AllocateDir(XLOGDIR);
3861 while ((xlde = ReadDir(xldir, XLOGDIR)) != NULL)
3863 if (IsBackupHistoryFileName(xlde->d_name))
3865 if (XLogArchiveCheckDone(xlde->d_name))
3867 elog(DEBUG2, "removing WAL backup history file \"%s\"",
3868 xlde->d_name);
3869 snprintf(path, sizeof(path), XLOGDIR "/%s", xlde->d_name);
3870 unlink(path);
3871 XLogArchiveCleanup(xlde->d_name);
3876 FreeDir(xldir);
3880 * I/O routines for pg_control
3882 * *ControlFile is a buffer in shared memory that holds an image of the
3883 * contents of pg_control. WriteControlFile() initializes pg_control
3884 * given a preloaded buffer, ReadControlFile() loads the buffer from
3885 * the pg_control file (during postmaster or standalone-backend startup),
3886 * and UpdateControlFile() rewrites pg_control after we modify xlog state.
3887 * InitControlFile() fills the buffer with initial values.
3889 * For simplicity, WriteControlFile() initializes the fields of pg_control
3890 * that are related to checking backend/database compatibility, and
3891 * ReadControlFile() verifies they are correct. We could split out the
3892 * I/O and compatibility-check functions, but there seems no need currently.
3895 static void
3896 InitControlFile(uint64 sysidentifier)
3898 char mock_auth_nonce[MOCK_AUTH_NONCE_LEN];
3901 * Generate a random nonce. This is used for authentication requests that
3902 * will fail because the user does not exist. The nonce is used to create
3903 * a genuine-looking password challenge for the non-existent user, in lieu
3904 * of an actual stored password.
3906 if (!pg_strong_random(mock_auth_nonce, MOCK_AUTH_NONCE_LEN))
3907 ereport(PANIC,
3908 (errcode(ERRCODE_INTERNAL_ERROR),
3909 errmsg("could not generate secret authorization token")));
3911 memset(ControlFile, 0, sizeof(ControlFileData));
3912 /* Initialize pg_control status fields */
3913 ControlFile->system_identifier = sysidentifier;
3914 memcpy(ControlFile->mock_authentication_nonce, mock_auth_nonce, MOCK_AUTH_NONCE_LEN);
3915 ControlFile->state = DB_SHUTDOWNED;
3916 ControlFile->unloggedLSN = FirstNormalUnloggedLSN;
3918 /* Set important parameter values for use when replaying WAL */
3919 ControlFile->MaxConnections = MaxConnections;
3920 ControlFile->max_worker_processes = max_worker_processes;
3921 ControlFile->max_wal_senders = max_wal_senders;
3922 ControlFile->max_prepared_xacts = max_prepared_xacts;
3923 ControlFile->max_locks_per_xact = max_locks_per_xact;
3924 ControlFile->wal_level = wal_level;
3925 ControlFile->wal_log_hints = wal_log_hints;
3926 ControlFile->track_commit_timestamp = track_commit_timestamp;
3927 ControlFile->data_checksum_version = bootstrap_data_checksum_version;
3930 static void
3931 WriteControlFile(void)
3933 int fd;
3934 char buffer[PG_CONTROL_FILE_SIZE]; /* need not be aligned */
3937 * Ensure that the size of the pg_control data structure is sane. See the
3938 * comments for these symbols in pg_control.h.
3940 StaticAssertStmt(sizeof(ControlFileData) <= PG_CONTROL_MAX_SAFE_SIZE,
3941 "pg_control is too large for atomic disk writes");
3942 StaticAssertStmt(sizeof(ControlFileData) <= PG_CONTROL_FILE_SIZE,
3943 "sizeof(ControlFileData) exceeds PG_CONTROL_FILE_SIZE");
3946 * Initialize version and compatibility-check fields
3948 ControlFile->pg_control_version = PG_CONTROL_VERSION;
3949 ControlFile->catalog_version_no = CATALOG_VERSION_NO;
3951 ControlFile->maxAlign = MAXIMUM_ALIGNOF;
3952 ControlFile->floatFormat = FLOATFORMAT_VALUE;
3954 ControlFile->blcksz = BLCKSZ;
3955 ControlFile->relseg_size = RELSEG_SIZE;
3956 ControlFile->xlog_blcksz = XLOG_BLCKSZ;
3957 ControlFile->xlog_seg_size = wal_segment_size;
3959 ControlFile->nameDataLen = NAMEDATALEN;
3960 ControlFile->indexMaxKeys = INDEX_MAX_KEYS;
3962 ControlFile->toast_max_chunk_size = TOAST_MAX_CHUNK_SIZE;
3963 ControlFile->loblksize = LOBLKSIZE;
3965 ControlFile->float8ByVal = FLOAT8PASSBYVAL;
3967 /* Contents are protected with a CRC */
3968 INIT_CRC32C(ControlFile->crc);
3969 COMP_CRC32C(ControlFile->crc,
3970 (char *) ControlFile,
3971 offsetof(ControlFileData, crc));
3972 FIN_CRC32C(ControlFile->crc);
3975 * We write out PG_CONTROL_FILE_SIZE bytes into pg_control, zero-padding
3976 * the excess over sizeof(ControlFileData). This reduces the odds of
3977 * premature-EOF errors when reading pg_control. We'll still fail when we
3978 * check the contents of the file, but hopefully with a more specific
3979 * error than "couldn't read pg_control".
3981 memset(buffer, 0, PG_CONTROL_FILE_SIZE);
3982 memcpy(buffer, ControlFile, sizeof(ControlFileData));
3984 fd = BasicOpenFile(XLOG_CONTROL_FILE,
3985 O_RDWR | O_CREAT | O_EXCL | PG_BINARY);
3986 if (fd < 0)
3987 ereport(PANIC,
3988 (errcode_for_file_access(),
3989 errmsg("could not create file \"%s\": %m",
3990 XLOG_CONTROL_FILE)));
3992 errno = 0;
3993 pgstat_report_wait_start(WAIT_EVENT_CONTROL_FILE_WRITE);
3994 if (write(fd, buffer, PG_CONTROL_FILE_SIZE) != PG_CONTROL_FILE_SIZE)
3996 /* if write didn't set errno, assume problem is no disk space */
3997 if (errno == 0)
3998 errno = ENOSPC;
3999 ereport(PANIC,
4000 (errcode_for_file_access(),
4001 errmsg("could not write to file \"%s\": %m",
4002 XLOG_CONTROL_FILE)));
4004 pgstat_report_wait_end();
4006 pgstat_report_wait_start(WAIT_EVENT_CONTROL_FILE_SYNC);
4007 if (pg_fsync(fd) != 0)
4008 ereport(PANIC,
4009 (errcode_for_file_access(),
4010 errmsg("could not fsync file \"%s\": %m",
4011 XLOG_CONTROL_FILE)));
4012 pgstat_report_wait_end();
4014 if (close(fd) != 0)
4015 ereport(PANIC,
4016 (errcode_for_file_access(),
4017 errmsg("could not close file \"%s\": %m",
4018 XLOG_CONTROL_FILE)));
4021 static void
4022 ReadControlFile(void)
4024 pg_crc32c crc;
4025 int fd;
4026 static char wal_segsz_str[20];
4027 int r;
4030 * Read data...
4032 fd = BasicOpenFile(XLOG_CONTROL_FILE,
4033 O_RDWR | PG_BINARY);
4034 if (fd < 0)
4035 ereport(PANIC,
4036 (errcode_for_file_access(),
4037 errmsg("could not open file \"%s\": %m",
4038 XLOG_CONTROL_FILE)));
4040 pgstat_report_wait_start(WAIT_EVENT_CONTROL_FILE_READ);
4041 r = read(fd, ControlFile, sizeof(ControlFileData));
4042 if (r != sizeof(ControlFileData))
4044 if (r < 0)
4045 ereport(PANIC,
4046 (errcode_for_file_access(),
4047 errmsg("could not read file \"%s\": %m",
4048 XLOG_CONTROL_FILE)));
4049 else
4050 ereport(PANIC,
4051 (errcode(ERRCODE_DATA_CORRUPTED),
4052 errmsg("could not read file \"%s\": read %d of %zu",
4053 XLOG_CONTROL_FILE, r, sizeof(ControlFileData))));
4055 pgstat_report_wait_end();
4057 close(fd);
4060 * Check for expected pg_control format version. If this is wrong, the
4061 * CRC check will likely fail because we'll be checking the wrong number
4062 * of bytes. Complaining about wrong version will probably be more
4063 * enlightening than complaining about wrong CRC.
4066 if (ControlFile->pg_control_version != PG_CONTROL_VERSION && ControlFile->pg_control_version % 65536 == 0 && ControlFile->pg_control_version / 65536 != 0)
4067 ereport(FATAL,
4068 (errmsg("database files are incompatible with server"),
4069 errdetail("The database cluster was initialized with PG_CONTROL_VERSION %d (0x%08x),"
4070 " but the server was compiled with PG_CONTROL_VERSION %d (0x%08x).",
4071 ControlFile->pg_control_version, ControlFile->pg_control_version,
4072 PG_CONTROL_VERSION, PG_CONTROL_VERSION),
4073 errhint("This could be a problem of mismatched byte ordering. It looks like you need to initdb.")));
4075 if (ControlFile->pg_control_version != PG_CONTROL_VERSION)
4076 ereport(FATAL,
4077 (errmsg("database files are incompatible with server"),
4078 errdetail("The database cluster was initialized with PG_CONTROL_VERSION %d,"
4079 " but the server was compiled with PG_CONTROL_VERSION %d.",
4080 ControlFile->pg_control_version, PG_CONTROL_VERSION),
4081 errhint("It looks like you need to initdb.")));
4083 /* Now check the CRC. */
4084 INIT_CRC32C(crc);
4085 COMP_CRC32C(crc,
4086 (char *) ControlFile,
4087 offsetof(ControlFileData, crc));
4088 FIN_CRC32C(crc);
4090 if (!EQ_CRC32C(crc, ControlFile->crc))
4091 ereport(FATAL,
4092 (errmsg("incorrect checksum in control file")));
4095 * Do compatibility checking immediately. If the database isn't
4096 * compatible with the backend executable, we want to abort before we can
4097 * possibly do any damage.
4099 if (ControlFile->catalog_version_no != CATALOG_VERSION_NO)
4100 ereport(FATAL,
4101 (errmsg("database files are incompatible with server"),
4102 errdetail("The database cluster was initialized with CATALOG_VERSION_NO %d,"
4103 " but the server was compiled with CATALOG_VERSION_NO %d.",
4104 ControlFile->catalog_version_no, CATALOG_VERSION_NO),
4105 errhint("It looks like you need to initdb.")));
4106 if (ControlFile->maxAlign != MAXIMUM_ALIGNOF)
4107 ereport(FATAL,
4108 (errmsg("database files are incompatible with server"),
4109 errdetail("The database cluster was initialized with MAXALIGN %d,"
4110 " but the server was compiled with MAXALIGN %d.",
4111 ControlFile->maxAlign, MAXIMUM_ALIGNOF),
4112 errhint("It looks like you need to initdb.")));
4113 if (ControlFile->floatFormat != FLOATFORMAT_VALUE)
4114 ereport(FATAL,
4115 (errmsg("database files are incompatible with server"),
4116 errdetail("The database cluster appears to use a different floating-point number format than the server executable."),
4117 errhint("It looks like you need to initdb.")));
4118 if (ControlFile->blcksz != BLCKSZ)
4119 ereport(FATAL,
4120 (errmsg("database files are incompatible with server"),
4121 errdetail("The database cluster was initialized with BLCKSZ %d,"
4122 " but the server was compiled with BLCKSZ %d.",
4123 ControlFile->blcksz, BLCKSZ),
4124 errhint("It looks like you need to recompile or initdb.")));
4125 if (ControlFile->relseg_size != RELSEG_SIZE)
4126 ereport(FATAL,
4127 (errmsg("database files are incompatible with server"),
4128 errdetail("The database cluster was initialized with RELSEG_SIZE %d,"
4129 " but the server was compiled with RELSEG_SIZE %d.",
4130 ControlFile->relseg_size, RELSEG_SIZE),
4131 errhint("It looks like you need to recompile or initdb.")));
4132 if (ControlFile->xlog_blcksz != XLOG_BLCKSZ)
4133 ereport(FATAL,
4134 (errmsg("database files are incompatible with server"),
4135 errdetail("The database cluster was initialized with XLOG_BLCKSZ %d,"
4136 " but the server was compiled with XLOG_BLCKSZ %d.",
4137 ControlFile->xlog_blcksz, XLOG_BLCKSZ),
4138 errhint("It looks like you need to recompile or initdb.")));
4139 if (ControlFile->nameDataLen != NAMEDATALEN)
4140 ereport(FATAL,
4141 (errmsg("database files are incompatible with server"),
4142 errdetail("The database cluster was initialized with NAMEDATALEN %d,"
4143 " but the server was compiled with NAMEDATALEN %d.",
4144 ControlFile->nameDataLen, NAMEDATALEN),
4145 errhint("It looks like you need to recompile or initdb.")));
4146 if (ControlFile->indexMaxKeys != INDEX_MAX_KEYS)
4147 ereport(FATAL,
4148 (errmsg("database files are incompatible with server"),
4149 errdetail("The database cluster was initialized with INDEX_MAX_KEYS %d,"
4150 " but the server was compiled with INDEX_MAX_KEYS %d.",
4151 ControlFile->indexMaxKeys, INDEX_MAX_KEYS),
4152 errhint("It looks like you need to recompile or initdb.")));
4153 if (ControlFile->toast_max_chunk_size != TOAST_MAX_CHUNK_SIZE)
4154 ereport(FATAL,
4155 (errmsg("database files are incompatible with server"),
4156 errdetail("The database cluster was initialized with TOAST_MAX_CHUNK_SIZE %d,"
4157 " but the server was compiled with TOAST_MAX_CHUNK_SIZE %d.",
4158 ControlFile->toast_max_chunk_size, (int) TOAST_MAX_CHUNK_SIZE),
4159 errhint("It looks like you need to recompile or initdb.")));
4160 if (ControlFile->loblksize != LOBLKSIZE)
4161 ereport(FATAL,
4162 (errmsg("database files are incompatible with server"),
4163 errdetail("The database cluster was initialized with LOBLKSIZE %d,"
4164 " but the server was compiled with LOBLKSIZE %d.",
4165 ControlFile->loblksize, (int) LOBLKSIZE),
4166 errhint("It looks like you need to recompile or initdb.")));
4168 #ifdef USE_FLOAT8_BYVAL
4169 if (ControlFile->float8ByVal != true)
4170 ereport(FATAL,
4171 (errmsg("database files are incompatible with server"),
4172 errdetail("The database cluster was initialized without USE_FLOAT8_BYVAL"
4173 " but the server was compiled with USE_FLOAT8_BYVAL."),
4174 errhint("It looks like you need to recompile or initdb.")));
4175 #else
4176 if (ControlFile->float8ByVal != false)
4177 ereport(FATAL,
4178 (errmsg("database files are incompatible with server"),
4179 errdetail("The database cluster was initialized with USE_FLOAT8_BYVAL"
4180 " but the server was compiled without USE_FLOAT8_BYVAL."),
4181 errhint("It looks like you need to recompile or initdb.")));
4182 #endif
4184 wal_segment_size = ControlFile->xlog_seg_size;
4186 if (!IsValidWalSegSize(wal_segment_size))
4187 ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4188 errmsg_plural("WAL segment size must be a power of two between 1 MB and 1 GB, but the control file specifies %d byte",
4189 "WAL segment size must be a power of two between 1 MB and 1 GB, but the control file specifies %d bytes",
4190 wal_segment_size,
4191 wal_segment_size)));
4193 snprintf(wal_segsz_str, sizeof(wal_segsz_str), "%d", wal_segment_size);
4194 SetConfigOption("wal_segment_size", wal_segsz_str, PGC_INTERNAL,
4195 PGC_S_OVERRIDE);
4197 /* check and update variables dependent on wal_segment_size */
4198 if (ConvertToXSegs(min_wal_size_mb, wal_segment_size) < 2)
4199 ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4200 errmsg("\"min_wal_size\" must be at least twice \"wal_segment_size\"")));
4202 if (ConvertToXSegs(max_wal_size_mb, wal_segment_size) < 2)
4203 ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4204 errmsg("\"max_wal_size\" must be at least twice \"wal_segment_size\"")));
4206 UsableBytesInSegment =
4207 (wal_segment_size / XLOG_BLCKSZ * UsableBytesInPage) -
4208 (SizeOfXLogLongPHD - SizeOfXLogShortPHD);
4210 CalculateCheckpointSegments();
4212 /* Make the initdb settings visible as GUC variables, too */
4213 SetConfigOption("data_checksums", DataChecksumsEnabled() ? "yes" : "no",
4214 PGC_INTERNAL, PGC_S_OVERRIDE);
4218 * Utility wrapper to update the control file. Note that the control
4219 * file gets flushed.
4221 static void
4222 UpdateControlFile(void)
4224 update_controlfile(DataDir, ControlFile, true);
4228 * Returns the unique system identifier from control file.
4230 uint64
4231 GetSystemIdentifier(void)
4233 Assert(ControlFile != NULL);
4234 return ControlFile->system_identifier;
4238 * Returns the random nonce from control file.
4240 char *
4241 GetMockAuthenticationNonce(void)
4243 Assert(ControlFile != NULL);
4244 return ControlFile->mock_authentication_nonce;
4248 * Are checksums enabled for data pages?
4250 bool
4251 DataChecksumsEnabled(void)
4253 Assert(ControlFile != NULL);
4254 return (ControlFile->data_checksum_version > 0);
4258 * Returns a fake LSN for unlogged relations.
4260 * Each call generates an LSN that is greater than any previous value
4261 * returned. The current counter value is saved and restored across clean
4262 * shutdowns, but like unlogged relations, does not survive a crash. This can
4263 * be used in lieu of real LSN values returned by XLogInsert, if you need an
4264 * LSN-like increasing sequence of numbers without writing any WAL.
4266 XLogRecPtr
4267 GetFakeLSNForUnloggedRel(void)
4269 XLogRecPtr nextUnloggedLSN;
4271 /* increment the unloggedLSN counter, need SpinLock */
4272 SpinLockAcquire(&XLogCtl->ulsn_lck);
4273 nextUnloggedLSN = XLogCtl->unloggedLSN++;
4274 SpinLockRelease(&XLogCtl->ulsn_lck);
4276 return nextUnloggedLSN;
4280 * Auto-tune the number of XLOG buffers.
4282 * The preferred setting for wal_buffers is about 3% of shared_buffers, with
4283 * a maximum of one XLOG segment (there is little reason to think that more
4284 * is helpful, at least so long as we force an fsync when switching log files)
4285 * and a minimum of 8 blocks (which was the default value prior to PostgreSQL
4286 * 9.1, when auto-tuning was added).
4288 * This should not be called until NBuffers has received its final value.
4290 static int
4291 XLOGChooseNumBuffers(void)
4293 int xbuffers;
4295 xbuffers = NBuffers / 32;
4296 if (xbuffers > (wal_segment_size / XLOG_BLCKSZ))
4297 xbuffers = (wal_segment_size / XLOG_BLCKSZ);
4298 if (xbuffers < 8)
4299 xbuffers = 8;
4300 return xbuffers;
4304 * GUC check_hook for wal_buffers
4306 bool
4307 check_wal_buffers(int *newval, void **extra, GucSource source)
4310 * -1 indicates a request for auto-tune.
4312 if (*newval == -1)
4315 * If we haven't yet changed the boot_val default of -1, just let it
4316 * be. We'll fix it when XLOGShmemSize is called.
4318 if (XLOGbuffers == -1)
4319 return true;
4321 /* Otherwise, substitute the auto-tune value */
4322 *newval = XLOGChooseNumBuffers();
4326 * We clamp manually-set values to at least 4 blocks. Prior to PostgreSQL
4327 * 9.1, a minimum of 4 was enforced by guc.c, but since that is no longer
4328 * the case, we just silently treat such values as a request for the
4329 * minimum. (We could throw an error instead, but that doesn't seem very
4330 * helpful.)
4332 if (*newval < 4)
4333 *newval = 4;
4335 return true;
4339 * Read the control file, set respective GUCs.
4341 * This is to be called during startup, including a crash recovery cycle,
4342 * unless in bootstrap mode, where no control file yet exists. As there's no
4343 * usable shared memory yet (its sizing can depend on the contents of the
4344 * control file!), first store the contents in local memory. XLOGShmemInit()
4345 * will then copy it to shared memory later.
4347 * reset just controls whether previous contents are to be expected (in the
4348 * reset case, there's a dangling pointer into old shared memory), or not.
4350 void
4351 LocalProcessControlFile(bool reset)
4353 Assert(reset || ControlFile == NULL);
4354 ControlFile = palloc(sizeof(ControlFileData));
4355 ReadControlFile();
4359 * Initialization of shared memory for XLOG
4361 Size
4362 XLOGShmemSize(void)
4364 Size size;
4367 * If the value of wal_buffers is -1, use the preferred auto-tune value.
4368 * This isn't an amazingly clean place to do this, but we must wait till
4369 * NBuffers has received its final value, and must do it before using the
4370 * value of XLOGbuffers to do anything important.
4372 if (XLOGbuffers == -1)
4374 char buf[32];
4376 snprintf(buf, sizeof(buf), "%d", XLOGChooseNumBuffers());
4377 SetConfigOption("wal_buffers", buf, PGC_POSTMASTER, PGC_S_OVERRIDE);
4379 Assert(XLOGbuffers > 0);
4381 /* XLogCtl */
4382 size = sizeof(XLogCtlData);
4384 /* WAL insertion locks, plus alignment */
4385 size = add_size(size, mul_size(sizeof(WALInsertLockPadded), NUM_XLOGINSERT_LOCKS + 1));
4386 /* xlblocks array */
4387 size = add_size(size, mul_size(sizeof(XLogRecPtr), XLOGbuffers));
4388 /* extra alignment padding for XLOG I/O buffers */
4389 size = add_size(size, XLOG_BLCKSZ);
4390 /* and the buffers themselves */
4391 size = add_size(size, mul_size(XLOG_BLCKSZ, XLOGbuffers));
4394 * Note: we don't count ControlFileData, it comes out of the "slop factor"
4395 * added by CreateSharedMemoryAndSemaphores. This lets us use this
4396 * routine again below to compute the actual allocation size.
4399 return size;
4402 void
4403 XLOGShmemInit(void)
4405 bool foundCFile,
4406 foundXLog;
4407 char *allocptr;
4408 int i;
4409 ControlFileData *localControlFile;
4411 #ifdef WAL_DEBUG
4414 * Create a memory context for WAL debugging that's exempt from the normal
4415 * "no pallocs in critical section" rule. Yes, that can lead to a PANIC if
4416 * an allocation fails, but wal_debug is not for production use anyway.
4418 if (walDebugCxt == NULL)
4420 walDebugCxt = AllocSetContextCreate(TopMemoryContext,
4421 "WAL Debug",
4422 ALLOCSET_DEFAULT_SIZES);
4423 MemoryContextAllowInCriticalSection(walDebugCxt, true);
4425 #endif
4428 XLogCtl = (XLogCtlData *)
4429 ShmemInitStruct("XLOG Ctl", XLOGShmemSize(), &foundXLog);
4431 localControlFile = ControlFile;
4432 ControlFile = (ControlFileData *)
4433 ShmemInitStruct("Control File", sizeof(ControlFileData), &foundCFile);
4435 if (foundCFile || foundXLog)
4437 /* both should be present or neither */
4438 Assert(foundCFile && foundXLog);
4440 /* Initialize local copy of WALInsertLocks */
4441 WALInsertLocks = XLogCtl->Insert.WALInsertLocks;
4443 if (localControlFile)
4444 pfree(localControlFile);
4445 return;
4447 memset(XLogCtl, 0, sizeof(XLogCtlData));
4450 * Already have read control file locally, unless in bootstrap mode. Move
4451 * contents into shared memory.
4453 if (localControlFile)
4455 memcpy(ControlFile, localControlFile, sizeof(ControlFileData));
4456 pfree(localControlFile);
4460 * Since XLogCtlData contains XLogRecPtr fields, its sizeof should be a
4461 * multiple of the alignment for same, so no extra alignment padding is
4462 * needed here.
4464 allocptr = ((char *) XLogCtl) + sizeof(XLogCtlData);
4465 XLogCtl->xlblocks = (XLogRecPtr *) allocptr;
4466 memset(XLogCtl->xlblocks, 0, sizeof(XLogRecPtr) * XLOGbuffers);
4467 allocptr += sizeof(XLogRecPtr) * XLOGbuffers;
4470 /* WAL insertion locks. Ensure they're aligned to the full padded size */
4471 allocptr += sizeof(WALInsertLockPadded) -
4472 ((uintptr_t) allocptr) % sizeof(WALInsertLockPadded);
4473 WALInsertLocks = XLogCtl->Insert.WALInsertLocks =
4474 (WALInsertLockPadded *) allocptr;
4475 allocptr += sizeof(WALInsertLockPadded) * NUM_XLOGINSERT_LOCKS;
4477 for (i = 0; i < NUM_XLOGINSERT_LOCKS; i++)
4479 LWLockInitialize(&WALInsertLocks[i].l.lock, LWTRANCHE_WAL_INSERT);
4480 WALInsertLocks[i].l.insertingAt = InvalidXLogRecPtr;
4481 WALInsertLocks[i].l.lastImportantAt = InvalidXLogRecPtr;
4485 * Align the start of the page buffers to a full xlog block size boundary.
4486 * This simplifies some calculations in XLOG insertion. It is also
4487 * required for O_DIRECT.
4489 allocptr = (char *) TYPEALIGN(XLOG_BLCKSZ, allocptr);
4490 XLogCtl->pages = allocptr;
4491 memset(XLogCtl->pages, 0, (Size) XLOG_BLCKSZ * XLOGbuffers);
4494 * Do basic initialization of XLogCtl shared data. (StartupXLOG will fill
4495 * in additional info.)
4497 XLogCtl->XLogCacheBlck = XLOGbuffers - 1;
4498 XLogCtl->SharedRecoveryState = RECOVERY_STATE_CRASH;
4499 XLogCtl->InstallXLogFileSegmentActive = false;
4500 XLogCtl->WalWriterSleeping = false;
4502 SpinLockInit(&XLogCtl->Insert.insertpos_lck);
4503 SpinLockInit(&XLogCtl->info_lck);
4504 SpinLockInit(&XLogCtl->ulsn_lck);
4508 * This func must be called ONCE on system install. It creates pg_control
4509 * and the initial XLOG segment.
4511 void
4512 BootStrapXLOG(void)
4514 CheckPoint checkPoint;
4515 char *buffer;
4516 XLogPageHeader page;
4517 XLogLongPageHeader longpage;
4518 XLogRecord *record;
4519 char *recptr;
4520 uint64 sysidentifier;
4521 struct timeval tv;
4522 pg_crc32c crc;
4524 /* allow ordinary WAL segment creation, like StartupXLOG() would */
4525 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
4526 XLogCtl->InstallXLogFileSegmentActive = true;
4527 LWLockRelease(ControlFileLock);
4530 * Select a hopefully-unique system identifier code for this installation.
4531 * We use the result of gettimeofday(), including the fractional seconds
4532 * field, as being about as unique as we can easily get. (Think not to
4533 * use random(), since it hasn't been seeded and there's no portable way
4534 * to seed it other than the system clock value...) The upper half of the
4535 * uint64 value is just the tv_sec part, while the lower half contains the
4536 * tv_usec part (which must fit in 20 bits), plus 12 bits from our current
4537 * PID for a little extra uniqueness. A person knowing this encoding can
4538 * determine the initialization time of the installation, which could
4539 * perhaps be useful sometimes.
4541 gettimeofday(&tv, NULL);
4542 sysidentifier = ((uint64) tv.tv_sec) << 32;
4543 sysidentifier |= ((uint64) tv.tv_usec) << 12;
4544 sysidentifier |= getpid() & 0xFFF;
4546 /* page buffer must be aligned suitably for O_DIRECT */
4547 buffer = (char *) palloc(XLOG_BLCKSZ + XLOG_BLCKSZ);
4548 page = (XLogPageHeader) TYPEALIGN(XLOG_BLCKSZ, buffer);
4549 memset(page, 0, XLOG_BLCKSZ);
4552 * Set up information for the initial checkpoint record
4554 * The initial checkpoint record is written to the beginning of the WAL
4555 * segment with logid=0 logseg=1. The very first WAL segment, 0/0, is not
4556 * used, so that we can use 0/0 to mean "before any valid WAL segment".
4558 checkPoint.redo = wal_segment_size + SizeOfXLogLongPHD;
4559 checkPoint.ThisTimeLineID = BootstrapTimeLineID;
4560 checkPoint.PrevTimeLineID = BootstrapTimeLineID;
4561 checkPoint.fullPageWrites = fullPageWrites;
4562 checkPoint.nextXid =
4563 FullTransactionIdFromEpochAndXid(0, FirstNormalTransactionId);
4564 checkPoint.nextOid = FirstGenbkiObjectId;
4565 checkPoint.nextMulti = FirstMultiXactId;
4566 checkPoint.nextMultiOffset = 0;
4567 checkPoint.oldestXid = FirstNormalTransactionId;
4568 checkPoint.oldestXidDB = TemplateDbOid;
4569 checkPoint.oldestMulti = FirstMultiXactId;
4570 checkPoint.oldestMultiDB = TemplateDbOid;
4571 checkPoint.oldestCommitTsXid = InvalidTransactionId;
4572 checkPoint.newestCommitTsXid = InvalidTransactionId;
4573 checkPoint.time = (pg_time_t) time(NULL);
4574 checkPoint.oldestActiveXid = InvalidTransactionId;
4576 ShmemVariableCache->nextXid = checkPoint.nextXid;
4577 ShmemVariableCache->nextOid = checkPoint.nextOid;
4578 ShmemVariableCache->oidCount = 0;
4579 MultiXactSetNextMXact(checkPoint.nextMulti, checkPoint.nextMultiOffset);
4580 AdvanceOldestClogXid(checkPoint.oldestXid);
4581 SetTransactionIdLimit(checkPoint.oldestXid, checkPoint.oldestXidDB);
4582 SetMultiXactIdLimit(checkPoint.oldestMulti, checkPoint.oldestMultiDB, true);
4583 SetCommitTsLimit(InvalidTransactionId, InvalidTransactionId);
4585 /* Set up the XLOG page header */
4586 page->xlp_magic = XLOG_PAGE_MAGIC;
4587 page->xlp_info = XLP_LONG_HEADER;
4588 page->xlp_tli = BootstrapTimeLineID;
4589 page->xlp_pageaddr = wal_segment_size;
4590 longpage = (XLogLongPageHeader) page;
4591 longpage->xlp_sysid = sysidentifier;
4592 longpage->xlp_seg_size = wal_segment_size;
4593 longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
4595 /* Insert the initial checkpoint record */
4596 recptr = ((char *) page + SizeOfXLogLongPHD);
4597 record = (XLogRecord *) recptr;
4598 record->xl_prev = 0;
4599 record->xl_xid = InvalidTransactionId;
4600 record->xl_tot_len = SizeOfXLogRecord + SizeOfXLogRecordDataHeaderShort + sizeof(checkPoint);
4601 record->xl_info = XLOG_CHECKPOINT_SHUTDOWN;
4602 record->xl_rmid = RM_XLOG_ID;
4603 recptr += SizeOfXLogRecord;
4604 /* fill the XLogRecordDataHeaderShort struct */
4605 *(recptr++) = (char) XLR_BLOCK_ID_DATA_SHORT;
4606 *(recptr++) = sizeof(checkPoint);
4607 memcpy(recptr, &checkPoint, sizeof(checkPoint));
4608 recptr += sizeof(checkPoint);
4609 Assert(recptr - (char *) record == record->xl_tot_len);
4611 INIT_CRC32C(crc);
4612 COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
4613 COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
4614 FIN_CRC32C(crc);
4615 record->xl_crc = crc;
4617 /* Create first XLOG segment file */
4618 openLogTLI = BootstrapTimeLineID;
4619 openLogFile = XLogFileInit(1, BootstrapTimeLineID);
4622 * We needn't bother with Reserve/ReleaseExternalFD here, since we'll
4623 * close the file again in a moment.
4626 /* Write the first page with the initial record */
4627 errno = 0;
4628 pgstat_report_wait_start(WAIT_EVENT_WAL_BOOTSTRAP_WRITE);
4629 if (write(openLogFile, page, XLOG_BLCKSZ) != XLOG_BLCKSZ)
4631 /* if write didn't set errno, assume problem is no disk space */
4632 if (errno == 0)
4633 errno = ENOSPC;
4634 ereport(PANIC,
4635 (errcode_for_file_access(),
4636 errmsg("could not write bootstrap write-ahead log file: %m")));
4638 pgstat_report_wait_end();
4640 pgstat_report_wait_start(WAIT_EVENT_WAL_BOOTSTRAP_SYNC);
4641 if (pg_fsync(openLogFile) != 0)
4642 ereport(PANIC,
4643 (errcode_for_file_access(),
4644 errmsg("could not fsync bootstrap write-ahead log file: %m")));
4645 pgstat_report_wait_end();
4647 if (close(openLogFile) != 0)
4648 ereport(PANIC,
4649 (errcode_for_file_access(),
4650 errmsg("could not close bootstrap write-ahead log file: %m")));
4652 openLogFile = -1;
4654 /* Now create pg_control */
4655 InitControlFile(sysidentifier);
4656 ControlFile->time = checkPoint.time;
4657 ControlFile->checkPoint = checkPoint.redo;
4658 ControlFile->checkPointCopy = checkPoint;
4660 /* some additional ControlFile fields are set in WriteControlFile() */
4661 WriteControlFile();
4663 /* Bootstrap the commit log, too */
4664 BootStrapCLOG();
4665 BootStrapCommitTs();
4666 BootStrapSUBTRANS();
4667 BootStrapMultiXact();
4669 pfree(buffer);
4672 * Force control file to be read - in contrast to normal processing we'd
4673 * otherwise never run the checks and GUC related initializations therein.
4675 ReadControlFile();
4678 static char *
4679 str_time(pg_time_t tnow)
4681 static char buf[128];
4683 pg_strftime(buf, sizeof(buf),
4684 "%Y-%m-%d %H:%M:%S %Z",
4685 pg_localtime(&tnow, log_timezone));
4687 return buf;
4691 * Initialize the first WAL segment on new timeline.
4693 static void
4694 XLogInitNewTimeline(TimeLineID endTLI, XLogRecPtr endOfLog, TimeLineID newTLI)
4696 char xlogfname[MAXFNAMELEN];
4697 XLogSegNo endLogSegNo;
4698 XLogSegNo startLogSegNo;
4700 /* we always switch to a new timeline after archive recovery */
4701 Assert(endTLI != newTLI);
4704 * Update min recovery point one last time.
4706 UpdateMinRecoveryPoint(InvalidXLogRecPtr, true);
4709 * Calculate the last segment on the old timeline, and the first segment
4710 * on the new timeline. If the switch happens in the middle of a segment,
4711 * they are the same, but if the switch happens exactly at a segment
4712 * boundary, startLogSegNo will be endLogSegNo + 1.
4714 XLByteToPrevSeg(endOfLog, endLogSegNo, wal_segment_size);
4715 XLByteToSeg(endOfLog, startLogSegNo, wal_segment_size);
4718 * Initialize the starting WAL segment for the new timeline. If the switch
4719 * happens in the middle of a segment, copy data from the last WAL segment
4720 * of the old timeline up to the switch point, to the starting WAL segment
4721 * on the new timeline.
4723 if (endLogSegNo == startLogSegNo)
4726 * Make a copy of the file on the new timeline.
4728 * Writing WAL isn't allowed yet, so there are no locking
4729 * considerations. But we should be just as tense as XLogFileInit to
4730 * avoid emplacing a bogus file.
4732 XLogFileCopy(newTLI, endLogSegNo, endTLI, endLogSegNo,
4733 XLogSegmentOffset(endOfLog, wal_segment_size));
4735 else
4738 * The switch happened at a segment boundary, so just create the next
4739 * segment on the new timeline.
4741 int fd;
4743 fd = XLogFileInit(startLogSegNo, newTLI);
4745 if (close(fd) != 0)
4747 char xlogfname[MAXFNAMELEN];
4748 int save_errno = errno;
4750 XLogFileName(xlogfname, newTLI, startLogSegNo, wal_segment_size);
4751 errno = save_errno;
4752 ereport(ERROR,
4753 (errcode_for_file_access(),
4754 errmsg("could not close file \"%s\": %m", xlogfname)));
4759 * Let's just make real sure there are not .ready or .done flags posted
4760 * for the new segment.
4762 XLogFileName(xlogfname, newTLI, startLogSegNo, wal_segment_size);
4763 XLogArchiveCleanup(xlogfname);
4767 * Perform cleanup actions at the conclusion of archive recovery.
4769 static void
4770 CleanupAfterArchiveRecovery(TimeLineID EndOfLogTLI, XLogRecPtr EndOfLog,
4771 TimeLineID newTLI)
4774 * Execute the recovery_end_command, if any.
4776 if (recoveryEndCommand && strcmp(recoveryEndCommand, "") != 0)
4777 ExecuteRecoveryCommand(recoveryEndCommand,
4778 "recovery_end_command",
4779 true,
4780 WAIT_EVENT_RECOVERY_END_COMMAND);
4783 * We switched to a new timeline. Clean up segments on the old timeline.
4785 * If there are any higher-numbered segments on the old timeline, remove
4786 * them. They might contain valid WAL, but they might also be
4787 * pre-allocated files containing garbage. In any case, they are not part
4788 * of the new timeline's history so we don't need them.
4790 RemoveNonParentXlogFiles(EndOfLog, newTLI);
4793 * If the switch happened in the middle of a segment, what to do with the
4794 * last, partial segment on the old timeline? If we don't archive it, and
4795 * the server that created the WAL never archives it either (e.g. because
4796 * it was hit by a meteor), it will never make it to the archive. That's
4797 * OK from our point of view, because the new segment that we created with
4798 * the new TLI contains all the WAL from the old timeline up to the switch
4799 * point. But if you later try to do PITR to the "missing" WAL on the old
4800 * timeline, recovery won't find it in the archive. It's physically
4801 * present in the new file with new TLI, but recovery won't look there
4802 * when it's recovering to the older timeline. On the other hand, if we
4803 * archive the partial segment, and the original server on that timeline
4804 * is still running and archives the completed version of the same segment
4805 * later, it will fail. (We used to do that in 9.4 and below, and it
4806 * caused such problems).
4808 * As a compromise, we rename the last segment with the .partial suffix,
4809 * and archive it. Archive recovery will never try to read .partial
4810 * segments, so they will normally go unused. But in the odd PITR case,
4811 * the administrator can copy them manually to the pg_wal directory
4812 * (removing the suffix). They can be useful in debugging, too.
4814 * If a .done or .ready file already exists for the old timeline, however,
4815 * we had already determined that the segment is complete, so we can let
4816 * it be archived normally. (In particular, if it was restored from the
4817 * archive to begin with, it's expected to have a .done file).
4819 if (XLogSegmentOffset(EndOfLog, wal_segment_size) != 0 &&
4820 XLogArchivingActive())
4822 char origfname[MAXFNAMELEN];
4823 XLogSegNo endLogSegNo;
4825 XLByteToPrevSeg(EndOfLog, endLogSegNo, wal_segment_size);
4826 XLogFileName(origfname, EndOfLogTLI, endLogSegNo, wal_segment_size);
4828 if (!XLogArchiveIsReadyOrDone(origfname))
4830 char origpath[MAXPGPATH];
4831 char partialfname[MAXFNAMELEN];
4832 char partialpath[MAXPGPATH];
4834 XLogFilePath(origpath, EndOfLogTLI, endLogSegNo, wal_segment_size);
4835 snprintf(partialfname, MAXFNAMELEN, "%s.partial", origfname);
4836 snprintf(partialpath, MAXPGPATH, "%s.partial", origpath);
4839 * Make sure there's no .done or .ready file for the .partial
4840 * file.
4842 XLogArchiveCleanup(partialfname);
4844 durable_rename(origpath, partialpath, ERROR);
4845 XLogArchiveNotify(partialfname);
4851 * Check to see if required parameters are set high enough on this server
4852 * for various aspects of recovery operation.
4854 * Note that all the parameters which this function tests need to be
4855 * listed in Administrator's Overview section in high-availability.sgml.
4856 * If you change them, don't forget to update the list.
4858 static void
4859 CheckRequiredParameterValues(void)
4862 * For archive recovery, the WAL must be generated with at least 'replica'
4863 * wal_level.
4865 if (ArchiveRecoveryRequested && ControlFile->wal_level == WAL_LEVEL_MINIMAL)
4867 ereport(FATAL,
4868 (errmsg("WAL was generated with wal_level=minimal, cannot continue recovering"),
4869 errdetail("This happens if you temporarily set wal_level=minimal on the server."),
4870 errhint("Use a backup taken after setting wal_level to higher than minimal.")));
4874 * For Hot Standby, the WAL must be generated with 'replica' mode, and we
4875 * must have at least as many backend slots as the primary.
4877 if (ArchiveRecoveryRequested && EnableHotStandby)
4879 /* We ignore autovacuum_max_workers when we make this test. */
4880 RecoveryRequiresIntParameter("max_connections",
4881 MaxConnections,
4882 ControlFile->MaxConnections);
4883 RecoveryRequiresIntParameter("max_worker_processes",
4884 max_worker_processes,
4885 ControlFile->max_worker_processes);
4886 RecoveryRequiresIntParameter("max_wal_senders",
4887 max_wal_senders,
4888 ControlFile->max_wal_senders);
4889 RecoveryRequiresIntParameter("max_prepared_transactions",
4890 max_prepared_xacts,
4891 ControlFile->max_prepared_xacts);
4892 RecoveryRequiresIntParameter("max_locks_per_transaction",
4893 max_locks_per_xact,
4894 ControlFile->max_locks_per_xact);
4899 * This must be called ONCE during postmaster or standalone-backend startup
4901 void
4902 StartupXLOG(void)
4904 XLogCtlInsert *Insert;
4905 CheckPoint checkPoint;
4906 bool wasShutdown;
4907 bool haveTblspcMap;
4908 bool haveBackupLabel;
4909 XLogRecPtr EndOfLog;
4910 TimeLineID EndOfLogTLI;
4911 TimeLineID newTLI;
4912 bool performedWalRecovery;
4913 EndOfWalRecoveryInfo *endOfRecoveryInfo;
4914 XLogRecPtr abortedRecPtr;
4915 XLogRecPtr missingContrecPtr;
4916 TransactionId oldestActiveXID;
4917 bool promoted = false;
4920 * We should have an aux process resource owner to use, and we should not
4921 * be in a transaction that's installed some other resowner.
4923 Assert(AuxProcessResourceOwner != NULL);
4924 Assert(CurrentResourceOwner == NULL ||
4925 CurrentResourceOwner == AuxProcessResourceOwner);
4926 CurrentResourceOwner = AuxProcessResourceOwner;
4929 * Check that contents look valid.
4931 if (!XRecOffIsValid(ControlFile->checkPoint))
4932 ereport(FATAL,
4933 (errmsg("control file contains invalid checkpoint location")));
4935 switch (ControlFile->state)
4937 case DB_SHUTDOWNED:
4940 * This is the expected case, so don't be chatty in standalone
4941 * mode
4943 ereport(IsPostmasterEnvironment ? LOG : NOTICE,
4944 (errmsg("database system was shut down at %s",
4945 str_time(ControlFile->time))));
4946 break;
4948 case DB_SHUTDOWNED_IN_RECOVERY:
4949 ereport(LOG,
4950 (errmsg("database system was shut down in recovery at %s",
4951 str_time(ControlFile->time))));
4952 break;
4954 case DB_SHUTDOWNING:
4955 ereport(LOG,
4956 (errmsg("database system shutdown was interrupted; last known up at %s",
4957 str_time(ControlFile->time))));
4958 break;
4960 case DB_IN_CRASH_RECOVERY:
4961 ereport(LOG,
4962 (errmsg("database system was interrupted while in recovery at %s",
4963 str_time(ControlFile->time)),
4964 errhint("This probably means that some data is corrupted and"
4965 " you will have to use the last backup for recovery.")));
4966 break;
4968 case DB_IN_ARCHIVE_RECOVERY:
4969 ereport(LOG,
4970 (errmsg("database system was interrupted while in recovery at log time %s",
4971 str_time(ControlFile->checkPointCopy.time)),
4972 errhint("If this has occurred more than once some data might be corrupted"
4973 " and you might need to choose an earlier recovery target.")));
4974 break;
4976 case DB_IN_PRODUCTION:
4977 ereport(LOG,
4978 (errmsg("database system was interrupted; last known up at %s",
4979 str_time(ControlFile->time))));
4980 break;
4982 default:
4983 ereport(FATAL,
4984 (errmsg("control file contains invalid database cluster state")));
4987 /* This is just to allow attaching to startup process with a debugger */
4988 #ifdef XLOG_REPLAY_DELAY
4989 if (ControlFile->state != DB_SHUTDOWNED)
4990 pg_usleep(60000000L);
4991 #endif
4994 * Verify that pg_wal and pg_wal/archive_status exist. In cases where
4995 * someone has performed a copy for PITR, these directories may have been
4996 * excluded and need to be re-created.
4998 ValidateXLOGDirectoryStructure();
5000 /* Set up timeout handler needed to report startup progress. */
5001 if (!IsBootstrapProcessingMode())
5002 RegisterTimeout(STARTUP_PROGRESS_TIMEOUT,
5003 startup_progress_timeout_handler);
5005 /*----------
5006 * If we previously crashed, perform a couple of actions:
5008 * - The pg_wal directory may still include some temporary WAL segments
5009 * used when creating a new segment, so perform some clean up to not
5010 * bloat this path. This is done first as there is no point to sync
5011 * this temporary data.
5013 * - There might be data which we had written, intending to fsync it, but
5014 * which we had not actually fsync'd yet. Therefore, a power failure in
5015 * the near future might cause earlier unflushed writes to be lost, even
5016 * though more recent data written to disk from here on would be
5017 * persisted. To avoid that, fsync the entire data directory.
5019 if (ControlFile->state != DB_SHUTDOWNED &&
5020 ControlFile->state != DB_SHUTDOWNED_IN_RECOVERY)
5022 RemoveTempXlogFiles();
5023 SyncDataDirectory();
5027 * Prepare for WAL recovery if needed.
5029 * InitWalRecovery analyzes the control file and the backup label file, if
5030 * any. It updates the in-memory ControlFile buffer according to the
5031 * starting checkpoint, and sets InRecovery and ArchiveRecoveryRequested.
5032 * It also applies the tablespace map file, if any.
5034 InitWalRecovery(ControlFile, &wasShutdown,
5035 &haveBackupLabel, &haveTblspcMap);
5036 checkPoint = ControlFile->checkPointCopy;
5038 /* initialize shared memory variables from the checkpoint record */
5039 ShmemVariableCache->nextXid = checkPoint.nextXid;
5040 ShmemVariableCache->nextOid = checkPoint.nextOid;
5041 ShmemVariableCache->oidCount = 0;
5042 MultiXactSetNextMXact(checkPoint.nextMulti, checkPoint.nextMultiOffset);
5043 AdvanceOldestClogXid(checkPoint.oldestXid);
5044 SetTransactionIdLimit(checkPoint.oldestXid, checkPoint.oldestXidDB);
5045 SetMultiXactIdLimit(checkPoint.oldestMulti, checkPoint.oldestMultiDB, true);
5046 SetCommitTsLimit(checkPoint.oldestCommitTsXid,
5047 checkPoint.newestCommitTsXid);
5048 XLogCtl->ckptFullXid = checkPoint.nextXid;
5051 * Clear out any old relcache cache files. This is *necessary* if we do
5052 * any WAL replay, since that would probably result in the cache files
5053 * being out of sync with database reality. In theory we could leave them
5054 * in place if the database had been cleanly shut down, but it seems
5055 * safest to just remove them always and let them be rebuilt during the
5056 * first backend startup. These files needs to be removed from all
5057 * directories including pg_tblspc, however the symlinks are created only
5058 * after reading tablespace_map file in case of archive recovery from
5059 * backup, so needs to clear old relcache files here after creating
5060 * symlinks.
5062 RelationCacheInitFileRemove();
5065 * Initialize replication slots, before there's a chance to remove
5066 * required resources.
5068 StartupReplicationSlots();
5071 * Startup logical state, needs to be setup now so we have proper data
5072 * during crash recovery.
5074 StartupReorderBuffer();
5077 * Startup CLOG. This must be done after ShmemVariableCache->nextXid has
5078 * been initialized and before we accept connections or begin WAL replay.
5080 StartupCLOG();
5083 * Startup MultiXact. We need to do this early to be able to replay
5084 * truncations.
5086 StartupMultiXact();
5089 * Ditto for commit timestamps. Activate the facility if the setting is
5090 * enabled in the control file, as there should be no tracking of commit
5091 * timestamps done when the setting was disabled. This facility can be
5092 * started or stopped when replaying a XLOG_PARAMETER_CHANGE record.
5094 if (ControlFile->track_commit_timestamp)
5095 StartupCommitTs();
5098 * Recover knowledge about replay progress of known replication partners.
5100 StartupReplicationOrigin();
5103 * Initialize unlogged LSN. On a clean shutdown, it's restored from the
5104 * control file. On recovery, all unlogged relations are blown away, so
5105 * the unlogged LSN counter can be reset too.
5107 if (ControlFile->state == DB_SHUTDOWNED)
5108 XLogCtl->unloggedLSN = ControlFile->unloggedLSN;
5109 else
5110 XLogCtl->unloggedLSN = FirstNormalUnloggedLSN;
5113 * Copy any missing timeline history files between 'now' and the recovery
5114 * target timeline from archive to pg_wal. While we don't need those files
5115 * ourselves - the history file of the recovery target timeline covers all
5116 * the previous timelines in the history too - a cascading standby server
5117 * might be interested in them. Or, if you archive the WAL from this
5118 * server to a different archive than the primary, it'd be good for all
5119 * the history files to get archived there after failover, so that you can
5120 * use one of the old timelines as a PITR target. Timeline history files
5121 * are small, so it's better to copy them unnecessarily than not copy them
5122 * and regret later.
5124 restoreTimeLineHistoryFiles(checkPoint.ThisTimeLineID, recoveryTargetTLI);
5127 * Before running in recovery, scan pg_twophase and fill in its status to
5128 * be able to work on entries generated by redo. Doing a scan before
5129 * taking any recovery action has the merit to discard any 2PC files that
5130 * are newer than the first record to replay, saving from any conflicts at
5131 * replay. This avoids as well any subsequent scans when doing recovery
5132 * of the on-disk two-phase data.
5134 restoreTwoPhaseData();
5136 lastFullPageWrites = checkPoint.fullPageWrites;
5138 RedoRecPtr = XLogCtl->RedoRecPtr = XLogCtl->Insert.RedoRecPtr = checkPoint.redo;
5139 doPageWrites = lastFullPageWrites;
5141 /* REDO */
5142 if (InRecovery)
5144 /* Initialize state for RecoveryInProgress() */
5145 SpinLockAcquire(&XLogCtl->info_lck);
5146 if (InArchiveRecovery)
5147 XLogCtl->SharedRecoveryState = RECOVERY_STATE_ARCHIVE;
5148 else
5149 XLogCtl->SharedRecoveryState = RECOVERY_STATE_CRASH;
5150 SpinLockRelease(&XLogCtl->info_lck);
5153 * Update pg_control to show that we are recovering and to show the
5154 * selected checkpoint as the place we are starting from. We also mark
5155 * pg_control with any minimum recovery stop point obtained from a
5156 * backup history file.
5158 * No need to hold ControlFileLock yet, we aren't up far enough.
5160 UpdateControlFile();
5163 * If there was a backup label file, it's done its job and the info
5164 * has now been propagated into pg_control. We must get rid of the
5165 * label file so that if we crash during recovery, we'll pick up at
5166 * the latest recovery restartpoint instead of going all the way back
5167 * to the backup start point. It seems prudent though to just rename
5168 * the file out of the way rather than delete it completely.
5170 if (haveBackupLabel)
5172 unlink(BACKUP_LABEL_OLD);
5173 durable_rename(BACKUP_LABEL_FILE, BACKUP_LABEL_OLD, FATAL);
5177 * If there was a tablespace_map file, it's done its job and the
5178 * symlinks have been created. We must get rid of the map file so
5179 * that if we crash during recovery, we don't create symlinks again.
5180 * It seems prudent though to just rename the file out of the way
5181 * rather than delete it completely.
5183 if (haveTblspcMap)
5185 unlink(TABLESPACE_MAP_OLD);
5186 durable_rename(TABLESPACE_MAP, TABLESPACE_MAP_OLD, FATAL);
5190 * Initialize our local copy of minRecoveryPoint. When doing crash
5191 * recovery we want to replay up to the end of WAL. Particularly, in
5192 * the case of a promoted standby minRecoveryPoint value in the
5193 * control file is only updated after the first checkpoint. However,
5194 * if the instance crashes before the first post-recovery checkpoint
5195 * is completed then recovery will use a stale location causing the
5196 * startup process to think that there are still invalid page
5197 * references when checking for data consistency.
5199 if (InArchiveRecovery)
5201 LocalMinRecoveryPoint = ControlFile->minRecoveryPoint;
5202 LocalMinRecoveryPointTLI = ControlFile->minRecoveryPointTLI;
5204 else
5206 LocalMinRecoveryPoint = InvalidXLogRecPtr;
5207 LocalMinRecoveryPointTLI = 0;
5211 * Reset pgstat data, because it may be invalid after recovery.
5213 pgstat_reset_all();
5215 /* Check that the GUCs used to generate the WAL allow recovery */
5216 CheckRequiredParameterValues();
5219 * We're in recovery, so unlogged relations may be trashed and must be
5220 * reset. This should be done BEFORE allowing Hot Standby
5221 * connections, so that read-only backends don't try to read whatever
5222 * garbage is left over from before.
5224 ResetUnloggedRelations(UNLOGGED_RELATION_CLEANUP);
5227 * Likewise, delete any saved transaction snapshot files that got left
5228 * behind by crashed backends.
5230 DeleteAllExportedSnapshotFiles();
5233 * Initialize for Hot Standby, if enabled. We won't let backends in
5234 * yet, not until we've reached the min recovery point specified in
5235 * control file and we've established a recovery snapshot from a
5236 * running-xacts WAL record.
5238 if (ArchiveRecoveryRequested && EnableHotStandby)
5240 TransactionId *xids;
5241 int nxids;
5243 ereport(DEBUG1,
5244 (errmsg_internal("initializing for hot standby")));
5246 InitRecoveryTransactionEnvironment();
5248 if (wasShutdown)
5249 oldestActiveXID = PrescanPreparedTransactions(&xids, &nxids);
5250 else
5251 oldestActiveXID = checkPoint.oldestActiveXid;
5252 Assert(TransactionIdIsValid(oldestActiveXID));
5254 /* Tell procarray about the range of xids it has to deal with */
5255 ProcArrayInitRecovery(XidFromFullTransactionId(ShmemVariableCache->nextXid));
5258 * Startup subtrans only. CLOG, MultiXact and commit timestamp
5259 * have already been started up and other SLRUs are not maintained
5260 * during recovery and need not be started yet.
5262 StartupSUBTRANS(oldestActiveXID);
5265 * If we're beginning at a shutdown checkpoint, we know that
5266 * nothing was running on the primary at this point. So fake-up an
5267 * empty running-xacts record and use that here and now. Recover
5268 * additional standby state for prepared transactions.
5270 if (wasShutdown)
5272 RunningTransactionsData running;
5273 TransactionId latestCompletedXid;
5276 * Construct a RunningTransactions snapshot representing a
5277 * shut down server, with only prepared transactions still
5278 * alive. We're never overflowed at this point because all
5279 * subxids are listed with their parent prepared transactions.
5281 running.xcnt = nxids;
5282 running.subxcnt = 0;
5283 running.subxid_overflow = false;
5284 running.nextXid = XidFromFullTransactionId(checkPoint.nextXid);
5285 running.oldestRunningXid = oldestActiveXID;
5286 latestCompletedXid = XidFromFullTransactionId(checkPoint.nextXid);
5287 TransactionIdRetreat(latestCompletedXid);
5288 Assert(TransactionIdIsNormal(latestCompletedXid));
5289 running.latestCompletedXid = latestCompletedXid;
5290 running.xids = xids;
5292 ProcArrayApplyRecoveryInfo(&running);
5294 StandbyRecoverPreparedTransactions();
5299 * We're all set for replaying the WAL now. Do it.
5301 PerformWalRecovery();
5302 performedWalRecovery = true;
5304 else
5305 performedWalRecovery = false;
5308 * Finish WAL recovery.
5310 endOfRecoveryInfo = FinishWalRecovery();
5311 EndOfLog = endOfRecoveryInfo->endOfLog;
5312 EndOfLogTLI = endOfRecoveryInfo->endOfLogTLI;
5313 abortedRecPtr = endOfRecoveryInfo->abortedRecPtr;
5314 missingContrecPtr = endOfRecoveryInfo->missingContrecPtr;
5317 * Complain if we did not roll forward far enough to render the backup
5318 * dump consistent. Note: it is indeed okay to look at the local variable
5319 * LocalMinRecoveryPoint here, even though ControlFile->minRecoveryPoint
5320 * might be further ahead --- ControlFile->minRecoveryPoint cannot have
5321 * been advanced beyond the WAL we processed.
5323 if (InRecovery &&
5324 (EndOfLog < LocalMinRecoveryPoint ||
5325 !XLogRecPtrIsInvalid(ControlFile->backupStartPoint)))
5328 * Ran off end of WAL before reaching end-of-backup WAL record, or
5329 * minRecoveryPoint. That's usually a bad sign, indicating that you
5330 * tried to recover from an online backup but never called
5331 * pg_stop_backup(), or you didn't archive all the WAL up to that
5332 * point. However, this also happens in crash recovery, if the system
5333 * crashes while an online backup is in progress. We must not treat
5334 * that as an error, or the database will refuse to start up.
5336 if (ArchiveRecoveryRequested || ControlFile->backupEndRequired)
5338 if (ControlFile->backupEndRequired)
5339 ereport(FATAL,
5340 (errmsg("WAL ends before end of online backup"),
5341 errhint("All WAL generated while online backup was taken must be available at recovery.")));
5342 else if (!XLogRecPtrIsInvalid(ControlFile->backupStartPoint))
5343 ereport(FATAL,
5344 (errmsg("WAL ends before end of online backup"),
5345 errhint("Online backup started with pg_start_backup() must be ended with pg_stop_backup(), and all WAL up to that point must be available at recovery.")));
5346 else
5347 ereport(FATAL,
5348 (errmsg("WAL ends before consistent recovery point")));
5353 * Reset unlogged relations to the contents of their INIT fork. This is
5354 * done AFTER recovery is complete so as to include any unlogged relations
5355 * created during recovery, but BEFORE recovery is marked as having
5356 * completed successfully. Otherwise we'd not retry if any of the post
5357 * end-of-recovery steps fail.
5359 if (InRecovery)
5360 ResetUnloggedRelations(UNLOGGED_RELATION_INIT);
5363 * Pre-scan prepared transactions to find out the range of XIDs present.
5364 * This information is not quite needed yet, but it is positioned here so
5365 * as potential problems are detected before any on-disk change is done.
5367 oldestActiveXID = PrescanPreparedTransactions(NULL, NULL);
5370 * Allow ordinary WAL segment creation before possibly switching to a new
5371 * timeline, which creates a new segment, and after the last ReadRecord().
5373 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
5374 XLogCtl->InstallXLogFileSegmentActive = true;
5375 LWLockRelease(ControlFileLock);
5378 * Consider whether we need to assign a new timeline ID.
5380 * If we did archive recovery, we always assign a new ID. This handles a
5381 * couple of issues. If we stopped short of the end of WAL during
5382 * recovery, then we are clearly generating a new timeline and must assign
5383 * it a unique new ID. Even if we ran to the end, modifying the current
5384 * last segment is problematic because it may result in trying to
5385 * overwrite an already-archived copy of that segment, and we encourage
5386 * DBAs to make their archive_commands reject that. We can dodge the
5387 * problem by making the new active segment have a new timeline ID.
5389 * In a normal crash recovery, we can just extend the timeline we were in.
5391 newTLI = endOfRecoveryInfo->lastRecTLI;
5392 if (ArchiveRecoveryRequested)
5394 newTLI = findNewestTimeLine(recoveryTargetTLI) + 1;
5395 ereport(LOG,
5396 (errmsg("selected new timeline ID: %u", newTLI)));
5399 * Make a writable copy of the last WAL segment. (Note that we also
5400 * have a copy of the last block of the old WAL in
5401 * endOfRecovery->lastPage; we will use that below.)
5403 XLogInitNewTimeline(EndOfLogTLI, EndOfLog, newTLI);
5406 * Remove the signal files out of the way, so that we don't
5407 * accidentally re-enter archive recovery mode in a subsequent crash.
5409 if (endOfRecoveryInfo->standby_signal_file_found)
5410 durable_unlink(STANDBY_SIGNAL_FILE, FATAL);
5412 if (endOfRecoveryInfo->recovery_signal_file_found)
5413 durable_unlink(RECOVERY_SIGNAL_FILE, FATAL);
5416 * Write the timeline history file, and have it archived. After this
5417 * point (or rather, as soon as the file is archived), the timeline
5418 * will appear as "taken" in the WAL archive and to any standby
5419 * servers. If we crash before actually switching to the new
5420 * timeline, standby servers will nevertheless think that we switched
5421 * to the new timeline, and will try to connect to the new timeline.
5422 * To minimize the window for that, try to do as little as possible
5423 * between here and writing the end-of-recovery record.
5425 writeTimeLineHistory(newTLI, recoveryTargetTLI,
5426 EndOfLog, endOfRecoveryInfo->recoveryStopReason);
5428 ereport(LOG,
5429 (errmsg("archive recovery complete")));
5432 /* Save the selected TimeLineID in shared memory, too */
5433 XLogCtl->InsertTimeLineID = newTLI;
5434 XLogCtl->PrevTimeLineID = endOfRecoveryInfo->lastRecTLI;
5437 * Actually, if WAL ended in an incomplete record, skip the parts that
5438 * made it through and start writing after the portion that persisted.
5439 * (It's critical to first write an OVERWRITE_CONTRECORD message, which
5440 * we'll do as soon as we're open for writing new WAL.)
5442 if (!XLogRecPtrIsInvalid(missingContrecPtr))
5444 Assert(!XLogRecPtrIsInvalid(abortedRecPtr));
5445 EndOfLog = missingContrecPtr;
5449 * Prepare to write WAL starting at EndOfLog location, and init xlog
5450 * buffer cache using the block containing the last record from the
5451 * previous incarnation.
5453 Insert = &XLogCtl->Insert;
5454 Insert->PrevBytePos = XLogRecPtrToBytePos(endOfRecoveryInfo->lastRec);
5455 Insert->CurrBytePos = XLogRecPtrToBytePos(EndOfLog);
5458 * Tricky point here: lastPage contains the *last* block that the LastRec
5459 * record spans, not the one it starts in. The last block is indeed the
5460 * one we want to use.
5462 if (EndOfLog % XLOG_BLCKSZ != 0)
5464 char *page;
5465 int len;
5466 int firstIdx;
5468 firstIdx = XLogRecPtrToBufIdx(EndOfLog);
5469 len = EndOfLog - endOfRecoveryInfo->lastPageBeginPtr;
5470 Assert(len < XLOG_BLCKSZ);
5472 /* Copy the valid part of the last block, and zero the rest */
5473 page = &XLogCtl->pages[firstIdx * XLOG_BLCKSZ];
5474 memcpy(page, endOfRecoveryInfo->lastPage, len);
5475 memset(page + len, 0, XLOG_BLCKSZ - len);
5477 XLogCtl->xlblocks[firstIdx] = endOfRecoveryInfo->lastPageBeginPtr + XLOG_BLCKSZ;
5478 XLogCtl->InitializedUpTo = endOfRecoveryInfo->lastPageBeginPtr + XLOG_BLCKSZ;
5480 else
5483 * There is no partial block to copy. Just set InitializedUpTo, and
5484 * let the first attempt to insert a log record to initialize the next
5485 * buffer.
5487 XLogCtl->InitializedUpTo = EndOfLog;
5490 LogwrtResult.Write = LogwrtResult.Flush = EndOfLog;
5492 XLogCtl->LogwrtResult = LogwrtResult;
5494 XLogCtl->LogwrtRqst.Write = EndOfLog;
5495 XLogCtl->LogwrtRqst.Flush = EndOfLog;
5498 * Preallocate additional log files, if wanted.
5500 PreallocXlogFiles(EndOfLog, newTLI);
5503 * Okay, we're officially UP.
5505 InRecovery = false;
5507 /* start the archive_timeout timer and LSN running */
5508 XLogCtl->lastSegSwitchTime = (pg_time_t) time(NULL);
5509 XLogCtl->lastSegSwitchLSN = EndOfLog;
5511 /* also initialize latestCompletedXid, to nextXid - 1 */
5512 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
5513 ShmemVariableCache->latestCompletedXid = ShmemVariableCache->nextXid;
5514 FullTransactionIdRetreat(&ShmemVariableCache->latestCompletedXid);
5515 LWLockRelease(ProcArrayLock);
5518 * Start up subtrans, if not already done for hot standby. (commit
5519 * timestamps are started below, if necessary.)
5521 if (standbyState == STANDBY_DISABLED)
5522 StartupSUBTRANS(oldestActiveXID);
5525 * Perform end of recovery actions for any SLRUs that need it.
5527 TrimCLOG();
5528 TrimMultiXact();
5530 /* Reload shared-memory state for prepared transactions */
5531 RecoverPreparedTransactions();
5533 /* Shut down xlogreader */
5534 ShutdownWalRecovery();
5536 /* Enable WAL writes for this backend only. */
5537 LocalSetXLogInsertAllowed();
5539 /* If necessary, write overwrite-contrecord before doing anything else */
5540 if (!XLogRecPtrIsInvalid(abortedRecPtr))
5542 Assert(!XLogRecPtrIsInvalid(missingContrecPtr));
5543 CreateOverwriteContrecordRecord(abortedRecPtr, missingContrecPtr, newTLI);
5547 * Update full_page_writes in shared memory and write an XLOG_FPW_CHANGE
5548 * record before resource manager writes cleanup WAL records or checkpoint
5549 * record is written.
5551 Insert->fullPageWrites = lastFullPageWrites;
5552 UpdateFullPageWrites();
5555 * Emit checkpoint or end-of-recovery record in XLOG, if required.
5557 if (performedWalRecovery)
5558 promoted = PerformRecoveryXLogAction();
5561 * If any of the critical GUCs have changed, log them before we allow
5562 * backends to write WAL.
5564 XLogReportParameters();
5566 /* If this is archive recovery, perform post-recovery cleanup actions. */
5567 if (ArchiveRecoveryRequested)
5568 CleanupAfterArchiveRecovery(EndOfLogTLI, EndOfLog, newTLI);
5571 * Local WAL inserts enabled, so it's time to finish initialization of
5572 * commit timestamp.
5574 CompleteCommitTsInitialization();
5577 * All done with end-of-recovery actions.
5579 * Now allow backends to write WAL and update the control file status in
5580 * consequence. SharedRecoveryState, that controls if backends can write
5581 * WAL, is updated while holding ControlFileLock to prevent other backends
5582 * to look at an inconsistent state of the control file in shared memory.
5583 * There is still a small window during which backends can write WAL and
5584 * the control file is still referring to a system not in DB_IN_PRODUCTION
5585 * state while looking at the on-disk control file.
5587 * Also, we use info_lck to update SharedRecoveryState to ensure that
5588 * there are no race conditions concerning visibility of other recent
5589 * updates to shared memory.
5591 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
5592 ControlFile->state = DB_IN_PRODUCTION;
5594 SpinLockAcquire(&XLogCtl->info_lck);
5595 XLogCtl->SharedRecoveryState = RECOVERY_STATE_DONE;
5596 SpinLockRelease(&XLogCtl->info_lck);
5598 UpdateControlFile();
5599 LWLockRelease(ControlFileLock);
5602 * Shutdown the recovery environment. This must occur after
5603 * RecoverPreparedTransactions() (see notes in lock_twophase_recover())
5604 * and after switching SharedRecoveryState to RECOVERY_STATE_DONE so as
5605 * any session building a snapshot will not rely on KnownAssignedXids as
5606 * RecoveryInProgress() would return false at this stage. This is
5607 * particularly critical for prepared 2PC transactions, that would still
5608 * need to be included in snapshots once recovery has ended.
5610 if (standbyState != STANDBY_DISABLED)
5611 ShutdownRecoveryTransactionEnvironment();
5614 * If there were cascading standby servers connected to us, nudge any wal
5615 * sender processes to notice that we've been promoted.
5617 WalSndWakeup();
5620 * If this was a promotion, request an (online) checkpoint now. This isn't
5621 * required for consistency, but the last restartpoint might be far back,
5622 * and in case of a crash, recovering from it might take a longer than is
5623 * appropriate now that we're not in standby mode anymore.
5625 if (promoted)
5626 RequestCheckpoint(CHECKPOINT_FORCE);
5630 * Callback from PerformWalRecovery(), called when we switch from crash
5631 * recovery to archive recovery mode. Updates the control file accordingly.
5633 void
5634 SwitchIntoArchiveRecovery(XLogRecPtr EndRecPtr, TimeLineID replayTLI)
5636 /* initialize minRecoveryPoint to this record */
5637 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
5638 ControlFile->state = DB_IN_ARCHIVE_RECOVERY;
5639 if (ControlFile->minRecoveryPoint < EndRecPtr)
5641 ControlFile->minRecoveryPoint = EndRecPtr;
5642 ControlFile->minRecoveryPointTLI = replayTLI;
5644 /* update local copy */
5645 LocalMinRecoveryPoint = ControlFile->minRecoveryPoint;
5646 LocalMinRecoveryPointTLI = ControlFile->minRecoveryPointTLI;
5649 * The startup process can update its local copy of minRecoveryPoint from
5650 * this point.
5652 updateMinRecoveryPoint = true;
5654 UpdateControlFile();
5657 * We update SharedRecoveryState while holding the lock on ControlFileLock
5658 * so both states are consistent in shared memory.
5660 SpinLockAcquire(&XLogCtl->info_lck);
5661 XLogCtl->SharedRecoveryState = RECOVERY_STATE_ARCHIVE;
5662 SpinLockRelease(&XLogCtl->info_lck);
5664 LWLockRelease(ControlFileLock);
5668 * Callback from PerformWalRecovery(), called when we reach the end of backup.
5669 * Updates the control file accordingly.
5671 void
5672 ReachedEndOfBackup(XLogRecPtr EndRecPtr, TimeLineID tli)
5675 * We have reached the end of base backup, as indicated by pg_control. The
5676 * data on disk is now consistent (unless minRecovery point is further
5677 * ahead, which can happen if we crashed during previous recovery). Reset
5678 * backupStartPoint and backupEndPoint, and update minRecoveryPoint to
5679 * make sure we don't allow starting up at an earlier point even if
5680 * recovery is stopped and restarted soon after this.
5682 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
5684 if (ControlFile->minRecoveryPoint < EndRecPtr)
5686 ControlFile->minRecoveryPoint = EndRecPtr;
5687 ControlFile->minRecoveryPointTLI = tli;
5690 ControlFile->backupStartPoint = InvalidXLogRecPtr;
5691 ControlFile->backupEndPoint = InvalidXLogRecPtr;
5692 ControlFile->backupEndRequired = false;
5693 UpdateControlFile();
5695 LWLockRelease(ControlFileLock);
5699 * Perform whatever XLOG actions are necessary at end of REDO.
5701 * The goal here is to make sure that we'll be able to recover properly if
5702 * we crash again. If we choose to write a checkpoint, we'll write a shutdown
5703 * checkpoint rather than an on-line one. This is not particularly critical,
5704 * but since we may be assigning a new TLI, using a shutdown checkpoint allows
5705 * us to have the rule that TLI only changes in shutdown checkpoints, which
5706 * allows some extra error checking in xlog_redo.
5708 static bool
5709 PerformRecoveryXLogAction(void)
5711 bool promoted = false;
5714 * Perform a checkpoint to update all our recovery activity to disk.
5716 * Note that we write a shutdown checkpoint rather than an on-line one.
5717 * This is not particularly critical, but since we may be assigning a new
5718 * TLI, using a shutdown checkpoint allows us to have the rule that TLI
5719 * only changes in shutdown checkpoints, which allows some extra error
5720 * checking in xlog_redo.
5722 * In promotion, only create a lightweight end-of-recovery record instead
5723 * of a full checkpoint. A checkpoint is requested later, after we're
5724 * fully out of recovery mode and already accepting queries.
5726 if (ArchiveRecoveryRequested && IsUnderPostmaster &&
5727 PromoteIsTriggered())
5729 promoted = true;
5732 * Insert a special WAL record to mark the end of recovery, since we
5733 * aren't doing a checkpoint. That means that the checkpointer process
5734 * may likely be in the middle of a time-smoothed restartpoint and
5735 * could continue to be for minutes after this. That sounds strange,
5736 * but the effect is roughly the same and it would be stranger to try
5737 * to come out of the restartpoint and then checkpoint. We request a
5738 * checkpoint later anyway, just for safety.
5740 CreateEndOfRecoveryRecord();
5742 else
5744 RequestCheckpoint(CHECKPOINT_END_OF_RECOVERY |
5745 CHECKPOINT_IMMEDIATE |
5746 CHECKPOINT_WAIT);
5749 return promoted;
5753 * Is the system still in recovery?
5755 * Unlike testing InRecovery, this works in any process that's connected to
5756 * shared memory.
5758 bool
5759 RecoveryInProgress(void)
5762 * We check shared state each time only until we leave recovery mode. We
5763 * can't re-enter recovery, so there's no need to keep checking after the
5764 * shared variable has once been seen false.
5766 if (!LocalRecoveryInProgress)
5767 return false;
5768 else
5771 * use volatile pointer to make sure we make a fresh read of the
5772 * shared variable.
5774 volatile XLogCtlData *xlogctl = XLogCtl;
5776 LocalRecoveryInProgress = (xlogctl->SharedRecoveryState != RECOVERY_STATE_DONE);
5779 * Note: We don't need a memory barrier when we're still in recovery.
5780 * We might exit recovery immediately after return, so the caller
5781 * can't rely on 'true' meaning that we're still in recovery anyway.
5784 return LocalRecoveryInProgress;
5789 * Returns current recovery state from shared memory.
5791 * This returned state is kept consistent with the contents of the control
5792 * file. See details about the possible values of RecoveryState in xlog.h.
5794 RecoveryState
5795 GetRecoveryState(void)
5797 RecoveryState retval;
5799 SpinLockAcquire(&XLogCtl->info_lck);
5800 retval = XLogCtl->SharedRecoveryState;
5801 SpinLockRelease(&XLogCtl->info_lck);
5803 return retval;
5807 * Is this process allowed to insert new WAL records?
5809 * Ordinarily this is essentially equivalent to !RecoveryInProgress().
5810 * But we also have provisions for forcing the result "true" or "false"
5811 * within specific processes regardless of the global state.
5813 bool
5814 XLogInsertAllowed(void)
5817 * If value is "unconditionally true" or "unconditionally false", just
5818 * return it. This provides the normal fast path once recovery is known
5819 * done.
5821 if (LocalXLogInsertAllowed >= 0)
5822 return (bool) LocalXLogInsertAllowed;
5825 * Else, must check to see if we're still in recovery.
5827 if (RecoveryInProgress())
5828 return false;
5831 * On exit from recovery, reset to "unconditionally true", since there is
5832 * no need to keep checking.
5834 LocalXLogInsertAllowed = 1;
5835 return true;
5839 * Make XLogInsertAllowed() return true in the current process only.
5841 * Note: it is allowed to switch LocalXLogInsertAllowed back to -1 later,
5842 * and even call LocalSetXLogInsertAllowed() again after that.
5844 * Returns the previous value of LocalXLogInsertAllowed.
5846 static int
5847 LocalSetXLogInsertAllowed(void)
5849 int oldXLogAllowed = LocalXLogInsertAllowed;
5851 LocalXLogInsertAllowed = 1;
5853 return oldXLogAllowed;
5857 * Return the current Redo pointer from shared memory.
5859 * As a side-effect, the local RedoRecPtr copy is updated.
5861 XLogRecPtr
5862 GetRedoRecPtr(void)
5864 XLogRecPtr ptr;
5867 * The possibly not up-to-date copy in XlogCtl is enough. Even if we
5868 * grabbed a WAL insertion lock to read the authoritative value in
5869 * Insert->RedoRecPtr, someone might update it just after we've released
5870 * the lock.
5872 SpinLockAcquire(&XLogCtl->info_lck);
5873 ptr = XLogCtl->RedoRecPtr;
5874 SpinLockRelease(&XLogCtl->info_lck);
5876 if (RedoRecPtr < ptr)
5877 RedoRecPtr = ptr;
5879 return RedoRecPtr;
5883 * Return information needed to decide whether a modified block needs a
5884 * full-page image to be included in the WAL record.
5886 * The returned values are cached copies from backend-private memory, and
5887 * possibly out-of-date or, indeed, uninitialized, in which case they will
5888 * be InvalidXLogRecPtr and false, respectively. XLogInsertRecord will
5889 * re-check them against up-to-date values, while holding the WAL insert lock.
5891 void
5892 GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p)
5894 *RedoRecPtr_p = RedoRecPtr;
5895 *doPageWrites_p = doPageWrites;
5899 * GetInsertRecPtr -- Returns the current insert position.
5901 * NOTE: The value *actually* returned is the position of the last full
5902 * xlog page. It lags behind the real insert position by at most 1 page.
5903 * For that, we don't need to scan through WAL insertion locks, and an
5904 * approximation is enough for the current usage of this function.
5906 XLogRecPtr
5907 GetInsertRecPtr(void)
5909 XLogRecPtr recptr;
5911 SpinLockAcquire(&XLogCtl->info_lck);
5912 recptr = XLogCtl->LogwrtRqst.Write;
5913 SpinLockRelease(&XLogCtl->info_lck);
5915 return recptr;
5919 * GetFlushRecPtr -- Returns the current flush position, ie, the last WAL
5920 * position known to be fsync'd to disk. This should only be used on a
5921 * system that is known not to be in recovery.
5923 XLogRecPtr
5924 GetFlushRecPtr(TimeLineID *insertTLI)
5926 Assert(XLogCtl->SharedRecoveryState == RECOVERY_STATE_DONE);
5928 SpinLockAcquire(&XLogCtl->info_lck);
5929 LogwrtResult = XLogCtl->LogwrtResult;
5930 SpinLockRelease(&XLogCtl->info_lck);
5933 * If we're writing and flushing WAL, the time line can't be changing, so
5934 * no lock is required.
5936 if (insertTLI)
5937 *insertTLI = XLogCtl->InsertTimeLineID;
5939 return LogwrtResult.Flush;
5943 * GetWALInsertionTimeLine -- Returns the current timeline of a system that
5944 * is not in recovery.
5946 TimeLineID
5947 GetWALInsertionTimeLine(void)
5949 Assert(XLogCtl->SharedRecoveryState == RECOVERY_STATE_DONE);
5951 /* Since the value can't be changing, no lock is required. */
5952 return XLogCtl->InsertTimeLineID;
5956 * GetLastImportantRecPtr -- Returns the LSN of the last important record
5957 * inserted. All records not explicitly marked as unimportant are considered
5958 * important.
5960 * The LSN is determined by computing the maximum of
5961 * WALInsertLocks[i].lastImportantAt.
5963 XLogRecPtr
5964 GetLastImportantRecPtr(void)
5966 XLogRecPtr res = InvalidXLogRecPtr;
5967 int i;
5969 for (i = 0; i < NUM_XLOGINSERT_LOCKS; i++)
5971 XLogRecPtr last_important;
5974 * Need to take a lock to prevent torn reads of the LSN, which are
5975 * possible on some of the supported platforms. WAL insert locks only
5976 * support exclusive mode, so we have to use that.
5978 LWLockAcquire(&WALInsertLocks[i].l.lock, LW_EXCLUSIVE);
5979 last_important = WALInsertLocks[i].l.lastImportantAt;
5980 LWLockRelease(&WALInsertLocks[i].l.lock);
5982 if (res < last_important)
5983 res = last_important;
5986 return res;
5990 * Get the time and LSN of the last xlog segment switch
5992 pg_time_t
5993 GetLastSegSwitchData(XLogRecPtr *lastSwitchLSN)
5995 pg_time_t result;
5997 /* Need WALWriteLock, but shared lock is sufficient */
5998 LWLockAcquire(WALWriteLock, LW_SHARED);
5999 result = XLogCtl->lastSegSwitchTime;
6000 *lastSwitchLSN = XLogCtl->lastSegSwitchLSN;
6001 LWLockRelease(WALWriteLock);
6003 return result;
6007 * This must be called ONCE during postmaster or standalone-backend shutdown
6009 void
6010 ShutdownXLOG(int code, Datum arg)
6013 * We should have an aux process resource owner to use, and we should not
6014 * be in a transaction that's installed some other resowner.
6016 Assert(AuxProcessResourceOwner != NULL);
6017 Assert(CurrentResourceOwner == NULL ||
6018 CurrentResourceOwner == AuxProcessResourceOwner);
6019 CurrentResourceOwner = AuxProcessResourceOwner;
6021 /* Don't be chatty in standalone mode */
6022 ereport(IsPostmasterEnvironment ? LOG : NOTICE,
6023 (errmsg("shutting down")));
6026 * Signal walsenders to move to stopping state.
6028 WalSndInitStopping();
6031 * Wait for WAL senders to be in stopping state. This prevents commands
6032 * from writing new WAL.
6034 WalSndWaitStopping();
6036 if (RecoveryInProgress())
6037 CreateRestartPoint(CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_IMMEDIATE);
6038 else
6041 * If archiving is enabled, rotate the last XLOG file so that all the
6042 * remaining records are archived (postmaster wakes up the archiver
6043 * process one more time at the end of shutdown). The checkpoint
6044 * record will go to the next XLOG file and won't be archived (yet).
6046 if (XLogArchivingActive())
6047 RequestXLogSwitch(false);
6049 CreateCheckPoint(CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_IMMEDIATE);
6054 * Log start of a checkpoint.
6056 static void
6057 LogCheckpointStart(int flags, bool restartpoint)
6059 if (restartpoint)
6060 ereport(LOG,
6061 /* translator: the placeholders show checkpoint options */
6062 (errmsg("restartpoint starting:%s%s%s%s%s%s%s%s",
6063 (flags & CHECKPOINT_IS_SHUTDOWN) ? " shutdown" : "",
6064 (flags & CHECKPOINT_END_OF_RECOVERY) ? " end-of-recovery" : "",
6065 (flags & CHECKPOINT_IMMEDIATE) ? " immediate" : "",
6066 (flags & CHECKPOINT_FORCE) ? " force" : "",
6067 (flags & CHECKPOINT_WAIT) ? " wait" : "",
6068 (flags & CHECKPOINT_CAUSE_XLOG) ? " wal" : "",
6069 (flags & CHECKPOINT_CAUSE_TIME) ? " time" : "",
6070 (flags & CHECKPOINT_FLUSH_ALL) ? " flush-all" : "")));
6071 else
6072 ereport(LOG,
6073 /* translator: the placeholders show checkpoint options */
6074 (errmsg("checkpoint starting:%s%s%s%s%s%s%s%s",
6075 (flags & CHECKPOINT_IS_SHUTDOWN) ? " shutdown" : "",
6076 (flags & CHECKPOINT_END_OF_RECOVERY) ? " end-of-recovery" : "",
6077 (flags & CHECKPOINT_IMMEDIATE) ? " immediate" : "",
6078 (flags & CHECKPOINT_FORCE) ? " force" : "",
6079 (flags & CHECKPOINT_WAIT) ? " wait" : "",
6080 (flags & CHECKPOINT_CAUSE_XLOG) ? " wal" : "",
6081 (flags & CHECKPOINT_CAUSE_TIME) ? " time" : "",
6082 (flags & CHECKPOINT_FLUSH_ALL) ? " flush-all" : "")));
6086 * Log end of a checkpoint.
6088 static void
6089 LogCheckpointEnd(bool restartpoint)
6091 long write_msecs,
6092 sync_msecs,
6093 total_msecs,
6094 longest_msecs,
6095 average_msecs;
6096 uint64 average_sync_time;
6098 CheckpointStats.ckpt_end_t = GetCurrentTimestamp();
6100 write_msecs = TimestampDifferenceMilliseconds(CheckpointStats.ckpt_write_t,
6101 CheckpointStats.ckpt_sync_t);
6103 sync_msecs = TimestampDifferenceMilliseconds(CheckpointStats.ckpt_sync_t,
6104 CheckpointStats.ckpt_sync_end_t);
6106 /* Accumulate checkpoint timing summary data, in milliseconds. */
6107 PendingCheckpointerStats.m_checkpoint_write_time += write_msecs;
6108 PendingCheckpointerStats.m_checkpoint_sync_time += sync_msecs;
6111 * All of the published timing statistics are accounted for. Only
6112 * continue if a log message is to be written.
6114 if (!log_checkpoints)
6115 return;
6117 total_msecs = TimestampDifferenceMilliseconds(CheckpointStats.ckpt_start_t,
6118 CheckpointStats.ckpt_end_t);
6121 * Timing values returned from CheckpointStats are in microseconds.
6122 * Convert to milliseconds for consistent printing.
6124 longest_msecs = (long) ((CheckpointStats.ckpt_longest_sync + 999) / 1000);
6126 average_sync_time = 0;
6127 if (CheckpointStats.ckpt_sync_rels > 0)
6128 average_sync_time = CheckpointStats.ckpt_agg_sync_time /
6129 CheckpointStats.ckpt_sync_rels;
6130 average_msecs = (long) ((average_sync_time + 999) / 1000);
6132 if (restartpoint)
6133 ereport(LOG,
6134 (errmsg("restartpoint complete: wrote %d buffers (%.1f%%); "
6135 "%d WAL file(s) added, %d removed, %d recycled; "
6136 "write=%ld.%03d s, sync=%ld.%03d s, total=%ld.%03d s; "
6137 "sync files=%d, longest=%ld.%03d s, average=%ld.%03d s; "
6138 "distance=%d kB, estimate=%d kB",
6139 CheckpointStats.ckpt_bufs_written,
6140 (double) CheckpointStats.ckpt_bufs_written * 100 / NBuffers,
6141 CheckpointStats.ckpt_segs_added,
6142 CheckpointStats.ckpt_segs_removed,
6143 CheckpointStats.ckpt_segs_recycled,
6144 write_msecs / 1000, (int) (write_msecs % 1000),
6145 sync_msecs / 1000, (int) (sync_msecs % 1000),
6146 total_msecs / 1000, (int) (total_msecs % 1000),
6147 CheckpointStats.ckpt_sync_rels,
6148 longest_msecs / 1000, (int) (longest_msecs % 1000),
6149 average_msecs / 1000, (int) (average_msecs % 1000),
6150 (int) (PrevCheckPointDistance / 1024.0),
6151 (int) (CheckPointDistanceEstimate / 1024.0))));
6152 else
6153 ereport(LOG,
6154 (errmsg("checkpoint complete: wrote %d buffers (%.1f%%); "
6155 "%d WAL file(s) added, %d removed, %d recycled; "
6156 "write=%ld.%03d s, sync=%ld.%03d s, total=%ld.%03d s; "
6157 "sync files=%d, longest=%ld.%03d s, average=%ld.%03d s; "
6158 "distance=%d kB, estimate=%d kB",
6159 CheckpointStats.ckpt_bufs_written,
6160 (double) CheckpointStats.ckpt_bufs_written * 100 / NBuffers,
6161 CheckpointStats.ckpt_segs_added,
6162 CheckpointStats.ckpt_segs_removed,
6163 CheckpointStats.ckpt_segs_recycled,
6164 write_msecs / 1000, (int) (write_msecs % 1000),
6165 sync_msecs / 1000, (int) (sync_msecs % 1000),
6166 total_msecs / 1000, (int) (total_msecs % 1000),
6167 CheckpointStats.ckpt_sync_rels,
6168 longest_msecs / 1000, (int) (longest_msecs % 1000),
6169 average_msecs / 1000, (int) (average_msecs % 1000),
6170 (int) (PrevCheckPointDistance / 1024.0),
6171 (int) (CheckPointDistanceEstimate / 1024.0))));
6175 * Update the estimate of distance between checkpoints.
6177 * The estimate is used to calculate the number of WAL segments to keep
6178 * preallocated, see XLOGfileslop().
6180 static void
6181 UpdateCheckPointDistanceEstimate(uint64 nbytes)
6184 * To estimate the number of segments consumed between checkpoints, keep a
6185 * moving average of the amount of WAL generated in previous checkpoint
6186 * cycles. However, if the load is bursty, with quiet periods and busy
6187 * periods, we want to cater for the peak load. So instead of a plain
6188 * moving average, let the average decline slowly if the previous cycle
6189 * used less WAL than estimated, but bump it up immediately if it used
6190 * more.
6192 * When checkpoints are triggered by max_wal_size, this should converge to
6193 * CheckpointSegments * wal_segment_size,
6195 * Note: This doesn't pay any attention to what caused the checkpoint.
6196 * Checkpoints triggered manually with CHECKPOINT command, or by e.g.
6197 * starting a base backup, are counted the same as those created
6198 * automatically. The slow-decline will largely mask them out, if they are
6199 * not frequent. If they are frequent, it seems reasonable to count them
6200 * in as any others; if you issue a manual checkpoint every 5 minutes and
6201 * never let a timed checkpoint happen, it makes sense to base the
6202 * preallocation on that 5 minute interval rather than whatever
6203 * checkpoint_timeout is set to.
6205 PrevCheckPointDistance = nbytes;
6206 if (CheckPointDistanceEstimate < nbytes)
6207 CheckPointDistanceEstimate = nbytes;
6208 else
6209 CheckPointDistanceEstimate =
6210 (0.90 * CheckPointDistanceEstimate + 0.10 * (double) nbytes);
6214 * Update the ps display for a process running a checkpoint. Note that
6215 * this routine should not do any allocations so as it can be called
6216 * from a critical section.
6218 static void
6219 update_checkpoint_display(int flags, bool restartpoint, bool reset)
6222 * The status is reported only for end-of-recovery and shutdown
6223 * checkpoints or shutdown restartpoints. Updating the ps display is
6224 * useful in those situations as it may not be possible to rely on
6225 * pg_stat_activity to see the status of the checkpointer or the startup
6226 * process.
6228 if ((flags & (CHECKPOINT_END_OF_RECOVERY | CHECKPOINT_IS_SHUTDOWN)) == 0)
6229 return;
6231 if (reset)
6232 set_ps_display("");
6233 else
6235 char activitymsg[128];
6237 snprintf(activitymsg, sizeof(activitymsg), "performing %s%s%s",
6238 (flags & CHECKPOINT_END_OF_RECOVERY) ? "end-of-recovery " : "",
6239 (flags & CHECKPOINT_IS_SHUTDOWN) ? "shutdown " : "",
6240 restartpoint ? "restartpoint" : "checkpoint");
6241 set_ps_display(activitymsg);
6247 * Perform a checkpoint --- either during shutdown, or on-the-fly
6249 * flags is a bitwise OR of the following:
6250 * CHECKPOINT_IS_SHUTDOWN: checkpoint is for database shutdown.
6251 * CHECKPOINT_END_OF_RECOVERY: checkpoint is for end of WAL recovery.
6252 * CHECKPOINT_IMMEDIATE: finish the checkpoint ASAP,
6253 * ignoring checkpoint_completion_target parameter.
6254 * CHECKPOINT_FORCE: force a checkpoint even if no XLOG activity has occurred
6255 * since the last one (implied by CHECKPOINT_IS_SHUTDOWN or
6256 * CHECKPOINT_END_OF_RECOVERY).
6257 * CHECKPOINT_FLUSH_ALL: also flush buffers of unlogged tables.
6259 * Note: flags contains other bits, of interest here only for logging purposes.
6260 * In particular note that this routine is synchronous and does not pay
6261 * attention to CHECKPOINT_WAIT.
6263 * If !shutdown then we are writing an online checkpoint. This is a very special
6264 * kind of operation and WAL record because the checkpoint action occurs over
6265 * a period of time yet logically occurs at just a single LSN. The logical
6266 * position of the WAL record (redo ptr) is the same or earlier than the
6267 * physical position. When we replay WAL we locate the checkpoint via its
6268 * physical position then read the redo ptr and actually start replay at the
6269 * earlier logical position. Note that we don't write *anything* to WAL at
6270 * the logical position, so that location could be any other kind of WAL record.
6271 * All of this mechanism allows us to continue working while we checkpoint.
6272 * As a result, timing of actions is critical here and be careful to note that
6273 * this function will likely take minutes to execute on a busy system.
6275 void
6276 CreateCheckPoint(int flags)
6278 bool shutdown;
6279 CheckPoint checkPoint;
6280 XLogRecPtr recptr;
6281 XLogSegNo _logSegNo;
6282 XLogCtlInsert *Insert = &XLogCtl->Insert;
6283 uint32 freespace;
6284 XLogRecPtr PriorRedoPtr;
6285 XLogRecPtr curInsert;
6286 XLogRecPtr last_important_lsn;
6287 VirtualTransactionId *vxids;
6288 int nvxids;
6289 int oldXLogAllowed = 0;
6292 * An end-of-recovery checkpoint is really a shutdown checkpoint, just
6293 * issued at a different time.
6295 if (flags & (CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_END_OF_RECOVERY))
6296 shutdown = true;
6297 else
6298 shutdown = false;
6300 /* sanity check */
6301 if (RecoveryInProgress() && (flags & CHECKPOINT_END_OF_RECOVERY) == 0)
6302 elog(ERROR, "can't create a checkpoint during recovery");
6305 * Prepare to accumulate statistics.
6307 * Note: because it is possible for log_checkpoints to change while a
6308 * checkpoint proceeds, we always accumulate stats, even if
6309 * log_checkpoints is currently off.
6311 MemSet(&CheckpointStats, 0, sizeof(CheckpointStats));
6312 CheckpointStats.ckpt_start_t = GetCurrentTimestamp();
6315 * Let smgr prepare for checkpoint; this has to happen outside the
6316 * critical section and before we determine the REDO pointer. Note that
6317 * smgr must not do anything that'd have to be undone if we decide no
6318 * checkpoint is needed.
6320 SyncPreCheckpoint();
6323 * Use a critical section to force system panic if we have trouble.
6325 START_CRIT_SECTION();
6327 if (shutdown)
6329 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
6330 ControlFile->state = DB_SHUTDOWNING;
6331 UpdateControlFile();
6332 LWLockRelease(ControlFileLock);
6335 /* Begin filling in the checkpoint WAL record */
6336 MemSet(&checkPoint, 0, sizeof(checkPoint));
6337 checkPoint.time = (pg_time_t) time(NULL);
6340 * For Hot Standby, derive the oldestActiveXid before we fix the redo
6341 * pointer. This allows us to begin accumulating changes to assemble our
6342 * starting snapshot of locks and transactions.
6344 if (!shutdown && XLogStandbyInfoActive())
6345 checkPoint.oldestActiveXid = GetOldestActiveTransactionId();
6346 else
6347 checkPoint.oldestActiveXid = InvalidTransactionId;
6350 * Get location of last important record before acquiring insert locks (as
6351 * GetLastImportantRecPtr() also locks WAL locks).
6353 last_important_lsn = GetLastImportantRecPtr();
6356 * We must block concurrent insertions while examining insert state to
6357 * determine the checkpoint REDO pointer.
6359 WALInsertLockAcquireExclusive();
6360 curInsert = XLogBytePosToRecPtr(Insert->CurrBytePos);
6363 * If this isn't a shutdown or forced checkpoint, and if there has been no
6364 * WAL activity requiring a checkpoint, skip it. The idea here is to
6365 * avoid inserting duplicate checkpoints when the system is idle.
6367 if ((flags & (CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_END_OF_RECOVERY |
6368 CHECKPOINT_FORCE)) == 0)
6370 if (last_important_lsn == ControlFile->checkPoint)
6372 WALInsertLockRelease();
6373 END_CRIT_SECTION();
6374 ereport(DEBUG1,
6375 (errmsg_internal("checkpoint skipped because system is idle")));
6376 return;
6381 * An end-of-recovery checkpoint is created before anyone is allowed to
6382 * write WAL. To allow us to write the checkpoint record, temporarily
6383 * enable XLogInsertAllowed.
6385 if (flags & CHECKPOINT_END_OF_RECOVERY)
6386 oldXLogAllowed = LocalSetXLogInsertAllowed();
6388 checkPoint.ThisTimeLineID = XLogCtl->InsertTimeLineID;
6389 if (flags & CHECKPOINT_END_OF_RECOVERY)
6390 checkPoint.PrevTimeLineID = XLogCtl->PrevTimeLineID;
6391 else
6392 checkPoint.PrevTimeLineID = checkPoint.ThisTimeLineID;
6394 checkPoint.fullPageWrites = Insert->fullPageWrites;
6397 * Compute new REDO record ptr = location of next XLOG record.
6399 * NB: this is NOT necessarily where the checkpoint record itself will be,
6400 * since other backends may insert more XLOG records while we're off doing
6401 * the buffer flush work. Those XLOG records are logically after the
6402 * checkpoint, even though physically before it. Got that?
6404 freespace = INSERT_FREESPACE(curInsert);
6405 if (freespace == 0)
6407 if (XLogSegmentOffset(curInsert, wal_segment_size) == 0)
6408 curInsert += SizeOfXLogLongPHD;
6409 else
6410 curInsert += SizeOfXLogShortPHD;
6412 checkPoint.redo = curInsert;
6415 * Here we update the shared RedoRecPtr for future XLogInsert calls; this
6416 * must be done while holding all the insertion locks.
6418 * Note: if we fail to complete the checkpoint, RedoRecPtr will be left
6419 * pointing past where it really needs to point. This is okay; the only
6420 * consequence is that XLogInsert might back up whole buffers that it
6421 * didn't really need to. We can't postpone advancing RedoRecPtr because
6422 * XLogInserts that happen while we are dumping buffers must assume that
6423 * their buffer changes are not included in the checkpoint.
6425 RedoRecPtr = XLogCtl->Insert.RedoRecPtr = checkPoint.redo;
6428 * Now we can release the WAL insertion locks, allowing other xacts to
6429 * proceed while we are flushing disk buffers.
6431 WALInsertLockRelease();
6433 /* Update the info_lck-protected copy of RedoRecPtr as well */
6434 SpinLockAcquire(&XLogCtl->info_lck);
6435 XLogCtl->RedoRecPtr = checkPoint.redo;
6436 SpinLockRelease(&XLogCtl->info_lck);
6439 * If enabled, log checkpoint start. We postpone this until now so as not
6440 * to log anything if we decided to skip the checkpoint.
6442 if (log_checkpoints)
6443 LogCheckpointStart(flags, false);
6445 /* Update the process title */
6446 update_checkpoint_display(flags, false, false);
6448 TRACE_POSTGRESQL_CHECKPOINT_START(flags);
6451 * Get the other info we need for the checkpoint record.
6453 * We don't need to save oldestClogXid in the checkpoint, it only matters
6454 * for the short period in which clog is being truncated, and if we crash
6455 * during that we'll redo the clog truncation and fix up oldestClogXid
6456 * there.
6458 LWLockAcquire(XidGenLock, LW_SHARED);
6459 checkPoint.nextXid = ShmemVariableCache->nextXid;
6460 checkPoint.oldestXid = ShmemVariableCache->oldestXid;
6461 checkPoint.oldestXidDB = ShmemVariableCache->oldestXidDB;
6462 LWLockRelease(XidGenLock);
6464 LWLockAcquire(CommitTsLock, LW_SHARED);
6465 checkPoint.oldestCommitTsXid = ShmemVariableCache->oldestCommitTsXid;
6466 checkPoint.newestCommitTsXid = ShmemVariableCache->newestCommitTsXid;
6467 LWLockRelease(CommitTsLock);
6469 LWLockAcquire(OidGenLock, LW_SHARED);
6470 checkPoint.nextOid = ShmemVariableCache->nextOid;
6471 if (!shutdown)
6472 checkPoint.nextOid += ShmemVariableCache->oidCount;
6473 LWLockRelease(OidGenLock);
6475 MultiXactGetCheckptMulti(shutdown,
6476 &checkPoint.nextMulti,
6477 &checkPoint.nextMultiOffset,
6478 &checkPoint.oldestMulti,
6479 &checkPoint.oldestMultiDB);
6482 * Having constructed the checkpoint record, ensure all shmem disk buffers
6483 * and commit-log buffers are flushed to disk.
6485 * This I/O could fail for various reasons. If so, we will fail to
6486 * complete the checkpoint, but there is no reason to force a system
6487 * panic. Accordingly, exit critical section while doing it.
6489 END_CRIT_SECTION();
6492 * In some cases there are groups of actions that must all occur on one
6493 * side or the other of a checkpoint record. Before flushing the
6494 * checkpoint record we must explicitly wait for any backend currently
6495 * performing those groups of actions.
6497 * One example is end of transaction, so we must wait for any transactions
6498 * that are currently in commit critical sections. If an xact inserted
6499 * its commit record into XLOG just before the REDO point, then a crash
6500 * restart from the REDO point would not replay that record, which means
6501 * that our flushing had better include the xact's update of pg_xact. So
6502 * we wait till he's out of his commit critical section before proceeding.
6503 * See notes in RecordTransactionCommit().
6505 * Because we've already released the insertion locks, this test is a bit
6506 * fuzzy: it is possible that we will wait for xacts we didn't really need
6507 * to wait for. But the delay should be short and it seems better to make
6508 * checkpoint take a bit longer than to hold off insertions longer than
6509 * necessary. (In fact, the whole reason we have this issue is that xact.c
6510 * does commit record XLOG insertion and clog update as two separate steps
6511 * protected by different locks, but again that seems best on grounds of
6512 * minimizing lock contention.)
6514 * A transaction that has not yet set delayChkpt when we look cannot be at
6515 * risk, since he's not inserted his commit record yet; and one that's
6516 * already cleared it is not at risk either, since he's done fixing clog
6517 * and we will correctly flush the update below. So we cannot miss any
6518 * xacts we need to wait for.
6520 vxids = GetVirtualXIDsDelayingChkpt(&nvxids);
6521 if (nvxids > 0)
6525 pg_usleep(10000L); /* wait for 10 msec */
6526 } while (HaveVirtualXIDsDelayingChkpt(vxids, nvxids));
6528 pfree(vxids);
6530 CheckPointGuts(checkPoint.redo, flags);
6533 * Take a snapshot of running transactions and write this to WAL. This
6534 * allows us to reconstruct the state of running transactions during
6535 * archive recovery, if required. Skip, if this info disabled.
6537 * If we are shutting down, or Startup process is completing crash
6538 * recovery we don't need to write running xact data.
6540 if (!shutdown && XLogStandbyInfoActive())
6541 LogStandbySnapshot();
6543 START_CRIT_SECTION();
6546 * Now insert the checkpoint record into XLOG.
6548 XLogBeginInsert();
6549 XLogRegisterData((char *) (&checkPoint), sizeof(checkPoint));
6550 recptr = XLogInsert(RM_XLOG_ID,
6551 shutdown ? XLOG_CHECKPOINT_SHUTDOWN :
6552 XLOG_CHECKPOINT_ONLINE);
6554 XLogFlush(recptr);
6557 * We mustn't write any new WAL after a shutdown checkpoint, or it will be
6558 * overwritten at next startup. No-one should even try, this just allows
6559 * sanity-checking. In the case of an end-of-recovery checkpoint, we want
6560 * to just temporarily disable writing until the system has exited
6561 * recovery.
6563 if (shutdown)
6565 if (flags & CHECKPOINT_END_OF_RECOVERY)
6566 LocalXLogInsertAllowed = oldXLogAllowed;
6567 else
6568 LocalXLogInsertAllowed = 0; /* never again write WAL */
6572 * We now have ProcLastRecPtr = start of actual checkpoint record, recptr
6573 * = end of actual checkpoint record.
6575 if (shutdown && checkPoint.redo != ProcLastRecPtr)
6576 ereport(PANIC,
6577 (errmsg("concurrent write-ahead log activity while database system is shutting down")));
6580 * Remember the prior checkpoint's redo ptr for
6581 * UpdateCheckPointDistanceEstimate()
6583 PriorRedoPtr = ControlFile->checkPointCopy.redo;
6586 * Update the control file.
6588 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
6589 if (shutdown)
6590 ControlFile->state = DB_SHUTDOWNED;
6591 ControlFile->checkPoint = ProcLastRecPtr;
6592 ControlFile->checkPointCopy = checkPoint;
6593 /* crash recovery should always recover to the end of WAL */
6594 ControlFile->minRecoveryPoint = InvalidXLogRecPtr;
6595 ControlFile->minRecoveryPointTLI = 0;
6598 * Persist unloggedLSN value. It's reset on crash recovery, so this goes
6599 * unused on non-shutdown checkpoints, but seems useful to store it always
6600 * for debugging purposes.
6602 SpinLockAcquire(&XLogCtl->ulsn_lck);
6603 ControlFile->unloggedLSN = XLogCtl->unloggedLSN;
6604 SpinLockRelease(&XLogCtl->ulsn_lck);
6606 UpdateControlFile();
6607 LWLockRelease(ControlFileLock);
6609 /* Update shared-memory copy of checkpoint XID/epoch */
6610 SpinLockAcquire(&XLogCtl->info_lck);
6611 XLogCtl->ckptFullXid = checkPoint.nextXid;
6612 SpinLockRelease(&XLogCtl->info_lck);
6615 * We are now done with critical updates; no need for system panic if we
6616 * have trouble while fooling with old log segments.
6618 END_CRIT_SECTION();
6621 * Let smgr do post-checkpoint cleanup (eg, deleting old files).
6623 SyncPostCheckpoint();
6626 * Update the average distance between checkpoints if the prior checkpoint
6627 * exists.
6629 if (PriorRedoPtr != InvalidXLogRecPtr)
6630 UpdateCheckPointDistanceEstimate(RedoRecPtr - PriorRedoPtr);
6633 * Delete old log files, those no longer needed for last checkpoint to
6634 * prevent the disk holding the xlog from growing full.
6636 XLByteToSeg(RedoRecPtr, _logSegNo, wal_segment_size);
6637 KeepLogSeg(recptr, &_logSegNo);
6638 if (InvalidateObsoleteReplicationSlots(_logSegNo))
6641 * Some slots have been invalidated; recalculate the old-segment
6642 * horizon, starting again from RedoRecPtr.
6644 XLByteToSeg(RedoRecPtr, _logSegNo, wal_segment_size);
6645 KeepLogSeg(recptr, &_logSegNo);
6647 _logSegNo--;
6648 RemoveOldXlogFiles(_logSegNo, RedoRecPtr, recptr,
6649 checkPoint.ThisTimeLineID);
6652 * Make more log segments if needed. (Do this after recycling old log
6653 * segments, since that may supply some of the needed files.)
6655 if (!shutdown)
6656 PreallocXlogFiles(recptr, checkPoint.ThisTimeLineID);
6659 * Truncate pg_subtrans if possible. We can throw away all data before
6660 * the oldest XMIN of any running transaction. No future transaction will
6661 * attempt to reference any pg_subtrans entry older than that (see Asserts
6662 * in subtrans.c). During recovery, though, we mustn't do this because
6663 * StartupSUBTRANS hasn't been called yet.
6665 if (!RecoveryInProgress())
6666 TruncateSUBTRANS(GetOldestTransactionIdConsideredRunning());
6668 /* Real work is done; log and update stats. */
6669 LogCheckpointEnd(false);
6671 /* Reset the process title */
6672 update_checkpoint_display(flags, false, true);
6674 TRACE_POSTGRESQL_CHECKPOINT_DONE(CheckpointStats.ckpt_bufs_written,
6675 NBuffers,
6676 CheckpointStats.ckpt_segs_added,
6677 CheckpointStats.ckpt_segs_removed,
6678 CheckpointStats.ckpt_segs_recycled);
6682 * Mark the end of recovery in WAL though without running a full checkpoint.
6683 * We can expect that a restartpoint is likely to be in progress as we
6684 * do this, though we are unwilling to wait for it to complete.
6686 * CreateRestartPoint() allows for the case where recovery may end before
6687 * the restartpoint completes so there is no concern of concurrent behaviour.
6689 static void
6690 CreateEndOfRecoveryRecord(void)
6692 xl_end_of_recovery xlrec;
6693 XLogRecPtr recptr;
6695 /* sanity check */
6696 if (!RecoveryInProgress())
6697 elog(ERROR, "can only be used to end recovery");
6699 xlrec.end_time = GetCurrentTimestamp();
6701 WALInsertLockAcquireExclusive();
6702 xlrec.ThisTimeLineID = XLogCtl->InsertTimeLineID;
6703 xlrec.PrevTimeLineID = XLogCtl->PrevTimeLineID;
6704 WALInsertLockRelease();
6706 START_CRIT_SECTION();
6708 XLogBeginInsert();
6709 XLogRegisterData((char *) &xlrec, sizeof(xl_end_of_recovery));
6710 recptr = XLogInsert(RM_XLOG_ID, XLOG_END_OF_RECOVERY);
6712 XLogFlush(recptr);
6715 * Update the control file so that crash recovery can follow the timeline
6716 * changes to this point.
6718 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
6719 ControlFile->minRecoveryPoint = recptr;
6720 ControlFile->minRecoveryPointTLI = xlrec.ThisTimeLineID;
6721 UpdateControlFile();
6722 LWLockRelease(ControlFileLock);
6724 END_CRIT_SECTION();
6728 * Write an OVERWRITE_CONTRECORD message.
6730 * When on WAL replay we expect a continuation record at the start of a page
6731 * that is not there, recovery ends and WAL writing resumes at that point.
6732 * But it's wrong to resume writing new WAL back at the start of the record
6733 * that was broken, because downstream consumers of that WAL (physical
6734 * replicas) are not prepared to "rewind". So the first action after
6735 * finishing replay of all valid WAL must be to write a record of this type
6736 * at the point where the contrecord was missing; to support xlogreader
6737 * detecting the special case, XLP_FIRST_IS_OVERWRITE_CONTRECORD is also added
6738 * to the page header where the record occurs. xlogreader has an ad-hoc
6739 * mechanism to report metadata about the broken record, which is what we
6740 * use here.
6742 * At replay time, XLP_FIRST_IS_OVERWRITE_CONTRECORD instructs xlogreader to
6743 * skip the record it was reading, and pass back the LSN of the skipped
6744 * record, so that its caller can verify (on "replay" of that record) that the
6745 * XLOG_OVERWRITE_CONTRECORD matches what was effectively overwritten.
6747 * 'aborted_lsn' is the beginning position of the record that was incomplete.
6748 * It is included in the WAL record. 'pagePtr' and 'newTLI' point to the
6749 * beginning of the XLOG page where the record is to be inserted. They must
6750 * match the current WAL insert position, they're passed here just so that we
6751 * can verify that.
6753 static XLogRecPtr
6754 CreateOverwriteContrecordRecord(XLogRecPtr aborted_lsn, XLogRecPtr pagePtr,
6755 TimeLineID newTLI)
6757 xl_overwrite_contrecord xlrec;
6758 XLogRecPtr recptr;
6759 XLogPageHeader pagehdr;
6760 XLogRecPtr startPos;
6762 /* sanity checks */
6763 if (!RecoveryInProgress())
6764 elog(ERROR, "can only be used at end of recovery");
6765 if (pagePtr % XLOG_BLCKSZ != 0)
6766 elog(ERROR, "invalid position for missing continuation record %X/%X",
6767 LSN_FORMAT_ARGS(pagePtr));
6769 /* The current WAL insert position should be right after the page header */
6770 startPos = pagePtr;
6771 if (XLogSegmentOffset(startPos, wal_segment_size) == 0)
6772 startPos += SizeOfXLogLongPHD;
6773 else
6774 startPos += SizeOfXLogShortPHD;
6775 recptr = GetXLogInsertRecPtr();
6776 if (recptr != startPos)
6777 elog(ERROR, "invalid WAL insert position %X/%X for OVERWRITE_CONTRECORD",
6778 LSN_FORMAT_ARGS(recptr));
6780 START_CRIT_SECTION();
6783 * Initialize the XLOG page header (by GetXLogBuffer), and set the
6784 * XLP_FIRST_IS_OVERWRITE_CONTRECORD flag.
6786 * No other backend is allowed to write WAL yet, so acquiring the WAL
6787 * insertion lock is just pro forma.
6789 WALInsertLockAcquire();
6790 pagehdr = (XLogPageHeader) GetXLogBuffer(pagePtr, newTLI);
6791 pagehdr->xlp_info |= XLP_FIRST_IS_OVERWRITE_CONTRECORD;
6792 WALInsertLockRelease();
6795 * Insert the XLOG_OVERWRITE_CONTRECORD record as the first record on the
6796 * page. We know it becomes the first record, because no other backend is
6797 * allowed to write WAL yet.
6799 XLogBeginInsert();
6800 xlrec.overwritten_lsn = aborted_lsn;
6801 xlrec.overwrite_time = GetCurrentTimestamp();
6802 XLogRegisterData((char *) &xlrec, sizeof(xl_overwrite_contrecord));
6803 recptr = XLogInsert(RM_XLOG_ID, XLOG_OVERWRITE_CONTRECORD);
6805 /* check that the record was inserted to the right place */
6806 if (ProcLastRecPtr != startPos)
6807 elog(ERROR, "OVERWRITE_CONTRECORD was inserted to unexpected position %X/%X",
6808 LSN_FORMAT_ARGS(ProcLastRecPtr));
6810 XLogFlush(recptr);
6812 END_CRIT_SECTION();
6814 return recptr;
6818 * Flush all data in shared memory to disk, and fsync
6820 * This is the common code shared between regular checkpoints and
6821 * recovery restartpoints.
6823 static void
6824 CheckPointGuts(XLogRecPtr checkPointRedo, int flags)
6826 CheckPointRelationMap();
6827 CheckPointReplicationSlots();
6828 CheckPointSnapBuild();
6829 CheckPointLogicalRewriteHeap();
6830 CheckPointReplicationOrigin();
6832 /* Write out all dirty data in SLRUs and the main buffer pool */
6833 TRACE_POSTGRESQL_BUFFER_CHECKPOINT_START(flags);
6834 CheckpointStats.ckpt_write_t = GetCurrentTimestamp();
6835 CheckPointCLOG();
6836 CheckPointCommitTs();
6837 CheckPointSUBTRANS();
6838 CheckPointMultiXact();
6839 CheckPointPredicate();
6840 CheckPointBuffers(flags);
6842 /* Perform all queued up fsyncs */
6843 TRACE_POSTGRESQL_BUFFER_CHECKPOINT_SYNC_START();
6844 CheckpointStats.ckpt_sync_t = GetCurrentTimestamp();
6845 ProcessSyncRequests();
6846 CheckpointStats.ckpt_sync_end_t = GetCurrentTimestamp();
6847 TRACE_POSTGRESQL_BUFFER_CHECKPOINT_DONE();
6849 /* We deliberately delay 2PC checkpointing as long as possible */
6850 CheckPointTwoPhase(checkPointRedo);
6854 * Save a checkpoint for recovery restart if appropriate
6856 * This function is called each time a checkpoint record is read from XLOG.
6857 * It must determine whether the checkpoint represents a safe restartpoint or
6858 * not. If so, the checkpoint record is stashed in shared memory so that
6859 * CreateRestartPoint can consult it. (Note that the latter function is
6860 * executed by the checkpointer, while this one will be executed by the
6861 * startup process.)
6863 static void
6864 RecoveryRestartPoint(const CheckPoint *checkPoint, XLogReaderState *record)
6867 * Also refrain from creating a restartpoint if we have seen any
6868 * references to non-existent pages. Restarting recovery from the
6869 * restartpoint would not see the references, so we would lose the
6870 * cross-check that the pages belonged to a relation that was dropped
6871 * later.
6873 if (XLogHaveInvalidPages())
6875 elog(trace_recovery(DEBUG2),
6876 "could not record restart point at %X/%X because there "
6877 "are unresolved references to invalid pages",
6878 LSN_FORMAT_ARGS(checkPoint->redo));
6879 return;
6883 * Copy the checkpoint record to shared memory, so that checkpointer can
6884 * work out the next time it wants to perform a restartpoint.
6886 SpinLockAcquire(&XLogCtl->info_lck);
6887 XLogCtl->lastCheckPointRecPtr = record->ReadRecPtr;
6888 XLogCtl->lastCheckPointEndPtr = record->EndRecPtr;
6889 XLogCtl->lastCheckPoint = *checkPoint;
6890 SpinLockRelease(&XLogCtl->info_lck);
6894 * Establish a restartpoint if possible.
6896 * This is similar to CreateCheckPoint, but is used during WAL recovery
6897 * to establish a point from which recovery can roll forward without
6898 * replaying the entire recovery log.
6900 * Returns true if a new restartpoint was established. We can only establish
6901 * a restartpoint if we have replayed a safe checkpoint record since last
6902 * restartpoint.
6904 bool
6905 CreateRestartPoint(int flags)
6907 XLogRecPtr lastCheckPointRecPtr;
6908 XLogRecPtr lastCheckPointEndPtr;
6909 CheckPoint lastCheckPoint;
6910 XLogRecPtr PriorRedoPtr;
6911 XLogRecPtr receivePtr;
6912 XLogRecPtr replayPtr;
6913 TimeLineID replayTLI;
6914 XLogRecPtr endptr;
6915 XLogSegNo _logSegNo;
6916 TimestampTz xtime;
6918 /* Get a local copy of the last safe checkpoint record. */
6919 SpinLockAcquire(&XLogCtl->info_lck);
6920 lastCheckPointRecPtr = XLogCtl->lastCheckPointRecPtr;
6921 lastCheckPointEndPtr = XLogCtl->lastCheckPointEndPtr;
6922 lastCheckPoint = XLogCtl->lastCheckPoint;
6923 SpinLockRelease(&XLogCtl->info_lck);
6926 * Check that we're still in recovery mode. It's ok if we exit recovery
6927 * mode after this check, the restart point is valid anyway.
6929 if (!RecoveryInProgress())
6931 ereport(DEBUG2,
6932 (errmsg_internal("skipping restartpoint, recovery has already ended")));
6933 return false;
6937 * If the last checkpoint record we've replayed is already our last
6938 * restartpoint, we can't perform a new restart point. We still update
6939 * minRecoveryPoint in that case, so that if this is a shutdown restart
6940 * point, we won't start up earlier than before. That's not strictly
6941 * necessary, but when hot standby is enabled, it would be rather weird if
6942 * the database opened up for read-only connections at a point-in-time
6943 * before the last shutdown. Such time travel is still possible in case of
6944 * immediate shutdown, though.
6946 * We don't explicitly advance minRecoveryPoint when we do create a
6947 * restartpoint. It's assumed that flushing the buffers will do that as a
6948 * side-effect.
6950 if (XLogRecPtrIsInvalid(lastCheckPointRecPtr) ||
6951 lastCheckPoint.redo <= ControlFile->checkPointCopy.redo)
6953 ereport(DEBUG2,
6954 (errmsg_internal("skipping restartpoint, already performed at %X/%X",
6955 LSN_FORMAT_ARGS(lastCheckPoint.redo))));
6957 UpdateMinRecoveryPoint(InvalidXLogRecPtr, true);
6958 if (flags & CHECKPOINT_IS_SHUTDOWN)
6960 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
6961 ControlFile->state = DB_SHUTDOWNED_IN_RECOVERY;
6962 UpdateControlFile();
6963 LWLockRelease(ControlFileLock);
6965 return false;
6969 * Update the shared RedoRecPtr so that the startup process can calculate
6970 * the number of segments replayed since last restartpoint, and request a
6971 * restartpoint if it exceeds CheckPointSegments.
6973 * Like in CreateCheckPoint(), hold off insertions to update it, although
6974 * during recovery this is just pro forma, because no WAL insertions are
6975 * happening.
6977 WALInsertLockAcquireExclusive();
6978 RedoRecPtr = XLogCtl->Insert.RedoRecPtr = lastCheckPoint.redo;
6979 WALInsertLockRelease();
6981 /* Also update the info_lck-protected copy */
6982 SpinLockAcquire(&XLogCtl->info_lck);
6983 XLogCtl->RedoRecPtr = lastCheckPoint.redo;
6984 SpinLockRelease(&XLogCtl->info_lck);
6987 * Prepare to accumulate statistics.
6989 * Note: because it is possible for log_checkpoints to change while a
6990 * checkpoint proceeds, we always accumulate stats, even if
6991 * log_checkpoints is currently off.
6993 MemSet(&CheckpointStats, 0, sizeof(CheckpointStats));
6994 CheckpointStats.ckpt_start_t = GetCurrentTimestamp();
6996 if (log_checkpoints)
6997 LogCheckpointStart(flags, true);
6999 /* Update the process title */
7000 update_checkpoint_display(flags, true, false);
7002 CheckPointGuts(lastCheckPoint.redo, flags);
7005 * Remember the prior checkpoint's redo ptr for
7006 * UpdateCheckPointDistanceEstimate()
7008 PriorRedoPtr = ControlFile->checkPointCopy.redo;
7011 * Update pg_control, using current time. Check that it still shows
7012 * DB_IN_ARCHIVE_RECOVERY state and an older checkpoint, else do nothing;
7013 * this is a quick hack to make sure nothing really bad happens if somehow
7014 * we get here after the end-of-recovery checkpoint.
7016 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
7017 if (ControlFile->state == DB_IN_ARCHIVE_RECOVERY &&
7018 ControlFile->checkPointCopy.redo < lastCheckPoint.redo)
7020 ControlFile->checkPoint = lastCheckPointRecPtr;
7021 ControlFile->checkPointCopy = lastCheckPoint;
7024 * Ensure minRecoveryPoint is past the checkpoint record. Normally,
7025 * this will have happened already while writing out dirty buffers,
7026 * but not necessarily - e.g. because no buffers were dirtied. We do
7027 * this because a non-exclusive base backup uses minRecoveryPoint to
7028 * determine which WAL files must be included in the backup, and the
7029 * file (or files) containing the checkpoint record must be included,
7030 * at a minimum. Note that for an ordinary restart of recovery there's
7031 * no value in having the minimum recovery point any earlier than this
7032 * anyway, because redo will begin just after the checkpoint record.
7034 if (ControlFile->minRecoveryPoint < lastCheckPointEndPtr)
7036 ControlFile->minRecoveryPoint = lastCheckPointEndPtr;
7037 ControlFile->minRecoveryPointTLI = lastCheckPoint.ThisTimeLineID;
7039 /* update local copy */
7040 LocalMinRecoveryPoint = ControlFile->minRecoveryPoint;
7041 LocalMinRecoveryPointTLI = ControlFile->minRecoveryPointTLI;
7043 if (flags & CHECKPOINT_IS_SHUTDOWN)
7044 ControlFile->state = DB_SHUTDOWNED_IN_RECOVERY;
7045 UpdateControlFile();
7047 LWLockRelease(ControlFileLock);
7050 * Update the average distance between checkpoints/restartpoints if the
7051 * prior checkpoint exists.
7053 if (PriorRedoPtr != InvalidXLogRecPtr)
7054 UpdateCheckPointDistanceEstimate(RedoRecPtr - PriorRedoPtr);
7057 * Delete old log files, those no longer needed for last restartpoint to
7058 * prevent the disk holding the xlog from growing full.
7060 XLByteToSeg(RedoRecPtr, _logSegNo, wal_segment_size);
7063 * Retreat _logSegNo using the current end of xlog replayed or received,
7064 * whichever is later.
7066 receivePtr = GetWalRcvFlushRecPtr(NULL, NULL);
7067 replayPtr = GetXLogReplayRecPtr(&replayTLI);
7068 endptr = (receivePtr < replayPtr) ? replayPtr : receivePtr;
7069 KeepLogSeg(endptr, &_logSegNo);
7070 if (InvalidateObsoleteReplicationSlots(_logSegNo))
7073 * Some slots have been invalidated; recalculate the old-segment
7074 * horizon, starting again from RedoRecPtr.
7076 XLByteToSeg(RedoRecPtr, _logSegNo, wal_segment_size);
7077 KeepLogSeg(endptr, &_logSegNo);
7079 _logSegNo--;
7082 * Try to recycle segments on a useful timeline. If we've been promoted
7083 * since the beginning of this restartpoint, use the new timeline chosen
7084 * at end of recovery. If we're still in recovery, use the timeline we're
7085 * currently replaying.
7087 * There is no guarantee that the WAL segments will be useful on the
7088 * current timeline; if recovery proceeds to a new timeline right after
7089 * this, the pre-allocated WAL segments on this timeline will not be used,
7090 * and will go wasted until recycled on the next restartpoint. We'll live
7091 * with that.
7093 if (!RecoveryInProgress())
7094 replayTLI = XLogCtl->InsertTimeLineID;
7096 RemoveOldXlogFiles(_logSegNo, RedoRecPtr, endptr, replayTLI);
7099 * Make more log segments if needed. (Do this after recycling old log
7100 * segments, since that may supply some of the needed files.)
7102 PreallocXlogFiles(endptr, replayTLI);
7105 * Truncate pg_subtrans if possible. We can throw away all data before
7106 * the oldest XMIN of any running transaction. No future transaction will
7107 * attempt to reference any pg_subtrans entry older than that (see Asserts
7108 * in subtrans.c). When hot standby is disabled, though, we mustn't do
7109 * this because StartupSUBTRANS hasn't been called yet.
7111 if (EnableHotStandby)
7112 TruncateSUBTRANS(GetOldestTransactionIdConsideredRunning());
7114 /* Real work is done; log and update stats. */
7115 LogCheckpointEnd(true);
7117 /* Reset the process title */
7118 update_checkpoint_display(flags, true, true);
7120 xtime = GetLatestXTime();
7121 ereport((log_checkpoints ? LOG : DEBUG2),
7122 (errmsg("recovery restart point at %X/%X",
7123 LSN_FORMAT_ARGS(lastCheckPoint.redo)),
7124 xtime ? errdetail("Last completed transaction was at log time %s.",
7125 timestamptz_to_str(xtime)) : 0));
7128 * Finally, execute archive_cleanup_command, if any.
7130 if (archiveCleanupCommand && strcmp(archiveCleanupCommand, "") != 0)
7131 ExecuteRecoveryCommand(archiveCleanupCommand,
7132 "archive_cleanup_command",
7133 false,
7134 WAIT_EVENT_ARCHIVE_CLEANUP_COMMAND);
7136 return true;
7140 * Report availability of WAL for the given target LSN
7141 * (typically a slot's restart_lsn)
7143 * Returns one of the following enum values:
7145 * * WALAVAIL_RESERVED means targetLSN is available and it is in the range of
7146 * max_wal_size.
7148 * * WALAVAIL_EXTENDED means it is still available by preserving extra
7149 * segments beyond max_wal_size. If max_slot_wal_keep_size is smaller
7150 * than max_wal_size, this state is not returned.
7152 * * WALAVAIL_UNRESERVED means it is being lost and the next checkpoint will
7153 * remove reserved segments. The walsender using this slot may return to the
7154 * above.
7156 * * WALAVAIL_REMOVED means it has been removed. A replication stream on
7157 * a slot with this LSN cannot continue after a restart.
7159 * * WALAVAIL_INVALID_LSN means the slot hasn't been set to reserve WAL.
7161 WALAvailability
7162 GetWALAvailability(XLogRecPtr targetLSN)
7164 XLogRecPtr currpos; /* current write LSN */
7165 XLogSegNo currSeg; /* segid of currpos */
7166 XLogSegNo targetSeg; /* segid of targetLSN */
7167 XLogSegNo oldestSeg; /* actual oldest segid */
7168 XLogSegNo oldestSegMaxWalSize; /* oldest segid kept by max_wal_size */
7169 XLogSegNo oldestSlotSeg; /* oldest segid kept by slot */
7170 uint64 keepSegs;
7173 * slot does not reserve WAL. Either deactivated, or has never been active
7175 if (XLogRecPtrIsInvalid(targetLSN))
7176 return WALAVAIL_INVALID_LSN;
7179 * Calculate the oldest segment currently reserved by all slots,
7180 * considering wal_keep_size and max_slot_wal_keep_size. Initialize
7181 * oldestSlotSeg to the current segment.
7183 currpos = GetXLogWriteRecPtr();
7184 XLByteToSeg(currpos, oldestSlotSeg, wal_segment_size);
7185 KeepLogSeg(currpos, &oldestSlotSeg);
7188 * Find the oldest extant segment file. We get 1 until checkpoint removes
7189 * the first WAL segment file since startup, which causes the status being
7190 * wrong under certain abnormal conditions but that doesn't actually harm.
7192 oldestSeg = XLogGetLastRemovedSegno() + 1;
7194 /* calculate oldest segment by max_wal_size */
7195 XLByteToSeg(currpos, currSeg, wal_segment_size);
7196 keepSegs = ConvertToXSegs(max_wal_size_mb, wal_segment_size) + 1;
7198 if (currSeg > keepSegs)
7199 oldestSegMaxWalSize = currSeg - keepSegs;
7200 else
7201 oldestSegMaxWalSize = 1;
7203 /* the segment we care about */
7204 XLByteToSeg(targetLSN, targetSeg, wal_segment_size);
7207 * No point in returning reserved or extended status values if the
7208 * targetSeg is known to be lost.
7210 if (targetSeg >= oldestSlotSeg)
7212 /* show "reserved" when targetSeg is within max_wal_size */
7213 if (targetSeg >= oldestSegMaxWalSize)
7214 return WALAVAIL_RESERVED;
7216 /* being retained by slots exceeding max_wal_size */
7217 return WALAVAIL_EXTENDED;
7220 /* WAL segments are no longer retained but haven't been removed yet */
7221 if (targetSeg >= oldestSeg)
7222 return WALAVAIL_UNRESERVED;
7224 /* Definitely lost */
7225 return WALAVAIL_REMOVED;
7230 * Retreat *logSegNo to the last segment that we need to retain because of
7231 * either wal_keep_size or replication slots.
7233 * This is calculated by subtracting wal_keep_size from the given xlog
7234 * location, recptr and by making sure that that result is below the
7235 * requirement of replication slots. For the latter criterion we do consider
7236 * the effects of max_slot_wal_keep_size: reserve at most that much space back
7237 * from recptr.
7239 * Note about replication slots: if this function calculates a value
7240 * that's further ahead than what slots need reserved, then affected
7241 * slots need to be invalidated and this function invoked again.
7242 * XXX it might be a good idea to rewrite this function so that
7243 * invalidation is optionally done here, instead.
7245 static void
7246 KeepLogSeg(XLogRecPtr recptr, XLogSegNo *logSegNo)
7248 XLogSegNo currSegNo;
7249 XLogSegNo segno;
7250 XLogRecPtr keep;
7252 XLByteToSeg(recptr, currSegNo, wal_segment_size);
7253 segno = currSegNo;
7256 * Calculate how many segments are kept by slots first, adjusting for
7257 * max_slot_wal_keep_size.
7259 keep = XLogGetReplicationSlotMinimumLSN();
7260 if (keep != InvalidXLogRecPtr)
7262 XLByteToSeg(keep, segno, wal_segment_size);
7264 /* Cap by max_slot_wal_keep_size ... */
7265 if (max_slot_wal_keep_size_mb >= 0)
7267 uint64 slot_keep_segs;
7269 slot_keep_segs =
7270 ConvertToXSegs(max_slot_wal_keep_size_mb, wal_segment_size);
7272 if (currSegNo - segno > slot_keep_segs)
7273 segno = currSegNo - slot_keep_segs;
7277 /* but, keep at least wal_keep_size if that's set */
7278 if (wal_keep_size_mb > 0)
7280 uint64 keep_segs;
7282 keep_segs = ConvertToXSegs(wal_keep_size_mb, wal_segment_size);
7283 if (currSegNo - segno < keep_segs)
7285 /* avoid underflow, don't go below 1 */
7286 if (currSegNo <= keep_segs)
7287 segno = 1;
7288 else
7289 segno = currSegNo - keep_segs;
7293 /* don't delete WAL segments newer than the calculated segment */
7294 if (segno < *logSegNo)
7295 *logSegNo = segno;
7299 * Write a NEXTOID log record
7301 void
7302 XLogPutNextOid(Oid nextOid)
7304 XLogBeginInsert();
7305 XLogRegisterData((char *) (&nextOid), sizeof(Oid));
7306 (void) XLogInsert(RM_XLOG_ID, XLOG_NEXTOID);
7309 * We need not flush the NEXTOID record immediately, because any of the
7310 * just-allocated OIDs could only reach disk as part of a tuple insert or
7311 * update that would have its own XLOG record that must follow the NEXTOID
7312 * record. Therefore, the standard buffer LSN interlock applied to those
7313 * records will ensure no such OID reaches disk before the NEXTOID record
7314 * does.
7316 * Note, however, that the above statement only covers state "within" the
7317 * database. When we use a generated OID as a file or directory name, we
7318 * are in a sense violating the basic WAL rule, because that filesystem
7319 * change may reach disk before the NEXTOID WAL record does. The impact
7320 * of this is that if a database crash occurs immediately afterward, we
7321 * might after restart re-generate the same OID and find that it conflicts
7322 * with the leftover file or directory. But since for safety's sake we
7323 * always loop until finding a nonconflicting filename, this poses no real
7324 * problem in practice. See pgsql-hackers discussion 27-Sep-2006.
7329 * Write an XLOG SWITCH record.
7331 * Here we just blindly issue an XLogInsert request for the record.
7332 * All the magic happens inside XLogInsert.
7334 * The return value is either the end+1 address of the switch record,
7335 * or the end+1 address of the prior segment if we did not need to
7336 * write a switch record because we are already at segment start.
7338 XLogRecPtr
7339 RequestXLogSwitch(bool mark_unimportant)
7341 XLogRecPtr RecPtr;
7343 /* XLOG SWITCH has no data */
7344 XLogBeginInsert();
7346 if (mark_unimportant)
7347 XLogSetRecordFlags(XLOG_MARK_UNIMPORTANT);
7348 RecPtr = XLogInsert(RM_XLOG_ID, XLOG_SWITCH);
7350 return RecPtr;
7354 * Write a RESTORE POINT record
7356 XLogRecPtr
7357 XLogRestorePoint(const char *rpName)
7359 XLogRecPtr RecPtr;
7360 xl_restore_point xlrec;
7362 xlrec.rp_time = GetCurrentTimestamp();
7363 strlcpy(xlrec.rp_name, rpName, MAXFNAMELEN);
7365 XLogBeginInsert();
7366 XLogRegisterData((char *) &xlrec, sizeof(xl_restore_point));
7368 RecPtr = XLogInsert(RM_XLOG_ID, XLOG_RESTORE_POINT);
7370 ereport(LOG,
7371 (errmsg("restore point \"%s\" created at %X/%X",
7372 rpName, LSN_FORMAT_ARGS(RecPtr))));
7374 return RecPtr;
7378 * Check if any of the GUC parameters that are critical for hot standby
7379 * have changed, and update the value in pg_control file if necessary.
7381 static void
7382 XLogReportParameters(void)
7384 if (wal_level != ControlFile->wal_level ||
7385 wal_log_hints != ControlFile->wal_log_hints ||
7386 MaxConnections != ControlFile->MaxConnections ||
7387 max_worker_processes != ControlFile->max_worker_processes ||
7388 max_wal_senders != ControlFile->max_wal_senders ||
7389 max_prepared_xacts != ControlFile->max_prepared_xacts ||
7390 max_locks_per_xact != ControlFile->max_locks_per_xact ||
7391 track_commit_timestamp != ControlFile->track_commit_timestamp)
7394 * The change in number of backend slots doesn't need to be WAL-logged
7395 * if archiving is not enabled, as you can't start archive recovery
7396 * with wal_level=minimal anyway. We don't really care about the
7397 * values in pg_control either if wal_level=minimal, but seems better
7398 * to keep them up-to-date to avoid confusion.
7400 if (wal_level != ControlFile->wal_level || XLogIsNeeded())
7402 xl_parameter_change xlrec;
7403 XLogRecPtr recptr;
7405 xlrec.MaxConnections = MaxConnections;
7406 xlrec.max_worker_processes = max_worker_processes;
7407 xlrec.max_wal_senders = max_wal_senders;
7408 xlrec.max_prepared_xacts = max_prepared_xacts;
7409 xlrec.max_locks_per_xact = max_locks_per_xact;
7410 xlrec.wal_level = wal_level;
7411 xlrec.wal_log_hints = wal_log_hints;
7412 xlrec.track_commit_timestamp = track_commit_timestamp;
7414 XLogBeginInsert();
7415 XLogRegisterData((char *) &xlrec, sizeof(xlrec));
7417 recptr = XLogInsert(RM_XLOG_ID, XLOG_PARAMETER_CHANGE);
7418 XLogFlush(recptr);
7421 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
7423 ControlFile->MaxConnections = MaxConnections;
7424 ControlFile->max_worker_processes = max_worker_processes;
7425 ControlFile->max_wal_senders = max_wal_senders;
7426 ControlFile->max_prepared_xacts = max_prepared_xacts;
7427 ControlFile->max_locks_per_xact = max_locks_per_xact;
7428 ControlFile->wal_level = wal_level;
7429 ControlFile->wal_log_hints = wal_log_hints;
7430 ControlFile->track_commit_timestamp = track_commit_timestamp;
7431 UpdateControlFile();
7433 LWLockRelease(ControlFileLock);
7438 * Update full_page_writes in shared memory, and write an
7439 * XLOG_FPW_CHANGE record if necessary.
7441 * Note: this function assumes there is no other process running
7442 * concurrently that could update it.
7444 void
7445 UpdateFullPageWrites(void)
7447 XLogCtlInsert *Insert = &XLogCtl->Insert;
7448 bool recoveryInProgress;
7451 * Do nothing if full_page_writes has not been changed.
7453 * It's safe to check the shared full_page_writes without the lock,
7454 * because we assume that there is no concurrently running process which
7455 * can update it.
7457 if (fullPageWrites == Insert->fullPageWrites)
7458 return;
7461 * Perform this outside critical section so that the WAL insert
7462 * initialization done by RecoveryInProgress() doesn't trigger an
7463 * assertion failure.
7465 recoveryInProgress = RecoveryInProgress();
7467 START_CRIT_SECTION();
7470 * It's always safe to take full page images, even when not strictly
7471 * required, but not the other round. So if we're setting full_page_writes
7472 * to true, first set it true and then write the WAL record. If we're
7473 * setting it to false, first write the WAL record and then set the global
7474 * flag.
7476 if (fullPageWrites)
7478 WALInsertLockAcquireExclusive();
7479 Insert->fullPageWrites = true;
7480 WALInsertLockRelease();
7484 * Write an XLOG_FPW_CHANGE record. This allows us to keep track of
7485 * full_page_writes during archive recovery, if required.
7487 if (XLogStandbyInfoActive() && !recoveryInProgress)
7489 XLogBeginInsert();
7490 XLogRegisterData((char *) (&fullPageWrites), sizeof(bool));
7492 XLogInsert(RM_XLOG_ID, XLOG_FPW_CHANGE);
7495 if (!fullPageWrites)
7497 WALInsertLockAcquireExclusive();
7498 Insert->fullPageWrites = false;
7499 WALInsertLockRelease();
7501 END_CRIT_SECTION();
7505 * XLOG resource manager's routines
7507 * Definitions of info values are in include/catalog/pg_control.h, though
7508 * not all record types are related to control file updates.
7510 * NOTE: Some XLOG record types that are directly related to WAL recovery
7511 * are handled in xlogrecovery_redo().
7513 void
7514 xlog_redo(XLogReaderState *record)
7516 uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
7517 XLogRecPtr lsn = record->EndRecPtr;
7520 * In XLOG rmgr, backup blocks are only used by XLOG_FPI and
7521 * XLOG_FPI_FOR_HINT records.
7523 Assert(info == XLOG_FPI || info == XLOG_FPI_FOR_HINT ||
7524 !XLogRecHasAnyBlockRefs(record));
7526 if (info == XLOG_NEXTOID)
7528 Oid nextOid;
7531 * We used to try to take the maximum of ShmemVariableCache->nextOid
7532 * and the recorded nextOid, but that fails if the OID counter wraps
7533 * around. Since no OID allocation should be happening during replay
7534 * anyway, better to just believe the record exactly. We still take
7535 * OidGenLock while setting the variable, just in case.
7537 memcpy(&nextOid, XLogRecGetData(record), sizeof(Oid));
7538 LWLockAcquire(OidGenLock, LW_EXCLUSIVE);
7539 ShmemVariableCache->nextOid = nextOid;
7540 ShmemVariableCache->oidCount = 0;
7541 LWLockRelease(OidGenLock);
7543 else if (info == XLOG_CHECKPOINT_SHUTDOWN)
7545 CheckPoint checkPoint;
7546 TimeLineID replayTLI;
7548 memcpy(&checkPoint, XLogRecGetData(record), sizeof(CheckPoint));
7549 /* In a SHUTDOWN checkpoint, believe the counters exactly */
7550 LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
7551 ShmemVariableCache->nextXid = checkPoint.nextXid;
7552 LWLockRelease(XidGenLock);
7553 LWLockAcquire(OidGenLock, LW_EXCLUSIVE);
7554 ShmemVariableCache->nextOid = checkPoint.nextOid;
7555 ShmemVariableCache->oidCount = 0;
7556 LWLockRelease(OidGenLock);
7557 MultiXactSetNextMXact(checkPoint.nextMulti,
7558 checkPoint.nextMultiOffset);
7560 MultiXactAdvanceOldest(checkPoint.oldestMulti,
7561 checkPoint.oldestMultiDB);
7564 * No need to set oldestClogXid here as well; it'll be set when we
7565 * redo an xl_clog_truncate if it changed since initialization.
7567 SetTransactionIdLimit(checkPoint.oldestXid, checkPoint.oldestXidDB);
7570 * If we see a shutdown checkpoint while waiting for an end-of-backup
7571 * record, the backup was canceled and the end-of-backup record will
7572 * never arrive.
7574 if (ArchiveRecoveryRequested &&
7575 !XLogRecPtrIsInvalid(ControlFile->backupStartPoint) &&
7576 XLogRecPtrIsInvalid(ControlFile->backupEndPoint))
7577 ereport(PANIC,
7578 (errmsg("online backup was canceled, recovery cannot continue")));
7581 * If we see a shutdown checkpoint, we know that nothing was running
7582 * on the primary at this point. So fake-up an empty running-xacts
7583 * record and use that here and now. Recover additional standby state
7584 * for prepared transactions.
7586 if (standbyState >= STANDBY_INITIALIZED)
7588 TransactionId *xids;
7589 int nxids;
7590 TransactionId oldestActiveXID;
7591 TransactionId latestCompletedXid;
7592 RunningTransactionsData running;
7594 oldestActiveXID = PrescanPreparedTransactions(&xids, &nxids);
7597 * Construct a RunningTransactions snapshot representing a shut
7598 * down server, with only prepared transactions still alive. We're
7599 * never overflowed at this point because all subxids are listed
7600 * with their parent prepared transactions.
7602 running.xcnt = nxids;
7603 running.subxcnt = 0;
7604 running.subxid_overflow = false;
7605 running.nextXid = XidFromFullTransactionId(checkPoint.nextXid);
7606 running.oldestRunningXid = oldestActiveXID;
7607 latestCompletedXid = XidFromFullTransactionId(checkPoint.nextXid);
7608 TransactionIdRetreat(latestCompletedXid);
7609 Assert(TransactionIdIsNormal(latestCompletedXid));
7610 running.latestCompletedXid = latestCompletedXid;
7611 running.xids = xids;
7613 ProcArrayApplyRecoveryInfo(&running);
7615 StandbyRecoverPreparedTransactions();
7618 /* ControlFile->checkPointCopy always tracks the latest ckpt XID */
7619 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
7620 ControlFile->checkPointCopy.nextXid = checkPoint.nextXid;
7621 LWLockRelease(ControlFileLock);
7623 /* Update shared-memory copy of checkpoint XID/epoch */
7624 SpinLockAcquire(&XLogCtl->info_lck);
7625 XLogCtl->ckptFullXid = checkPoint.nextXid;
7626 SpinLockRelease(&XLogCtl->info_lck);
7629 * We should've already switched to the new TLI before replaying this
7630 * record.
7632 (void) GetCurrentReplayRecPtr(&replayTLI);
7633 if (checkPoint.ThisTimeLineID != replayTLI)
7634 ereport(PANIC,
7635 (errmsg("unexpected timeline ID %u (should be %u) in shutdown checkpoint record",
7636 checkPoint.ThisTimeLineID, replayTLI)));
7638 RecoveryRestartPoint(&checkPoint, record);
7640 else if (info == XLOG_CHECKPOINT_ONLINE)
7642 CheckPoint checkPoint;
7643 TimeLineID replayTLI;
7645 memcpy(&checkPoint, XLogRecGetData(record), sizeof(CheckPoint));
7646 /* In an ONLINE checkpoint, treat the XID counter as a minimum */
7647 LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
7648 if (FullTransactionIdPrecedes(ShmemVariableCache->nextXid,
7649 checkPoint.nextXid))
7650 ShmemVariableCache->nextXid = checkPoint.nextXid;
7651 LWLockRelease(XidGenLock);
7654 * We ignore the nextOid counter in an ONLINE checkpoint, preferring
7655 * to track OID assignment through XLOG_NEXTOID records. The nextOid
7656 * counter is from the start of the checkpoint and might well be stale
7657 * compared to later XLOG_NEXTOID records. We could try to take the
7658 * maximum of the nextOid counter and our latest value, but since
7659 * there's no particular guarantee about the speed with which the OID
7660 * counter wraps around, that's a risky thing to do. In any case,
7661 * users of the nextOid counter are required to avoid assignment of
7662 * duplicates, so that a somewhat out-of-date value should be safe.
7665 /* Handle multixact */
7666 MultiXactAdvanceNextMXact(checkPoint.nextMulti,
7667 checkPoint.nextMultiOffset);
7670 * NB: This may perform multixact truncation when replaying WAL
7671 * generated by an older primary.
7673 MultiXactAdvanceOldest(checkPoint.oldestMulti,
7674 checkPoint.oldestMultiDB);
7675 if (TransactionIdPrecedes(ShmemVariableCache->oldestXid,
7676 checkPoint.oldestXid))
7677 SetTransactionIdLimit(checkPoint.oldestXid,
7678 checkPoint.oldestXidDB);
7679 /* ControlFile->checkPointCopy always tracks the latest ckpt XID */
7680 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
7681 ControlFile->checkPointCopy.nextXid = checkPoint.nextXid;
7682 LWLockRelease(ControlFileLock);
7684 /* Update shared-memory copy of checkpoint XID/epoch */
7685 SpinLockAcquire(&XLogCtl->info_lck);
7686 XLogCtl->ckptFullXid = checkPoint.nextXid;
7687 SpinLockRelease(&XLogCtl->info_lck);
7689 /* TLI should not change in an on-line checkpoint */
7690 (void) GetCurrentReplayRecPtr(&replayTLI);
7691 if (checkPoint.ThisTimeLineID != replayTLI)
7692 ereport(PANIC,
7693 (errmsg("unexpected timeline ID %u (should be %u) in online checkpoint record",
7694 checkPoint.ThisTimeLineID, replayTLI)));
7696 RecoveryRestartPoint(&checkPoint, record);
7698 else if (info == XLOG_OVERWRITE_CONTRECORD)
7700 /* nothing to do here, handled in xlogrecovery_redo() */
7702 else if (info == XLOG_END_OF_RECOVERY)
7704 xl_end_of_recovery xlrec;
7705 TimeLineID replayTLI;
7707 memcpy(&xlrec, XLogRecGetData(record), sizeof(xl_end_of_recovery));
7710 * For Hot Standby, we could treat this like a Shutdown Checkpoint,
7711 * but this case is rarer and harder to test, so the benefit doesn't
7712 * outweigh the potential extra cost of maintenance.
7716 * We should've already switched to the new TLI before replaying this
7717 * record.
7719 (void) GetCurrentReplayRecPtr(&replayTLI);
7720 if (xlrec.ThisTimeLineID != replayTLI)
7721 ereport(PANIC,
7722 (errmsg("unexpected timeline ID %u (should be %u) in end-of-recovery record",
7723 xlrec.ThisTimeLineID, replayTLI)));
7725 else if (info == XLOG_NOOP)
7727 /* nothing to do here */
7729 else if (info == XLOG_SWITCH)
7731 /* nothing to do here */
7733 else if (info == XLOG_RESTORE_POINT)
7735 /* nothing to do here, handled in xlogrecovery.c */
7737 else if (info == XLOG_FPI || info == XLOG_FPI_FOR_HINT)
7740 * XLOG_FPI records contain nothing else but one or more block
7741 * references. Every block reference must include a full-page image
7742 * even if full_page_writes was disabled when the record was generated
7743 * - otherwise there would be no point in this record.
7745 * XLOG_FPI_FOR_HINT records are generated when a page needs to be
7746 * WAL-logged because of a hint bit update. They are only generated
7747 * when checksums and/or wal_log_hints are enabled. They may include
7748 * no full-page images if full_page_writes was disabled when they were
7749 * generated. In this case there is nothing to do here.
7751 * No recovery conflicts are generated by these generic records - if a
7752 * resource manager needs to generate conflicts, it has to define a
7753 * separate WAL record type and redo routine.
7755 for (uint8 block_id = 0; block_id <= XLogRecMaxBlockId(record); block_id++)
7757 Buffer buffer;
7759 if (!XLogRecHasBlockImage(record, block_id))
7761 if (info == XLOG_FPI)
7762 elog(ERROR, "XLOG_FPI record did not contain a full-page image");
7763 continue;
7766 if (XLogReadBufferForRedo(record, block_id, &buffer) != BLK_RESTORED)
7767 elog(ERROR, "unexpected XLogReadBufferForRedo result when restoring backup block");
7768 UnlockReleaseBuffer(buffer);
7771 else if (info == XLOG_BACKUP_END)
7773 /* nothing to do here, handled in xlogrecovery_redo() */
7775 else if (info == XLOG_PARAMETER_CHANGE)
7777 xl_parameter_change xlrec;
7779 /* Update our copy of the parameters in pg_control */
7780 memcpy(&xlrec, XLogRecGetData(record), sizeof(xl_parameter_change));
7782 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
7783 ControlFile->MaxConnections = xlrec.MaxConnections;
7784 ControlFile->max_worker_processes = xlrec.max_worker_processes;
7785 ControlFile->max_wal_senders = xlrec.max_wal_senders;
7786 ControlFile->max_prepared_xacts = xlrec.max_prepared_xacts;
7787 ControlFile->max_locks_per_xact = xlrec.max_locks_per_xact;
7788 ControlFile->wal_level = xlrec.wal_level;
7789 ControlFile->wal_log_hints = xlrec.wal_log_hints;
7792 * Update minRecoveryPoint to ensure that if recovery is aborted, we
7793 * recover back up to this point before allowing hot standby again.
7794 * This is important if the max_* settings are decreased, to ensure
7795 * you don't run queries against the WAL preceding the change. The
7796 * local copies cannot be updated as long as crash recovery is
7797 * happening and we expect all the WAL to be replayed.
7799 if (InArchiveRecovery)
7801 LocalMinRecoveryPoint = ControlFile->minRecoveryPoint;
7802 LocalMinRecoveryPointTLI = ControlFile->minRecoveryPointTLI;
7804 if (LocalMinRecoveryPoint != InvalidXLogRecPtr && LocalMinRecoveryPoint < lsn)
7806 TimeLineID replayTLI;
7808 (void) GetCurrentReplayRecPtr(&replayTLI);
7809 ControlFile->minRecoveryPoint = lsn;
7810 ControlFile->minRecoveryPointTLI = replayTLI;
7813 CommitTsParameterChange(xlrec.track_commit_timestamp,
7814 ControlFile->track_commit_timestamp);
7815 ControlFile->track_commit_timestamp = xlrec.track_commit_timestamp;
7817 UpdateControlFile();
7818 LWLockRelease(ControlFileLock);
7820 /* Check to see if any parameter change gives a problem on recovery */
7821 CheckRequiredParameterValues();
7823 else if (info == XLOG_FPW_CHANGE)
7825 bool fpw;
7827 memcpy(&fpw, XLogRecGetData(record), sizeof(bool));
7830 * Update the LSN of the last replayed XLOG_FPW_CHANGE record so that
7831 * do_pg_start_backup() and do_pg_stop_backup() can check whether
7832 * full_page_writes has been disabled during online backup.
7834 if (!fpw)
7836 SpinLockAcquire(&XLogCtl->info_lck);
7837 if (XLogCtl->lastFpwDisableRecPtr < record->ReadRecPtr)
7838 XLogCtl->lastFpwDisableRecPtr = record->ReadRecPtr;
7839 SpinLockRelease(&XLogCtl->info_lck);
7842 /* Keep track of full_page_writes */
7843 lastFullPageWrites = fpw;
7848 * Return the (possible) sync flag used for opening a file, depending on the
7849 * value of the GUC wal_sync_method.
7851 static int
7852 get_sync_bit(int method)
7854 int o_direct_flag = 0;
7856 /* If fsync is disabled, never open in sync mode */
7857 if (!enableFsync)
7858 return 0;
7861 * Optimize writes by bypassing kernel cache with O_DIRECT when using
7862 * O_SYNC/O_FSYNC and O_DSYNC. But only if archiving and streaming are
7863 * disabled, otherwise the archive command or walsender process will read
7864 * the WAL soon after writing it, which is guaranteed to cause a physical
7865 * read if we bypassed the kernel cache. We also skip the
7866 * posix_fadvise(POSIX_FADV_DONTNEED) call in XLogFileClose() for the same
7867 * reason.
7869 * Never use O_DIRECT in walreceiver process for similar reasons; the WAL
7870 * written by walreceiver is normally read by the startup process soon
7871 * after it's written. Also, walreceiver performs unaligned writes, which
7872 * don't work with O_DIRECT, so it is required for correctness too.
7874 if (!XLogIsNeeded() && !AmWalReceiverProcess())
7875 o_direct_flag = PG_O_DIRECT;
7877 switch (method)
7880 * enum values for all sync options are defined even if they are
7881 * not supported on the current platform. But if not, they are
7882 * not included in the enum option array, and therefore will never
7883 * be seen here.
7885 case SYNC_METHOD_FSYNC:
7886 case SYNC_METHOD_FSYNC_WRITETHROUGH:
7887 case SYNC_METHOD_FDATASYNC:
7888 return 0;
7889 #ifdef OPEN_SYNC_FLAG
7890 case SYNC_METHOD_OPEN:
7891 return OPEN_SYNC_FLAG | o_direct_flag;
7892 #endif
7893 #ifdef OPEN_DATASYNC_FLAG
7894 case SYNC_METHOD_OPEN_DSYNC:
7895 return OPEN_DATASYNC_FLAG | o_direct_flag;
7896 #endif
7897 default:
7898 /* can't happen (unless we are out of sync with option array) */
7899 elog(ERROR, "unrecognized wal_sync_method: %d", method);
7900 return 0; /* silence warning */
7905 * GUC support
7907 void
7908 assign_xlog_sync_method(int new_sync_method, void *extra)
7910 if (sync_method != new_sync_method)
7913 * To ensure that no blocks escape unsynced, force an fsync on the
7914 * currently open log segment (if any). Also, if the open flag is
7915 * changing, close the log file so it will be reopened (with new flag
7916 * bit) at next use.
7918 if (openLogFile >= 0)
7920 pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC_METHOD_ASSIGN);
7921 if (pg_fsync(openLogFile) != 0)
7923 char xlogfname[MAXFNAMELEN];
7924 int save_errno;
7926 save_errno = errno;
7927 XLogFileName(xlogfname, openLogTLI, openLogSegNo,
7928 wal_segment_size);
7929 errno = save_errno;
7930 ereport(PANIC,
7931 (errcode_for_file_access(),
7932 errmsg("could not fsync file \"%s\": %m", xlogfname)));
7935 pgstat_report_wait_end();
7936 if (get_sync_bit(sync_method) != get_sync_bit(new_sync_method))
7937 XLogFileClose();
7944 * Issue appropriate kind of fsync (if any) for an XLOG output file.
7946 * 'fd' is a file descriptor for the XLOG file to be fsync'd.
7947 * 'segno' is for error reporting purposes.
7949 void
7950 issue_xlog_fsync(int fd, XLogSegNo segno, TimeLineID tli)
7952 char *msg = NULL;
7953 instr_time start;
7955 Assert(tli != 0);
7958 * Quick exit if fsync is disabled or write() has already synced the WAL
7959 * file.
7961 if (!enableFsync ||
7962 sync_method == SYNC_METHOD_OPEN ||
7963 sync_method == SYNC_METHOD_OPEN_DSYNC)
7964 return;
7966 /* Measure I/O timing to sync the WAL file */
7967 if (track_wal_io_timing)
7968 INSTR_TIME_SET_CURRENT(start);
7970 pgstat_report_wait_start(WAIT_EVENT_WAL_SYNC);
7971 switch (sync_method)
7973 case SYNC_METHOD_FSYNC:
7974 if (pg_fsync_no_writethrough(fd) != 0)
7975 msg = _("could not fsync file \"%s\": %m");
7976 break;
7977 #ifdef HAVE_FSYNC_WRITETHROUGH
7978 case SYNC_METHOD_FSYNC_WRITETHROUGH:
7979 if (pg_fsync_writethrough(fd) != 0)
7980 msg = _("could not fsync write-through file \"%s\": %m");
7981 break;
7982 #endif
7983 #ifdef HAVE_FDATASYNC
7984 case SYNC_METHOD_FDATASYNC:
7985 if (pg_fdatasync(fd) != 0)
7986 msg = _("could not fdatasync file \"%s\": %m");
7987 break;
7988 #endif
7989 case SYNC_METHOD_OPEN:
7990 case SYNC_METHOD_OPEN_DSYNC:
7991 /* not reachable */
7992 Assert(false);
7993 break;
7994 default:
7995 elog(PANIC, "unrecognized wal_sync_method: %d", sync_method);
7996 break;
7999 /* PANIC if failed to fsync */
8000 if (msg)
8002 char xlogfname[MAXFNAMELEN];
8003 int save_errno = errno;
8005 XLogFileName(xlogfname, tli, segno, wal_segment_size);
8006 errno = save_errno;
8007 ereport(PANIC,
8008 (errcode_for_file_access(),
8009 errmsg(msg, xlogfname)));
8012 pgstat_report_wait_end();
8015 * Increment the I/O timing and the number of times WAL files were synced.
8017 if (track_wal_io_timing)
8019 instr_time duration;
8021 INSTR_TIME_SET_CURRENT(duration);
8022 INSTR_TIME_SUBTRACT(duration, start);
8023 WalStats.m_wal_sync_time += INSTR_TIME_GET_MICROSEC(duration);
8026 WalStats.m_wal_sync++;
8030 * do_pg_start_backup
8032 * Utility function called at the start of an online backup. It creates the
8033 * necessary starting checkpoint and constructs the backup label file.
8035 * There are two kind of backups: exclusive and non-exclusive. An exclusive
8036 * backup is started with pg_start_backup(), and there can be only one active
8037 * at a time. The backup and tablespace map files of an exclusive backup are
8038 * written to $PGDATA/backup_label and $PGDATA/tablespace_map, and they are
8039 * removed by pg_stop_backup().
8041 * A non-exclusive backup is used for the streaming base backups (see
8042 * src/backend/replication/basebackup.c). The difference to exclusive backups
8043 * is that the backup label and tablespace map files are not written to disk.
8044 * Instead, their would-be contents are returned in *labelfile and *tblspcmapfile,
8045 * and the caller is responsible for including them in the backup archive as
8046 * 'backup_label' and 'tablespace_map'. There can be many non-exclusive backups
8047 * active at the same time, and they don't conflict with an exclusive backup
8048 * either.
8050 * labelfile and tblspcmapfile must be passed as NULL when starting an
8051 * exclusive backup, and as initially-empty StringInfos for a non-exclusive
8052 * backup.
8054 * If "tablespaces" isn't NULL, it receives a list of tablespaceinfo structs
8055 * describing the cluster's tablespaces.
8057 * tblspcmapfile is required mainly for tar format in windows as native windows
8058 * utilities are not able to create symlinks while extracting files from tar.
8059 * However for consistency, the same is used for all platforms.
8061 * Returns the minimum WAL location that must be present to restore from this
8062 * backup, and the corresponding timeline ID in *starttli_p.
8064 * Every successfully started non-exclusive backup must be stopped by calling
8065 * do_pg_stop_backup() or do_pg_abort_backup().
8067 * It is the responsibility of the caller of this function to verify the
8068 * permissions of the calling user!
8070 XLogRecPtr
8071 do_pg_start_backup(const char *backupidstr, bool fast, TimeLineID *starttli_p,
8072 StringInfo labelfile, List **tablespaces,
8073 StringInfo tblspcmapfile)
8075 bool exclusive = (labelfile == NULL);
8076 bool backup_started_in_recovery = false;
8077 XLogRecPtr checkpointloc;
8078 XLogRecPtr startpoint;
8079 TimeLineID starttli;
8080 pg_time_t stamp_time;
8081 char strfbuf[128];
8082 char xlogfilename[MAXFNAMELEN];
8083 XLogSegNo _logSegNo;
8084 struct stat stat_buf;
8085 FILE *fp;
8087 backup_started_in_recovery = RecoveryInProgress();
8090 * Currently only non-exclusive backup can be taken during recovery.
8092 if (backup_started_in_recovery && exclusive)
8093 ereport(ERROR,
8094 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
8095 errmsg("recovery is in progress"),
8096 errhint("WAL control functions cannot be executed during recovery.")));
8099 * During recovery, we don't need to check WAL level. Because, if WAL
8100 * level is not sufficient, it's impossible to get here during recovery.
8102 if (!backup_started_in_recovery && !XLogIsNeeded())
8103 ereport(ERROR,
8104 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
8105 errmsg("WAL level not sufficient for making an online backup"),
8106 errhint("wal_level must be set to \"replica\" or \"logical\" at server start.")));
8108 if (strlen(backupidstr) > MAXPGPATH)
8109 ereport(ERROR,
8110 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
8111 errmsg("backup label too long (max %d bytes)",
8112 MAXPGPATH)));
8115 * Mark backup active in shared memory. We must do full-page WAL writes
8116 * during an on-line backup even if not doing so at other times, because
8117 * it's quite possible for the backup dump to obtain a "torn" (partially
8118 * written) copy of a database page if it reads the page concurrently with
8119 * our write to the same page. This can be fixed as long as the first
8120 * write to the page in the WAL sequence is a full-page write. Hence, we
8121 * turn on forcePageWrites and then force a CHECKPOINT, to ensure there
8122 * are no dirty pages in shared memory that might get dumped while the
8123 * backup is in progress without having a corresponding WAL record. (Once
8124 * the backup is complete, we need not force full-page writes anymore,
8125 * since we expect that any pages not modified during the backup interval
8126 * must have been correctly captured by the backup.)
8128 * Note that forcePageWrites has no effect during an online backup from
8129 * the standby.
8131 * We must hold all the insertion locks to change the value of
8132 * forcePageWrites, to ensure adequate interlocking against
8133 * XLogInsertRecord().
8135 WALInsertLockAcquireExclusive();
8136 if (exclusive)
8139 * At first, mark that we're now starting an exclusive backup, to
8140 * ensure that there are no other sessions currently running
8141 * pg_start_backup() or pg_stop_backup().
8143 if (XLogCtl->Insert.exclusiveBackupState != EXCLUSIVE_BACKUP_NONE)
8145 WALInsertLockRelease();
8146 ereport(ERROR,
8147 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
8148 errmsg("a backup is already in progress"),
8149 errhint("Run pg_stop_backup() and try again.")));
8151 XLogCtl->Insert.exclusiveBackupState = EXCLUSIVE_BACKUP_STARTING;
8153 else
8154 XLogCtl->Insert.nonExclusiveBackups++;
8155 XLogCtl->Insert.forcePageWrites = true;
8156 WALInsertLockRelease();
8158 /* Ensure we release forcePageWrites if fail below */
8159 PG_ENSURE_ERROR_CLEANUP(pg_start_backup_callback, (Datum) BoolGetDatum(exclusive));
8161 bool gotUniqueStartpoint = false;
8162 DIR *tblspcdir;
8163 struct dirent *de;
8164 tablespaceinfo *ti;
8165 int datadirpathlen;
8168 * Force an XLOG file switch before the checkpoint, to ensure that the
8169 * WAL segment the checkpoint is written to doesn't contain pages with
8170 * old timeline IDs. That would otherwise happen if you called
8171 * pg_start_backup() right after restoring from a PITR archive: the
8172 * first WAL segment containing the startup checkpoint has pages in
8173 * the beginning with the old timeline ID. That can cause trouble at
8174 * recovery: we won't have a history file covering the old timeline if
8175 * pg_wal directory was not included in the base backup and the WAL
8176 * archive was cleared too before starting the backup.
8178 * This also ensures that we have emitted a WAL page header that has
8179 * XLP_BKP_REMOVABLE off before we emit the checkpoint record.
8180 * Therefore, if a WAL archiver (such as pglesslog) is trying to
8181 * compress out removable backup blocks, it won't remove any that
8182 * occur after this point.
8184 * During recovery, we skip forcing XLOG file switch, which means that
8185 * the backup taken during recovery is not available for the special
8186 * recovery case described above.
8188 if (!backup_started_in_recovery)
8189 RequestXLogSwitch(false);
8193 bool checkpointfpw;
8196 * Force a CHECKPOINT. Aside from being necessary to prevent torn
8197 * page problems, this guarantees that two successive backup runs
8198 * will have different checkpoint positions and hence different
8199 * history file names, even if nothing happened in between.
8201 * During recovery, establish a restartpoint if possible. We use
8202 * the last restartpoint as the backup starting checkpoint. This
8203 * means that two successive backup runs can have same checkpoint
8204 * positions.
8206 * Since the fact that we are executing do_pg_start_backup()
8207 * during recovery means that checkpointer is running, we can use
8208 * RequestCheckpoint() to establish a restartpoint.
8210 * We use CHECKPOINT_IMMEDIATE only if requested by user (via
8211 * passing fast = true). Otherwise this can take awhile.
8213 RequestCheckpoint(CHECKPOINT_FORCE | CHECKPOINT_WAIT |
8214 (fast ? CHECKPOINT_IMMEDIATE : 0));
8217 * Now we need to fetch the checkpoint record location, and also
8218 * its REDO pointer. The oldest point in WAL that would be needed
8219 * to restore starting from the checkpoint is precisely the REDO
8220 * pointer.
8222 LWLockAcquire(ControlFileLock, LW_SHARED);
8223 checkpointloc = ControlFile->checkPoint;
8224 startpoint = ControlFile->checkPointCopy.redo;
8225 starttli = ControlFile->checkPointCopy.ThisTimeLineID;
8226 checkpointfpw = ControlFile->checkPointCopy.fullPageWrites;
8227 LWLockRelease(ControlFileLock);
8229 if (backup_started_in_recovery)
8231 XLogRecPtr recptr;
8234 * Check to see if all WAL replayed during online backup
8235 * (i.e., since last restartpoint used as backup starting
8236 * checkpoint) contain full-page writes.
8238 SpinLockAcquire(&XLogCtl->info_lck);
8239 recptr = XLogCtl->lastFpwDisableRecPtr;
8240 SpinLockRelease(&XLogCtl->info_lck);
8242 if (!checkpointfpw || startpoint <= recptr)
8243 ereport(ERROR,
8244 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
8245 errmsg("WAL generated with full_page_writes=off was replayed "
8246 "since last restartpoint"),
8247 errhint("This means that the backup being taken on the standby "
8248 "is corrupt and should not be used. "
8249 "Enable full_page_writes and run CHECKPOINT on the primary, "
8250 "and then try an online backup again.")));
8253 * During recovery, since we don't use the end-of-backup WAL
8254 * record and don't write the backup history file, the
8255 * starting WAL location doesn't need to be unique. This means
8256 * that two base backups started at the same time might use
8257 * the same checkpoint as starting locations.
8259 gotUniqueStartpoint = true;
8263 * If two base backups are started at the same time (in WAL sender
8264 * processes), we need to make sure that they use different
8265 * checkpoints as starting locations, because we use the starting
8266 * WAL location as a unique identifier for the base backup in the
8267 * end-of-backup WAL record and when we write the backup history
8268 * file. Perhaps it would be better generate a separate unique ID
8269 * for each backup instead of forcing another checkpoint, but
8270 * taking a checkpoint right after another is not that expensive
8271 * either because only few buffers have been dirtied yet.
8273 WALInsertLockAcquireExclusive();
8274 if (XLogCtl->Insert.lastBackupStart < startpoint)
8276 XLogCtl->Insert.lastBackupStart = startpoint;
8277 gotUniqueStartpoint = true;
8279 WALInsertLockRelease();
8280 } while (!gotUniqueStartpoint);
8282 XLByteToSeg(startpoint, _logSegNo, wal_segment_size);
8283 XLogFileName(xlogfilename, starttli, _logSegNo, wal_segment_size);
8286 * Construct tablespace_map file. If caller isn't interested in this,
8287 * we make a local StringInfo.
8289 if (tblspcmapfile == NULL)
8290 tblspcmapfile = makeStringInfo();
8292 datadirpathlen = strlen(DataDir);
8294 /* Collect information about all tablespaces */
8295 tblspcdir = AllocateDir("pg_tblspc");
8296 while ((de = ReadDir(tblspcdir, "pg_tblspc")) != NULL)
8298 char fullpath[MAXPGPATH + 10];
8299 char linkpath[MAXPGPATH];
8300 char *relpath = NULL;
8301 int rllen;
8302 StringInfoData escapedpath;
8303 char *s;
8305 /* Skip anything that doesn't look like a tablespace */
8306 if (strspn(de->d_name, "0123456789") != strlen(de->d_name))
8307 continue;
8309 snprintf(fullpath, sizeof(fullpath), "pg_tblspc/%s", de->d_name);
8312 * Skip anything that isn't a symlink/junction. For testing only,
8313 * we sometimes use allow_in_place_tablespaces to create
8314 * directories directly under pg_tblspc, which would fail below.
8316 #ifdef WIN32
8317 if (!pgwin32_is_junction(fullpath))
8318 continue;
8319 #else
8320 if (get_dirent_type(fullpath, de, false, ERROR) != PGFILETYPE_LNK)
8321 continue;
8322 #endif
8324 #if defined(HAVE_READLINK) || defined(WIN32)
8325 rllen = readlink(fullpath, linkpath, sizeof(linkpath));
8326 if (rllen < 0)
8328 ereport(WARNING,
8329 (errmsg("could not read symbolic link \"%s\": %m",
8330 fullpath)));
8331 continue;
8333 else if (rllen >= sizeof(linkpath))
8335 ereport(WARNING,
8336 (errmsg("symbolic link \"%s\" target is too long",
8337 fullpath)));
8338 continue;
8340 linkpath[rllen] = '\0';
8343 * Build a backslash-escaped version of the link path to include
8344 * in the tablespace map file.
8346 initStringInfo(&escapedpath);
8347 for (s = linkpath; *s; s++)
8349 if (*s == '\n' || *s == '\r' || *s == '\\')
8350 appendStringInfoChar(&escapedpath, '\\');
8351 appendStringInfoChar(&escapedpath, *s);
8355 * Relpath holds the relative path of the tablespace directory
8356 * when it's located within PGDATA, or NULL if it's located
8357 * elsewhere.
8359 if (rllen > datadirpathlen &&
8360 strncmp(linkpath, DataDir, datadirpathlen) == 0 &&
8361 IS_DIR_SEP(linkpath[datadirpathlen]))
8362 relpath = linkpath + datadirpathlen + 1;
8364 ti = palloc(sizeof(tablespaceinfo));
8365 ti->oid = pstrdup(de->d_name);
8366 ti->path = pstrdup(linkpath);
8367 ti->rpath = relpath ? pstrdup(relpath) : NULL;
8368 ti->size = -1;
8370 if (tablespaces)
8371 *tablespaces = lappend(*tablespaces, ti);
8373 appendStringInfo(tblspcmapfile, "%s %s\n",
8374 ti->oid, escapedpath.data);
8376 pfree(escapedpath.data);
8377 #else
8380 * If the platform does not have symbolic links, it should not be
8381 * possible to have tablespaces - clearly somebody else created
8382 * them. Warn about it and ignore.
8384 ereport(WARNING,
8385 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
8386 errmsg("tablespaces are not supported on this platform")));
8387 #endif
8389 FreeDir(tblspcdir);
8392 * Construct backup label file. If caller isn't interested in this,
8393 * we make a local StringInfo.
8395 if (labelfile == NULL)
8396 labelfile = makeStringInfo();
8398 /* Use the log timezone here, not the session timezone */
8399 stamp_time = (pg_time_t) time(NULL);
8400 pg_strftime(strfbuf, sizeof(strfbuf),
8401 "%Y-%m-%d %H:%M:%S %Z",
8402 pg_localtime(&stamp_time, log_timezone));
8403 appendStringInfo(labelfile, "START WAL LOCATION: %X/%X (file %s)\n",
8404 LSN_FORMAT_ARGS(startpoint), xlogfilename);
8405 appendStringInfo(labelfile, "CHECKPOINT LOCATION: %X/%X\n",
8406 LSN_FORMAT_ARGS(checkpointloc));
8407 appendStringInfo(labelfile, "BACKUP METHOD: %s\n",
8408 exclusive ? "pg_start_backup" : "streamed");
8409 appendStringInfo(labelfile, "BACKUP FROM: %s\n",
8410 backup_started_in_recovery ? "standby" : "primary");
8411 appendStringInfo(labelfile, "START TIME: %s\n", strfbuf);
8412 appendStringInfo(labelfile, "LABEL: %s\n", backupidstr);
8413 appendStringInfo(labelfile, "START TIMELINE: %u\n", starttli);
8416 * Okay, write the file, or return its contents to caller.
8418 if (exclusive)
8421 * Check for existing backup label --- implies a backup is already
8422 * running. (XXX given that we checked exclusiveBackupState
8423 * above, maybe it would be OK to just unlink any such label
8424 * file?)
8426 if (stat(BACKUP_LABEL_FILE, &stat_buf) != 0)
8428 if (errno != ENOENT)
8429 ereport(ERROR,
8430 (errcode_for_file_access(),
8431 errmsg("could not stat file \"%s\": %m",
8432 BACKUP_LABEL_FILE)));
8434 else
8435 ereport(ERROR,
8436 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
8437 errmsg("a backup is already in progress"),
8438 errhint("If you're sure there is no backup in progress, remove file \"%s\" and try again.",
8439 BACKUP_LABEL_FILE)));
8441 fp = AllocateFile(BACKUP_LABEL_FILE, "w");
8443 if (!fp)
8444 ereport(ERROR,
8445 (errcode_for_file_access(),
8446 errmsg("could not create file \"%s\": %m",
8447 BACKUP_LABEL_FILE)));
8448 if (fwrite(labelfile->data, labelfile->len, 1, fp) != 1 ||
8449 fflush(fp) != 0 ||
8450 pg_fsync(fileno(fp)) != 0 ||
8451 ferror(fp) ||
8452 FreeFile(fp))
8453 ereport(ERROR,
8454 (errcode_for_file_access(),
8455 errmsg("could not write file \"%s\": %m",
8456 BACKUP_LABEL_FILE)));
8457 /* Allocated locally for exclusive backups, so free separately */
8458 pfree(labelfile->data);
8459 pfree(labelfile);
8461 /* Write backup tablespace_map file. */
8462 if (tblspcmapfile->len > 0)
8464 if (stat(TABLESPACE_MAP, &stat_buf) != 0)
8466 if (errno != ENOENT)
8467 ereport(ERROR,
8468 (errcode_for_file_access(),
8469 errmsg("could not stat file \"%s\": %m",
8470 TABLESPACE_MAP)));
8472 else
8473 ereport(ERROR,
8474 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
8475 errmsg("a backup is already in progress"),
8476 errhint("If you're sure there is no backup in progress, remove file \"%s\" and try again.",
8477 TABLESPACE_MAP)));
8479 fp = AllocateFile(TABLESPACE_MAP, "w");
8481 if (!fp)
8482 ereport(ERROR,
8483 (errcode_for_file_access(),
8484 errmsg("could not create file \"%s\": %m",
8485 TABLESPACE_MAP)));
8486 if (fwrite(tblspcmapfile->data, tblspcmapfile->len, 1, fp) != 1 ||
8487 fflush(fp) != 0 ||
8488 pg_fsync(fileno(fp)) != 0 ||
8489 ferror(fp) ||
8490 FreeFile(fp))
8491 ereport(ERROR,
8492 (errcode_for_file_access(),
8493 errmsg("could not write file \"%s\": %m",
8494 TABLESPACE_MAP)));
8497 /* Allocated locally for exclusive backups, so free separately */
8498 pfree(tblspcmapfile->data);
8499 pfree(tblspcmapfile);
8502 PG_END_ENSURE_ERROR_CLEANUP(pg_start_backup_callback, (Datum) BoolGetDatum(exclusive));
8505 * Mark that start phase has correctly finished for an exclusive backup.
8506 * Session-level locks are updated as well to reflect that state.
8508 * Note that CHECK_FOR_INTERRUPTS() must not occur while updating backup
8509 * counters and session-level lock. Otherwise they can be updated
8510 * inconsistently, and which might cause do_pg_abort_backup() to fail.
8512 if (exclusive)
8514 WALInsertLockAcquireExclusive();
8515 XLogCtl->Insert.exclusiveBackupState = EXCLUSIVE_BACKUP_IN_PROGRESS;
8517 /* Set session-level lock */
8518 sessionBackupState = SESSION_BACKUP_EXCLUSIVE;
8519 WALInsertLockRelease();
8521 else
8522 sessionBackupState = SESSION_BACKUP_NON_EXCLUSIVE;
8525 * We're done. As a convenience, return the starting WAL location.
8527 if (starttli_p)
8528 *starttli_p = starttli;
8529 return startpoint;
8532 /* Error cleanup callback for pg_start_backup */
8533 static void
8534 pg_start_backup_callback(int code, Datum arg)
8536 bool exclusive = DatumGetBool(arg);
8538 /* Update backup counters and forcePageWrites on failure */
8539 WALInsertLockAcquireExclusive();
8540 if (exclusive)
8542 Assert(XLogCtl->Insert.exclusiveBackupState == EXCLUSIVE_BACKUP_STARTING);
8543 XLogCtl->Insert.exclusiveBackupState = EXCLUSIVE_BACKUP_NONE;
8545 else
8547 Assert(XLogCtl->Insert.nonExclusiveBackups > 0);
8548 XLogCtl->Insert.nonExclusiveBackups--;
8551 if (XLogCtl->Insert.exclusiveBackupState == EXCLUSIVE_BACKUP_NONE &&
8552 XLogCtl->Insert.nonExclusiveBackups == 0)
8554 XLogCtl->Insert.forcePageWrites = false;
8556 WALInsertLockRelease();
8560 * Error cleanup callback for pg_stop_backup
8562 static void
8563 pg_stop_backup_callback(int code, Datum arg)
8565 bool exclusive = DatumGetBool(arg);
8567 /* Update backup status on failure */
8568 WALInsertLockAcquireExclusive();
8569 if (exclusive)
8571 Assert(XLogCtl->Insert.exclusiveBackupState == EXCLUSIVE_BACKUP_STOPPING);
8572 XLogCtl->Insert.exclusiveBackupState = EXCLUSIVE_BACKUP_IN_PROGRESS;
8574 WALInsertLockRelease();
8578 * Utility routine to fetch the session-level status of a backup running.
8580 SessionBackupState
8581 get_backup_status(void)
8583 return sessionBackupState;
8587 * do_pg_stop_backup
8589 * Utility function called at the end of an online backup. It cleans up the
8590 * backup state and can optionally wait for WAL segments to be archived.
8592 * If labelfile is NULL, this stops an exclusive backup. Otherwise this stops
8593 * the non-exclusive backup specified by 'labelfile'.
8595 * Returns the last WAL location that must be present to restore from this
8596 * backup, and the corresponding timeline ID in *stoptli_p.
8598 * It is the responsibility of the caller of this function to verify the
8599 * permissions of the calling user!
8601 XLogRecPtr
8602 do_pg_stop_backup(char *labelfile, bool waitforarchive, TimeLineID *stoptli_p)
8604 bool exclusive = (labelfile == NULL);
8605 bool backup_started_in_recovery = false;
8606 XLogRecPtr startpoint;
8607 XLogRecPtr stoppoint;
8608 TimeLineID stoptli;
8609 pg_time_t stamp_time;
8610 char strfbuf[128];
8611 char histfilepath[MAXPGPATH];
8612 char startxlogfilename[MAXFNAMELEN];
8613 char stopxlogfilename[MAXFNAMELEN];
8614 char lastxlogfilename[MAXFNAMELEN];
8615 char histfilename[MAXFNAMELEN];
8616 char backupfrom[20];
8617 XLogSegNo _logSegNo;
8618 FILE *lfp;
8619 FILE *fp;
8620 char ch;
8621 int seconds_before_warning;
8622 int waits = 0;
8623 bool reported_waiting = false;
8624 char *remaining;
8625 char *ptr;
8626 uint32 hi,
8629 backup_started_in_recovery = RecoveryInProgress();
8632 * Currently only non-exclusive backup can be taken during recovery.
8634 if (backup_started_in_recovery && exclusive)
8635 ereport(ERROR,
8636 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
8637 errmsg("recovery is in progress"),
8638 errhint("WAL control functions cannot be executed during recovery.")));
8641 * During recovery, we don't need to check WAL level. Because, if WAL
8642 * level is not sufficient, it's impossible to get here during recovery.
8644 if (!backup_started_in_recovery && !XLogIsNeeded())
8645 ereport(ERROR,
8646 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
8647 errmsg("WAL level not sufficient for making an online backup"),
8648 errhint("wal_level must be set to \"replica\" or \"logical\" at server start.")));
8650 if (exclusive)
8653 * At first, mark that we're now stopping an exclusive backup, to
8654 * ensure that there are no other sessions currently running
8655 * pg_start_backup() or pg_stop_backup().
8657 WALInsertLockAcquireExclusive();
8658 if (XLogCtl->Insert.exclusiveBackupState != EXCLUSIVE_BACKUP_IN_PROGRESS)
8660 WALInsertLockRelease();
8661 ereport(ERROR,
8662 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
8663 errmsg("exclusive backup not in progress")));
8665 XLogCtl->Insert.exclusiveBackupState = EXCLUSIVE_BACKUP_STOPPING;
8666 WALInsertLockRelease();
8669 * Remove backup_label. In case of failure, the state for an exclusive
8670 * backup is switched back to in-progress.
8672 PG_ENSURE_ERROR_CLEANUP(pg_stop_backup_callback, (Datum) BoolGetDatum(exclusive));
8675 * Read the existing label file into memory.
8677 struct stat statbuf;
8678 int r;
8680 if (stat(BACKUP_LABEL_FILE, &statbuf))
8682 /* should not happen per the upper checks */
8683 if (errno != ENOENT)
8684 ereport(ERROR,
8685 (errcode_for_file_access(),
8686 errmsg("could not stat file \"%s\": %m",
8687 BACKUP_LABEL_FILE)));
8688 ereport(ERROR,
8689 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
8690 errmsg("a backup is not in progress")));
8693 lfp = AllocateFile(BACKUP_LABEL_FILE, "r");
8694 if (!lfp)
8696 ereport(ERROR,
8697 (errcode_for_file_access(),
8698 errmsg("could not read file \"%s\": %m",
8699 BACKUP_LABEL_FILE)));
8701 labelfile = palloc(statbuf.st_size + 1);
8702 r = fread(labelfile, statbuf.st_size, 1, lfp);
8703 labelfile[statbuf.st_size] = '\0';
8706 * Close and remove the backup label file
8708 if (r != 1 || ferror(lfp) || FreeFile(lfp))
8709 ereport(ERROR,
8710 (errcode_for_file_access(),
8711 errmsg("could not read file \"%s\": %m",
8712 BACKUP_LABEL_FILE)));
8713 durable_unlink(BACKUP_LABEL_FILE, ERROR);
8716 * Remove tablespace_map file if present, it is created only if
8717 * there are tablespaces.
8719 durable_unlink(TABLESPACE_MAP, DEBUG1);
8721 PG_END_ENSURE_ERROR_CLEANUP(pg_stop_backup_callback, (Datum) BoolGetDatum(exclusive));
8725 * OK to update backup counters, forcePageWrites and session-level lock.
8727 * Note that CHECK_FOR_INTERRUPTS() must not occur while updating them.
8728 * Otherwise they can be updated inconsistently, and which might cause
8729 * do_pg_abort_backup() to fail.
8731 WALInsertLockAcquireExclusive();
8732 if (exclusive)
8734 XLogCtl->Insert.exclusiveBackupState = EXCLUSIVE_BACKUP_NONE;
8736 else
8739 * The user-visible pg_start/stop_backup() functions that operate on
8740 * exclusive backups can be called at any time, but for non-exclusive
8741 * backups, it is expected that each do_pg_start_backup() call is
8742 * matched by exactly one do_pg_stop_backup() call.
8744 Assert(XLogCtl->Insert.nonExclusiveBackups > 0);
8745 XLogCtl->Insert.nonExclusiveBackups--;
8748 if (XLogCtl->Insert.exclusiveBackupState == EXCLUSIVE_BACKUP_NONE &&
8749 XLogCtl->Insert.nonExclusiveBackups == 0)
8751 XLogCtl->Insert.forcePageWrites = false;
8755 * Clean up session-level lock.
8757 * You might think that WALInsertLockRelease() can be called before
8758 * cleaning up session-level lock because session-level lock doesn't need
8759 * to be protected with WAL insertion lock. But since
8760 * CHECK_FOR_INTERRUPTS() can occur in it, session-level lock must be
8761 * cleaned up before it.
8763 sessionBackupState = SESSION_BACKUP_NONE;
8765 WALInsertLockRelease();
8768 * Read and parse the START WAL LOCATION line (this code is pretty crude,
8769 * but we are not expecting any variability in the file format).
8771 if (sscanf(labelfile, "START WAL LOCATION: %X/%X (file %24s)%c",
8772 &hi, &lo, startxlogfilename,
8773 &ch) != 4 || ch != '\n')
8774 ereport(ERROR,
8775 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
8776 errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
8777 startpoint = ((uint64) hi) << 32 | lo;
8778 remaining = strchr(labelfile, '\n') + 1; /* %n is not portable enough */
8781 * Parse the BACKUP FROM line. If we are taking an online backup from the
8782 * standby, we confirm that the standby has not been promoted during the
8783 * backup.
8785 ptr = strstr(remaining, "BACKUP FROM:");
8786 if (!ptr || sscanf(ptr, "BACKUP FROM: %19s\n", backupfrom) != 1)
8787 ereport(ERROR,
8788 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
8789 errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
8790 if (strcmp(backupfrom, "standby") == 0 && !backup_started_in_recovery)
8791 ereport(ERROR,
8792 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
8793 errmsg("the standby was promoted during online backup"),
8794 errhint("This means that the backup being taken is corrupt "
8795 "and should not be used. "
8796 "Try taking another online backup.")));
8799 * During recovery, we don't write an end-of-backup record. We assume that
8800 * pg_control was backed up last and its minimum recovery point can be
8801 * available as the backup end location. Since we don't have an
8802 * end-of-backup record, we use the pg_control value to check whether
8803 * we've reached the end of backup when starting recovery from this
8804 * backup. We have no way of checking if pg_control wasn't backed up last
8805 * however.
8807 * We don't force a switch to new WAL file but it is still possible to
8808 * wait for all the required files to be archived if waitforarchive is
8809 * true. This is okay if we use the backup to start a standby and fetch
8810 * the missing WAL using streaming replication. But in the case of an
8811 * archive recovery, a user should set waitforarchive to true and wait for
8812 * them to be archived to ensure that all the required files are
8813 * available.
8815 * We return the current minimum recovery point as the backup end
8816 * location. Note that it can be greater than the exact backup end
8817 * location if the minimum recovery point is updated after the backup of
8818 * pg_control. This is harmless for current uses.
8820 * XXX currently a backup history file is for informational and debug
8821 * purposes only. It's not essential for an online backup. Furthermore,
8822 * even if it's created, it will not be archived during recovery because
8823 * an archiver is not invoked. So it doesn't seem worthwhile to write a
8824 * backup history file during recovery.
8826 if (backup_started_in_recovery)
8828 XLogRecPtr recptr;
8831 * Check to see if all WAL replayed during online backup contain
8832 * full-page writes.
8834 SpinLockAcquire(&XLogCtl->info_lck);
8835 recptr = XLogCtl->lastFpwDisableRecPtr;
8836 SpinLockRelease(&XLogCtl->info_lck);
8838 if (startpoint <= recptr)
8839 ereport(ERROR,
8840 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
8841 errmsg("WAL generated with full_page_writes=off was replayed "
8842 "during online backup"),
8843 errhint("This means that the backup being taken on the standby "
8844 "is corrupt and should not be used. "
8845 "Enable full_page_writes and run CHECKPOINT on the primary, "
8846 "and then try an online backup again.")));
8849 LWLockAcquire(ControlFileLock, LW_SHARED);
8850 stoppoint = ControlFile->minRecoveryPoint;
8851 stoptli = ControlFile->minRecoveryPointTLI;
8852 LWLockRelease(ControlFileLock);
8854 else
8857 * Write the backup-end xlog record
8859 XLogBeginInsert();
8860 XLogRegisterData((char *) (&startpoint), sizeof(startpoint));
8861 stoppoint = XLogInsert(RM_XLOG_ID, XLOG_BACKUP_END);
8864 * Given that we're not in recovery, InsertTimeLineID is set and can't
8865 * change, so we can read it without a lock.
8867 stoptli = XLogCtl->InsertTimeLineID;
8870 * Force a switch to a new xlog segment file, so that the backup is
8871 * valid as soon as archiver moves out the current segment file.
8873 RequestXLogSwitch(false);
8875 XLByteToPrevSeg(stoppoint, _logSegNo, wal_segment_size);
8876 XLogFileName(stopxlogfilename, stoptli, _logSegNo, wal_segment_size);
8878 /* Use the log timezone here, not the session timezone */
8879 stamp_time = (pg_time_t) time(NULL);
8880 pg_strftime(strfbuf, sizeof(strfbuf),
8881 "%Y-%m-%d %H:%M:%S %Z",
8882 pg_localtime(&stamp_time, log_timezone));
8885 * Write the backup history file
8887 XLByteToSeg(startpoint, _logSegNo, wal_segment_size);
8888 BackupHistoryFilePath(histfilepath, stoptli, _logSegNo,
8889 startpoint, wal_segment_size);
8890 fp = AllocateFile(histfilepath, "w");
8891 if (!fp)
8892 ereport(ERROR,
8893 (errcode_for_file_access(),
8894 errmsg("could not create file \"%s\": %m",
8895 histfilepath)));
8896 fprintf(fp, "START WAL LOCATION: %X/%X (file %s)\n",
8897 LSN_FORMAT_ARGS(startpoint), startxlogfilename);
8898 fprintf(fp, "STOP WAL LOCATION: %X/%X (file %s)\n",
8899 LSN_FORMAT_ARGS(stoppoint), stopxlogfilename);
8902 * Transfer remaining lines including label and start timeline to
8903 * history file.
8905 fprintf(fp, "%s", remaining);
8906 fprintf(fp, "STOP TIME: %s\n", strfbuf);
8907 fprintf(fp, "STOP TIMELINE: %u\n", stoptli);
8908 if (fflush(fp) || ferror(fp) || FreeFile(fp))
8909 ereport(ERROR,
8910 (errcode_for_file_access(),
8911 errmsg("could not write file \"%s\": %m",
8912 histfilepath)));
8915 * Clean out any no-longer-needed history files. As a side effect,
8916 * this will post a .ready file for the newly created history file,
8917 * notifying the archiver that history file may be archived
8918 * immediately.
8920 CleanupBackupHistory();
8924 * If archiving is enabled, wait for all the required WAL files to be
8925 * archived before returning. If archiving isn't enabled, the required WAL
8926 * needs to be transported via streaming replication (hopefully with
8927 * wal_keep_size set high enough), or some more exotic mechanism like
8928 * polling and copying files from pg_wal with script. We have no knowledge
8929 * of those mechanisms, so it's up to the user to ensure that he gets all
8930 * the required WAL.
8932 * We wait until both the last WAL file filled during backup and the
8933 * history file have been archived, and assume that the alphabetic sorting
8934 * property of the WAL files ensures any earlier WAL files are safely
8935 * archived as well.
8937 * We wait forever, since archive_command is supposed to work and we
8938 * assume the admin wanted his backup to work completely. If you don't
8939 * wish to wait, then either waitforarchive should be passed in as false,
8940 * or you can set statement_timeout. Also, some notices are issued to
8941 * clue in anyone who might be doing this interactively.
8944 if (waitforarchive &&
8945 ((!backup_started_in_recovery && XLogArchivingActive()) ||
8946 (backup_started_in_recovery && XLogArchivingAlways())))
8948 XLByteToPrevSeg(stoppoint, _logSegNo, wal_segment_size);
8949 XLogFileName(lastxlogfilename, stoptli, _logSegNo, wal_segment_size);
8951 XLByteToSeg(startpoint, _logSegNo, wal_segment_size);
8952 BackupHistoryFileName(histfilename, stoptli, _logSegNo,
8953 startpoint, wal_segment_size);
8955 seconds_before_warning = 60;
8956 waits = 0;
8958 while (XLogArchiveIsBusy(lastxlogfilename) ||
8959 XLogArchiveIsBusy(histfilename))
8961 CHECK_FOR_INTERRUPTS();
8963 if (!reported_waiting && waits > 5)
8965 ereport(NOTICE,
8966 (errmsg("base backup done, waiting for required WAL segments to be archived")));
8967 reported_waiting = true;
8970 (void) WaitLatch(MyLatch,
8971 WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
8972 1000L,
8973 WAIT_EVENT_BACKUP_WAIT_WAL_ARCHIVE);
8974 ResetLatch(MyLatch);
8976 if (++waits >= seconds_before_warning)
8978 seconds_before_warning *= 2; /* This wraps in >10 years... */
8979 ereport(WARNING,
8980 (errmsg("still waiting for all required WAL segments to be archived (%d seconds elapsed)",
8981 waits),
8982 errhint("Check that your archive_command is executing properly. "
8983 "You can safely cancel this backup, "
8984 "but the database backup will not be usable without all the WAL segments.")));
8988 ereport(NOTICE,
8989 (errmsg("all required WAL segments have been archived")));
8991 else if (waitforarchive)
8992 ereport(NOTICE,
8993 (errmsg("WAL archiving is not enabled; you must ensure that all required WAL segments are copied through other means to complete the backup")));
8996 * We're done. As a convenience, return the ending WAL location.
8998 if (stoptli_p)
8999 *stoptli_p = stoptli;
9000 return stoppoint;
9005 * do_pg_abort_backup: abort a running backup
9007 * This does just the most basic steps of do_pg_stop_backup(), by taking the
9008 * system out of backup mode, thus making it a lot more safe to call from
9009 * an error handler.
9011 * The caller can pass 'arg' as 'true' or 'false' to control whether a warning
9012 * is emitted.
9014 * NB: This is only for aborting a non-exclusive backup that doesn't write
9015 * backup_label. A backup started with pg_start_backup() needs to be finished
9016 * with pg_stop_backup().
9018 * NB: This gets used as a before_shmem_exit handler, hence the odd-looking
9019 * signature.
9021 void
9022 do_pg_abort_backup(int code, Datum arg)
9024 bool emit_warning = DatumGetBool(arg);
9027 * Quick exit if session is not keeping around a non-exclusive backup
9028 * already started.
9030 if (sessionBackupState != SESSION_BACKUP_NON_EXCLUSIVE)
9031 return;
9033 WALInsertLockAcquireExclusive();
9034 Assert(XLogCtl->Insert.nonExclusiveBackups > 0);
9035 XLogCtl->Insert.nonExclusiveBackups--;
9037 if (XLogCtl->Insert.exclusiveBackupState == EXCLUSIVE_BACKUP_NONE &&
9038 XLogCtl->Insert.nonExclusiveBackups == 0)
9040 XLogCtl->Insert.forcePageWrites = false;
9042 WALInsertLockRelease();
9044 if (emit_warning)
9045 ereport(WARNING,
9046 (errmsg("aborting backup due to backend exiting before pg_stop_backup was called")));
9050 * Register a handler that will warn about unterminated backups at end of
9051 * session, unless this has already been done.
9053 void
9054 register_persistent_abort_backup_handler(void)
9056 static bool already_done = false;
9058 if (already_done)
9059 return;
9060 before_shmem_exit(do_pg_abort_backup, DatumGetBool(true));
9061 already_done = true;
9065 * Get latest WAL insert pointer
9067 XLogRecPtr
9068 GetXLogInsertRecPtr(void)
9070 XLogCtlInsert *Insert = &XLogCtl->Insert;
9071 uint64 current_bytepos;
9073 SpinLockAcquire(&Insert->insertpos_lck);
9074 current_bytepos = Insert->CurrBytePos;
9075 SpinLockRelease(&Insert->insertpos_lck);
9077 return XLogBytePosToRecPtr(current_bytepos);
9081 * Get latest WAL write pointer
9083 XLogRecPtr
9084 GetXLogWriteRecPtr(void)
9086 SpinLockAcquire(&XLogCtl->info_lck);
9087 LogwrtResult = XLogCtl->LogwrtResult;
9088 SpinLockRelease(&XLogCtl->info_lck);
9090 return LogwrtResult.Write;
9094 * Returns the redo pointer of the last checkpoint or restartpoint. This is
9095 * the oldest point in WAL that we still need, if we have to restart recovery.
9097 void
9098 GetOldestRestartPoint(XLogRecPtr *oldrecptr, TimeLineID *oldtli)
9100 LWLockAcquire(ControlFileLock, LW_SHARED);
9101 *oldrecptr = ControlFile->checkPointCopy.redo;
9102 *oldtli = ControlFile->checkPointCopy.ThisTimeLineID;
9103 LWLockRelease(ControlFileLock);
9107 * BackupInProgress: check if online backup mode is active
9109 * This is done by checking for existence of the "backup_label" file.
9111 bool
9112 BackupInProgress(void)
9114 struct stat stat_buf;
9116 return (stat(BACKUP_LABEL_FILE, &stat_buf) == 0);
9120 * CancelBackup: rename the "backup_label" and "tablespace_map"
9121 * files to cancel backup mode
9123 * If the "backup_label" file exists, it will be renamed to "backup_label.old".
9124 * Similarly, if the "tablespace_map" file exists, it will be renamed to
9125 * "tablespace_map.old".
9127 * Note that this will render an online backup in progress
9128 * useless. To correctly finish an online backup, pg_stop_backup must be
9129 * called.
9131 void
9132 CancelBackup(void)
9134 struct stat stat_buf;
9136 /* if the backup_label file is not there, return */
9137 if (stat(BACKUP_LABEL_FILE, &stat_buf) < 0)
9138 return;
9140 /* remove leftover file from previously canceled backup if it exists */
9141 unlink(BACKUP_LABEL_OLD);
9143 if (durable_rename(BACKUP_LABEL_FILE, BACKUP_LABEL_OLD, DEBUG1) != 0)
9145 ereport(WARNING,
9146 (errcode_for_file_access(),
9147 errmsg("online backup mode was not canceled"),
9148 errdetail("File \"%s\" could not be renamed to \"%s\": %m.",
9149 BACKUP_LABEL_FILE, BACKUP_LABEL_OLD)));
9150 return;
9153 /* if the tablespace_map file is not there, return */
9154 if (stat(TABLESPACE_MAP, &stat_buf) < 0)
9156 ereport(LOG,
9157 (errmsg("online backup mode canceled"),
9158 errdetail("File \"%s\" was renamed to \"%s\".",
9159 BACKUP_LABEL_FILE, BACKUP_LABEL_OLD)));
9160 return;
9163 /* remove leftover file from previously canceled backup if it exists */
9164 unlink(TABLESPACE_MAP_OLD);
9166 if (durable_rename(TABLESPACE_MAP, TABLESPACE_MAP_OLD, DEBUG1) == 0)
9168 ereport(LOG,
9169 (errmsg("online backup mode canceled"),
9170 errdetail("Files \"%s\" and \"%s\" were renamed to "
9171 "\"%s\" and \"%s\", respectively.",
9172 BACKUP_LABEL_FILE, TABLESPACE_MAP,
9173 BACKUP_LABEL_OLD, TABLESPACE_MAP_OLD)));
9175 else
9177 ereport(WARNING,
9178 (errcode_for_file_access(),
9179 errmsg("online backup mode canceled"),
9180 errdetail("File \"%s\" was renamed to \"%s\", but "
9181 "file \"%s\" could not be renamed to \"%s\": %m.",
9182 BACKUP_LABEL_FILE, BACKUP_LABEL_OLD,
9183 TABLESPACE_MAP, TABLESPACE_MAP_OLD)));
9187 /* Thin wrapper around ShutdownWalRcv(). */
9188 void
9189 XLogShutdownWalRcv(void)
9191 ShutdownWalRcv();
9193 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
9194 XLogCtl->InstallXLogFileSegmentActive = false;
9195 LWLockRelease(ControlFileLock);
9198 /* Enable WAL file recycling and preallocation. */
9199 void
9200 SetInstallXLogFileSegmentActive(void)
9202 LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
9203 XLogCtl->InstallXLogFileSegmentActive = true;
9204 LWLockRelease(ControlFileLock);
9207 bool
9208 IsInstallXLogFileSegmentActive(void)
9210 bool result;
9212 LWLockAcquire(ControlFileLock, LW_SHARED);
9213 result = XLogCtl->InstallXLogFileSegmentActive;
9214 LWLockRelease(ControlFileLock);
9216 return result;
9220 * Update the WalWriterSleeping flag.
9222 void
9223 SetWalWriterSleeping(bool sleeping)
9225 SpinLockAcquire(&XLogCtl->info_lck);
9226 XLogCtl->WalWriterSleeping = sleeping;
9227 SpinLockRelease(&XLogCtl->info_lck);