1 /*-------------------------------------------------------------------------
4 * PostgreSQL transaction log manager
7 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 *-------------------------------------------------------------------------
26 #include "access/clog.h"
27 #include "access/multixact.h"
28 #include "access/subtrans.h"
29 #include "access/transam.h"
30 #include "access/tuptoaster.h"
31 #include "access/twophase.h"
32 #include "access/xact.h"
33 #include "access/xlog_internal.h"
34 #include "access/xlogutils.h"
35 #include "catalog/catversion.h"
36 #include "catalog/pg_control.h"
37 #include "catalog/pg_type.h"
39 #include "miscadmin.h"
41 #include "postmaster/bgwriter.h"
42 #include "storage/bufmgr.h"
43 #include "storage/fd.h"
44 #include "storage/ipc.h"
45 #include "storage/pmsignal.h"
46 #include "storage/procarray.h"
47 #include "storage/smgr.h"
48 #include "storage/spin.h"
49 #include "utils/builtins.h"
50 #include "utils/guc.h"
51 #include "utils/ps_status.h"
55 /* File path names (all relative to $PGDATA) */
56 #define BACKUP_LABEL_FILE "backup_label"
57 #define BACKUP_LABEL_OLD "backup_label.old"
58 #define RECOVERY_COMMAND_FILE "recovery.conf"
59 #define RECOVERY_COMMAND_DONE "recovery.done"
62 /* User-settable parameters */
63 int CheckPointSegments
= 3;
65 int XLogArchiveTimeout
= 0;
66 bool XLogArchiveMode
= false;
67 char *XLogArchiveCommand
= NULL
;
68 bool fullPageWrites
= true;
69 bool log_checkpoints
= false;
70 int sync_method
= DEFAULT_SYNC_METHOD
;
73 bool XLOG_DEBUG
= false;
77 * XLOGfileslop is the maximum number of preallocated future XLOG segments.
78 * When we are done with an old XLOG segment file, we will recycle it as a
79 * future XLOG segment as long as there aren't already XLOGfileslop future
80 * segments; else we'll delete it. This could be made a separate GUC
81 * variable, but at present I think it's sufficient to hardwire it as
82 * 2*CheckPointSegments+1. Under normal conditions, a checkpoint will free
83 * no more than 2*CheckPointSegments log segments, and we want to recycle all
84 * of them; the +1 allows boundary cases to happen without wasting a
85 * delete/create-segment cycle.
87 #define XLOGfileslop (2*CheckPointSegments + 1)
92 const struct config_enum_entry sync_method_options
[] = {
93 {"fsync", SYNC_METHOD_FSYNC
, false},
94 #ifdef HAVE_FSYNC_WRITETHROUGH
95 {"fsync_writethrough", SYNC_METHOD_FSYNC_WRITETHROUGH
, false},
98 {"fdatasync", SYNC_METHOD_FDATASYNC
, false},
100 #ifdef OPEN_SYNC_FLAG
101 {"open_sync", SYNC_METHOD_OPEN
, false},
103 #ifdef OPEN_DATASYNC_FLAG
104 {"open_datasync", SYNC_METHOD_OPEN_DSYNC
, false},
110 * Statistics for current checkpoint are collected in this global struct.
111 * Because only the background writer or a stand-alone backend can perform
112 * checkpoints, this will be unused in normal backends.
114 CheckpointStatsData CheckpointStats
;
117 * ThisTimeLineID will be same in all backends --- it identifies current
118 * WAL timeline for the database system.
120 TimeLineID ThisTimeLineID
= 0;
122 /* Are we doing recovery from XLOG? */
123 bool InRecovery
= false;
125 /* Are we recovering using offline XLOG archives? */
126 static bool InArchiveRecovery
= false;
128 /* Was the last xlog file restored from archive, or local? */
129 static bool restoredFromArchive
= false;
131 /* options taken from recovery.conf */
132 static char *recoveryRestoreCommand
= NULL
;
133 static bool recoveryTarget
= false;
134 static bool recoveryTargetExact
= false;
135 static bool recoveryTargetInclusive
= true;
136 static bool recoveryLogRestartpoints
= false;
137 static TransactionId recoveryTargetXid
;
138 static TimestampTz recoveryTargetTime
;
139 static TimestampTz recoveryLastXTime
= 0;
141 /* if recoveryStopsHere returns true, it saves actual stop xid/time here */
142 static TransactionId recoveryStopXid
;
143 static TimestampTz recoveryStopTime
;
144 static bool recoveryStopAfter
;
147 * During normal operation, the only timeline we care about is ThisTimeLineID.
148 * During recovery, however, things are more complicated. To simplify life
149 * for rmgr code, we keep ThisTimeLineID set to the "current" timeline as we
150 * scan through the WAL history (that is, it is the line that was active when
151 * the currently-scanned WAL record was generated). We also need these
154 * recoveryTargetTLI: the desired timeline that we want to end in.
156 * expectedTLIs: an integer list of recoveryTargetTLI and the TLIs of
157 * its known parents, newest first (so recoveryTargetTLI is always the
158 * first list member). Only these TLIs are expected to be seen in the WAL
159 * segments we read, and indeed only these TLIs will be considered as
160 * candidate WAL files to open at all.
162 * curFileTLI: the TLI appearing in the name of the current input WAL file.
163 * (This is not necessarily the same as ThisTimeLineID, because we could
164 * be scanning data that was copied from an ancestor timeline when the current
165 * file was created.) During a sequential scan we do not allow this value
168 static TimeLineID recoveryTargetTLI
;
169 static List
*expectedTLIs
;
170 static TimeLineID curFileTLI
;
173 * ProcLastRecPtr points to the start of the last XLOG record inserted by the
174 * current backend. It is updated for all inserts. XactLastRecEnd points to
175 * end+1 of the last record, and is reset when we end a top-level transaction,
176 * or start a new one; so it can be used to tell if the current transaction has
177 * created any XLOG records.
179 static XLogRecPtr ProcLastRecPtr
= {0, 0};
181 XLogRecPtr XactLastRecEnd
= {0, 0};
184 * RedoRecPtr is this backend's local copy of the REDO record pointer
185 * (which is almost but not quite the same as a pointer to the most recent
186 * CHECKPOINT record). We update this from the shared-memory copy,
187 * XLogCtl->Insert.RedoRecPtr, whenever we can safely do so (ie, when we
188 * hold the Insert lock). See XLogInsert for details. We are also allowed
189 * to update from XLogCtl->Insert.RedoRecPtr if we hold the info_lck;
190 * see GetRedoRecPtr. A freshly spawned backend obtains the value during
193 static XLogRecPtr RedoRecPtr
;
196 * Shared-memory data structures for XLOG control
198 * LogwrtRqst indicates a byte position that we need to write and/or fsync
199 * the log up to (all records before that point must be written or fsynced).
200 * LogwrtResult indicates the byte positions we have already written/fsynced.
201 * These structs are identical but are declared separately to indicate their
202 * slightly different functions.
204 * We do a lot of pushups to minimize the amount of access to lockable
205 * shared memory values. There are actually three shared-memory copies of
206 * LogwrtResult, plus one unshared copy in each backend. Here's how it works:
207 * XLogCtl->LogwrtResult is protected by info_lck
208 * XLogCtl->Write.LogwrtResult is protected by WALWriteLock
209 * XLogCtl->Insert.LogwrtResult is protected by WALInsertLock
210 * One must hold the associated lock to read or write any of these, but
211 * of course no lock is needed to read/write the unshared LogwrtResult.
213 * XLogCtl->LogwrtResult and XLogCtl->Write.LogwrtResult are both "always
214 * right", since both are updated by a write or flush operation before
215 * it releases WALWriteLock. The point of keeping XLogCtl->Write.LogwrtResult
216 * is that it can be examined/modified by code that already holds WALWriteLock
217 * without needing to grab info_lck as well.
219 * XLogCtl->Insert.LogwrtResult may lag behind the reality of the other two,
220 * but is updated when convenient. Again, it exists for the convenience of
221 * code that is already holding WALInsertLock but not the other locks.
223 * The unshared LogwrtResult may lag behind any or all of these, and again
224 * is updated when convenient.
226 * The request bookkeeping is simpler: there is a shared XLogCtl->LogwrtRqst
227 * (protected by info_lck), but we don't need to cache any copies of it.
229 * Note that this all works because the request and result positions can only
230 * advance forward, never back up, and so we can easily determine which of two
231 * values is "more up to date".
233 * info_lck is only held long enough to read/update the protected variables,
234 * so it's a plain spinlock. The other locks are held longer (potentially
235 * over I/O operations), so we use LWLocks for them. These locks are:
237 * WALInsertLock: must be held to insert a record into the WAL buffers.
239 * WALWriteLock: must be held to write WAL buffers to disk (XLogWrite or
242 * ControlFileLock: must be held to read/update control file or create
245 * CheckpointLock: must be held to do a checkpoint (ensures only one
246 * checkpointer at a time; currently, with all checkpoints done by the
247 * bgwriter, this is just pro forma).
252 typedef struct XLogwrtRqst
254 XLogRecPtr Write
; /* last byte + 1 to write out */
255 XLogRecPtr Flush
; /* last byte + 1 to flush */
258 typedef struct XLogwrtResult
260 XLogRecPtr Write
; /* last byte + 1 written out */
261 XLogRecPtr Flush
; /* last byte + 1 flushed */
265 * Shared state data for XLogInsert.
267 typedef struct XLogCtlInsert
269 XLogwrtResult LogwrtResult
; /* a recent value of LogwrtResult */
270 XLogRecPtr PrevRecord
; /* start of previously-inserted record */
271 int curridx
; /* current block index in cache */
272 XLogPageHeader currpage
; /* points to header of block in cache */
273 char *currpos
; /* current insertion point in cache */
274 XLogRecPtr RedoRecPtr
; /* current redo point for insertions */
275 bool forcePageWrites
; /* forcing full-page writes for PITR? */
279 * Shared state data for XLogWrite/XLogFlush.
281 typedef struct XLogCtlWrite
283 XLogwrtResult LogwrtResult
; /* current value of LogwrtResult */
284 int curridx
; /* cache index of next block to write */
285 pg_time_t lastSegSwitchTime
; /* time of last xlog segment switch */
289 * Total shared-memory state for XLOG.
291 typedef struct XLogCtlData
293 /* Protected by WALInsertLock: */
294 XLogCtlInsert Insert
;
296 /* Protected by info_lck: */
297 XLogwrtRqst LogwrtRqst
;
298 XLogwrtResult LogwrtResult
;
299 uint32 ckptXidEpoch
; /* nextXID & epoch of latest checkpoint */
300 TransactionId ckptXid
;
301 XLogRecPtr asyncCommitLSN
; /* LSN of newest async commit */
303 /* Protected by WALWriteLock: */
307 * These values do not change after startup, although the pointed-to pages
308 * and xlblocks values certainly do. Permission to read/write the pages
309 * and xlblocks values depends on WALInsertLock and WALWriteLock.
311 char *pages
; /* buffers for unwritten XLOG pages */
312 XLogRecPtr
*xlblocks
; /* 1st byte ptr-s + XLOG_BLCKSZ */
313 int XLogCacheBlck
; /* highest allocated xlog buffer index */
314 TimeLineID ThisTimeLineID
;
316 slock_t info_lck
; /* locks shared variables shown above */
319 static XLogCtlData
*XLogCtl
= NULL
;
322 * We maintain an image of pg_control in shared memory.
324 static ControlFileData
*ControlFile
= NULL
;
327 * Macros for managing XLogInsert state. In most cases, the calling routine
328 * has local copies of XLogCtl->Insert and/or XLogCtl->Insert->curridx,
329 * so these are passed as parameters instead of being fetched via XLogCtl.
332 /* Free space remaining in the current xlog page buffer */
333 #define INSERT_FREESPACE(Insert) \
334 (XLOG_BLCKSZ - ((Insert)->currpos - (char *) (Insert)->currpage))
336 /* Construct XLogRecPtr value for current insertion point */
337 #define INSERT_RECPTR(recptr,Insert,curridx) \
339 (recptr).xlogid = XLogCtl->xlblocks[curridx].xlogid, \
341 XLogCtl->xlblocks[curridx].xrecoff - INSERT_FREESPACE(Insert) \
344 #define PrevBufIdx(idx) \
345 (((idx) == 0) ? XLogCtl->XLogCacheBlck : ((idx) - 1))
347 #define NextBufIdx(idx) \
348 (((idx) == XLogCtl->XLogCacheBlck) ? 0 : ((idx) + 1))
351 * Private, possibly out-of-date copy of shared LogwrtResult.
352 * See discussion above.
354 static XLogwrtResult LogwrtResult
= {{0, 0}, {0, 0}};
357 * openLogFile is -1 or a kernel FD for an open log file segment.
358 * When it's open, openLogOff is the current seek offset in the file.
359 * openLogId/openLogSeg identify the segment. These variables are only
360 * used to write the XLOG, and so will normally refer to the active segment.
362 static int openLogFile
= -1;
363 static uint32 openLogId
= 0;
364 static uint32 openLogSeg
= 0;
365 static uint32 openLogOff
= 0;
368 * These variables are used similarly to the ones above, but for reading
369 * the XLOG. Note, however, that readOff generally represents the offset
370 * of the page just read, not the seek position of the FD itself, which
371 * will be just past that page.
373 static int readFile
= -1;
374 static uint32 readId
= 0;
375 static uint32 readSeg
= 0;
376 static uint32 readOff
= 0;
378 /* Buffer for currently read page (XLOG_BLCKSZ bytes) */
379 static char *readBuf
= NULL
;
381 /* Buffer for current ReadRecord result (expandable) */
382 static char *readRecordBuf
= NULL
;
383 static uint32 readRecordBufSize
= 0;
385 /* State information for XLOG reading */
386 static XLogRecPtr ReadRecPtr
; /* start of last record read */
387 static XLogRecPtr EndRecPtr
; /* end+1 of last record read */
388 static XLogRecord
*nextRecord
= NULL
;
389 static TimeLineID lastPageTLI
= 0;
391 static bool InRedo
= false;
394 static void XLogArchiveNotify(const char *xlog
);
395 static void XLogArchiveNotifySeg(uint32 log
, uint32 seg
);
396 static bool XLogArchiveCheckDone(const char *xlog
);
397 static bool XLogArchiveIsBusy(const char *xlog
);
398 static void XLogArchiveCleanup(const char *xlog
);
399 static void readRecoveryCommandFile(void);
400 static void exitArchiveRecovery(TimeLineID endTLI
,
401 uint32 endLogId
, uint32 endLogSeg
);
402 static bool recoveryStopsHere(XLogRecord
*record
, bool *includeThis
);
403 static void CheckPointGuts(XLogRecPtr checkPointRedo
, int flags
);
405 static bool XLogCheckBuffer(XLogRecData
*rdata
, bool doPageWrites
,
406 XLogRecPtr
*lsn
, BkpBlock
*bkpb
);
407 static bool AdvanceXLInsertBuffer(bool new_segment
);
408 static void XLogWrite(XLogwrtRqst WriteRqst
, bool flexible
, bool xlog_switch
);
409 static int XLogFileInit(uint32 log
, uint32 seg
,
410 bool *use_existent
, bool use_lock
);
411 static bool InstallXLogFileSegment(uint32
*log
, uint32
*seg
, char *tmppath
,
412 bool find_free
, int *max_advance
,
414 static int XLogFileOpen(uint32 log
, uint32 seg
);
415 static int XLogFileRead(uint32 log
, uint32 seg
, int emode
);
416 static void XLogFileClose(void);
417 static bool RestoreArchivedFile(char *path
, const char *xlogfname
,
418 const char *recovername
, off_t expectedSize
);
419 static void PreallocXlogFiles(XLogRecPtr endptr
);
420 static void RemoveOldXlogFiles(uint32 log
, uint32 seg
, XLogRecPtr endptr
);
421 static void ValidateXLOGDirectoryStructure(void);
422 static void CleanupBackupHistory(void);
423 static XLogRecord
*ReadRecord(XLogRecPtr
*RecPtr
, int emode
);
424 static bool ValidXLOGHeader(XLogPageHeader hdr
, int emode
);
425 static XLogRecord
*ReadCheckpointRecord(XLogRecPtr RecPtr
, int whichChkpt
);
426 static List
*readTimeLineHistory(TimeLineID targetTLI
);
427 static bool existsTimeLineHistory(TimeLineID probeTLI
);
428 static TimeLineID
findNewestTimeLine(TimeLineID startTLI
);
429 static void writeTimeLineHistory(TimeLineID newTLI
, TimeLineID parentTLI
,
431 uint32 endLogId
, uint32 endLogSeg
);
432 static void WriteControlFile(void);
433 static void ReadControlFile(void);
434 static char *str_time(pg_time_t tnow
);
436 static void xlog_outrec(StringInfo buf
, XLogRecord
*record
);
438 static void issue_xlog_fsync(void);
439 static void pg_start_backup_callback(int code
, Datum arg
);
440 static bool read_backup_label(XLogRecPtr
*checkPointLoc
,
441 XLogRecPtr
*minRecoveryLoc
);
442 static void rm_redo_error_callback(void *arg
);
443 static int get_sync_bit(int method
);
447 * Insert an XLOG record having the specified RMID and info bytes,
448 * with the body of the record being the data chunk(s) described by
449 * the rdata chain (see xlog.h for notes about rdata).
451 * Returns XLOG pointer to end of record (beginning of next record).
452 * This can be used as LSN for data pages affected by the logged action.
453 * (LSN is the XLOG point up to which the XLOG must be flushed to disk
454 * before the data page can be written out. This implements the basic
455 * WAL rule "write the log before the data".)
457 * NB: this routine feels free to scribble on the XLogRecData structs,
458 * though not on the data they reference. This is OK since the XLogRecData
459 * structs are always just temporaries in the calling code.
462 XLogInsert(RmgrId rmid
, uint8 info
, XLogRecData
*rdata
)
464 XLogCtlInsert
*Insert
= &XLogCtl
->Insert
;
466 XLogContRecord
*contrecord
;
468 XLogRecPtr WriteRqst
;
472 Buffer dtbuf
[XLR_MAX_BKP_BLOCKS
];
473 bool dtbuf_bkp
[XLR_MAX_BKP_BLOCKS
];
474 BkpBlock dtbuf_xlg
[XLR_MAX_BKP_BLOCKS
];
475 XLogRecPtr dtbuf_lsn
[XLR_MAX_BKP_BLOCKS
];
476 XLogRecData dtbuf_rdt1
[XLR_MAX_BKP_BLOCKS
];
477 XLogRecData dtbuf_rdt2
[XLR_MAX_BKP_BLOCKS
];
478 XLogRecData dtbuf_rdt3
[XLR_MAX_BKP_BLOCKS
];
485 bool isLogSwitch
= (rmid
== RM_XLOG_ID
&& info
== XLOG_SWITCH
);
487 /* info's high bits are reserved for use by me */
488 if (info
& XLR_INFO_MASK
)
489 elog(PANIC
, "invalid xlog info mask %02X", info
);
491 TRACE_POSTGRESQL_XLOG_INSERT(rmid
, info
);
494 * In bootstrap mode, we don't actually log anything but XLOG resources;
495 * return a phony record pointer.
497 if (IsBootstrapProcessingMode() && rmid
!= RM_XLOG_ID
)
500 RecPtr
.xrecoff
= SizeOfXLogLongPHD
; /* start of 1st chkpt record */
505 * Here we scan the rdata chain, determine which buffers must be backed
506 * up, and compute the CRC values for the data. Note that the record
507 * header isn't added into the CRC initially since we don't know the final
508 * length or info bits quite yet. Thus, the CRC will represent the CRC of
509 * the whole record in the order "rdata, then backup blocks, then record
512 * We may have to loop back to here if a race condition is detected below.
513 * We could prevent the race by doing all this work while holding the
514 * insert lock, but it seems better to avoid doing CRC calculations while
515 * holding the lock. This means we have to be careful about modifying the
516 * rdata chain until we know we aren't going to loop back again. The only
517 * change we allow ourselves to make earlier is to set rdt->data = NULL in
518 * chain items we have decided we will have to back up the whole buffer
519 * for. This is OK because we will certainly decide the same thing again
520 * for those items if we do it over; doing it here saves an extra pass
521 * over the chain later.
524 for (i
= 0; i
< XLR_MAX_BKP_BLOCKS
; i
++)
526 dtbuf
[i
] = InvalidBuffer
;
527 dtbuf_bkp
[i
] = false;
531 * Decide if we need to do full-page writes in this XLOG record: true if
532 * full_page_writes is on or we have a PITR request for it. Since we
533 * don't yet have the insert lock, forcePageWrites could change under us,
534 * but we'll recheck it once we have the lock.
536 doPageWrites
= fullPageWrites
|| Insert
->forcePageWrites
;
538 INIT_CRC32(rdata_crc
);
542 if (rdt
->buffer
== InvalidBuffer
)
544 /* Simple data, just include it */
546 COMP_CRC32(rdata_crc
, rdt
->data
, rdt
->len
);
550 /* Find info for buffer */
551 for (i
= 0; i
< XLR_MAX_BKP_BLOCKS
; i
++)
553 if (rdt
->buffer
== dtbuf
[i
])
555 /* Buffer already referenced by earlier chain item */
561 COMP_CRC32(rdata_crc
, rdt
->data
, rdt
->len
);
565 if (dtbuf
[i
] == InvalidBuffer
)
567 /* OK, put it in this slot */
568 dtbuf
[i
] = rdt
->buffer
;
569 if (XLogCheckBuffer(rdt
, doPageWrites
,
570 &(dtbuf_lsn
[i
]), &(dtbuf_xlg
[i
])))
578 COMP_CRC32(rdata_crc
, rdt
->data
, rdt
->len
);
583 if (i
>= XLR_MAX_BKP_BLOCKS
)
584 elog(PANIC
, "can backup at most %d blocks per xlog record",
587 /* Break out of loop when rdt points to last chain item */
588 if (rdt
->next
== NULL
)
594 * Now add the backup block headers and data into the CRC
596 for (i
= 0; i
< XLR_MAX_BKP_BLOCKS
; i
++)
600 BkpBlock
*bkpb
= &(dtbuf_xlg
[i
]);
603 COMP_CRC32(rdata_crc
,
606 page
= (char *) BufferGetBlock(dtbuf
[i
]);
607 if (bkpb
->hole_length
== 0)
609 COMP_CRC32(rdata_crc
,
615 /* must skip the hole */
616 COMP_CRC32(rdata_crc
,
619 COMP_CRC32(rdata_crc
,
620 page
+ (bkpb
->hole_offset
+ bkpb
->hole_length
),
621 BLCKSZ
- (bkpb
->hole_offset
+ bkpb
->hole_length
));
627 * NOTE: We disallow len == 0 because it provides a useful bit of extra
628 * error checking in ReadRecord. This means that all callers of
629 * XLogInsert must supply at least some not-in-a-buffer data. However, we
630 * make an exception for XLOG SWITCH records because we don't want them to
631 * ever cross a segment boundary.
633 if (len
== 0 && !isLogSwitch
)
634 elog(PANIC
, "invalid xlog record length %u", len
);
636 START_CRIT_SECTION();
638 /* Now wait to get insert lock */
639 LWLockAcquire(WALInsertLock
, LW_EXCLUSIVE
);
642 * Check to see if my RedoRecPtr is out of date. If so, may have to go
643 * back and recompute everything. This can only happen just after a
644 * checkpoint, so it's better to be slow in this case and fast otherwise.
646 * If we aren't doing full-page writes then RedoRecPtr doesn't actually
647 * affect the contents of the XLOG record, so we'll update our local copy
648 * but not force a recomputation.
650 if (!XLByteEQ(RedoRecPtr
, Insert
->RedoRecPtr
))
652 Assert(XLByteLT(RedoRecPtr
, Insert
->RedoRecPtr
));
653 RedoRecPtr
= Insert
->RedoRecPtr
;
657 for (i
= 0; i
< XLR_MAX_BKP_BLOCKS
; i
++)
659 if (dtbuf
[i
] == InvalidBuffer
)
661 if (dtbuf_bkp
[i
] == false &&
662 XLByteLE(dtbuf_lsn
[i
], RedoRecPtr
))
665 * Oops, this buffer now needs to be backed up, but we
666 * didn't think so above. Start over.
668 LWLockRelease(WALInsertLock
);
677 * Also check to see if forcePageWrites was just turned on; if we weren't
678 * already doing full-page writes then go back and recompute. (If it was
679 * just turned off, we could recompute the record without full pages, but
680 * we choose not to bother.)
682 if (Insert
->forcePageWrites
&& !doPageWrites
)
684 /* Oops, must redo it with full-page data */
685 LWLockRelease(WALInsertLock
);
691 * Make additional rdata chain entries for the backup blocks, so that we
692 * don't need to special-case them in the write loop. Note that we have
693 * now irrevocably changed the input rdata chain. At the exit of this
694 * loop, write_len includes the backup block data.
696 * Also set the appropriate info bits to show which buffers were backed
697 * up. The i'th XLR_SET_BKP_BLOCK bit corresponds to the i'th distinct
698 * buffer value (ignoring InvalidBuffer) appearing in the rdata chain.
701 for (i
= 0; i
< XLR_MAX_BKP_BLOCKS
; i
++)
709 info
|= XLR_SET_BKP_BLOCK(i
);
711 bkpb
= &(dtbuf_xlg
[i
]);
712 page
= (char *) BufferGetBlock(dtbuf
[i
]);
714 rdt
->next
= &(dtbuf_rdt1
[i
]);
717 rdt
->data
= (char *) bkpb
;
718 rdt
->len
= sizeof(BkpBlock
);
719 write_len
+= sizeof(BkpBlock
);
721 rdt
->next
= &(dtbuf_rdt2
[i
]);
724 if (bkpb
->hole_length
== 0)
733 /* must skip the hole */
735 rdt
->len
= bkpb
->hole_offset
;
736 write_len
+= bkpb
->hole_offset
;
738 rdt
->next
= &(dtbuf_rdt3
[i
]);
741 rdt
->data
= page
+ (bkpb
->hole_offset
+ bkpb
->hole_length
);
742 rdt
->len
= BLCKSZ
- (bkpb
->hole_offset
+ bkpb
->hole_length
);
743 write_len
+= rdt
->len
;
749 * If we backed up any full blocks and online backup is not in progress,
750 * mark the backup blocks as removable. This allows the WAL archiver to
751 * know whether it is safe to compress archived WAL data by transforming
752 * full-block records into the non-full-block format.
754 * Note: we could just set the flag whenever !forcePageWrites, but
755 * defining it like this leaves the info bit free for some potential other
756 * use in records without any backup blocks.
758 if ((info
& XLR_BKP_BLOCK_MASK
) && !Insert
->forcePageWrites
)
759 info
|= XLR_BKP_REMOVABLE
;
762 * If there isn't enough space on the current XLOG page for a record
763 * header, advance to the next page (leaving the unused space as zeroes).
766 freespace
= INSERT_FREESPACE(Insert
);
767 if (freespace
< SizeOfXLogRecord
)
769 updrqst
= AdvanceXLInsertBuffer(false);
770 freespace
= INSERT_FREESPACE(Insert
);
773 /* Compute record's XLOG location */
774 curridx
= Insert
->curridx
;
775 INSERT_RECPTR(RecPtr
, Insert
, curridx
);
778 * If the record is an XLOG_SWITCH, and we are exactly at the start of a
779 * segment, we need not insert it (and don't want to because we'd like
780 * consecutive switch requests to be no-ops). Instead, make sure
781 * everything is written and flushed through the end of the prior segment,
782 * and return the prior segment's end address.
785 (RecPtr
.xrecoff
% XLogSegSize
) == SizeOfXLogLongPHD
)
787 /* We can release insert lock immediately */
788 LWLockRelease(WALInsertLock
);
790 RecPtr
.xrecoff
-= SizeOfXLogLongPHD
;
791 if (RecPtr
.xrecoff
== 0)
793 /* crossing a logid boundary */
795 RecPtr
.xrecoff
= XLogFileSize
;
798 LWLockAcquire(WALWriteLock
, LW_EXCLUSIVE
);
799 LogwrtResult
= XLogCtl
->Write
.LogwrtResult
;
800 if (!XLByteLE(RecPtr
, LogwrtResult
.Flush
))
802 XLogwrtRqst FlushRqst
;
804 FlushRqst
.Write
= RecPtr
;
805 FlushRqst
.Flush
= RecPtr
;
806 XLogWrite(FlushRqst
, false, false);
808 LWLockRelease(WALWriteLock
);
815 /* Insert record header */
817 record
= (XLogRecord
*) Insert
->currpos
;
818 record
->xl_prev
= Insert
->PrevRecord
;
819 record
->xl_xid
= GetCurrentTransactionIdIfAny();
820 record
->xl_tot_len
= SizeOfXLogRecord
+ write_len
;
821 record
->xl_len
= len
; /* doesn't include backup blocks */
822 record
->xl_info
= info
;
823 record
->xl_rmid
= rmid
;
825 /* Now we can finish computing the record's CRC */
826 COMP_CRC32(rdata_crc
, (char *) record
+ sizeof(pg_crc32
),
827 SizeOfXLogRecord
- sizeof(pg_crc32
));
828 FIN_CRC32(rdata_crc
);
829 record
->xl_crc
= rdata_crc
;
836 initStringInfo(&buf
);
837 appendStringInfo(&buf
, "INSERT @ %X/%X: ",
838 RecPtr
.xlogid
, RecPtr
.xrecoff
);
839 xlog_outrec(&buf
, record
);
840 if (rdata
->data
!= NULL
)
842 appendStringInfo(&buf
, " - ");
843 RmgrTable
[record
->xl_rmid
].rm_desc(&buf
, record
->xl_info
, rdata
->data
);
845 elog(LOG
, "%s", buf
.data
);
850 /* Record begin of record in appropriate places */
851 ProcLastRecPtr
= RecPtr
;
852 Insert
->PrevRecord
= RecPtr
;
854 Insert
->currpos
+= SizeOfXLogRecord
;
855 freespace
-= SizeOfXLogRecord
;
858 * Append the data, including backup blocks if any
862 while (rdata
->data
== NULL
)
867 if (rdata
->len
> freespace
)
869 memcpy(Insert
->currpos
, rdata
->data
, freespace
);
870 rdata
->data
+= freespace
;
871 rdata
->len
-= freespace
;
872 write_len
-= freespace
;
876 memcpy(Insert
->currpos
, rdata
->data
, rdata
->len
);
877 freespace
-= rdata
->len
;
878 write_len
-= rdata
->len
;
879 Insert
->currpos
+= rdata
->len
;
885 /* Use next buffer */
886 updrqst
= AdvanceXLInsertBuffer(false);
887 curridx
= Insert
->curridx
;
888 /* Insert cont-record header */
889 Insert
->currpage
->xlp_info
|= XLP_FIRST_IS_CONTRECORD
;
890 contrecord
= (XLogContRecord
*) Insert
->currpos
;
891 contrecord
->xl_rem_len
= write_len
;
892 Insert
->currpos
+= SizeOfXLogContRecord
;
893 freespace
= INSERT_FREESPACE(Insert
);
896 /* Ensure next record will be properly aligned */
897 Insert
->currpos
= (char *) Insert
->currpage
+
898 MAXALIGN(Insert
->currpos
- (char *) Insert
->currpage
);
899 freespace
= INSERT_FREESPACE(Insert
);
902 * The recptr I return is the beginning of the *next* record. This will be
903 * stored as LSN for changed data pages...
905 INSERT_RECPTR(RecPtr
, Insert
, curridx
);
908 * If the record is an XLOG_SWITCH, we must now write and flush all the
909 * existing data, and then forcibly advance to the start of the next
910 * segment. It's not good to do this I/O while holding the insert lock,
911 * but there seems too much risk of confusion if we try to release the
912 * lock sooner. Fortunately xlog switch needn't be a high-performance
913 * operation anyway...
917 XLogCtlWrite
*Write
= &XLogCtl
->Write
;
918 XLogwrtRqst FlushRqst
;
919 XLogRecPtr OldSegEnd
;
921 TRACE_POSTGRESQL_XLOG_SWITCH();
923 LWLockAcquire(WALWriteLock
, LW_EXCLUSIVE
);
926 * Flush through the end of the page containing XLOG_SWITCH, and
927 * perform end-of-segment actions (eg, notifying archiver).
929 WriteRqst
= XLogCtl
->xlblocks
[curridx
];
930 FlushRqst
.Write
= WriteRqst
;
931 FlushRqst
.Flush
= WriteRqst
;
932 XLogWrite(FlushRqst
, false, true);
934 /* Set up the next buffer as first page of next segment */
935 /* Note: AdvanceXLInsertBuffer cannot need to do I/O here */
936 (void) AdvanceXLInsertBuffer(true);
938 /* There should be no unwritten data */
939 curridx
= Insert
->curridx
;
940 Assert(curridx
== Write
->curridx
);
942 /* Compute end address of old segment */
943 OldSegEnd
= XLogCtl
->xlblocks
[curridx
];
944 OldSegEnd
.xrecoff
-= XLOG_BLCKSZ
;
945 if (OldSegEnd
.xrecoff
== 0)
947 /* crossing a logid boundary */
948 OldSegEnd
.xlogid
-= 1;
949 OldSegEnd
.xrecoff
= XLogFileSize
;
952 /* Make it look like we've written and synced all of old segment */
953 LogwrtResult
.Write
= OldSegEnd
;
954 LogwrtResult
.Flush
= OldSegEnd
;
957 * Update shared-memory status --- this code should match XLogWrite
960 /* use volatile pointer to prevent code rearrangement */
961 volatile XLogCtlData
*xlogctl
= XLogCtl
;
963 SpinLockAcquire(&xlogctl
->info_lck
);
964 xlogctl
->LogwrtResult
= LogwrtResult
;
965 if (XLByteLT(xlogctl
->LogwrtRqst
.Write
, LogwrtResult
.Write
))
966 xlogctl
->LogwrtRqst
.Write
= LogwrtResult
.Write
;
967 if (XLByteLT(xlogctl
->LogwrtRqst
.Flush
, LogwrtResult
.Flush
))
968 xlogctl
->LogwrtRqst
.Flush
= LogwrtResult
.Flush
;
969 SpinLockRelease(&xlogctl
->info_lck
);
972 Write
->LogwrtResult
= LogwrtResult
;
974 LWLockRelease(WALWriteLock
);
976 updrqst
= false; /* done already */
980 /* normal case, ie not xlog switch */
982 /* Need to update shared LogwrtRqst if some block was filled up */
983 if (freespace
< SizeOfXLogRecord
)
985 /* curridx is filled and available for writing out */
990 /* if updrqst already set, write through end of previous buf */
991 curridx
= PrevBufIdx(curridx
);
993 WriteRqst
= XLogCtl
->xlblocks
[curridx
];
996 LWLockRelease(WALInsertLock
);
1000 /* use volatile pointer to prevent code rearrangement */
1001 volatile XLogCtlData
*xlogctl
= XLogCtl
;
1003 SpinLockAcquire(&xlogctl
->info_lck
);
1004 /* advance global request to include new block(s) */
1005 if (XLByteLT(xlogctl
->LogwrtRqst
.Write
, WriteRqst
))
1006 xlogctl
->LogwrtRqst
.Write
= WriteRqst
;
1007 /* update local result copy while I have the chance */
1008 LogwrtResult
= xlogctl
->LogwrtResult
;
1009 SpinLockRelease(&xlogctl
->info_lck
);
1012 XactLastRecEnd
= RecPtr
;
1020 * Determine whether the buffer referenced by an XLogRecData item has to
1021 * be backed up, and if so fill a BkpBlock struct for it. In any case
1022 * save the buffer's LSN at *lsn.
1025 XLogCheckBuffer(XLogRecData
*rdata
, bool doPageWrites
,
1026 XLogRecPtr
*lsn
, BkpBlock
*bkpb
)
1030 page
= BufferGetPage(rdata
->buffer
);
1033 * XXX We assume page LSN is first data on *every* page that can be passed
1034 * to XLogInsert, whether it otherwise has the standard page layout or
1037 *lsn
= PageGetLSN(page
);
1040 XLByteLE(PageGetLSN(page
), RedoRecPtr
))
1043 * The page needs to be backed up, so set up *bkpb
1045 BufferGetTag(rdata
->buffer
, &bkpb
->node
, &bkpb
->fork
, &bkpb
->block
);
1047 if (rdata
->buffer_std
)
1049 /* Assume we can omit data between pd_lower and pd_upper */
1050 uint16 lower
= ((PageHeader
) page
)->pd_lower
;
1051 uint16 upper
= ((PageHeader
) page
)->pd_upper
;
1053 if (lower
>= SizeOfPageHeaderData
&&
1057 bkpb
->hole_offset
= lower
;
1058 bkpb
->hole_length
= upper
- lower
;
1062 /* No "hole" to compress out */
1063 bkpb
->hole_offset
= 0;
1064 bkpb
->hole_length
= 0;
1069 /* Not a standard page header, don't try to eliminate "hole" */
1070 bkpb
->hole_offset
= 0;
1071 bkpb
->hole_length
= 0;
1074 return true; /* buffer requires backup */
1077 return false; /* buffer does not need to be backed up */
1083 * Create an archive notification file
1085 * The name of the notification file is the message that will be picked up
1086 * by the archiver, e.g. we write 0000000100000001000000C6.ready
1087 * and the archiver then knows to archive XLOGDIR/0000000100000001000000C6,
1088 * then when complete, rename it to 0000000100000001000000C6.done
1091 XLogArchiveNotify(const char *xlog
)
1093 char archiveStatusPath
[MAXPGPATH
];
1096 /* insert an otherwise empty file called <XLOG>.ready */
1097 StatusFilePath(archiveStatusPath
, xlog
, ".ready");
1098 fd
= AllocateFile(archiveStatusPath
, "w");
1102 (errcode_for_file_access(),
1103 errmsg("could not create archive status file \"%s\": %m",
1104 archiveStatusPath
)));
1110 (errcode_for_file_access(),
1111 errmsg("could not write archive status file \"%s\": %m",
1112 archiveStatusPath
)));
1116 /* Notify archiver that it's got something to do */
1117 if (IsUnderPostmaster
)
1118 SendPostmasterSignal(PMSIGNAL_WAKEN_ARCHIVER
);
1122 * Convenience routine to notify using log/seg representation of filename
1125 XLogArchiveNotifySeg(uint32 log
, uint32 seg
)
1127 char xlog
[MAXFNAMELEN
];
1129 XLogFileName(xlog
, ThisTimeLineID
, log
, seg
);
1130 XLogArchiveNotify(xlog
);
1134 * XLogArchiveCheckDone
1136 * This is called when we are ready to delete or recycle an old XLOG segment
1137 * file or backup history file. If it is okay to delete it then return true.
1138 * If it is not time to delete it, make sure a .ready file exists, and return
1141 * If <XLOG>.done exists, then return true; else if <XLOG>.ready exists,
1142 * then return false; else create <XLOG>.ready and return false.
1144 * The reason we do things this way is so that if the original attempt to
1145 * create <XLOG>.ready fails, we'll retry during subsequent checkpoints.
1148 XLogArchiveCheckDone(const char *xlog
)
1150 char archiveStatusPath
[MAXPGPATH
];
1151 struct stat stat_buf
;
1153 /* Always deletable if archiving is off */
1154 if (!XLogArchivingActive())
1157 /* First check for .done --- this means archiver is done with it */
1158 StatusFilePath(archiveStatusPath
, xlog
, ".done");
1159 if (stat(archiveStatusPath
, &stat_buf
) == 0)
1162 /* check for .ready --- this means archiver is still busy with it */
1163 StatusFilePath(archiveStatusPath
, xlog
, ".ready");
1164 if (stat(archiveStatusPath
, &stat_buf
) == 0)
1167 /* Race condition --- maybe archiver just finished, so recheck */
1168 StatusFilePath(archiveStatusPath
, xlog
, ".done");
1169 if (stat(archiveStatusPath
, &stat_buf
) == 0)
1172 /* Retry creation of the .ready file */
1173 XLogArchiveNotify(xlog
);
1180 * Check to see if an XLOG segment file is still unarchived.
1181 * This is almost but not quite the inverse of XLogArchiveCheckDone: in
1182 * the first place we aren't chartered to recreate the .ready file, and
1183 * in the second place we should consider that if the file is already gone
1184 * then it's not busy. (This check is needed to handle the race condition
1185 * that a checkpoint already deleted the no-longer-needed file.)
1188 XLogArchiveIsBusy(const char *xlog
)
1190 char archiveStatusPath
[MAXPGPATH
];
1191 struct stat stat_buf
;
1193 /* First check for .done --- this means archiver is done with it */
1194 StatusFilePath(archiveStatusPath
, xlog
, ".done");
1195 if (stat(archiveStatusPath
, &stat_buf
) == 0)
1198 /* check for .ready --- this means archiver is still busy with it */
1199 StatusFilePath(archiveStatusPath
, xlog
, ".ready");
1200 if (stat(archiveStatusPath
, &stat_buf
) == 0)
1203 /* Race condition --- maybe archiver just finished, so recheck */
1204 StatusFilePath(archiveStatusPath
, xlog
, ".done");
1205 if (stat(archiveStatusPath
, &stat_buf
) == 0)
1209 * Check to see if the WAL file has been removed by checkpoint,
1210 * which implies it has already been archived, and explains why we
1211 * can't see a status file for it.
1213 snprintf(archiveStatusPath
, MAXPGPATH
, XLOGDIR
"/%s", xlog
);
1214 if (stat(archiveStatusPath
, &stat_buf
) != 0 &&
1222 * XLogArchiveCleanup
1224 * Cleanup archive notification file(s) for a particular xlog segment
1227 XLogArchiveCleanup(const char *xlog
)
1229 char archiveStatusPath
[MAXPGPATH
];
1231 /* Remove the .done file */
1232 StatusFilePath(archiveStatusPath
, xlog
, ".done");
1233 unlink(archiveStatusPath
);
1234 /* should we complain about failure? */
1236 /* Remove the .ready file if present --- normally it shouldn't be */
1237 StatusFilePath(archiveStatusPath
, xlog
, ".ready");
1238 unlink(archiveStatusPath
);
1239 /* should we complain about failure? */
1243 * Advance the Insert state to the next buffer page, writing out the next
1244 * buffer if it still contains unwritten data.
1246 * If new_segment is TRUE then we set up the next buffer page as the first
1247 * page of the next xlog segment file, possibly but not usually the next
1248 * consecutive file page.
1250 * The global LogwrtRqst.Write pointer needs to be advanced to include the
1251 * just-filled page. If we can do this for free (without an extra lock),
1252 * we do so here. Otherwise the caller must do it. We return TRUE if the
1253 * request update still needs to be done, FALSE if we did it internally.
1255 * Must be called with WALInsertLock held.
1258 AdvanceXLInsertBuffer(bool new_segment
)
1260 XLogCtlInsert
*Insert
= &XLogCtl
->Insert
;
1261 XLogCtlWrite
*Write
= &XLogCtl
->Write
;
1262 int nextidx
= NextBufIdx(Insert
->curridx
);
1263 bool update_needed
= true;
1264 XLogRecPtr OldPageRqstPtr
;
1265 XLogwrtRqst WriteRqst
;
1266 XLogRecPtr NewPageEndPtr
;
1267 XLogPageHeader NewPage
;
1269 /* Use Insert->LogwrtResult copy if it's more fresh */
1270 if (XLByteLT(LogwrtResult
.Write
, Insert
->LogwrtResult
.Write
))
1271 LogwrtResult
= Insert
->LogwrtResult
;
1274 * Get ending-offset of the buffer page we need to replace (this may be
1275 * zero if the buffer hasn't been used yet). Fall through if it's already
1278 OldPageRqstPtr
= XLogCtl
->xlblocks
[nextidx
];
1279 if (!XLByteLE(OldPageRqstPtr
, LogwrtResult
.Write
))
1281 /* nope, got work to do... */
1282 XLogRecPtr FinishedPageRqstPtr
;
1284 FinishedPageRqstPtr
= XLogCtl
->xlblocks
[Insert
->curridx
];
1286 /* Before waiting, get info_lck and update LogwrtResult */
1288 /* use volatile pointer to prevent code rearrangement */
1289 volatile XLogCtlData
*xlogctl
= XLogCtl
;
1291 SpinLockAcquire(&xlogctl
->info_lck
);
1292 if (XLByteLT(xlogctl
->LogwrtRqst
.Write
, FinishedPageRqstPtr
))
1293 xlogctl
->LogwrtRqst
.Write
= FinishedPageRqstPtr
;
1294 LogwrtResult
= xlogctl
->LogwrtResult
;
1295 SpinLockRelease(&xlogctl
->info_lck
);
1298 update_needed
= false; /* Did the shared-request update */
1300 if (XLByteLE(OldPageRqstPtr
, LogwrtResult
.Write
))
1302 /* OK, someone wrote it already */
1303 Insert
->LogwrtResult
= LogwrtResult
;
1307 /* Must acquire write lock */
1308 LWLockAcquire(WALWriteLock
, LW_EXCLUSIVE
);
1309 LogwrtResult
= Write
->LogwrtResult
;
1310 if (XLByteLE(OldPageRqstPtr
, LogwrtResult
.Write
))
1312 /* OK, someone wrote it already */
1313 LWLockRelease(WALWriteLock
);
1314 Insert
->LogwrtResult
= LogwrtResult
;
1319 * Have to write buffers while holding insert lock. This is
1320 * not good, so only write as much as we absolutely must.
1322 TRACE_POSTGRESQL_WAL_BUFFER_WRITE_DIRTY_START();
1323 WriteRqst
.Write
= OldPageRqstPtr
;
1324 WriteRqst
.Flush
.xlogid
= 0;
1325 WriteRqst
.Flush
.xrecoff
= 0;
1326 XLogWrite(WriteRqst
, false, false);
1327 LWLockRelease(WALWriteLock
);
1328 Insert
->LogwrtResult
= LogwrtResult
;
1329 TRACE_POSTGRESQL_WAL_BUFFER_WRITE_DIRTY_DONE();
1335 * Now the next buffer slot is free and we can set it up to be the next
1338 NewPageEndPtr
= XLogCtl
->xlblocks
[Insert
->curridx
];
1342 /* force it to a segment start point */
1343 NewPageEndPtr
.xrecoff
+= XLogSegSize
- 1;
1344 NewPageEndPtr
.xrecoff
-= NewPageEndPtr
.xrecoff
% XLogSegSize
;
1347 if (NewPageEndPtr
.xrecoff
>= XLogFileSize
)
1349 /* crossing a logid boundary */
1350 NewPageEndPtr
.xlogid
+= 1;
1351 NewPageEndPtr
.xrecoff
= XLOG_BLCKSZ
;
1354 NewPageEndPtr
.xrecoff
+= XLOG_BLCKSZ
;
1355 XLogCtl
->xlblocks
[nextidx
] = NewPageEndPtr
;
1356 NewPage
= (XLogPageHeader
) (XLogCtl
->pages
+ nextidx
* (Size
) XLOG_BLCKSZ
);
1358 Insert
->curridx
= nextidx
;
1359 Insert
->currpage
= NewPage
;
1361 Insert
->currpos
= ((char *) NewPage
) +SizeOfXLogShortPHD
;
1364 * Be sure to re-zero the buffer so that bytes beyond what we've written
1365 * will look like zeroes and not valid XLOG records...
1367 MemSet((char *) NewPage
, 0, XLOG_BLCKSZ
);
1370 * Fill the new page's header
1372 NewPage
->xlp_magic
= XLOG_PAGE_MAGIC
;
1374 /* NewPage->xlp_info = 0; */ /* done by memset */
1375 NewPage
->xlp_tli
= ThisTimeLineID
;
1376 NewPage
->xlp_pageaddr
.xlogid
= NewPageEndPtr
.xlogid
;
1377 NewPage
->xlp_pageaddr
.xrecoff
= NewPageEndPtr
.xrecoff
- XLOG_BLCKSZ
;
1380 * If first page of an XLOG segment file, make it a long header.
1382 if ((NewPage
->xlp_pageaddr
.xrecoff
% XLogSegSize
) == 0)
1384 XLogLongPageHeader NewLongPage
= (XLogLongPageHeader
) NewPage
;
1386 NewLongPage
->xlp_sysid
= ControlFile
->system_identifier
;
1387 NewLongPage
->xlp_seg_size
= XLogSegSize
;
1388 NewLongPage
->xlp_xlog_blcksz
= XLOG_BLCKSZ
;
1389 NewPage
->xlp_info
|= XLP_LONG_HEADER
;
1391 Insert
->currpos
= ((char *) NewPage
) +SizeOfXLogLongPHD
;
1394 return update_needed
;
1398 * Check whether we've consumed enough xlog space that a checkpoint is needed.
1400 * Caller must have just finished filling the open log file (so that
1401 * openLogId/openLogSeg are valid). We measure the distance from RedoRecPtr
1402 * to the open log file and see if that exceeds CheckPointSegments.
1404 * Note: it is caller's responsibility that RedoRecPtr is up-to-date.
1407 XLogCheckpointNeeded(void)
1410 * A straight computation of segment number could overflow 32 bits. Rather
1411 * than assuming we have working 64-bit arithmetic, we compare the
1412 * highest-order bits separately, and force a checkpoint immediately when
1417 uint32 old_highbits
,
1420 old_segno
= (RedoRecPtr
.xlogid
% XLogSegSize
) * XLogSegsPerFile
+
1421 (RedoRecPtr
.xrecoff
/ XLogSegSize
);
1422 old_highbits
= RedoRecPtr
.xlogid
/ XLogSegSize
;
1423 new_segno
= (openLogId
% XLogSegSize
) * XLogSegsPerFile
+ openLogSeg
;
1424 new_highbits
= openLogId
/ XLogSegSize
;
1425 if (new_highbits
!= old_highbits
||
1426 new_segno
>= old_segno
+ (uint32
) (CheckPointSegments
- 1))
1432 * Write and/or fsync the log at least as far as WriteRqst indicates.
1434 * If flexible == TRUE, we don't have to write as far as WriteRqst, but
1435 * may stop at any convenient boundary (such as a cache or logfile boundary).
1436 * This option allows us to avoid uselessly issuing multiple writes when a
1437 * single one would do.
1439 * If xlog_switch == TRUE, we are intending an xlog segment switch, so
1440 * perform end-of-segment actions after writing the last page, even if
1441 * it's not physically the end of its segment. (NB: this will work properly
1442 * only if caller specifies WriteRqst == page-end and flexible == false,
1443 * and there is some data to write.)
1445 * Must be called with WALWriteLock held.
1448 XLogWrite(XLogwrtRqst WriteRqst
, bool flexible
, bool xlog_switch
)
1450 XLogCtlWrite
*Write
= &XLogCtl
->Write
;
1452 bool last_iteration
;
1460 /* We should always be inside a critical section here */
1461 Assert(CritSectionCount
> 0);
1464 * Update local LogwrtResult (caller probably did this already, but...)
1466 LogwrtResult
= Write
->LogwrtResult
;
1469 * Since successive pages in the xlog cache are consecutively allocated,
1470 * we can usually gather multiple pages together and issue just one
1471 * write() call. npages is the number of pages we have determined can be
1472 * written together; startidx is the cache block index of the first one,
1473 * and startoffset is the file offset at which it should go. The latter
1474 * two variables are only valid when npages > 0, but we must initialize
1475 * all of them to keep the compiler quiet.
1482 * Within the loop, curridx is the cache block index of the page to
1483 * consider writing. We advance Write->curridx only after successfully
1484 * writing pages. (Right now, this refinement is useless since we are
1485 * going to PANIC if any error occurs anyway; but someday it may come in
1488 curridx
= Write
->curridx
;
1490 while (XLByteLT(LogwrtResult
.Write
, WriteRqst
.Write
))
1493 * Make sure we're not ahead of the insert process. This could happen
1494 * if we're passed a bogus WriteRqst.Write that is past the end of the
1495 * last page that's been initialized by AdvanceXLInsertBuffer.
1497 if (!XLByteLT(LogwrtResult
.Write
, XLogCtl
->xlblocks
[curridx
]))
1498 elog(PANIC
, "xlog write request %X/%X is past end of log %X/%X",
1499 LogwrtResult
.Write
.xlogid
, LogwrtResult
.Write
.xrecoff
,
1500 XLogCtl
->xlblocks
[curridx
].xlogid
,
1501 XLogCtl
->xlblocks
[curridx
].xrecoff
);
1503 /* Advance LogwrtResult.Write to end of current buffer page */
1504 LogwrtResult
.Write
= XLogCtl
->xlblocks
[curridx
];
1505 ispartialpage
= XLByteLT(WriteRqst
.Write
, LogwrtResult
.Write
);
1507 if (!XLByteInPrevSeg(LogwrtResult
.Write
, openLogId
, openLogSeg
))
1510 * Switch to new logfile segment. We cannot have any pending
1511 * pages here (since we dump what we have at segment end).
1513 Assert(npages
== 0);
1514 if (openLogFile
>= 0)
1516 XLByteToPrevSeg(LogwrtResult
.Write
, openLogId
, openLogSeg
);
1518 /* create/use new log file */
1519 use_existent
= true;
1520 openLogFile
= XLogFileInit(openLogId
, openLogSeg
,
1521 &use_existent
, true);
1525 /* Make sure we have the current logfile open */
1526 if (openLogFile
< 0)
1528 XLByteToPrevSeg(LogwrtResult
.Write
, openLogId
, openLogSeg
);
1529 openLogFile
= XLogFileOpen(openLogId
, openLogSeg
);
1533 /* Add current page to the set of pending pages-to-dump */
1536 /* first of group */
1538 startoffset
= (LogwrtResult
.Write
.xrecoff
- XLOG_BLCKSZ
) % XLogSegSize
;
1543 * Dump the set if this will be the last loop iteration, or if we are
1544 * at the last page of the cache area (since the next page won't be
1545 * contiguous in memory), or if we are at the end of the logfile
1548 last_iteration
= !XLByteLT(LogwrtResult
.Write
, WriteRqst
.Write
);
1550 finishing_seg
= !ispartialpage
&&
1551 (startoffset
+ npages
* XLOG_BLCKSZ
) >= XLogSegSize
;
1553 if (last_iteration
||
1554 curridx
== XLogCtl
->XLogCacheBlck
||
1560 /* Need to seek in the file? */
1561 if (openLogOff
!= startoffset
)
1563 if (lseek(openLogFile
, (off_t
) startoffset
, SEEK_SET
) < 0)
1565 (errcode_for_file_access(),
1566 errmsg("could not seek in log file %u, "
1567 "segment %u to offset %u: %m",
1568 openLogId
, openLogSeg
, startoffset
)));
1569 openLogOff
= startoffset
;
1572 /* OK to write the page(s) */
1573 from
= XLogCtl
->pages
+ startidx
* (Size
) XLOG_BLCKSZ
;
1574 nbytes
= npages
* (Size
) XLOG_BLCKSZ
;
1576 if (write(openLogFile
, from
, nbytes
) != nbytes
)
1578 /* if write didn't set errno, assume no disk space */
1582 (errcode_for_file_access(),
1583 errmsg("could not write to log file %u, segment %u "
1584 "at offset %u, length %lu: %m",
1585 openLogId
, openLogSeg
,
1586 openLogOff
, (unsigned long) nbytes
)));
1589 /* Update state for write */
1590 openLogOff
+= nbytes
;
1591 Write
->curridx
= ispartialpage
? curridx
: NextBufIdx(curridx
);
1595 * If we just wrote the whole last page of a logfile segment,
1596 * fsync the segment immediately. This avoids having to go back
1597 * and re-open prior segments when an fsync request comes along
1598 * later. Doing it here ensures that one and only one backend will
1599 * perform this fsync.
1601 * We also do this if this is the last page written for an xlog
1604 * This is also the right place to notify the Archiver that the
1605 * segment is ready to copy to archival storage, and to update the
1606 * timer for archive_timeout, and to signal for a checkpoint if
1607 * too many logfile segments have been used since the last
1610 if (finishing_seg
|| (xlog_switch
&& last_iteration
))
1613 LogwrtResult
.Flush
= LogwrtResult
.Write
; /* end of page */
1615 if (XLogArchivingActive())
1616 XLogArchiveNotifySeg(openLogId
, openLogSeg
);
1618 Write
->lastSegSwitchTime
= (pg_time_t
) time(NULL
);
1621 * Signal bgwriter to start a checkpoint if we've consumed too
1622 * much xlog since the last one. For speed, we first check
1623 * using the local copy of RedoRecPtr, which might be out of
1624 * date; if it looks like a checkpoint is needed, forcibly
1625 * update RedoRecPtr and recheck.
1627 if (IsUnderPostmaster
&&
1628 XLogCheckpointNeeded())
1630 (void) GetRedoRecPtr();
1631 if (XLogCheckpointNeeded())
1632 RequestCheckpoint(CHECKPOINT_CAUSE_XLOG
);
1639 /* Only asked to write a partial page */
1640 LogwrtResult
.Write
= WriteRqst
.Write
;
1643 curridx
= NextBufIdx(curridx
);
1645 /* If flexible, break out of loop as soon as we wrote something */
1646 if (flexible
&& npages
== 0)
1650 Assert(npages
== 0);
1651 Assert(curridx
== Write
->curridx
);
1654 * If asked to flush, do so
1656 if (XLByteLT(LogwrtResult
.Flush
, WriteRqst
.Flush
) &&
1657 XLByteLT(LogwrtResult
.Flush
, LogwrtResult
.Write
))
1660 * Could get here without iterating above loop, in which case we might
1661 * have no open file or the wrong one. However, we do not need to
1662 * fsync more than one file.
1664 if (sync_method
!= SYNC_METHOD_OPEN
&&
1665 sync_method
!= SYNC_METHOD_OPEN_DSYNC
)
1667 if (openLogFile
>= 0 &&
1668 !XLByteInPrevSeg(LogwrtResult
.Write
, openLogId
, openLogSeg
))
1670 if (openLogFile
< 0)
1672 XLByteToPrevSeg(LogwrtResult
.Write
, openLogId
, openLogSeg
);
1673 openLogFile
= XLogFileOpen(openLogId
, openLogSeg
);
1678 LogwrtResult
.Flush
= LogwrtResult
.Write
;
1682 * Update shared-memory status
1684 * We make sure that the shared 'request' values do not fall behind the
1685 * 'result' values. This is not absolutely essential, but it saves some
1686 * code in a couple of places.
1689 /* use volatile pointer to prevent code rearrangement */
1690 volatile XLogCtlData
*xlogctl
= XLogCtl
;
1692 SpinLockAcquire(&xlogctl
->info_lck
);
1693 xlogctl
->LogwrtResult
= LogwrtResult
;
1694 if (XLByteLT(xlogctl
->LogwrtRqst
.Write
, LogwrtResult
.Write
))
1695 xlogctl
->LogwrtRqst
.Write
= LogwrtResult
.Write
;
1696 if (XLByteLT(xlogctl
->LogwrtRqst
.Flush
, LogwrtResult
.Flush
))
1697 xlogctl
->LogwrtRqst
.Flush
= LogwrtResult
.Flush
;
1698 SpinLockRelease(&xlogctl
->info_lck
);
1701 Write
->LogwrtResult
= LogwrtResult
;
1705 * Record the LSN for an asynchronous transaction commit.
1706 * (This should not be called for aborts, nor for synchronous commits.)
1709 XLogSetAsyncCommitLSN(XLogRecPtr asyncCommitLSN
)
1711 /* use volatile pointer to prevent code rearrangement */
1712 volatile XLogCtlData
*xlogctl
= XLogCtl
;
1714 SpinLockAcquire(&xlogctl
->info_lck
);
1715 if (XLByteLT(xlogctl
->asyncCommitLSN
, asyncCommitLSN
))
1716 xlogctl
->asyncCommitLSN
= asyncCommitLSN
;
1717 SpinLockRelease(&xlogctl
->info_lck
);
1721 * Ensure that all XLOG data through the given position is flushed to disk.
1723 * NOTE: this differs from XLogWrite mainly in that the WALWriteLock is not
1724 * already held, and we try to avoid acquiring it if possible.
1727 XLogFlush(XLogRecPtr record
)
1729 XLogRecPtr WriteRqstPtr
;
1730 XLogwrtRqst WriteRqst
;
1732 /* Disabled during REDO */
1736 /* Quick exit if already known flushed */
1737 if (XLByteLE(record
, LogwrtResult
.Flush
))
1742 elog(LOG
, "xlog flush request %X/%X; write %X/%X; flush %X/%X",
1743 record
.xlogid
, record
.xrecoff
,
1744 LogwrtResult
.Write
.xlogid
, LogwrtResult
.Write
.xrecoff
,
1745 LogwrtResult
.Flush
.xlogid
, LogwrtResult
.Flush
.xrecoff
);
1748 START_CRIT_SECTION();
1751 * Since fsync is usually a horribly expensive operation, we try to
1752 * piggyback as much data as we can on each fsync: if we see any more data
1753 * entered into the xlog buffer, we'll write and fsync that too, so that
1754 * the final value of LogwrtResult.Flush is as large as possible. This
1755 * gives us some chance of avoiding another fsync immediately after.
1758 /* initialize to given target; may increase below */
1759 WriteRqstPtr
= record
;
1761 /* read LogwrtResult and update local state */
1763 /* use volatile pointer to prevent code rearrangement */
1764 volatile XLogCtlData
*xlogctl
= XLogCtl
;
1766 SpinLockAcquire(&xlogctl
->info_lck
);
1767 if (XLByteLT(WriteRqstPtr
, xlogctl
->LogwrtRqst
.Write
))
1768 WriteRqstPtr
= xlogctl
->LogwrtRqst
.Write
;
1769 LogwrtResult
= xlogctl
->LogwrtResult
;
1770 SpinLockRelease(&xlogctl
->info_lck
);
1774 if (!XLByteLE(record
, LogwrtResult
.Flush
))
1776 /* now wait for the write lock */
1777 LWLockAcquire(WALWriteLock
, LW_EXCLUSIVE
);
1778 LogwrtResult
= XLogCtl
->Write
.LogwrtResult
;
1779 if (!XLByteLE(record
, LogwrtResult
.Flush
))
1781 /* try to write/flush later additions to XLOG as well */
1782 if (LWLockConditionalAcquire(WALInsertLock
, LW_EXCLUSIVE
))
1784 XLogCtlInsert
*Insert
= &XLogCtl
->Insert
;
1785 uint32 freespace
= INSERT_FREESPACE(Insert
);
1787 if (freespace
< SizeOfXLogRecord
) /* buffer is full */
1788 WriteRqstPtr
= XLogCtl
->xlblocks
[Insert
->curridx
];
1791 WriteRqstPtr
= XLogCtl
->xlblocks
[Insert
->curridx
];
1792 WriteRqstPtr
.xrecoff
-= freespace
;
1794 LWLockRelease(WALInsertLock
);
1795 WriteRqst
.Write
= WriteRqstPtr
;
1796 WriteRqst
.Flush
= WriteRqstPtr
;
1800 WriteRqst
.Write
= WriteRqstPtr
;
1801 WriteRqst
.Flush
= record
;
1803 XLogWrite(WriteRqst
, false, false);
1805 LWLockRelease(WALWriteLock
);
1811 * If we still haven't flushed to the request point then we have a
1812 * problem; most likely, the requested flush point is past end of XLOG.
1813 * This has been seen to occur when a disk page has a corrupted LSN.
1815 * Formerly we treated this as a PANIC condition, but that hurts the
1816 * system's robustness rather than helping it: we do not want to take down
1817 * the whole system due to corruption on one data page. In particular, if
1818 * the bad page is encountered again during recovery then we would be
1819 * unable to restart the database at all! (This scenario has actually
1820 * happened in the field several times with 7.1 releases. Note that we
1821 * cannot get here while InRedo is true, but if the bad page is brought in
1822 * and marked dirty during recovery then CreateCheckPoint will try to
1823 * flush it at the end of recovery.)
1825 * The current approach is to ERROR under normal conditions, but only
1826 * WARNING during recovery, so that the system can be brought up even if
1827 * there's a corrupt LSN. Note that for calls from xact.c, the ERROR will
1828 * be promoted to PANIC since xact.c calls this routine inside a critical
1829 * section. However, calls from bufmgr.c are not within critical sections
1830 * and so we will not force a restart for a bad LSN on a data page.
1832 if (XLByteLT(LogwrtResult
.Flush
, record
))
1833 elog(InRecovery
? WARNING
: ERROR
,
1834 "xlog flush request %X/%X is not satisfied --- flushed only to %X/%X",
1835 record
.xlogid
, record
.xrecoff
,
1836 LogwrtResult
.Flush
.xlogid
, LogwrtResult
.Flush
.xrecoff
);
1840 * Flush xlog, but without specifying exactly where to flush to.
1842 * We normally flush only completed blocks; but if there is nothing to do on
1843 * that basis, we check for unflushed async commits in the current incomplete
1844 * block, and flush through the latest one of those. Thus, if async commits
1845 * are not being used, we will flush complete blocks only. We can guarantee
1846 * that async commits reach disk after at most three cycles; normally only
1847 * one or two. (We allow XLogWrite to write "flexibly", meaning it can stop
1848 * at the end of the buffer ring; this makes a difference only with very high
1849 * load or long wal_writer_delay, but imposes one extra cycle for the worst
1850 * case for async commits.)
1852 * This routine is invoked periodically by the background walwriter process.
1855 XLogBackgroundFlush(void)
1857 XLogRecPtr WriteRqstPtr
;
1858 bool flexible
= true;
1860 /* read LogwrtResult and update local state */
1862 /* use volatile pointer to prevent code rearrangement */
1863 volatile XLogCtlData
*xlogctl
= XLogCtl
;
1865 SpinLockAcquire(&xlogctl
->info_lck
);
1866 LogwrtResult
= xlogctl
->LogwrtResult
;
1867 WriteRqstPtr
= xlogctl
->LogwrtRqst
.Write
;
1868 SpinLockRelease(&xlogctl
->info_lck
);
1871 /* back off to last completed page boundary */
1872 WriteRqstPtr
.xrecoff
-= WriteRqstPtr
.xrecoff
% XLOG_BLCKSZ
;
1874 /* if we have already flushed that far, consider async commit records */
1875 if (XLByteLE(WriteRqstPtr
, LogwrtResult
.Flush
))
1877 /* use volatile pointer to prevent code rearrangement */
1878 volatile XLogCtlData
*xlogctl
= XLogCtl
;
1880 SpinLockAcquire(&xlogctl
->info_lck
);
1881 WriteRqstPtr
= xlogctl
->asyncCommitLSN
;
1882 SpinLockRelease(&xlogctl
->info_lck
);
1883 flexible
= false; /* ensure it all gets written */
1886 /* Done if already known flushed */
1887 if (XLByteLE(WriteRqstPtr
, LogwrtResult
.Flush
))
1892 elog(LOG
, "xlog bg flush request %X/%X; write %X/%X; flush %X/%X",
1893 WriteRqstPtr
.xlogid
, WriteRqstPtr
.xrecoff
,
1894 LogwrtResult
.Write
.xlogid
, LogwrtResult
.Write
.xrecoff
,
1895 LogwrtResult
.Flush
.xlogid
, LogwrtResult
.Flush
.xrecoff
);
1898 START_CRIT_SECTION();
1900 /* now wait for the write lock */
1901 LWLockAcquire(WALWriteLock
, LW_EXCLUSIVE
);
1902 LogwrtResult
= XLogCtl
->Write
.LogwrtResult
;
1903 if (!XLByteLE(WriteRqstPtr
, LogwrtResult
.Flush
))
1905 XLogwrtRqst WriteRqst
;
1907 WriteRqst
.Write
= WriteRqstPtr
;
1908 WriteRqst
.Flush
= WriteRqstPtr
;
1909 XLogWrite(WriteRqst
, flexible
, false);
1911 LWLockRelease(WALWriteLock
);
1917 * Flush any previous asynchronously-committed transactions' commit records.
1919 * NOTE: it is unwise to assume that this provides any strong guarantees.
1920 * In particular, because of the inexact LSN bookkeeping used by clog.c,
1921 * we cannot assume that hint bits will be settable for these transactions.
1924 XLogAsyncCommitFlush(void)
1926 XLogRecPtr WriteRqstPtr
;
1928 /* use volatile pointer to prevent code rearrangement */
1929 volatile XLogCtlData
*xlogctl
= XLogCtl
;
1931 SpinLockAcquire(&xlogctl
->info_lck
);
1932 WriteRqstPtr
= xlogctl
->asyncCommitLSN
;
1933 SpinLockRelease(&xlogctl
->info_lck
);
1935 XLogFlush(WriteRqstPtr
);
1939 * Test whether XLOG data has been flushed up to (at least) the given position.
1941 * Returns true if a flush is still needed. (It may be that someone else
1942 * is already in process of flushing that far, however.)
1945 XLogNeedsFlush(XLogRecPtr record
)
1947 /* Quick exit if already known flushed */
1948 if (XLByteLE(record
, LogwrtResult
.Flush
))
1951 /* read LogwrtResult and update local state */
1953 /* use volatile pointer to prevent code rearrangement */
1954 volatile XLogCtlData
*xlogctl
= XLogCtl
;
1956 SpinLockAcquire(&xlogctl
->info_lck
);
1957 LogwrtResult
= xlogctl
->LogwrtResult
;
1958 SpinLockRelease(&xlogctl
->info_lck
);
1962 if (XLByteLE(record
, LogwrtResult
.Flush
))
1969 * Create a new XLOG file segment, or open a pre-existing one.
1971 * log, seg: identify segment to be created/opened.
1973 * *use_existent: if TRUE, OK to use a pre-existing file (else, any
1974 * pre-existing file will be deleted). On return, TRUE if a pre-existing
1977 * use_lock: if TRUE, acquire ControlFileLock while moving file into
1978 * place. This should be TRUE except during bootstrap log creation. The
1979 * caller must *not* hold the lock at call.
1981 * Returns FD of opened file.
1983 * Note: errors here are ERROR not PANIC because we might or might not be
1984 * inside a critical section (eg, during checkpoint there is no reason to
1985 * take down the system on failure). They will promote to PANIC if we are
1986 * in a critical section.
1989 XLogFileInit(uint32 log
, uint32 seg
,
1990 bool *use_existent
, bool use_lock
)
1992 char path
[MAXPGPATH
];
1993 char tmppath
[MAXPGPATH
];
1995 uint32 installed_log
;
1996 uint32 installed_seg
;
2001 XLogFilePath(path
, ThisTimeLineID
, log
, seg
);
2004 * Try to use existent file (checkpoint maker may have created it already)
2008 fd
= BasicOpenFile(path
, O_RDWR
| PG_BINARY
| get_sync_bit(sync_method
),
2012 if (errno
!= ENOENT
)
2014 (errcode_for_file_access(),
2015 errmsg("could not open file \"%s\" (log file %u, segment %u): %m",
2023 * Initialize an empty (all zeroes) segment. NOTE: it is possible that
2024 * another process is doing the same thing. If so, we will end up
2025 * pre-creating an extra log segment. That seems OK, and better than
2026 * holding the lock throughout this lengthy process.
2028 elog(DEBUG2
, "creating and filling new WAL file");
2030 snprintf(tmppath
, MAXPGPATH
, XLOGDIR
"/xlogtemp.%d", (int) getpid());
2034 /* do not use get_sync_bit() here --- want to fsync only at end of fill */
2035 fd
= BasicOpenFile(tmppath
, O_RDWR
| O_CREAT
| O_EXCL
| PG_BINARY
,
2039 (errcode_for_file_access(),
2040 errmsg("could not create file \"%s\": %m", tmppath
)));
2043 * Zero-fill the file. We have to do this the hard way to ensure that all
2044 * the file space has really been allocated --- on platforms that allow
2045 * "holes" in files, just seeking to the end doesn't allocate intermediate
2046 * space. This way, we know that we have all the space and (after the
2047 * fsync below) that all the indirect blocks are down on disk. Therefore,
2048 * fdatasync(2) or O_DSYNC will be sufficient to sync future writes to the
2051 * Note: palloc zbuffer, instead of just using a local char array, to
2052 * ensure it is reasonably well-aligned; this may save a few cycles
2053 * transferring data to the kernel.
2055 zbuffer
= (char *) palloc0(XLOG_BLCKSZ
);
2056 for (nbytes
= 0; nbytes
< XLogSegSize
; nbytes
+= XLOG_BLCKSZ
)
2059 if ((int) write(fd
, zbuffer
, XLOG_BLCKSZ
) != (int) XLOG_BLCKSZ
)
2061 int save_errno
= errno
;
2064 * If we fail to make the file, delete it to release disk space
2067 /* if write didn't set errno, assume problem is no disk space */
2068 errno
= save_errno
? save_errno
: ENOSPC
;
2071 (errcode_for_file_access(),
2072 errmsg("could not write to file \"%s\": %m", tmppath
)));
2077 if (pg_fsync(fd
) != 0)
2079 (errcode_for_file_access(),
2080 errmsg("could not fsync file \"%s\": %m", tmppath
)));
2084 (errcode_for_file_access(),
2085 errmsg("could not close file \"%s\": %m", tmppath
)));
2088 * Now move the segment into place with its final name.
2090 * If caller didn't want to use a pre-existing file, get rid of any
2091 * pre-existing file. Otherwise, cope with possibility that someone else
2092 * has created the file while we were filling ours: if so, use ours to
2093 * pre-create a future log segment.
2095 installed_log
= log
;
2096 installed_seg
= seg
;
2097 max_advance
= XLOGfileslop
;
2098 if (!InstallXLogFileSegment(&installed_log
, &installed_seg
, tmppath
,
2099 *use_existent
, &max_advance
,
2102 /* No need for any more future segments... */
2106 elog(DEBUG2
, "done creating and filling new WAL file");
2108 /* Set flag to tell caller there was no existent file */
2109 *use_existent
= false;
2111 /* Now open original target segment (might not be file I just made) */
2112 fd
= BasicOpenFile(path
, O_RDWR
| PG_BINARY
| get_sync_bit(sync_method
),
2116 (errcode_for_file_access(),
2117 errmsg("could not open file \"%s\" (log file %u, segment %u): %m",
2124 * Create a new XLOG file segment by copying a pre-existing one.
2126 * log, seg: identify segment to be created.
2128 * srcTLI, srclog, srcseg: identify segment to be copied (could be from
2129 * a different timeline)
2131 * Currently this is only used during recovery, and so there are no locking
2132 * considerations. But we should be just as tense as XLogFileInit to avoid
2133 * emplacing a bogus file.
2136 XLogFileCopy(uint32 log
, uint32 seg
,
2137 TimeLineID srcTLI
, uint32 srclog
, uint32 srcseg
)
2139 char path
[MAXPGPATH
];
2140 char tmppath
[MAXPGPATH
];
2141 char buffer
[XLOG_BLCKSZ
];
2147 * Open the source file
2149 XLogFilePath(path
, srcTLI
, srclog
, srcseg
);
2150 srcfd
= BasicOpenFile(path
, O_RDONLY
| PG_BINARY
, 0);
2153 (errcode_for_file_access(),
2154 errmsg("could not open file \"%s\": %m", path
)));
2157 * Copy into a temp file name.
2159 snprintf(tmppath
, MAXPGPATH
, XLOGDIR
"/xlogtemp.%d", (int) getpid());
2163 /* do not use get_sync_bit() here --- want to fsync only at end of fill */
2164 fd
= BasicOpenFile(tmppath
, O_RDWR
| O_CREAT
| O_EXCL
| PG_BINARY
,
2168 (errcode_for_file_access(),
2169 errmsg("could not create file \"%s\": %m", tmppath
)));
2172 * Do the data copying.
2174 for (nbytes
= 0; nbytes
< XLogSegSize
; nbytes
+= sizeof(buffer
))
2177 if ((int) read(srcfd
, buffer
, sizeof(buffer
)) != (int) sizeof(buffer
))
2181 (errcode_for_file_access(),
2182 errmsg("could not read file \"%s\": %m", path
)));
2185 (errmsg("not enough data in file \"%s\"", path
)));
2188 if ((int) write(fd
, buffer
, sizeof(buffer
)) != (int) sizeof(buffer
))
2190 int save_errno
= errno
;
2193 * If we fail to make the file, delete it to release disk space
2196 /* if write didn't set errno, assume problem is no disk space */
2197 errno
= save_errno
? save_errno
: ENOSPC
;
2200 (errcode_for_file_access(),
2201 errmsg("could not write to file \"%s\": %m", tmppath
)));
2205 if (pg_fsync(fd
) != 0)
2207 (errcode_for_file_access(),
2208 errmsg("could not fsync file \"%s\": %m", tmppath
)));
2212 (errcode_for_file_access(),
2213 errmsg("could not close file \"%s\": %m", tmppath
)));
2218 * Now move the segment into place with its final name.
2220 if (!InstallXLogFileSegment(&log
, &seg
, tmppath
, false, NULL
, false))
2221 elog(ERROR
, "InstallXLogFileSegment should not have failed");
2225 * Install a new XLOG segment file as a current or future log segment.
2227 * This is used both to install a newly-created segment (which has a temp
2228 * filename while it's being created) and to recycle an old segment.
2230 * *log, *seg: identify segment to install as (or first possible target).
2231 * When find_free is TRUE, these are modified on return to indicate the
2232 * actual installation location or last segment searched.
2234 * tmppath: initial name of file to install. It will be renamed into place.
2236 * find_free: if TRUE, install the new segment at the first empty log/seg
2237 * number at or after the passed numbers. If FALSE, install the new segment
2238 * exactly where specified, deleting any existing segment file there.
2240 * *max_advance: maximum number of log/seg slots to advance past the starting
2241 * point. Fail if no free slot is found in this range. On return, reduced
2242 * by the number of slots skipped over. (Irrelevant, and may be NULL,
2243 * when find_free is FALSE.)
2245 * use_lock: if TRUE, acquire ControlFileLock while moving file into
2246 * place. This should be TRUE except during bootstrap log creation. The
2247 * caller must *not* hold the lock at call.
2249 * Returns TRUE if file installed, FALSE if not installed because of
2250 * exceeding max_advance limit. On Windows, we also return FALSE if we
2251 * can't rename the file into place because someone's got it open.
2252 * (Any other kind of failure causes ereport().)
2255 InstallXLogFileSegment(uint32
*log
, uint32
*seg
, char *tmppath
,
2256 bool find_free
, int *max_advance
,
2259 char path
[MAXPGPATH
];
2260 struct stat stat_buf
;
2262 XLogFilePath(path
, ThisTimeLineID
, *log
, *seg
);
2265 * We want to be sure that only one process does this at a time.
2268 LWLockAcquire(ControlFileLock
, LW_EXCLUSIVE
);
2272 /* Force installation: get rid of any pre-existing segment file */
2277 /* Find a free slot to put it in */
2278 while (stat(path
, &stat_buf
) == 0)
2280 if (*max_advance
<= 0)
2282 /* Failed to find a free slot within specified range */
2284 LWLockRelease(ControlFileLock
);
2287 NextLogSeg(*log
, *seg
);
2289 XLogFilePath(path
, ThisTimeLineID
, *log
, *seg
);
2294 * Prefer link() to rename() here just to be really sure that we don't
2295 * overwrite an existing logfile. However, there shouldn't be one, so
2296 * rename() is an acceptable substitute except for the truly paranoid.
2298 #if HAVE_WORKING_LINK
2299 if (link(tmppath
, path
) < 0)
2301 (errcode_for_file_access(),
2302 errmsg("could not link file \"%s\" to \"%s\" (initialization of log file %u, segment %u): %m",
2303 tmppath
, path
, *log
, *seg
)));
2306 if (rename(tmppath
, path
) < 0)
2309 #if !defined(__CYGWIN__)
2310 if (GetLastError() == ERROR_ACCESS_DENIED
)
2312 if (errno
== EACCES
)
2316 LWLockRelease(ControlFileLock
);
2322 (errcode_for_file_access(),
2323 errmsg("could not rename file \"%s\" to \"%s\" (initialization of log file %u, segment %u): %m",
2324 tmppath
, path
, *log
, *seg
)));
2329 LWLockRelease(ControlFileLock
);
2335 * Open a pre-existing logfile segment for writing.
2338 XLogFileOpen(uint32 log
, uint32 seg
)
2340 char path
[MAXPGPATH
];
2343 XLogFilePath(path
, ThisTimeLineID
, log
, seg
);
2345 fd
= BasicOpenFile(path
, O_RDWR
| PG_BINARY
| get_sync_bit(sync_method
),
2349 (errcode_for_file_access(),
2350 errmsg("could not open file \"%s\" (log file %u, segment %u): %m",
2357 * Open a logfile segment for reading (during recovery).
2360 XLogFileRead(uint32 log
, uint32 seg
, int emode
)
2362 char path
[MAXPGPATH
];
2363 char xlogfname
[MAXFNAMELEN
];
2364 char activitymsg
[MAXFNAMELEN
+ 16];
2369 * Loop looking for a suitable timeline ID: we might need to read any of
2370 * the timelines listed in expectedTLIs.
2372 * We expect curFileTLI on entry to be the TLI of the preceding file in
2373 * sequence, or 0 if there was no predecessor. We do not allow curFileTLI
2374 * to go backwards; this prevents us from picking up the wrong file when a
2375 * parent timeline extends to higher segment numbers than the child we
2378 foreach(cell
, expectedTLIs
)
2380 TimeLineID tli
= (TimeLineID
) lfirst_int(cell
);
2382 if (tli
< curFileTLI
)
2383 break; /* don't bother looking at too-old TLIs */
2385 XLogFileName(xlogfname
, tli
, log
, seg
);
2387 if (InArchiveRecovery
)
2389 /* Report recovery progress in PS display */
2390 snprintf(activitymsg
, sizeof(activitymsg
), "waiting for %s",
2392 set_ps_display(activitymsg
, false);
2394 restoredFromArchive
= RestoreArchivedFile(path
, xlogfname
,
2399 XLogFilePath(path
, tli
, log
, seg
);
2401 fd
= BasicOpenFile(path
, O_RDONLY
| PG_BINARY
, 0);
2407 /* Report recovery progress in PS display */
2408 snprintf(activitymsg
, sizeof(activitymsg
), "recovering %s",
2410 set_ps_display(activitymsg
, false);
2414 if (errno
!= ENOENT
) /* unexpected failure? */
2416 (errcode_for_file_access(),
2417 errmsg("could not open file \"%s\" (log file %u, segment %u): %m",
2421 /* Couldn't find it. For simplicity, complain about front timeline */
2422 XLogFilePath(path
, recoveryTargetTLI
, log
, seg
);
2425 (errcode_for_file_access(),
2426 errmsg("could not open file \"%s\" (log file %u, segment %u): %m",
2432 * Close the current logfile segment for writing.
2437 Assert(openLogFile
>= 0);
2440 * WAL segment files will not be re-read in normal operation, so we advise
2441 * the OS to release any cached pages. But do not do so if WAL archiving
2442 * is active, because archiver process could use the cache to read the WAL
2443 * segment. Also, don't bother with it if we are using O_DIRECT, since
2444 * the kernel is presumably not caching in that case.
2446 #if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED)
2447 if (!XLogArchivingActive() &&
2448 (get_sync_bit(sync_method
) & PG_O_DIRECT
) == 0)
2449 (void) posix_fadvise(openLogFile
, 0, 0, POSIX_FADV_DONTNEED
);
2452 if (close(openLogFile
))
2454 (errcode_for_file_access(),
2455 errmsg("could not close log file %u, segment %u: %m",
2456 openLogId
, openLogSeg
)));
2461 * Attempt to retrieve the specified file from off-line archival storage.
2462 * If successful, fill "path" with its complete path (note that this will be
2463 * a temp file name that doesn't follow the normal naming convention), and
2466 * If not successful, fill "path" with the name of the normal on-line file
2467 * (which may or may not actually exist, but we'll try to use it), and return
2470 * For fixed-size files, the caller may pass the expected size as an
2471 * additional crosscheck on successful recovery. If the file size is not
2472 * known, set expectedSize = 0.
2475 RestoreArchivedFile(char *path
, const char *xlogfname
,
2476 const char *recovername
, off_t expectedSize
)
2478 char xlogpath
[MAXPGPATH
];
2479 char xlogRestoreCmd
[MAXPGPATH
];
2480 char lastRestartPointFname
[MAXPGPATH
];
2486 struct stat stat_buf
;
2491 * When doing archive recovery, we always prefer an archived log file even
2492 * if a file of the same name exists in XLOGDIR. The reason is that the
2493 * file in XLOGDIR could be an old, un-filled or partly-filled version
2494 * that was copied and restored as part of backing up $PGDATA.
2496 * We could try to optimize this slightly by checking the local copy
2497 * lastchange timestamp against the archived copy, but we have no API to
2498 * do this, nor can we guarantee that the lastchange timestamp was
2499 * preserved correctly when we copied to archive. Our aim is robustness,
2500 * so we elect not to do this.
2502 * If we cannot obtain the log file from the archive, however, we will try
2503 * to use the XLOGDIR file if it exists. This is so that we can make use
2504 * of log segments that weren't yet transferred to the archive.
2506 * Notice that we don't actually overwrite any files when we copy back
2507 * from archive because the recoveryRestoreCommand may inadvertently
2508 * restore inappropriate xlogs, or they may be corrupt, so we may wish to
2509 * fallback to the segments remaining in current XLOGDIR later. The
2510 * copy-from-archive filename is always the same, ensuring that we don't
2511 * run out of disk space on long recoveries.
2513 snprintf(xlogpath
, MAXPGPATH
, XLOGDIR
"/%s", recovername
);
2516 * Make sure there is no existing file named recovername.
2518 if (stat(xlogpath
, &stat_buf
) != 0)
2520 if (errno
!= ENOENT
)
2522 (errcode_for_file_access(),
2523 errmsg("could not stat file \"%s\": %m",
2528 if (unlink(xlogpath
) != 0)
2530 (errcode_for_file_access(),
2531 errmsg("could not remove file \"%s\": %m",
2536 * Calculate the archive file cutoff point for use during log shipping
2537 * replication. All files earlier than this point can be deleted
2538 * from the archive, though there is no requirement to do so.
2540 * We initialise this with the filename of an InvalidXLogRecPtr, which
2541 * will prevent the deletion of any WAL files from the archive
2542 * because of the alphabetic sorting property of WAL filenames.
2544 * Once we have successfully located the redo pointer of the checkpoint
2545 * from which we start recovery we never request a file prior to the redo
2546 * pointer of the last restartpoint. When redo begins we know that we
2547 * have successfully located it, so there is no need for additional
2548 * status flags to signify the point when we can begin deleting WAL files
2553 XLByteToSeg(ControlFile
->checkPointCopy
.redo
,
2554 restartLog
, restartSeg
);
2555 XLogFileName(lastRestartPointFname
,
2556 ControlFile
->checkPointCopy
.ThisTimeLineID
,
2557 restartLog
, restartSeg
);
2558 /* we shouldn't need anything earlier than last restart point */
2559 Assert(strcmp(lastRestartPointFname
, xlogfname
) <= 0);
2562 XLogFileName(lastRestartPointFname
, 0, 0, 0);
2565 * construct the command to be executed
2567 dp
= xlogRestoreCmd
;
2568 endp
= xlogRestoreCmd
+ MAXPGPATH
- 1;
2571 for (sp
= recoveryRestoreCommand
; *sp
; sp
++)
2578 /* %p: relative path of target file */
2580 StrNCpy(dp
, xlogpath
, endp
- dp
);
2581 make_native_path(dp
);
2585 /* %f: filename of desired file */
2587 StrNCpy(dp
, xlogfname
, endp
- dp
);
2591 /* %r: filename of last restartpoint */
2593 StrNCpy(dp
, lastRestartPointFname
, endp
- dp
);
2597 /* convert %% to a single % */
2603 /* otherwise treat the % as not special */
2618 (errmsg_internal("executing restore command \"%s\"",
2622 * Copy xlog from archival storage to XLOGDIR
2624 rc
= system(xlogRestoreCmd
);
2628 * command apparently succeeded, but let's make sure the file is
2629 * really there now and has the correct size.
2631 * XXX I made wrong-size a fatal error to ensure the DBA would notice
2632 * it, but is that too strong? We could try to plow ahead with a
2633 * local copy of the file ... but the problem is that there probably
2634 * isn't one, and we'd incorrectly conclude we've reached the end of
2635 * WAL and we're done recovering ...
2637 if (stat(xlogpath
, &stat_buf
) == 0)
2639 if (expectedSize
> 0 && stat_buf
.st_size
!= expectedSize
)
2641 (errmsg("archive file \"%s\" has wrong size: %lu instead of %lu",
2643 (unsigned long) stat_buf
.st_size
,
2644 (unsigned long) expectedSize
)));
2648 (errmsg("restored log file \"%s\" from archive",
2650 strcpy(path
, xlogpath
);
2657 if (errno
!= ENOENT
)
2659 (errcode_for_file_access(),
2660 errmsg("could not stat file \"%s\": %m",
2666 * Remember, we rollforward UNTIL the restore fails so failure here is
2667 * just part of the process... that makes it difficult to determine
2668 * whether the restore failed because there isn't an archive to restore,
2669 * or because the administrator has specified the restore program
2670 * incorrectly. We have to assume the former.
2672 * However, if the failure was due to any sort of signal, it's best to
2673 * punt and abort recovery. (If we "return false" here, upper levels will
2674 * assume that recovery is complete and start up the database!) It's
2675 * essential to abort on child SIGINT and SIGQUIT, because per spec
2676 * system() ignores SIGINT and SIGQUIT while waiting; if we see one of
2677 * those it's a good bet we should have gotten it too. Aborting on other
2678 * signals such as SIGTERM seems a good idea as well.
2680 * Per the Single Unix Spec, shells report exit status > 128 when a called
2681 * command died on a signal. Also, 126 and 127 are used to report
2682 * problems such as an unfindable command; treat those as fatal errors
2685 signaled
= WIFSIGNALED(rc
) || WEXITSTATUS(rc
) > 125;
2687 ereport(signaled
? FATAL
: DEBUG2
,
2688 (errmsg("could not restore file \"%s\" from archive: return code %d",
2692 * if an archived file is not available, there might still be a version of
2693 * this file in XLOGDIR, so return that as the filename to open.
2695 * In many recovery scenarios we expect this to fail also, but if so that
2696 * just means we've reached the end of WAL.
2698 snprintf(path
, MAXPGPATH
, XLOGDIR
"/%s", xlogfname
);
2703 * Preallocate log files beyond the specified log endpoint.
2705 * XXX this is currently extremely conservative, since it forces only one
2706 * future log segment to exist, and even that only if we are 75% done with
2707 * the current one. This is only appropriate for very low-WAL-volume systems.
2708 * High-volume systems will be OK once they've built up a sufficient set of
2709 * recycled log segments, but the startup transient is likely to include
2710 * a lot of segment creations by foreground processes, which is not so good.
2713 PreallocXlogFiles(XLogRecPtr endptr
)
2720 XLByteToPrevSeg(endptr
, _logId
, _logSeg
);
2721 if ((endptr
.xrecoff
- 1) % XLogSegSize
>=
2722 (uint32
) (0.75 * XLogSegSize
))
2724 NextLogSeg(_logId
, _logSeg
);
2725 use_existent
= true;
2726 lf
= XLogFileInit(_logId
, _logSeg
, &use_existent
, true);
2729 CheckpointStats
.ckpt_segs_added
++;
2734 * Recycle or remove all log files older or equal to passed log/seg#
2736 * endptr is current (or recent) end of xlog; this is used to determine
2737 * whether we want to recycle rather than delete no-longer-wanted log files.
2740 RemoveOldXlogFiles(uint32 log
, uint32 seg
, XLogRecPtr endptr
)
2746 struct dirent
*xlde
;
2747 char lastoff
[MAXFNAMELEN
];
2748 char path
[MAXPGPATH
];
2751 * Initialize info about where to try to recycle to. We allow recycling
2752 * segments up to XLOGfileslop segments beyond the current XLOG location.
2754 XLByteToPrevSeg(endptr
, endlogId
, endlogSeg
);
2755 max_advance
= XLOGfileslop
;
2757 xldir
= AllocateDir(XLOGDIR
);
2760 (errcode_for_file_access(),
2761 errmsg("could not open transaction log directory \"%s\": %m",
2764 XLogFileName(lastoff
, ThisTimeLineID
, log
, seg
);
2766 while ((xlde
= ReadDir(xldir
, XLOGDIR
)) != NULL
)
2769 * We ignore the timeline part of the XLOG segment identifiers in
2770 * deciding whether a segment is still needed. This ensures that we
2771 * won't prematurely remove a segment from a parent timeline. We could
2772 * probably be a little more proactive about removing segments of
2773 * non-parent timelines, but that would be a whole lot more
2776 * We use the alphanumeric sorting property of the filenames to decide
2777 * which ones are earlier than the lastoff segment.
2779 if (strlen(xlde
->d_name
) == 24 &&
2780 strspn(xlde
->d_name
, "0123456789ABCDEF") == 24 &&
2781 strcmp(xlde
->d_name
+ 8, lastoff
+ 8) <= 0)
2783 if (XLogArchiveCheckDone(xlde
->d_name
))
2785 snprintf(path
, MAXPGPATH
, XLOGDIR
"/%s", xlde
->d_name
);
2788 * Before deleting the file, see if it can be recycled as a
2789 * future log segment.
2791 if (InstallXLogFileSegment(&endlogId
, &endlogSeg
, path
,
2796 (errmsg("recycled transaction log file \"%s\"",
2798 CheckpointStats
.ckpt_segs_recycled
++;
2799 /* Needn't recheck that slot on future iterations */
2800 if (max_advance
> 0)
2802 NextLogSeg(endlogId
, endlogSeg
);
2808 /* No need for any more future segments... */
2810 (errmsg("removing transaction log file \"%s\"",
2813 CheckpointStats
.ckpt_segs_removed
++;
2816 XLogArchiveCleanup(xlde
->d_name
);
2825 * Verify whether pg_xlog and pg_xlog/archive_status exist.
2826 * If the latter does not exist, recreate it.
2828 * It is not the goal of this function to verify the contents of these
2829 * directories, but to help in cases where someone has performed a cluster
2830 * copy for PITR purposes but omitted pg_xlog from the copy.
2832 * We could also recreate pg_xlog if it doesn't exist, but a deliberate
2833 * policy decision was made not to. It is fairly common for pg_xlog to be
2834 * a symlink, and if that was the DBA's intent then automatically making a
2835 * plain directory would result in degraded performance with no notice.
2838 ValidateXLOGDirectoryStructure(void)
2840 char path
[MAXPGPATH
];
2841 struct stat stat_buf
;
2843 /* Check for pg_xlog; if it doesn't exist, error out */
2844 if (stat(XLOGDIR
, &stat_buf
) != 0 ||
2845 !S_ISDIR(stat_buf
.st_mode
))
2847 (errmsg("required WAL directory \"%s\" does not exist",
2850 /* Check for archive_status */
2851 snprintf(path
, MAXPGPATH
, XLOGDIR
"/archive_status");
2852 if (stat(path
, &stat_buf
) == 0)
2854 /* Check for weird cases where it exists but isn't a directory */
2855 if (!S_ISDIR(stat_buf
.st_mode
))
2857 (errmsg("required WAL directory \"%s\" does not exist",
2863 (errmsg("creating missing WAL directory \"%s\"", path
)));
2864 if (mkdir(path
, 0700) < 0)
2866 (errmsg("could not create missing directory \"%s\": %m",
2872 * Remove previous backup history files. This also retries creation of
2873 * .ready files for any backup history files for which XLogArchiveNotify
2877 CleanupBackupHistory(void)
2880 struct dirent
*xlde
;
2881 char path
[MAXPGPATH
];
2883 xldir
= AllocateDir(XLOGDIR
);
2886 (errcode_for_file_access(),
2887 errmsg("could not open transaction log directory \"%s\": %m",
2890 while ((xlde
= ReadDir(xldir
, XLOGDIR
)) != NULL
)
2892 if (strlen(xlde
->d_name
) > 24 &&
2893 strspn(xlde
->d_name
, "0123456789ABCDEF") == 24 &&
2894 strcmp(xlde
->d_name
+ strlen(xlde
->d_name
) - strlen(".backup"),
2897 if (XLogArchiveCheckDone(xlde
->d_name
))
2900 (errmsg("removing transaction log backup history file \"%s\"",
2902 snprintf(path
, MAXPGPATH
, XLOGDIR
"/%s", xlde
->d_name
);
2904 XLogArchiveCleanup(xlde
->d_name
);
2913 * Restore the backup blocks present in an XLOG record, if any.
2915 * We assume all of the record has been read into memory at *record.
2917 * Note: when a backup block is available in XLOG, we restore it
2918 * unconditionally, even if the page in the database appears newer.
2919 * This is to protect ourselves against database pages that were partially
2920 * or incorrectly written during a crash. We assume that the XLOG data
2921 * must be good because it has passed a CRC check, while the database
2922 * page might not be. This will force us to replay all subsequent
2923 * modifications of the page that appear in XLOG, rather than possibly
2924 * ignoring them as already applied, but that's not a huge drawback.
2926 * If 'cleanup' is true, a cleanup lock is used when restoring blocks.
2927 * Otherwise, a normal exclusive lock is used. At the moment, that's just
2928 * pro forma, because there can't be any regular backends in the system
2929 * during recovery. The 'cleanup' argument applies to all backup blocks
2930 * in the WAL record, that suffices for now.
2933 RestoreBkpBlocks(XLogRecPtr lsn
, XLogRecord
*record
, bool cleanup
)
2941 if (!(record
->xl_info
& XLR_BKP_BLOCK_MASK
))
2944 blk
= (char *) XLogRecGetData(record
) + record
->xl_len
;
2945 for (i
= 0; i
< XLR_MAX_BKP_BLOCKS
; i
++)
2947 if (!(record
->xl_info
& XLR_SET_BKP_BLOCK(i
)))
2950 memcpy(&bkpb
, blk
, sizeof(BkpBlock
));
2951 blk
+= sizeof(BkpBlock
);
2953 buffer
= XLogReadBufferExtended(bkpb
.node
, bkpb
.fork
, bkpb
.block
,
2955 Assert(BufferIsValid(buffer
));
2957 LockBufferForCleanup(buffer
);
2959 LockBuffer(buffer
, BUFFER_LOCK_EXCLUSIVE
);
2961 page
= (Page
) BufferGetPage(buffer
);
2963 if (bkpb
.hole_length
== 0)
2965 memcpy((char *) page
, blk
, BLCKSZ
);
2969 /* must zero-fill the hole */
2970 MemSet((char *) page
, 0, BLCKSZ
);
2971 memcpy((char *) page
, blk
, bkpb
.hole_offset
);
2972 memcpy((char *) page
+ (bkpb
.hole_offset
+ bkpb
.hole_length
),
2973 blk
+ bkpb
.hole_offset
,
2974 BLCKSZ
- (bkpb
.hole_offset
+ bkpb
.hole_length
));
2977 PageSetLSN(page
, lsn
);
2978 PageSetTLI(page
, ThisTimeLineID
);
2979 MarkBufferDirty(buffer
);
2980 UnlockReleaseBuffer(buffer
);
2982 blk
+= BLCKSZ
- bkpb
.hole_length
;
2987 * CRC-check an XLOG record. We do not believe the contents of an XLOG
2988 * record (other than to the minimal extent of computing the amount of
2989 * data to read in) until we've checked the CRCs.
2991 * We assume all of the record has been read into memory at *record.
2994 RecordIsValid(XLogRecord
*record
, XLogRecPtr recptr
, int emode
)
2998 uint32 len
= record
->xl_len
;
3002 /* First the rmgr data */
3004 COMP_CRC32(crc
, XLogRecGetData(record
), len
);
3006 /* Add in the backup blocks, if any */
3007 blk
= (char *) XLogRecGetData(record
) + len
;
3008 for (i
= 0; i
< XLR_MAX_BKP_BLOCKS
; i
++)
3012 if (!(record
->xl_info
& XLR_SET_BKP_BLOCK(i
)))
3015 memcpy(&bkpb
, blk
, sizeof(BkpBlock
));
3016 if (bkpb
.hole_offset
+ bkpb
.hole_length
> BLCKSZ
)
3019 (errmsg("incorrect hole size in record at %X/%X",
3020 recptr
.xlogid
, recptr
.xrecoff
)));
3023 blen
= sizeof(BkpBlock
) + BLCKSZ
- bkpb
.hole_length
;
3024 COMP_CRC32(crc
, blk
, blen
);
3028 /* Check that xl_tot_len agrees with our calculation */
3029 if (blk
!= (char *) record
+ record
->xl_tot_len
)
3032 (errmsg("incorrect total length in record at %X/%X",
3033 recptr
.xlogid
, recptr
.xrecoff
)));
3037 /* Finally include the record header */
3038 COMP_CRC32(crc
, (char *) record
+ sizeof(pg_crc32
),
3039 SizeOfXLogRecord
- sizeof(pg_crc32
));
3042 if (!EQ_CRC32(record
->xl_crc
, crc
))
3045 (errmsg("incorrect resource manager data checksum in record at %X/%X",
3046 recptr
.xlogid
, recptr
.xrecoff
)));
3054 * Attempt to read an XLOG record.
3056 * If RecPtr is not NULL, try to read a record at that position. Otherwise
3057 * try to read a record just after the last one previously read.
3059 * If no valid record is available, returns NULL, or fails if emode is PANIC.
3060 * (emode must be either PANIC or LOG.)
3062 * The record is copied into readRecordBuf, so that on successful return,
3063 * the returned record pointer always points there.
3066 ReadRecord(XLogRecPtr
*RecPtr
, int emode
)
3070 XLogRecPtr tmpRecPtr
= EndRecPtr
;
3071 bool randAccess
= false;
3074 uint32 targetPageOff
;
3075 uint32 targetRecOff
;
3076 uint32 pageHeaderSize
;
3078 if (readBuf
== NULL
)
3081 * First time through, permanently allocate readBuf. We do it this
3082 * way, rather than just making a static array, for two reasons: (1)
3083 * no need to waste the storage in most instantiations of the backend;
3084 * (2) a static char array isn't guaranteed to have any particular
3085 * alignment, whereas malloc() will provide MAXALIGN'd storage.
3087 readBuf
= (char *) malloc(XLOG_BLCKSZ
);
3088 Assert(readBuf
!= NULL
);
3093 RecPtr
= &tmpRecPtr
;
3094 /* fast case if next record is on same page */
3095 if (nextRecord
!= NULL
)
3097 record
= nextRecord
;
3100 /* align old recptr to next page */
3101 if (tmpRecPtr
.xrecoff
% XLOG_BLCKSZ
!= 0)
3102 tmpRecPtr
.xrecoff
+= (XLOG_BLCKSZ
- tmpRecPtr
.xrecoff
% XLOG_BLCKSZ
);
3103 if (tmpRecPtr
.xrecoff
>= XLogFileSize
)
3105 (tmpRecPtr
.xlogid
)++;
3106 tmpRecPtr
.xrecoff
= 0;
3108 /* We will account for page header size below */
3112 if (!XRecOffIsValid(RecPtr
->xrecoff
))
3114 (errmsg("invalid record offset at %X/%X",
3115 RecPtr
->xlogid
, RecPtr
->xrecoff
)));
3118 * Since we are going to a random position in WAL, forget any prior
3119 * state about what timeline we were in, and allow it to be any
3120 * timeline in expectedTLIs. We also set a flag to allow curFileTLI
3121 * to go backwards (but we can't reset that variable right here, since
3122 * we might not change files at all).
3124 lastPageTLI
= 0; /* see comment in ValidXLOGHeader */
3125 randAccess
= true; /* allow curFileTLI to go backwards too */
3128 if (readFile
>= 0 && !XLByteInSeg(*RecPtr
, readId
, readSeg
))
3133 XLByteToSeg(*RecPtr
, readId
, readSeg
);
3136 /* Now it's okay to reset curFileTLI if random fetch */
3140 readFile
= XLogFileRead(readId
, readSeg
, emode
);
3142 goto next_record_is_invalid
;
3145 * Whenever switching to a new WAL segment, we read the first page of
3146 * the file and validate its header, even if that's not where the
3147 * target record is. This is so that we can check the additional
3148 * identification info that is present in the first page's "long"
3152 if (read(readFile
, readBuf
, XLOG_BLCKSZ
) != XLOG_BLCKSZ
)
3155 (errcode_for_file_access(),
3156 errmsg("could not read from log file %u, segment %u, offset %u: %m",
3157 readId
, readSeg
, readOff
)));
3158 goto next_record_is_invalid
;
3160 if (!ValidXLOGHeader((XLogPageHeader
) readBuf
, emode
))
3161 goto next_record_is_invalid
;
3164 targetPageOff
= ((RecPtr
->xrecoff
% XLogSegSize
) / XLOG_BLCKSZ
) * XLOG_BLCKSZ
;
3165 if (readOff
!= targetPageOff
)
3167 readOff
= targetPageOff
;
3168 if (lseek(readFile
, (off_t
) readOff
, SEEK_SET
) < 0)
3171 (errcode_for_file_access(),
3172 errmsg("could not seek in log file %u, segment %u to offset %u: %m",
3173 readId
, readSeg
, readOff
)));
3174 goto next_record_is_invalid
;
3176 if (read(readFile
, readBuf
, XLOG_BLCKSZ
) != XLOG_BLCKSZ
)
3179 (errcode_for_file_access(),
3180 errmsg("could not read from log file %u, segment %u, offset %u: %m",
3181 readId
, readSeg
, readOff
)));
3182 goto next_record_is_invalid
;
3184 if (!ValidXLOGHeader((XLogPageHeader
) readBuf
, emode
))
3185 goto next_record_is_invalid
;
3187 pageHeaderSize
= XLogPageHeaderSize((XLogPageHeader
) readBuf
);
3188 targetRecOff
= RecPtr
->xrecoff
% XLOG_BLCKSZ
;
3189 if (targetRecOff
== 0)
3192 * Can only get here in the continuing-from-prev-page case, because
3193 * XRecOffIsValid eliminated the zero-page-offset case otherwise. Need
3194 * to skip over the new page's header.
3196 tmpRecPtr
.xrecoff
+= pageHeaderSize
;
3197 targetRecOff
= pageHeaderSize
;
3199 else if (targetRecOff
< pageHeaderSize
)
3202 (errmsg("invalid record offset at %X/%X",
3203 RecPtr
->xlogid
, RecPtr
->xrecoff
)));
3204 goto next_record_is_invalid
;
3206 if ((((XLogPageHeader
) readBuf
)->xlp_info
& XLP_FIRST_IS_CONTRECORD
) &&
3207 targetRecOff
== pageHeaderSize
)
3210 (errmsg("contrecord is requested by %X/%X",
3211 RecPtr
->xlogid
, RecPtr
->xrecoff
)));
3212 goto next_record_is_invalid
;
3214 record
= (XLogRecord
*) ((char *) readBuf
+ RecPtr
->xrecoff
% XLOG_BLCKSZ
);
3219 * xl_len == 0 is bad data for everything except XLOG SWITCH, where it is
3222 if (record
->xl_rmid
== RM_XLOG_ID
&& record
->xl_info
== XLOG_SWITCH
)
3224 if (record
->xl_len
!= 0)
3227 (errmsg("invalid xlog switch record at %X/%X",
3228 RecPtr
->xlogid
, RecPtr
->xrecoff
)));
3229 goto next_record_is_invalid
;
3232 else if (record
->xl_len
== 0)
3235 (errmsg("record with zero length at %X/%X",
3236 RecPtr
->xlogid
, RecPtr
->xrecoff
)));
3237 goto next_record_is_invalid
;
3239 if (record
->xl_tot_len
< SizeOfXLogRecord
+ record
->xl_len
||
3240 record
->xl_tot_len
> SizeOfXLogRecord
+ record
->xl_len
+
3241 XLR_MAX_BKP_BLOCKS
* (sizeof(BkpBlock
) + BLCKSZ
))
3244 (errmsg("invalid record length at %X/%X",
3245 RecPtr
->xlogid
, RecPtr
->xrecoff
)));
3246 goto next_record_is_invalid
;
3248 if (record
->xl_rmid
> RM_MAX_ID
)
3251 (errmsg("invalid resource manager ID %u at %X/%X",
3252 record
->xl_rmid
, RecPtr
->xlogid
, RecPtr
->xrecoff
)));
3253 goto next_record_is_invalid
;
3258 * We can't exactly verify the prev-link, but surely it should be less
3259 * than the record's own address.
3261 if (!XLByteLT(record
->xl_prev
, *RecPtr
))
3264 (errmsg("record with incorrect prev-link %X/%X at %X/%X",
3265 record
->xl_prev
.xlogid
, record
->xl_prev
.xrecoff
,
3266 RecPtr
->xlogid
, RecPtr
->xrecoff
)));
3267 goto next_record_is_invalid
;
3273 * Record's prev-link should exactly match our previous location. This
3274 * check guards against torn WAL pages where a stale but valid-looking
3275 * WAL record starts on a sector boundary.
3277 if (!XLByteEQ(record
->xl_prev
, ReadRecPtr
))
3280 (errmsg("record with incorrect prev-link %X/%X at %X/%X",
3281 record
->xl_prev
.xlogid
, record
->xl_prev
.xrecoff
,
3282 RecPtr
->xlogid
, RecPtr
->xrecoff
)));
3283 goto next_record_is_invalid
;
3288 * Allocate or enlarge readRecordBuf as needed. To avoid useless small
3289 * increases, round its size to a multiple of XLOG_BLCKSZ, and make sure
3290 * it's at least 4*Max(BLCKSZ, XLOG_BLCKSZ) to start with. (That is
3291 * enough for all "normal" records, but very large commit or abort records
3292 * might need more space.)
3294 total_len
= record
->xl_tot_len
;
3295 if (total_len
> readRecordBufSize
)
3297 uint32 newSize
= total_len
;
3299 newSize
+= XLOG_BLCKSZ
- (newSize
% XLOG_BLCKSZ
);
3300 newSize
= Max(newSize
, 4 * Max(BLCKSZ
, XLOG_BLCKSZ
));
3302 free(readRecordBuf
);
3303 readRecordBuf
= (char *) malloc(newSize
);
3306 readRecordBufSize
= 0;
3307 /* We treat this as a "bogus data" condition */
3309 (errmsg("record length %u at %X/%X too long",
3310 total_len
, RecPtr
->xlogid
, RecPtr
->xrecoff
)));
3311 goto next_record_is_invalid
;
3313 readRecordBufSize
= newSize
;
3316 buffer
= readRecordBuf
;
3318 len
= XLOG_BLCKSZ
- RecPtr
->xrecoff
% XLOG_BLCKSZ
;
3319 if (total_len
> len
)
3321 /* Need to reassemble record */
3322 XLogContRecord
*contrecord
;
3323 uint32 gotlen
= len
;
3325 memcpy(buffer
, record
, len
);
3326 record
= (XLogRecord
*) buffer
;
3330 readOff
+= XLOG_BLCKSZ
;
3331 if (readOff
>= XLogSegSize
)
3335 NextLogSeg(readId
, readSeg
);
3336 readFile
= XLogFileRead(readId
, readSeg
, emode
);
3338 goto next_record_is_invalid
;
3341 if (read(readFile
, readBuf
, XLOG_BLCKSZ
) != XLOG_BLCKSZ
)
3344 (errcode_for_file_access(),
3345 errmsg("could not read from log file %u, segment %u, offset %u: %m",
3346 readId
, readSeg
, readOff
)));
3347 goto next_record_is_invalid
;
3349 if (!ValidXLOGHeader((XLogPageHeader
) readBuf
, emode
))
3350 goto next_record_is_invalid
;
3351 if (!(((XLogPageHeader
) readBuf
)->xlp_info
& XLP_FIRST_IS_CONTRECORD
))
3354 (errmsg("there is no contrecord flag in log file %u, segment %u, offset %u",
3355 readId
, readSeg
, readOff
)));
3356 goto next_record_is_invalid
;
3358 pageHeaderSize
= XLogPageHeaderSize((XLogPageHeader
) readBuf
);
3359 contrecord
= (XLogContRecord
*) ((char *) readBuf
+ pageHeaderSize
);
3360 if (contrecord
->xl_rem_len
== 0 ||
3361 total_len
!= (contrecord
->xl_rem_len
+ gotlen
))
3364 (errmsg("invalid contrecord length %u in log file %u, segment %u, offset %u",
3365 contrecord
->xl_rem_len
,
3366 readId
, readSeg
, readOff
)));
3367 goto next_record_is_invalid
;
3369 len
= XLOG_BLCKSZ
- pageHeaderSize
- SizeOfXLogContRecord
;
3370 if (contrecord
->xl_rem_len
> len
)
3372 memcpy(buffer
, (char *) contrecord
+ SizeOfXLogContRecord
, len
);
3377 memcpy(buffer
, (char *) contrecord
+ SizeOfXLogContRecord
,
3378 contrecord
->xl_rem_len
);
3381 if (!RecordIsValid(record
, *RecPtr
, emode
))
3382 goto next_record_is_invalid
;
3383 pageHeaderSize
= XLogPageHeaderSize((XLogPageHeader
) readBuf
);
3384 if (XLOG_BLCKSZ
- SizeOfXLogRecord
>= pageHeaderSize
+
3385 MAXALIGN(SizeOfXLogContRecord
+ contrecord
->xl_rem_len
))
3387 nextRecord
= (XLogRecord
*) ((char *) contrecord
+
3388 MAXALIGN(SizeOfXLogContRecord
+ contrecord
->xl_rem_len
));
3390 EndRecPtr
.xlogid
= readId
;
3391 EndRecPtr
.xrecoff
= readSeg
* XLogSegSize
+ readOff
+
3393 MAXALIGN(SizeOfXLogContRecord
+ contrecord
->xl_rem_len
);
3394 ReadRecPtr
= *RecPtr
;
3395 /* needn't worry about XLOG SWITCH, it can't cross page boundaries */
3399 /* Record does not cross a page boundary */
3400 if (!RecordIsValid(record
, *RecPtr
, emode
))
3401 goto next_record_is_invalid
;
3402 if (XLOG_BLCKSZ
- SizeOfXLogRecord
>= RecPtr
->xrecoff
% XLOG_BLCKSZ
+
3403 MAXALIGN(total_len
))
3404 nextRecord
= (XLogRecord
*) ((char *) record
+ MAXALIGN(total_len
));
3405 EndRecPtr
.xlogid
= RecPtr
->xlogid
;
3406 EndRecPtr
.xrecoff
= RecPtr
->xrecoff
+ MAXALIGN(total_len
);
3407 ReadRecPtr
= *RecPtr
;
3408 memcpy(buffer
, record
, total_len
);
3411 * Special processing if it's an XLOG SWITCH record
3413 if (record
->xl_rmid
== RM_XLOG_ID
&& record
->xl_info
== XLOG_SWITCH
)
3415 /* Pretend it extends to end of segment */
3416 EndRecPtr
.xrecoff
+= XLogSegSize
- 1;
3417 EndRecPtr
.xrecoff
-= EndRecPtr
.xrecoff
% XLogSegSize
;
3418 nextRecord
= NULL
; /* definitely not on same page */
3421 * Pretend that readBuf contains the last page of the segment. This is
3422 * just to avoid Assert failure in StartupXLOG if XLOG ends with this
3425 readOff
= XLogSegSize
- XLOG_BLCKSZ
;
3427 return (XLogRecord
*) buffer
;
3429 next_record_is_invalid
:;
3440 * Check whether the xlog header of a page just read in looks valid.
3442 * This is just a convenience subroutine to avoid duplicated code in
3443 * ReadRecord. It's not intended for use from anywhere else.
3446 ValidXLOGHeader(XLogPageHeader hdr
, int emode
)
3450 if (hdr
->xlp_magic
!= XLOG_PAGE_MAGIC
)
3453 (errmsg("invalid magic number %04X in log file %u, segment %u, offset %u",
3454 hdr
->xlp_magic
, readId
, readSeg
, readOff
)));
3457 if ((hdr
->xlp_info
& ~XLP_ALL_FLAGS
) != 0)
3460 (errmsg("invalid info bits %04X in log file %u, segment %u, offset %u",
3461 hdr
->xlp_info
, readId
, readSeg
, readOff
)));
3464 if (hdr
->xlp_info
& XLP_LONG_HEADER
)
3466 XLogLongPageHeader longhdr
= (XLogLongPageHeader
) hdr
;
3468 if (longhdr
->xlp_sysid
!= ControlFile
->system_identifier
)
3470 char fhdrident_str
[32];
3471 char sysident_str
[32];
3474 * Format sysids separately to keep platform-dependent format code
3475 * out of the translatable message string.
3477 snprintf(fhdrident_str
, sizeof(fhdrident_str
), UINT64_FORMAT
,
3478 longhdr
->xlp_sysid
);
3479 snprintf(sysident_str
, sizeof(sysident_str
), UINT64_FORMAT
,
3480 ControlFile
->system_identifier
);
3482 (errmsg("WAL file is from different system"),
3483 errdetail("WAL file SYSID is %s, pg_control SYSID is %s",
3484 fhdrident_str
, sysident_str
)));
3487 if (longhdr
->xlp_seg_size
!= XLogSegSize
)
3490 (errmsg("WAL file is from different system"),
3491 errdetail("Incorrect XLOG_SEG_SIZE in page header.")));
3494 if (longhdr
->xlp_xlog_blcksz
!= XLOG_BLCKSZ
)
3497 (errmsg("WAL file is from different system"),
3498 errdetail("Incorrect XLOG_BLCKSZ in page header.")));
3502 else if (readOff
== 0)
3504 /* hmm, first page of file doesn't have a long header? */
3506 (errmsg("invalid info bits %04X in log file %u, segment %u, offset %u",
3507 hdr
->xlp_info
, readId
, readSeg
, readOff
)));
3511 recaddr
.xlogid
= readId
;
3512 recaddr
.xrecoff
= readSeg
* XLogSegSize
+ readOff
;
3513 if (!XLByteEQ(hdr
->xlp_pageaddr
, recaddr
))
3516 (errmsg("unexpected pageaddr %X/%X in log file %u, segment %u, offset %u",
3517 hdr
->xlp_pageaddr
.xlogid
, hdr
->xlp_pageaddr
.xrecoff
,
3518 readId
, readSeg
, readOff
)));
3523 * Check page TLI is one of the expected values.
3525 if (!list_member_int(expectedTLIs
, (int) hdr
->xlp_tli
))
3528 (errmsg("unexpected timeline ID %u in log file %u, segment %u, offset %u",
3530 readId
, readSeg
, readOff
)));
3535 * Since child timelines are always assigned a TLI greater than their
3536 * immediate parent's TLI, we should never see TLI go backwards across
3537 * successive pages of a consistent WAL sequence.
3539 * Of course this check should only be applied when advancing sequentially
3540 * across pages; therefore ReadRecord resets lastPageTLI to zero when
3541 * going to a random page.
3543 if (hdr
->xlp_tli
< lastPageTLI
)
3546 (errmsg("out-of-sequence timeline ID %u (after %u) in log file %u, segment %u, offset %u",
3547 hdr
->xlp_tli
, lastPageTLI
,
3548 readId
, readSeg
, readOff
)));
3551 lastPageTLI
= hdr
->xlp_tli
;
3556 * Try to read a timeline's history file.
3558 * If successful, return the list of component TLIs (the given TLI followed by
3559 * its ancestor TLIs). If we can't find the history file, assume that the
3560 * timeline has no parents, and return a list of just the specified timeline
3564 readTimeLineHistory(TimeLineID targetTLI
)
3567 char path
[MAXPGPATH
];
3568 char histfname
[MAXFNAMELEN
];
3569 char fline
[MAXPGPATH
];
3572 if (InArchiveRecovery
)
3574 TLHistoryFileName(histfname
, targetTLI
);
3575 RestoreArchivedFile(path
, histfname
, "RECOVERYHISTORY", 0);
3578 TLHistoryFilePath(path
, targetTLI
);
3580 fd
= AllocateFile(path
, "r");
3583 if (errno
!= ENOENT
)
3585 (errcode_for_file_access(),
3586 errmsg("could not open file \"%s\": %m", path
)));
3587 /* Not there, so assume no parents */
3588 return list_make1_int((int) targetTLI
);
3596 while (fgets(fline
, sizeof(fline
), fd
) != NULL
)
3598 /* skip leading whitespace and check for # comment */
3603 for (ptr
= fline
; *ptr
; ptr
++)
3605 if (!isspace((unsigned char) *ptr
))
3608 if (*ptr
== '\0' || *ptr
== '#')
3611 /* expect a numeric timeline ID as first field of line */
3612 tli
= (TimeLineID
) strtoul(ptr
, &endptr
, 0);
3615 (errmsg("syntax error in history file: %s", fline
),
3616 errhint("Expected a numeric timeline ID.")));
3619 tli
<= (TimeLineID
) linitial_int(result
))
3621 (errmsg("invalid data in history file: %s", fline
),
3622 errhint("Timeline IDs must be in increasing sequence.")));
3624 /* Build list with newest item first */
3625 result
= lcons_int((int) tli
, result
);
3627 /* we ignore the remainder of each line */
3633 targetTLI
<= (TimeLineID
) linitial_int(result
))
3635 (errmsg("invalid data in history file \"%s\"", path
),
3636 errhint("Timeline IDs must be less than child timeline's ID.")));
3638 result
= lcons_int((int) targetTLI
, result
);
3641 (errmsg_internal("history of timeline %u is %s",
3642 targetTLI
, nodeToString(result
))));
3648 * Probe whether a timeline history file exists for the given timeline ID
3651 existsTimeLineHistory(TimeLineID probeTLI
)
3653 char path
[MAXPGPATH
];
3654 char histfname
[MAXFNAMELEN
];
3657 if (InArchiveRecovery
)
3659 TLHistoryFileName(histfname
, probeTLI
);
3660 RestoreArchivedFile(path
, histfname
, "RECOVERYHISTORY", 0);
3663 TLHistoryFilePath(path
, probeTLI
);
3665 fd
= AllocateFile(path
, "r");
3673 if (errno
!= ENOENT
)
3675 (errcode_for_file_access(),
3676 errmsg("could not open file \"%s\": %m", path
)));
3682 * Find the newest existing timeline, assuming that startTLI exists.
3684 * Note: while this is somewhat heuristic, it does positively guarantee
3685 * that (result + 1) is not a known timeline, and therefore it should
3686 * be safe to assign that ID to a new timeline.
3689 findNewestTimeLine(TimeLineID startTLI
)
3691 TimeLineID newestTLI
;
3692 TimeLineID probeTLI
;
3695 * The algorithm is just to probe for the existence of timeline history
3696 * files. XXX is it useful to allow gaps in the sequence?
3698 newestTLI
= startTLI
;
3700 for (probeTLI
= startTLI
+ 1;; probeTLI
++)
3702 if (existsTimeLineHistory(probeTLI
))
3704 newestTLI
= probeTLI
; /* probeTLI exists */
3708 /* doesn't exist, assume we're done */
3717 * Create a new timeline history file.
3719 * newTLI: ID of the new timeline
3720 * parentTLI: ID of its immediate parent
3721 * endTLI et al: ID of the last used WAL file, for annotation purposes
3723 * Currently this is only used during recovery, and so there are no locking
3724 * considerations. But we should be just as tense as XLogFileInit to avoid
3725 * emplacing a bogus file.
3728 writeTimeLineHistory(TimeLineID newTLI
, TimeLineID parentTLI
,
3729 TimeLineID endTLI
, uint32 endLogId
, uint32 endLogSeg
)
3731 char path
[MAXPGPATH
];
3732 char tmppath
[MAXPGPATH
];
3733 char histfname
[MAXFNAMELEN
];
3734 char xlogfname
[MAXFNAMELEN
];
3735 char buffer
[BLCKSZ
];
3740 Assert(newTLI
> parentTLI
); /* else bad selection of newTLI */
3743 * Write into a temp file name.
3745 snprintf(tmppath
, MAXPGPATH
, XLOGDIR
"/xlogtemp.%d", (int) getpid());
3749 /* do not use get_sync_bit() here --- want to fsync only at end of fill */
3750 fd
= BasicOpenFile(tmppath
, O_RDWR
| O_CREAT
| O_EXCL
,
3754 (errcode_for_file_access(),
3755 errmsg("could not create file \"%s\": %m", tmppath
)));
3758 * If a history file exists for the parent, copy it verbatim
3760 if (InArchiveRecovery
)
3762 TLHistoryFileName(histfname
, parentTLI
);
3763 RestoreArchivedFile(path
, histfname
, "RECOVERYHISTORY", 0);
3766 TLHistoryFilePath(path
, parentTLI
);
3768 srcfd
= BasicOpenFile(path
, O_RDONLY
, 0);
3771 if (errno
!= ENOENT
)
3773 (errcode_for_file_access(),
3774 errmsg("could not open file \"%s\": %m", path
)));
3775 /* Not there, so assume parent has no parents */
3782 nbytes
= (int) read(srcfd
, buffer
, sizeof(buffer
));
3783 if (nbytes
< 0 || errno
!= 0)
3785 (errcode_for_file_access(),
3786 errmsg("could not read file \"%s\": %m", path
)));
3790 if ((int) write(fd
, buffer
, nbytes
) != nbytes
)
3792 int save_errno
= errno
;
3795 * If we fail to make the file, delete it to release disk
3801 * if write didn't set errno, assume problem is no disk space
3803 errno
= save_errno
? save_errno
: ENOSPC
;
3806 (errcode_for_file_access(),
3807 errmsg("could not write to file \"%s\": %m", tmppath
)));
3814 * Append one line with the details of this timeline split.
3816 * If we did have a parent file, insert an extra newline just in case the
3817 * parent file failed to end with one.
3819 XLogFileName(xlogfname
, endTLI
, endLogId
, endLogSeg
);
3821 snprintf(buffer
, sizeof(buffer
),
3822 "%s%u\t%s\t%s transaction %u at %s\n",
3823 (srcfd
< 0) ? "" : "\n",
3826 recoveryStopAfter
? "after" : "before",
3828 timestamptz_to_str(recoveryStopTime
));
3830 nbytes
= strlen(buffer
);
3832 if ((int) write(fd
, buffer
, nbytes
) != nbytes
)
3834 int save_errno
= errno
;
3837 * If we fail to make the file, delete it to release disk space
3840 /* if write didn't set errno, assume problem is no disk space */
3841 errno
= save_errno
? save_errno
: ENOSPC
;
3844 (errcode_for_file_access(),
3845 errmsg("could not write to file \"%s\": %m", tmppath
)));
3848 if (pg_fsync(fd
) != 0)
3850 (errcode_for_file_access(),
3851 errmsg("could not fsync file \"%s\": %m", tmppath
)));
3855 (errcode_for_file_access(),
3856 errmsg("could not close file \"%s\": %m", tmppath
)));
3860 * Now move the completed history file into place with its final name.
3862 TLHistoryFilePath(path
, newTLI
);
3865 * Prefer link() to rename() here just to be really sure that we don't
3866 * overwrite an existing logfile. However, there shouldn't be one, so
3867 * rename() is an acceptable substitute except for the truly paranoid.
3869 #if HAVE_WORKING_LINK
3870 if (link(tmppath
, path
) < 0)
3872 (errcode_for_file_access(),
3873 errmsg("could not link file \"%s\" to \"%s\": %m",
3877 if (rename(tmppath
, path
) < 0)
3879 (errcode_for_file_access(),
3880 errmsg("could not rename file \"%s\" to \"%s\": %m",
3884 /* The history file can be archived immediately. */
3885 TLHistoryFileName(histfname
, newTLI
);
3886 XLogArchiveNotify(histfname
);
3890 * I/O routines for pg_control
3892 * *ControlFile is a buffer in shared memory that holds an image of the
3893 * contents of pg_control. WriteControlFile() initializes pg_control
3894 * given a preloaded buffer, ReadControlFile() loads the buffer from
3895 * the pg_control file (during postmaster or standalone-backend startup),
3896 * and UpdateControlFile() rewrites pg_control after we modify xlog state.
3898 * For simplicity, WriteControlFile() initializes the fields of pg_control
3899 * that are related to checking backend/database compatibility, and
3900 * ReadControlFile() verifies they are correct. We could split out the
3901 * I/O and compatibility-check functions, but there seems no need currently.
3904 WriteControlFile(void)
3907 char buffer
[PG_CONTROL_SIZE
]; /* need not be aligned */
3910 * Initialize version and compatibility-check fields
3912 ControlFile
->pg_control_version
= PG_CONTROL_VERSION
;
3913 ControlFile
->catalog_version_no
= CATALOG_VERSION_NO
;
3915 ControlFile
->maxAlign
= MAXIMUM_ALIGNOF
;
3916 ControlFile
->floatFormat
= FLOATFORMAT_VALUE
;
3918 ControlFile
->blcksz
= BLCKSZ
;
3919 ControlFile
->relseg_size
= RELSEG_SIZE
;
3920 ControlFile
->xlog_blcksz
= XLOG_BLCKSZ
;
3921 ControlFile
->xlog_seg_size
= XLOG_SEG_SIZE
;
3923 ControlFile
->nameDataLen
= NAMEDATALEN
;
3924 ControlFile
->indexMaxKeys
= INDEX_MAX_KEYS
;
3926 ControlFile
->toast_max_chunk_size
= TOAST_MAX_CHUNK_SIZE
;
3928 #ifdef HAVE_INT64_TIMESTAMP
3929 ControlFile
->enableIntTimes
= true;
3931 ControlFile
->enableIntTimes
= false;
3933 ControlFile
->float4ByVal
= FLOAT4PASSBYVAL
;
3934 ControlFile
->float8ByVal
= FLOAT8PASSBYVAL
;
3936 /* Contents are protected with a CRC */
3937 INIT_CRC32(ControlFile
->crc
);
3938 COMP_CRC32(ControlFile
->crc
,
3939 (char *) ControlFile
,
3940 offsetof(ControlFileData
, crc
));
3941 FIN_CRC32(ControlFile
->crc
);
3944 * We write out PG_CONTROL_SIZE bytes into pg_control, zero-padding the
3945 * excess over sizeof(ControlFileData). This reduces the odds of
3946 * premature-EOF errors when reading pg_control. We'll still fail when we
3947 * check the contents of the file, but hopefully with a more specific
3948 * error than "couldn't read pg_control".
3950 if (sizeof(ControlFileData
) > PG_CONTROL_SIZE
)
3951 elog(PANIC
, "sizeof(ControlFileData) is larger than PG_CONTROL_SIZE; fix either one");
3953 memset(buffer
, 0, PG_CONTROL_SIZE
);
3954 memcpy(buffer
, ControlFile
, sizeof(ControlFileData
));
3956 fd
= BasicOpenFile(XLOG_CONTROL_FILE
,
3957 O_RDWR
| O_CREAT
| O_EXCL
| PG_BINARY
,
3961 (errcode_for_file_access(),
3962 errmsg("could not create control file \"%s\": %m",
3963 XLOG_CONTROL_FILE
)));
3966 if (write(fd
, buffer
, PG_CONTROL_SIZE
) != PG_CONTROL_SIZE
)
3968 /* if write didn't set errno, assume problem is no disk space */
3972 (errcode_for_file_access(),
3973 errmsg("could not write to control file: %m")));
3976 if (pg_fsync(fd
) != 0)
3978 (errcode_for_file_access(),
3979 errmsg("could not fsync control file: %m")));
3983 (errcode_for_file_access(),
3984 errmsg("could not close control file: %m")));
3988 ReadControlFile(void)
3996 fd
= BasicOpenFile(XLOG_CONTROL_FILE
,
4001 (errcode_for_file_access(),
4002 errmsg("could not open control file \"%s\": %m",
4003 XLOG_CONTROL_FILE
)));
4005 if (read(fd
, ControlFile
, sizeof(ControlFileData
)) != sizeof(ControlFileData
))
4007 (errcode_for_file_access(),
4008 errmsg("could not read from control file: %m")));
4013 * Check for expected pg_control format version. If this is wrong, the
4014 * CRC check will likely fail because we'll be checking the wrong number
4015 * of bytes. Complaining about wrong version will probably be more
4016 * enlightening than complaining about wrong CRC.
4019 if (ControlFile
->pg_control_version
!= PG_CONTROL_VERSION
&& ControlFile
->pg_control_version
% 65536 == 0 && ControlFile
->pg_control_version
/ 65536 != 0)
4021 (errmsg("database files are incompatible with server"),
4022 errdetail("The database cluster was initialized with PG_CONTROL_VERSION %d (0x%08x),"
4023 " but the server was compiled with PG_CONTROL_VERSION %d (0x%08x).",
4024 ControlFile
->pg_control_version
, ControlFile
->pg_control_version
,
4025 PG_CONTROL_VERSION
, PG_CONTROL_VERSION
),
4026 errhint("This could be a problem of mismatched byte ordering. It looks like you need to initdb.")));
4028 if (ControlFile
->pg_control_version
!= PG_CONTROL_VERSION
)
4030 (errmsg("database files are incompatible with server"),
4031 errdetail("The database cluster was initialized with PG_CONTROL_VERSION %d,"
4032 " but the server was compiled with PG_CONTROL_VERSION %d.",
4033 ControlFile
->pg_control_version
, PG_CONTROL_VERSION
),
4034 errhint("It looks like you need to initdb.")));
4036 /* Now check the CRC. */
4039 (char *) ControlFile
,
4040 offsetof(ControlFileData
, crc
));
4043 if (!EQ_CRC32(crc
, ControlFile
->crc
))
4045 (errmsg("incorrect checksum in control file")));
4048 * Do compatibility checking immediately. If the database isn't
4049 * compatible with the backend executable, we want to abort before we
4050 * can possibly do any damage.
4052 if (ControlFile
->catalog_version_no
!= CATALOG_VERSION_NO
)
4054 (errmsg("database files are incompatible with server"),
4055 errdetail("The database cluster was initialized with CATALOG_VERSION_NO %d,"
4056 " but the server was compiled with CATALOG_VERSION_NO %d.",
4057 ControlFile
->catalog_version_no
, CATALOG_VERSION_NO
),
4058 errhint("It looks like you need to initdb.")));
4059 if (ControlFile
->maxAlign
!= MAXIMUM_ALIGNOF
)
4061 (errmsg("database files are incompatible with server"),
4062 errdetail("The database cluster was initialized with MAXALIGN %d,"
4063 " but the server was compiled with MAXALIGN %d.",
4064 ControlFile
->maxAlign
, MAXIMUM_ALIGNOF
),
4065 errhint("It looks like you need to initdb.")));
4066 if (ControlFile
->floatFormat
!= FLOATFORMAT_VALUE
)
4068 (errmsg("database files are incompatible with server"),
4069 errdetail("The database cluster appears to use a different floating-point number format than the server executable."),
4070 errhint("It looks like you need to initdb.")));
4071 if (ControlFile
->blcksz
!= BLCKSZ
)
4073 (errmsg("database files are incompatible with server"),
4074 errdetail("The database cluster was initialized with BLCKSZ %d,"
4075 " but the server was compiled with BLCKSZ %d.",
4076 ControlFile
->blcksz
, BLCKSZ
),
4077 errhint("It looks like you need to recompile or initdb.")));
4078 if (ControlFile
->relseg_size
!= RELSEG_SIZE
)
4080 (errmsg("database files are incompatible with server"),
4081 errdetail("The database cluster was initialized with RELSEG_SIZE %d,"
4082 " but the server was compiled with RELSEG_SIZE %d.",
4083 ControlFile
->relseg_size
, RELSEG_SIZE
),
4084 errhint("It looks like you need to recompile or initdb.")));
4085 if (ControlFile
->xlog_blcksz
!= XLOG_BLCKSZ
)
4087 (errmsg("database files are incompatible with server"),
4088 errdetail("The database cluster was initialized with XLOG_BLCKSZ %d,"
4089 " but the server was compiled with XLOG_BLCKSZ %d.",
4090 ControlFile
->xlog_blcksz
, XLOG_BLCKSZ
),
4091 errhint("It looks like you need to recompile or initdb.")));
4092 if (ControlFile
->xlog_seg_size
!= XLOG_SEG_SIZE
)
4094 (errmsg("database files are incompatible with server"),
4095 errdetail("The database cluster was initialized with XLOG_SEG_SIZE %d,"
4096 " but the server was compiled with XLOG_SEG_SIZE %d.",
4097 ControlFile
->xlog_seg_size
, XLOG_SEG_SIZE
),
4098 errhint("It looks like you need to recompile or initdb.")));
4099 if (ControlFile
->nameDataLen
!= NAMEDATALEN
)
4101 (errmsg("database files are incompatible with server"),
4102 errdetail("The database cluster was initialized with NAMEDATALEN %d,"
4103 " but the server was compiled with NAMEDATALEN %d.",
4104 ControlFile
->nameDataLen
, NAMEDATALEN
),
4105 errhint("It looks like you need to recompile or initdb.")));
4106 if (ControlFile
->indexMaxKeys
!= INDEX_MAX_KEYS
)
4108 (errmsg("database files are incompatible with server"),
4109 errdetail("The database cluster was initialized with INDEX_MAX_KEYS %d,"
4110 " but the server was compiled with INDEX_MAX_KEYS %d.",
4111 ControlFile
->indexMaxKeys
, INDEX_MAX_KEYS
),
4112 errhint("It looks like you need to recompile or initdb.")));
4113 if (ControlFile
->toast_max_chunk_size
!= TOAST_MAX_CHUNK_SIZE
)
4115 (errmsg("database files are incompatible with server"),
4116 errdetail("The database cluster was initialized with TOAST_MAX_CHUNK_SIZE %d,"
4117 " but the server was compiled with TOAST_MAX_CHUNK_SIZE %d.",
4118 ControlFile
->toast_max_chunk_size
, (int) TOAST_MAX_CHUNK_SIZE
),
4119 errhint("It looks like you need to recompile or initdb.")));
4121 #ifdef HAVE_INT64_TIMESTAMP
4122 if (ControlFile
->enableIntTimes
!= true)
4124 (errmsg("database files are incompatible with server"),
4125 errdetail("The database cluster was initialized without HAVE_INT64_TIMESTAMP"
4126 " but the server was compiled with HAVE_INT64_TIMESTAMP."),
4127 errhint("It looks like you need to recompile or initdb.")));
4129 if (ControlFile
->enableIntTimes
!= false)
4131 (errmsg("database files are incompatible with server"),
4132 errdetail("The database cluster was initialized with HAVE_INT64_TIMESTAMP"
4133 " but the server was compiled without HAVE_INT64_TIMESTAMP."),
4134 errhint("It looks like you need to recompile or initdb.")));
4137 #ifdef USE_FLOAT4_BYVAL
4138 if (ControlFile
->float4ByVal
!= true)
4140 (errmsg("database files are incompatible with server"),
4141 errdetail("The database cluster was initialized without USE_FLOAT4_BYVAL"
4142 " but the server was compiled with USE_FLOAT4_BYVAL."),
4143 errhint("It looks like you need to recompile or initdb.")));
4145 if (ControlFile
->float4ByVal
!= false)
4147 (errmsg("database files are incompatible with server"),
4148 errdetail("The database cluster was initialized with USE_FLOAT4_BYVAL"
4149 " but the server was compiled without USE_FLOAT4_BYVAL."),
4150 errhint("It looks like you need to recompile or initdb.")));
4153 #ifdef USE_FLOAT8_BYVAL
4154 if (ControlFile
->float8ByVal
!= true)
4156 (errmsg("database files are incompatible with server"),
4157 errdetail("The database cluster was initialized without USE_FLOAT8_BYVAL"
4158 " but the server was compiled with USE_FLOAT8_BYVAL."),
4159 errhint("It looks like you need to recompile or initdb.")));
4161 if (ControlFile
->float8ByVal
!= false)
4163 (errmsg("database files are incompatible with server"),
4164 errdetail("The database cluster was initialized with USE_FLOAT8_BYVAL"
4165 " but the server was compiled without USE_FLOAT8_BYVAL."),
4166 errhint("It looks like you need to recompile or initdb.")));
4171 UpdateControlFile(void)
4175 INIT_CRC32(ControlFile
->crc
);
4176 COMP_CRC32(ControlFile
->crc
,
4177 (char *) ControlFile
,
4178 offsetof(ControlFileData
, crc
));
4179 FIN_CRC32(ControlFile
->crc
);
4181 fd
= BasicOpenFile(XLOG_CONTROL_FILE
,
4186 (errcode_for_file_access(),
4187 errmsg("could not open control file \"%s\": %m",
4188 XLOG_CONTROL_FILE
)));
4191 if (write(fd
, ControlFile
, sizeof(ControlFileData
)) != sizeof(ControlFileData
))
4193 /* if write didn't set errno, assume problem is no disk space */
4197 (errcode_for_file_access(),
4198 errmsg("could not write to control file: %m")));
4201 if (pg_fsync(fd
) != 0)
4203 (errcode_for_file_access(),
4204 errmsg("could not fsync control file: %m")));
4208 (errcode_for_file_access(),
4209 errmsg("could not close control file: %m")));
4213 * Initialization of shared memory for XLOG
4221 size
= sizeof(XLogCtlData
);
4222 /* xlblocks array */
4223 size
= add_size(size
, mul_size(sizeof(XLogRecPtr
), XLOGbuffers
));
4224 /* extra alignment padding for XLOG I/O buffers */
4225 size
= add_size(size
, ALIGNOF_XLOG_BUFFER
);
4226 /* and the buffers themselves */
4227 size
= add_size(size
, mul_size(XLOG_BLCKSZ
, XLOGbuffers
));
4230 * Note: we don't count ControlFileData, it comes out of the "slop factor"
4231 * added by CreateSharedMemoryAndSemaphores. This lets us use this
4232 * routine again below to compute the actual allocation size.
4245 ControlFile
= (ControlFileData
*)
4246 ShmemInitStruct("Control File", sizeof(ControlFileData
), &foundCFile
);
4247 XLogCtl
= (XLogCtlData
*)
4248 ShmemInitStruct("XLOG Ctl", XLOGShmemSize(), &foundXLog
);
4250 if (foundCFile
|| foundXLog
)
4252 /* both should be present or neither */
4253 Assert(foundCFile
&& foundXLog
);
4257 memset(XLogCtl
, 0, sizeof(XLogCtlData
));
4260 * Since XLogCtlData contains XLogRecPtr fields, its sizeof should be a
4261 * multiple of the alignment for same, so no extra alignment padding is
4264 allocptr
= ((char *) XLogCtl
) + sizeof(XLogCtlData
);
4265 XLogCtl
->xlblocks
= (XLogRecPtr
*) allocptr
;
4266 memset(XLogCtl
->xlblocks
, 0, sizeof(XLogRecPtr
) * XLOGbuffers
);
4267 allocptr
+= sizeof(XLogRecPtr
) * XLOGbuffers
;
4270 * Align the start of the page buffers to an ALIGNOF_XLOG_BUFFER boundary.
4272 allocptr
= (char *) TYPEALIGN(ALIGNOF_XLOG_BUFFER
, allocptr
);
4273 XLogCtl
->pages
= allocptr
;
4274 memset(XLogCtl
->pages
, 0, (Size
) XLOG_BLCKSZ
* XLOGbuffers
);
4277 * Do basic initialization of XLogCtl shared data. (StartupXLOG will fill
4278 * in additional info.)
4280 XLogCtl
->XLogCacheBlck
= XLOGbuffers
- 1;
4281 XLogCtl
->Insert
.currpage
= (XLogPageHeader
) (XLogCtl
->pages
);
4282 SpinLockInit(&XLogCtl
->info_lck
);
4285 * If we are not in bootstrap mode, pg_control should already exist. Read
4286 * and validate it immediately (see comments in ReadControlFile() for the
4289 if (!IsBootstrapProcessingMode())
4294 * This func must be called ONCE on system install. It creates pg_control
4295 * and the initial XLOG segment.
4300 CheckPoint checkPoint
;
4302 XLogPageHeader page
;
4303 XLogLongPageHeader longpage
;
4306 uint64 sysidentifier
;
4311 * Select a hopefully-unique system identifier code for this installation.
4312 * We use the result of gettimeofday(), including the fractional seconds
4313 * field, as being about as unique as we can easily get. (Think not to
4314 * use random(), since it hasn't been seeded and there's no portable way
4315 * to seed it other than the system clock value...) The upper half of the
4316 * uint64 value is just the tv_sec part, while the lower half is the XOR
4317 * of tv_sec and tv_usec. This is to ensure that we don't lose uniqueness
4318 * unnecessarily if "uint64" is really only 32 bits wide. A person
4319 * knowing this encoding can determine the initialization time of the
4320 * installation, which could perhaps be useful sometimes.
4322 gettimeofday(&tv
, NULL
);
4323 sysidentifier
= ((uint64
) tv
.tv_sec
) << 32;
4324 sysidentifier
|= (uint32
) (tv
.tv_sec
| tv
.tv_usec
);
4326 /* First timeline ID is always 1 */
4329 /* page buffer must be aligned suitably for O_DIRECT */
4330 buffer
= (char *) palloc(XLOG_BLCKSZ
+ ALIGNOF_XLOG_BUFFER
);
4331 page
= (XLogPageHeader
) TYPEALIGN(ALIGNOF_XLOG_BUFFER
, buffer
);
4332 memset(page
, 0, XLOG_BLCKSZ
);
4334 /* Set up information for the initial checkpoint record */
4335 checkPoint
.redo
.xlogid
= 0;
4336 checkPoint
.redo
.xrecoff
= SizeOfXLogLongPHD
;
4337 checkPoint
.ThisTimeLineID
= ThisTimeLineID
;
4338 checkPoint
.nextXidEpoch
= 0;
4339 checkPoint
.nextXid
= FirstNormalTransactionId
;
4340 checkPoint
.nextOid
= FirstBootstrapObjectId
;
4341 checkPoint
.nextMulti
= FirstMultiXactId
;
4342 checkPoint
.nextMultiOffset
= 0;
4343 checkPoint
.time
= (pg_time_t
) time(NULL
);
4345 ShmemVariableCache
->nextXid
= checkPoint
.nextXid
;
4346 ShmemVariableCache
->nextOid
= checkPoint
.nextOid
;
4347 ShmemVariableCache
->oidCount
= 0;
4348 MultiXactSetNextMXact(checkPoint
.nextMulti
, checkPoint
.nextMultiOffset
);
4350 /* Set up the XLOG page header */
4351 page
->xlp_magic
= XLOG_PAGE_MAGIC
;
4352 page
->xlp_info
= XLP_LONG_HEADER
;
4353 page
->xlp_tli
= ThisTimeLineID
;
4354 page
->xlp_pageaddr
.xlogid
= 0;
4355 page
->xlp_pageaddr
.xrecoff
= 0;
4356 longpage
= (XLogLongPageHeader
) page
;
4357 longpage
->xlp_sysid
= sysidentifier
;
4358 longpage
->xlp_seg_size
= XLogSegSize
;
4359 longpage
->xlp_xlog_blcksz
= XLOG_BLCKSZ
;
4361 /* Insert the initial checkpoint record */
4362 record
= (XLogRecord
*) ((char *) page
+ SizeOfXLogLongPHD
);
4363 record
->xl_prev
.xlogid
= 0;
4364 record
->xl_prev
.xrecoff
= 0;
4365 record
->xl_xid
= InvalidTransactionId
;
4366 record
->xl_tot_len
= SizeOfXLogRecord
+ sizeof(checkPoint
);
4367 record
->xl_len
= sizeof(checkPoint
);
4368 record
->xl_info
= XLOG_CHECKPOINT_SHUTDOWN
;
4369 record
->xl_rmid
= RM_XLOG_ID
;
4370 memcpy(XLogRecGetData(record
), &checkPoint
, sizeof(checkPoint
));
4373 COMP_CRC32(crc
, &checkPoint
, sizeof(checkPoint
));
4374 COMP_CRC32(crc
, (char *) record
+ sizeof(pg_crc32
),
4375 SizeOfXLogRecord
- sizeof(pg_crc32
));
4377 record
->xl_crc
= crc
;
4379 /* Create first XLOG segment file */
4380 use_existent
= false;
4381 openLogFile
= XLogFileInit(0, 0, &use_existent
, false);
4383 /* Write the first page with the initial record */
4385 if (write(openLogFile
, page
, XLOG_BLCKSZ
) != XLOG_BLCKSZ
)
4387 /* if write didn't set errno, assume problem is no disk space */
4391 (errcode_for_file_access(),
4392 errmsg("could not write bootstrap transaction log file: %m")));
4395 if (pg_fsync(openLogFile
) != 0)
4397 (errcode_for_file_access(),
4398 errmsg("could not fsync bootstrap transaction log file: %m")));
4400 if (close(openLogFile
))
4402 (errcode_for_file_access(),
4403 errmsg("could not close bootstrap transaction log file: %m")));
4407 /* Now create pg_control */
4409 memset(ControlFile
, 0, sizeof(ControlFileData
));
4410 /* Initialize pg_control status fields */
4411 ControlFile
->system_identifier
= sysidentifier
;
4412 ControlFile
->state
= DB_SHUTDOWNED
;
4413 ControlFile
->time
= checkPoint
.time
;
4414 ControlFile
->checkPoint
= checkPoint
.redo
;
4415 ControlFile
->checkPointCopy
= checkPoint
;
4416 /* some additional ControlFile fields are set in WriteControlFile() */
4420 /* Bootstrap the commit log, too */
4422 BootStrapSUBTRANS();
4423 BootStrapMultiXact();
4429 str_time(pg_time_t tnow
)
4431 static char buf
[128];
4433 pg_strftime(buf
, sizeof(buf
),
4434 "%Y-%m-%d %H:%M:%S %Z",
4435 pg_localtime(&tnow
, log_timezone
));
4441 * See if there is a recovery command file (recovery.conf), and if so
4442 * read in parameters for archive recovery.
4444 * XXX longer term intention is to expand this to
4445 * cater for additional parameters and controls
4446 * possibly use a flex lexer similar to the GUC one
4449 readRecoveryCommandFile(void)
4452 char cmdline
[MAXPGPATH
];
4453 TimeLineID rtli
= 0;
4454 bool rtliGiven
= false;
4455 bool syntaxError
= false;
4457 fd
= AllocateFile(RECOVERY_COMMAND_FILE
, "r");
4460 if (errno
== ENOENT
)
4461 return; /* not there, so no archive recovery */
4463 (errcode_for_file_access(),
4464 errmsg("could not open recovery command file \"%s\": %m",
4465 RECOVERY_COMMAND_FILE
)));
4469 (errmsg("starting archive recovery")));
4474 while (fgets(cmdline
, sizeof(cmdline
), fd
) != NULL
)
4476 /* skip leading whitespace and check for # comment */
4481 for (ptr
= cmdline
; *ptr
; ptr
++)
4483 if (!isspace((unsigned char) *ptr
))
4486 if (*ptr
== '\0' || *ptr
== '#')
4489 /* identify the quoted parameter value */
4490 tok1
= strtok(ptr
, "'");
4496 tok2
= strtok(NULL
, "'");
4502 /* reparse to get just the parameter name */
4503 tok1
= strtok(ptr
, " \t=");
4510 if (strcmp(tok1
, "restore_command") == 0)
4512 recoveryRestoreCommand
= pstrdup(tok2
);
4514 (errmsg("restore_command = '%s'",
4515 recoveryRestoreCommand
)));
4517 else if (strcmp(tok1
, "recovery_target_timeline") == 0)
4520 if (strcmp(tok2
, "latest") == 0)
4525 rtli
= (TimeLineID
) strtoul(tok2
, NULL
, 0);
4526 if (errno
== EINVAL
|| errno
== ERANGE
)
4528 (errmsg("recovery_target_timeline is not a valid number: \"%s\"",
4533 (errmsg("recovery_target_timeline = %u", rtli
)));
4536 (errmsg("recovery_target_timeline = latest")));
4538 else if (strcmp(tok1
, "recovery_target_xid") == 0)
4541 recoveryTargetXid
= (TransactionId
) strtoul(tok2
, NULL
, 0);
4542 if (errno
== EINVAL
|| errno
== ERANGE
)
4544 (errmsg("recovery_target_xid is not a valid number: \"%s\"",
4547 (errmsg("recovery_target_xid = %u",
4548 recoveryTargetXid
)));
4549 recoveryTarget
= true;
4550 recoveryTargetExact
= true;
4552 else if (strcmp(tok1
, "recovery_target_time") == 0)
4555 * if recovery_target_xid specified, then this overrides
4556 * recovery_target_time
4558 if (recoveryTargetExact
)
4560 recoveryTarget
= true;
4561 recoveryTargetExact
= false;
4564 * Convert the time string given by the user to TimestampTz form.
4566 recoveryTargetTime
=
4567 DatumGetTimestampTz(DirectFunctionCall3(timestamptz_in
,
4568 CStringGetDatum(tok2
),
4569 ObjectIdGetDatum(InvalidOid
),
4570 Int32GetDatum(-1)));
4572 (errmsg("recovery_target_time = '%s'",
4573 timestamptz_to_str(recoveryTargetTime
))));
4575 else if (strcmp(tok1
, "recovery_target_inclusive") == 0)
4578 * does nothing if a recovery_target is not also set
4580 if (!parse_bool(tok2
, &recoveryTargetInclusive
))
4582 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
4583 errmsg("parameter \"recovery_target_inclusive\" requires a Boolean value")));
4585 (errmsg("recovery_target_inclusive = %s", tok2
)));
4587 else if (strcmp(tok1
, "log_restartpoints") == 0)
4590 * does nothing if a recovery_target is not also set
4592 if (!parse_bool(tok2
, &recoveryLogRestartpoints
))
4594 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
4595 errmsg("parameter \"log_restartpoints\" requires a Boolean value")));
4597 (errmsg("log_restartpoints = %s", tok2
)));
4601 (errmsg("unrecognized recovery parameter \"%s\"",
4609 (errmsg("syntax error in recovery command file: %s",
4611 errhint("Lines should have the format parameter = 'value'.")));
4613 /* Check that required parameters were supplied */
4614 if (recoveryRestoreCommand
== NULL
)
4616 (errmsg("recovery command file \"%s\" did not specify restore_command",
4617 RECOVERY_COMMAND_FILE
)));
4619 /* Enable fetching from archive recovery area */
4620 InArchiveRecovery
= true;
4623 * If user specified recovery_target_timeline, validate it or compute the
4624 * "latest" value. We can't do this until after we've gotten the restore
4625 * command and set InArchiveRecovery, because we need to fetch timeline
4626 * history files from the archive.
4632 /* Timeline 1 does not have a history file, all else should */
4633 if (rtli
!= 1 && !existsTimeLineHistory(rtli
))
4635 (errmsg("recovery target timeline %u does not exist",
4637 recoveryTargetTLI
= rtli
;
4641 /* We start the "latest" search from pg_control's timeline */
4642 recoveryTargetTLI
= findNewestTimeLine(recoveryTargetTLI
);
4648 * Exit archive-recovery state
4651 exitArchiveRecovery(TimeLineID endTLI
, uint32 endLogId
, uint32 endLogSeg
)
4653 char recoveryPath
[MAXPGPATH
];
4654 char xlogpath
[MAXPGPATH
];
4657 * We are no longer in archive recovery state.
4659 InArchiveRecovery
= false;
4662 * We should have the ending log segment currently open. Verify, and then
4663 * close it (to avoid problems on Windows with trying to rename or delete
4666 Assert(readFile
>= 0);
4667 Assert(readId
== endLogId
);
4668 Assert(readSeg
== endLogSeg
);
4674 * If the segment was fetched from archival storage, we want to replace
4675 * the existing xlog segment (if any) with the archival version. This is
4676 * because whatever is in XLOGDIR is very possibly older than what we have
4677 * from the archives, since it could have come from restoring a PGDATA
4678 * backup. In any case, the archival version certainly is more
4679 * descriptive of what our current database state is, because that is what
4682 * Note that if we are establishing a new timeline, ThisTimeLineID is
4683 * already set to the new value, and so we will create a new file instead
4684 * of overwriting any existing file. (This is, in fact, always the case
4687 snprintf(recoveryPath
, MAXPGPATH
, XLOGDIR
"/RECOVERYXLOG");
4688 XLogFilePath(xlogpath
, ThisTimeLineID
, endLogId
, endLogSeg
);
4690 if (restoredFromArchive
)
4693 (errmsg_internal("moving last restored xlog to \"%s\"",
4695 unlink(xlogpath
); /* might or might not exist */
4696 if (rename(recoveryPath
, xlogpath
) != 0)
4698 (errcode_for_file_access(),
4699 errmsg("could not rename file \"%s\" to \"%s\": %m",
4700 recoveryPath
, xlogpath
)));
4701 /* XXX might we need to fix permissions on the file? */
4706 * If the latest segment is not archival, but there's still a
4707 * RECOVERYXLOG laying about, get rid of it.
4709 unlink(recoveryPath
); /* ignore any error */
4712 * If we are establishing a new timeline, we have to copy data from
4713 * the last WAL segment of the old timeline to create a starting WAL
4714 * segment for the new timeline.
4716 if (endTLI
!= ThisTimeLineID
)
4717 XLogFileCopy(endLogId
, endLogSeg
,
4718 endTLI
, endLogId
, endLogSeg
);
4722 * Let's just make real sure there are not .ready or .done flags posted
4723 * for the new segment.
4725 XLogFileName(xlogpath
, ThisTimeLineID
, endLogId
, endLogSeg
);
4726 XLogArchiveCleanup(xlogpath
);
4728 /* Get rid of any remaining recovered timeline-history file, too */
4729 snprintf(recoveryPath
, MAXPGPATH
, XLOGDIR
"/RECOVERYHISTORY");
4730 unlink(recoveryPath
); /* ignore any error */
4733 * Rename the config file out of the way, so that we don't accidentally
4734 * re-enter archive recovery mode in a subsequent crash.
4736 unlink(RECOVERY_COMMAND_DONE
);
4737 if (rename(RECOVERY_COMMAND_FILE
, RECOVERY_COMMAND_DONE
) != 0)
4739 (errcode_for_file_access(),
4740 errmsg("could not rename file \"%s\" to \"%s\": %m",
4741 RECOVERY_COMMAND_FILE
, RECOVERY_COMMAND_DONE
)));
4744 (errmsg("archive recovery complete")));
4748 * For point-in-time recovery, this function decides whether we want to
4749 * stop applying the XLOG at or after the current record.
4751 * Returns TRUE if we are stopping, FALSE otherwise. On TRUE return,
4752 * *includeThis is set TRUE if we should apply this record before stopping.
4754 * We also track the timestamp of the latest applied COMMIT/ABORT record
4755 * in recoveryLastXTime, for logging purposes.
4756 * Also, some information is saved in recoveryStopXid et al for use in
4757 * annotating the new timeline's history file.
4760 recoveryStopsHere(XLogRecord
*record
, bool *includeThis
)
4764 TimestampTz recordXtime
;
4766 /* We only consider stopping at COMMIT or ABORT records */
4767 if (record
->xl_rmid
!= RM_XACT_ID
)
4769 record_info
= record
->xl_info
& ~XLR_INFO_MASK
;
4770 if (record_info
== XLOG_XACT_COMMIT
)
4772 xl_xact_commit
*recordXactCommitData
;
4774 recordXactCommitData
= (xl_xact_commit
*) XLogRecGetData(record
);
4775 recordXtime
= recordXactCommitData
->xact_time
;
4777 else if (record_info
== XLOG_XACT_ABORT
)
4779 xl_xact_abort
*recordXactAbortData
;
4781 recordXactAbortData
= (xl_xact_abort
*) XLogRecGetData(record
);
4782 recordXtime
= recordXactAbortData
->xact_time
;
4787 /* Do we have a PITR target at all? */
4788 if (!recoveryTarget
)
4790 recoveryLastXTime
= recordXtime
;
4794 if (recoveryTargetExact
)
4797 * there can be only one transaction end record with this exact
4800 * when testing for an xid, we MUST test for equality only, since
4801 * transactions are numbered in the order they start, not the order
4802 * they complete. A higher numbered xid will complete before you about
4803 * 50% of the time...
4805 stopsHere
= (record
->xl_xid
== recoveryTargetXid
);
4807 *includeThis
= recoveryTargetInclusive
;
4812 * there can be many transactions that share the same commit time, so
4813 * we stop after the last one, if we are inclusive, or stop at the
4814 * first one if we are exclusive
4816 if (recoveryTargetInclusive
)
4817 stopsHere
= (recordXtime
> recoveryTargetTime
);
4819 stopsHere
= (recordXtime
>= recoveryTargetTime
);
4821 *includeThis
= false;
4826 recoveryStopXid
= record
->xl_xid
;
4827 recoveryStopTime
= recordXtime
;
4828 recoveryStopAfter
= *includeThis
;
4830 if (record_info
== XLOG_XACT_COMMIT
)
4832 if (recoveryStopAfter
)
4834 (errmsg("recovery stopping after commit of transaction %u, time %s",
4836 timestamptz_to_str(recoveryStopTime
))));
4839 (errmsg("recovery stopping before commit of transaction %u, time %s",
4841 timestamptz_to_str(recoveryStopTime
))));
4845 if (recoveryStopAfter
)
4847 (errmsg("recovery stopping after abort of transaction %u, time %s",
4849 timestamptz_to_str(recoveryStopTime
))));
4852 (errmsg("recovery stopping before abort of transaction %u, time %s",
4854 timestamptz_to_str(recoveryStopTime
))));
4857 if (recoveryStopAfter
)
4858 recoveryLastXTime
= recordXtime
;
4861 recoveryLastXTime
= recordXtime
;
4867 * This must be called ONCE during postmaster or standalone-backend startup
4872 XLogCtlInsert
*Insert
;
4873 CheckPoint checkPoint
;
4875 bool reachedStopPoint
= false;
4876 bool haveBackupLabel
= false;
4886 TransactionId oldestActiveXID
;
4889 * Read control file and check XLOG status looks valid.
4891 * Note: in most control paths, *ControlFile is already valid and we need
4892 * not do ReadControlFile() here, but might as well do it to be sure.
4896 if (ControlFile
->state
< DB_SHUTDOWNED
||
4897 ControlFile
->state
> DB_IN_PRODUCTION
||
4898 !XRecOffIsValid(ControlFile
->checkPoint
.xrecoff
))
4900 (errmsg("control file contains invalid data")));
4902 if (ControlFile
->state
== DB_SHUTDOWNED
)
4904 (errmsg("database system was shut down at %s",
4905 str_time(ControlFile
->time
))));
4906 else if (ControlFile
->state
== DB_SHUTDOWNING
)
4908 (errmsg("database system shutdown was interrupted; last known up at %s",
4909 str_time(ControlFile
->time
))));
4910 else if (ControlFile
->state
== DB_IN_CRASH_RECOVERY
)
4912 (errmsg("database system was interrupted while in recovery at %s",
4913 str_time(ControlFile
->time
)),
4914 errhint("This probably means that some data is corrupted and"
4915 " you will have to use the last backup for recovery.")));
4916 else if (ControlFile
->state
== DB_IN_ARCHIVE_RECOVERY
)
4918 (errmsg("database system was interrupted while in recovery at log time %s",
4919 str_time(ControlFile
->checkPointCopy
.time
)),
4920 errhint("If this has occurred more than once some data might be corrupted"
4921 " and you might need to choose an earlier recovery target.")));
4922 else if (ControlFile
->state
== DB_IN_PRODUCTION
)
4924 (errmsg("database system was interrupted; last known up at %s",
4925 str_time(ControlFile
->time
))));
4927 /* This is just to allow attaching to startup process with a debugger */
4928 #ifdef XLOG_REPLAY_DELAY
4929 if (ControlFile
->state
!= DB_SHUTDOWNED
)
4930 pg_usleep(60000000L);
4934 * Verify that pg_xlog and pg_xlog/archive_status exist. In cases where
4935 * someone has performed a copy for PITR, these directories may have
4936 * been excluded and need to be re-created.
4938 ValidateXLOGDirectoryStructure();
4941 * Initialize on the assumption we want to recover to the same timeline
4942 * that's active according to pg_control.
4944 recoveryTargetTLI
= ControlFile
->checkPointCopy
.ThisTimeLineID
;
4947 * Check for recovery control file, and if so set up state for offline
4950 readRecoveryCommandFile();
4952 /* Now we can determine the list of expected TLIs */
4953 expectedTLIs
= readTimeLineHistory(recoveryTargetTLI
);
4956 * If pg_control's timeline is not in expectedTLIs, then we cannot
4957 * proceed: the backup is not part of the history of the requested
4960 if (!list_member_int(expectedTLIs
,
4961 (int) ControlFile
->checkPointCopy
.ThisTimeLineID
))
4963 (errmsg("requested timeline %u is not a child of database system timeline %u",
4965 ControlFile
->checkPointCopy
.ThisTimeLineID
)));
4967 if (read_backup_label(&checkPointLoc
, &minRecoveryLoc
))
4970 * When a backup_label file is present, we want to roll forward from
4971 * the checkpoint it identifies, rather than using pg_control.
4973 record
= ReadCheckpointRecord(checkPointLoc
, 0);
4977 (errmsg("checkpoint record is at %X/%X",
4978 checkPointLoc
.xlogid
, checkPointLoc
.xrecoff
)));
4979 InRecovery
= true; /* force recovery even if SHUTDOWNED */
4984 (errmsg("could not locate required checkpoint record"),
4985 errhint("If you are not restoring from a backup, try removing the file \"%s/backup_label\".", DataDir
)));
4987 /* set flag to delete it later */
4988 haveBackupLabel
= true;
4993 * Get the last valid checkpoint record. If the latest one according
4994 * to pg_control is broken, try the next-to-last one.
4996 checkPointLoc
= ControlFile
->checkPoint
;
4997 record
= ReadCheckpointRecord(checkPointLoc
, 1);
5001 (errmsg("checkpoint record is at %X/%X",
5002 checkPointLoc
.xlogid
, checkPointLoc
.xrecoff
)));
5006 checkPointLoc
= ControlFile
->prevCheckPoint
;
5007 record
= ReadCheckpointRecord(checkPointLoc
, 2);
5011 (errmsg("using previous checkpoint record at %X/%X",
5012 checkPointLoc
.xlogid
, checkPointLoc
.xrecoff
)));
5013 InRecovery
= true; /* force recovery even if SHUTDOWNED */
5017 (errmsg("could not locate a valid checkpoint record")));
5021 LastRec
= RecPtr
= checkPointLoc
;
5022 memcpy(&checkPoint
, XLogRecGetData(record
), sizeof(CheckPoint
));
5023 wasShutdown
= (record
->xl_info
== XLOG_CHECKPOINT_SHUTDOWN
);
5026 (errmsg("redo record is at %X/%X; shutdown %s",
5027 checkPoint
.redo
.xlogid
, checkPoint
.redo
.xrecoff
,
5028 wasShutdown
? "TRUE" : "FALSE")));
5030 (errmsg("next transaction ID: %u/%u; next OID: %u",
5031 checkPoint
.nextXidEpoch
, checkPoint
.nextXid
,
5032 checkPoint
.nextOid
)));
5034 (errmsg("next MultiXactId: %u; next MultiXactOffset: %u",
5035 checkPoint
.nextMulti
, checkPoint
.nextMultiOffset
)));
5036 if (!TransactionIdIsNormal(checkPoint
.nextXid
))
5038 (errmsg("invalid next transaction ID")));
5040 ShmemVariableCache
->nextXid
= checkPoint
.nextXid
;
5041 ShmemVariableCache
->nextOid
= checkPoint
.nextOid
;
5042 ShmemVariableCache
->oidCount
= 0;
5043 MultiXactSetNextMXact(checkPoint
.nextMulti
, checkPoint
.nextMultiOffset
);
5046 * We must replay WAL entries using the same TimeLineID they were created
5047 * under, so temporarily adopt the TLI indicated by the checkpoint (see
5048 * also xlog_redo()).
5050 ThisTimeLineID
= checkPoint
.ThisTimeLineID
;
5052 RedoRecPtr
= XLogCtl
->Insert
.RedoRecPtr
= checkPoint
.redo
;
5054 if (XLByteLT(RecPtr
, checkPoint
.redo
))
5056 (errmsg("invalid redo in checkpoint record")));
5059 * Check whether we need to force recovery from WAL. If it appears to
5060 * have been a clean shutdown and we did not have a recovery.conf file,
5061 * then assume no recovery needed.
5063 if (XLByteLT(checkPoint
.redo
, RecPtr
))
5067 (errmsg("invalid redo record in shutdown checkpoint")));
5070 else if (ControlFile
->state
!= DB_SHUTDOWNED
)
5072 else if (InArchiveRecovery
)
5074 /* force recovery due to presence of recovery.conf */
5084 * Update pg_control to show that we are recovering and to show the
5085 * selected checkpoint as the place we are starting from. We also mark
5086 * pg_control with any minimum recovery stop point obtained from a
5087 * backup history file.
5089 if (InArchiveRecovery
)
5092 (errmsg("automatic recovery in progress")));
5093 ControlFile
->state
= DB_IN_ARCHIVE_RECOVERY
;
5098 (errmsg("database system was not properly shut down; "
5099 "automatic recovery in progress")));
5100 ControlFile
->state
= DB_IN_CRASH_RECOVERY
;
5102 ControlFile
->prevCheckPoint
= ControlFile
->checkPoint
;
5103 ControlFile
->checkPoint
= checkPointLoc
;
5104 ControlFile
->checkPointCopy
= checkPoint
;
5105 if (minRecoveryLoc
.xlogid
!= 0 || minRecoveryLoc
.xrecoff
!= 0)
5106 ControlFile
->minRecoveryPoint
= minRecoveryLoc
;
5107 ControlFile
->time
= (pg_time_t
) time(NULL
);
5108 UpdateControlFile();
5111 * If there was a backup label file, it's done its job and the info
5112 * has now been propagated into pg_control. We must get rid of the
5113 * label file so that if we crash during recovery, we'll pick up at
5114 * the latest recovery restartpoint instead of going all the way back
5115 * to the backup start point. It seems prudent though to just rename
5116 * the file out of the way rather than delete it completely.
5118 if (haveBackupLabel
)
5120 unlink(BACKUP_LABEL_OLD
);
5121 if (rename(BACKUP_LABEL_FILE
, BACKUP_LABEL_OLD
) != 0)
5123 (errcode_for_file_access(),
5124 errmsg("could not rename file \"%s\" to \"%s\": %m",
5125 BACKUP_LABEL_FILE
, BACKUP_LABEL_OLD
)));
5128 /* Initialize resource managers */
5129 for (rmid
= 0; rmid
<= RM_MAX_ID
; rmid
++)
5131 if (RmgrTable
[rmid
].rm_startup
!= NULL
)
5132 RmgrTable
[rmid
].rm_startup();
5136 * Find the first record that logically follows the checkpoint --- it
5137 * might physically precede it, though.
5139 if (XLByteLT(checkPoint
.redo
, RecPtr
))
5141 /* back up to find the record */
5142 record
= ReadRecord(&(checkPoint
.redo
), PANIC
);
5146 /* just have to read next record after CheckPoint */
5147 record
= ReadRecord(NULL
, LOG
);
5152 bool recoveryContinue
= true;
5153 bool recoveryApply
= true;
5154 ErrorContextCallback errcontext
;
5158 (errmsg("redo starts at %X/%X",
5159 ReadRecPtr
.xlogid
, ReadRecPtr
.xrecoff
)));
5162 * main redo apply loop
5171 initStringInfo(&buf
);
5172 appendStringInfo(&buf
, "REDO @ %X/%X; LSN %X/%X: ",
5173 ReadRecPtr
.xlogid
, ReadRecPtr
.xrecoff
,
5174 EndRecPtr
.xlogid
, EndRecPtr
.xrecoff
);
5175 xlog_outrec(&buf
, record
);
5176 appendStringInfo(&buf
, " - ");
5177 RmgrTable
[record
->xl_rmid
].rm_desc(&buf
,
5179 XLogRecGetData(record
));
5180 elog(LOG
, "%s", buf
.data
);
5186 * Have we reached our recovery target?
5188 if (recoveryStopsHere(record
, &recoveryApply
))
5190 reachedStopPoint
= true; /* see below */
5191 recoveryContinue
= false;
5196 /* Setup error traceback support for ereport() */
5197 errcontext
.callback
= rm_redo_error_callback
;
5198 errcontext
.arg
= (void *) record
;
5199 errcontext
.previous
= error_context_stack
;
5200 error_context_stack
= &errcontext
;
5202 /* nextXid must be beyond record's xid */
5203 if (TransactionIdFollowsOrEquals(record
->xl_xid
,
5204 ShmemVariableCache
->nextXid
))
5206 ShmemVariableCache
->nextXid
= record
->xl_xid
;
5207 TransactionIdAdvance(ShmemVariableCache
->nextXid
);
5210 RmgrTable
[record
->xl_rmid
].rm_redo(EndRecPtr
, record
);
5212 /* Pop the error context stack */
5213 error_context_stack
= errcontext
.previous
;
5215 LastRec
= ReadRecPtr
;
5217 record
= ReadRecord(NULL
, LOG
);
5218 } while (record
!= NULL
&& recoveryContinue
);
5221 * end of main redo apply loop
5225 (errmsg("redo done at %X/%X",
5226 ReadRecPtr
.xlogid
, ReadRecPtr
.xrecoff
)));
5227 if (recoveryLastXTime
)
5229 (errmsg("last completed transaction was at log time %s",
5230 timestamptz_to_str(recoveryLastXTime
))));
5235 /* there are no WAL records following the checkpoint */
5237 (errmsg("redo is not required")));
5242 * Re-fetch the last valid or last applied record, so we can identify the
5243 * exact endpoint of what we consider the valid portion of WAL.
5245 record
= ReadRecord(&LastRec
, PANIC
);
5246 EndOfLog
= EndRecPtr
;
5247 XLByteToPrevSeg(EndOfLog
, endLogId
, endLogSeg
);
5250 * Complain if we did not roll forward far enough to render the backup
5253 if (XLByteLT(EndOfLog
, ControlFile
->minRecoveryPoint
))
5255 if (reachedStopPoint
) /* stopped because of stop request */
5257 (errmsg("requested recovery stop point is before end time of backup dump")));
5258 else /* ran off end of WAL */
5260 (errmsg("WAL ends before end time of backup dump")));
5264 * Consider whether we need to assign a new timeline ID.
5266 * If we are doing an archive recovery, we always assign a new ID. This
5267 * handles a couple of issues. If we stopped short of the end of WAL
5268 * during recovery, then we are clearly generating a new timeline and must
5269 * assign it a unique new ID. Even if we ran to the end, modifying the
5270 * current last segment is problematic because it may result in trying to
5271 * overwrite an already-archived copy of that segment, and we encourage
5272 * DBAs to make their archive_commands reject that. We can dodge the
5273 * problem by making the new active segment have a new timeline ID.
5275 * In a normal crash recovery, we can just extend the timeline we were in.
5277 if (InArchiveRecovery
)
5279 ThisTimeLineID
= findNewestTimeLine(recoveryTargetTLI
) + 1;
5281 (errmsg("selected new timeline ID: %u", ThisTimeLineID
)));
5282 writeTimeLineHistory(ThisTimeLineID
, recoveryTargetTLI
,
5283 curFileTLI
, endLogId
, endLogSeg
);
5286 /* Save the selected TimeLineID in shared memory, too */
5287 XLogCtl
->ThisTimeLineID
= ThisTimeLineID
;
5290 * We are now done reading the old WAL. Turn off archive fetching if it
5291 * was active, and make a writable copy of the last WAL segment. (Note
5292 * that we also have a copy of the last block of the old WAL in readBuf;
5293 * we will use that below.)
5295 if (InArchiveRecovery
)
5296 exitArchiveRecovery(curFileTLI
, endLogId
, endLogSeg
);
5299 * Prepare to write WAL starting at EndOfLog position, and init xlog
5300 * buffer cache using the block containing the last record from the
5301 * previous incarnation.
5303 openLogId
= endLogId
;
5304 openLogSeg
= endLogSeg
;
5305 openLogFile
= XLogFileOpen(openLogId
, openLogSeg
);
5307 Insert
= &XLogCtl
->Insert
;
5308 Insert
->PrevRecord
= LastRec
;
5309 XLogCtl
->xlblocks
[0].xlogid
= openLogId
;
5310 XLogCtl
->xlblocks
[0].xrecoff
=
5311 ((EndOfLog
.xrecoff
- 1) / XLOG_BLCKSZ
+ 1) * XLOG_BLCKSZ
;
5314 * Tricky point here: readBuf contains the *last* block that the LastRec
5315 * record spans, not the one it starts in. The last block is indeed the
5316 * one we want to use.
5318 Assert(readOff
== (XLogCtl
->xlblocks
[0].xrecoff
- XLOG_BLCKSZ
) % XLogSegSize
);
5319 memcpy((char *) Insert
->currpage
, readBuf
, XLOG_BLCKSZ
);
5320 Insert
->currpos
= (char *) Insert
->currpage
+
5321 (EndOfLog
.xrecoff
+ XLOG_BLCKSZ
- XLogCtl
->xlblocks
[0].xrecoff
);
5323 LogwrtResult
.Write
= LogwrtResult
.Flush
= EndOfLog
;
5325 XLogCtl
->Write
.LogwrtResult
= LogwrtResult
;
5326 Insert
->LogwrtResult
= LogwrtResult
;
5327 XLogCtl
->LogwrtResult
= LogwrtResult
;
5329 XLogCtl
->LogwrtRqst
.Write
= EndOfLog
;
5330 XLogCtl
->LogwrtRqst
.Flush
= EndOfLog
;
5332 freespace
= INSERT_FREESPACE(Insert
);
5335 /* Make sure rest of page is zero */
5336 MemSet(Insert
->currpos
, 0, freespace
);
5337 XLogCtl
->Write
.curridx
= 0;
5342 * Whenever Write.LogwrtResult points to exactly the end of a page,
5343 * Write.curridx must point to the *next* page (see XLogWrite()).
5345 * Note: it might seem we should do AdvanceXLInsertBuffer() here, but
5346 * this is sufficient. The first actual attempt to insert a log
5347 * record will advance the insert state.
5349 XLogCtl
->Write
.curridx
= NextBufIdx(0);
5352 /* Pre-scan prepared transactions to find out the range of XIDs present */
5353 oldestActiveXID
= PrescanPreparedTransactions();
5360 * Allow resource managers to do any required cleanup.
5362 for (rmid
= 0; rmid
<= RM_MAX_ID
; rmid
++)
5364 if (RmgrTable
[rmid
].rm_cleanup
!= NULL
)
5365 RmgrTable
[rmid
].rm_cleanup();
5369 * Check to see if the XLOG sequence contained any unresolved
5370 * references to uninitialized pages.
5372 XLogCheckInvalidPages();
5375 * Reset pgstat data, because it may be invalid after recovery.
5380 * Perform a checkpoint to update all our recovery activity to disk.
5382 * Note that we write a shutdown checkpoint rather than an on-line
5383 * one. This is not particularly critical, but since we may be
5384 * assigning a new TLI, using a shutdown checkpoint allows us to have
5385 * the rule that TLI only changes in shutdown checkpoints, which
5386 * allows some extra error checking in xlog_redo.
5388 CreateCheckPoint(CHECKPOINT_IS_SHUTDOWN
| CHECKPOINT_IMMEDIATE
);
5392 * Preallocate additional log files, if wanted.
5394 PreallocXlogFiles(EndOfLog
);
5397 * Okay, we're officially UP.
5401 ControlFile
->state
= DB_IN_PRODUCTION
;
5402 ControlFile
->time
= (pg_time_t
) time(NULL
);
5403 UpdateControlFile();
5405 /* start the archive_timeout timer running */
5406 XLogCtl
->Write
.lastSegSwitchTime
= ControlFile
->time
;
5408 /* initialize shared-memory copy of latest checkpoint XID/epoch */
5409 XLogCtl
->ckptXidEpoch
= ControlFile
->checkPointCopy
.nextXidEpoch
;
5410 XLogCtl
->ckptXid
= ControlFile
->checkPointCopy
.nextXid
;
5412 /* also initialize latestCompletedXid, to nextXid - 1 */
5413 ShmemVariableCache
->latestCompletedXid
= ShmemVariableCache
->nextXid
;
5414 TransactionIdRetreat(ShmemVariableCache
->latestCompletedXid
);
5416 /* Start up the commit log and related stuff, too */
5418 StartupSUBTRANS(oldestActiveXID
);
5421 /* Reload shared-memory state for prepared transactions */
5422 RecoverPreparedTransactions();
5424 /* Shut down readFile facility, free space */
5437 free(readRecordBuf
);
5438 readRecordBuf
= NULL
;
5439 readRecordBufSize
= 0;
5444 * Subroutine to try to fetch and validate a prior checkpoint record.
5446 * whichChkpt identifies the checkpoint (merely for reporting purposes).
5447 * 1 for "primary", 2 for "secondary", 0 for "other" (backup_label)
5450 ReadCheckpointRecord(XLogRecPtr RecPtr
, int whichChkpt
)
5454 if (!XRecOffIsValid(RecPtr
.xrecoff
))
5460 (errmsg("invalid primary checkpoint link in control file")));
5464 (errmsg("invalid secondary checkpoint link in control file")));
5468 (errmsg("invalid checkpoint link in backup_label file")));
5474 record
= ReadRecord(&RecPtr
, LOG
);
5482 (errmsg("invalid primary checkpoint record")));
5486 (errmsg("invalid secondary checkpoint record")));
5490 (errmsg("invalid checkpoint record")));
5495 if (record
->xl_rmid
!= RM_XLOG_ID
)
5501 (errmsg("invalid resource manager ID in primary checkpoint record")));
5505 (errmsg("invalid resource manager ID in secondary checkpoint record")));
5509 (errmsg("invalid resource manager ID in checkpoint record")));
5514 if (record
->xl_info
!= XLOG_CHECKPOINT_SHUTDOWN
&&
5515 record
->xl_info
!= XLOG_CHECKPOINT_ONLINE
)
5521 (errmsg("invalid xl_info in primary checkpoint record")));
5525 (errmsg("invalid xl_info in secondary checkpoint record")));
5529 (errmsg("invalid xl_info in checkpoint record")));
5534 if (record
->xl_len
!= sizeof(CheckPoint
) ||
5535 record
->xl_tot_len
!= SizeOfXLogRecord
+ sizeof(CheckPoint
))
5541 (errmsg("invalid length of primary checkpoint record")));
5545 (errmsg("invalid length of secondary checkpoint record")));
5549 (errmsg("invalid length of checkpoint record")));
5558 * This must be called during startup of a backend process, except that
5559 * it need not be called in a standalone backend (which does StartupXLOG
5560 * instead). We need to initialize the local copies of ThisTimeLineID and
5563 * Note: before Postgres 8.0, we went to some effort to keep the postmaster
5564 * process's copies of ThisTimeLineID and RedoRecPtr valid too. This was
5565 * unnecessary however, since the postmaster itself never touches XLOG anyway.
5568 InitXLOGAccess(void)
5570 /* ThisTimeLineID doesn't change so we need no lock to copy it */
5571 ThisTimeLineID
= XLogCtl
->ThisTimeLineID
;
5572 /* Use GetRedoRecPtr to copy the RedoRecPtr safely */
5573 (void) GetRedoRecPtr();
5577 * Once spawned, a backend may update its local RedoRecPtr from
5578 * XLogCtl->Insert.RedoRecPtr; it must hold the insert lock or info_lck
5579 * to do so. This is done in XLogInsert() or GetRedoRecPtr().
5584 /* use volatile pointer to prevent code rearrangement */
5585 volatile XLogCtlData
*xlogctl
= XLogCtl
;
5587 SpinLockAcquire(&xlogctl
->info_lck
);
5588 Assert(XLByteLE(RedoRecPtr
, xlogctl
->Insert
.RedoRecPtr
));
5589 RedoRecPtr
= xlogctl
->Insert
.RedoRecPtr
;
5590 SpinLockRelease(&xlogctl
->info_lck
);
5596 * GetInsertRecPtr -- Returns the current insert position.
5598 * NOTE: The value *actually* returned is the position of the last full
5599 * xlog page. It lags behind the real insert position by at most 1 page.
5600 * For that, we don't need to acquire WALInsertLock which can be quite
5601 * heavily contended, and an approximation is enough for the current
5602 * usage of this function.
5605 GetInsertRecPtr(void)
5607 /* use volatile pointer to prevent code rearrangement */
5608 volatile XLogCtlData
*xlogctl
= XLogCtl
;
5611 SpinLockAcquire(&xlogctl
->info_lck
);
5612 recptr
= xlogctl
->LogwrtRqst
.Write
;
5613 SpinLockRelease(&xlogctl
->info_lck
);
5619 * Get the time of the last xlog segment switch
5622 GetLastSegSwitchTime(void)
5626 /* Need WALWriteLock, but shared lock is sufficient */
5627 LWLockAcquire(WALWriteLock
, LW_SHARED
);
5628 result
= XLogCtl
->Write
.lastSegSwitchTime
;
5629 LWLockRelease(WALWriteLock
);
5635 * GetNextXidAndEpoch - get the current nextXid value and associated epoch
5637 * This is exported for use by code that would like to have 64-bit XIDs.
5638 * We don't really support such things, but all XIDs within the system
5639 * can be presumed "close to" the result, and thus the epoch associated
5640 * with them can be determined.
5643 GetNextXidAndEpoch(TransactionId
*xid
, uint32
*epoch
)
5645 uint32 ckptXidEpoch
;
5646 TransactionId ckptXid
;
5647 TransactionId nextXid
;
5649 /* Must read checkpoint info first, else have race condition */
5651 /* use volatile pointer to prevent code rearrangement */
5652 volatile XLogCtlData
*xlogctl
= XLogCtl
;
5654 SpinLockAcquire(&xlogctl
->info_lck
);
5655 ckptXidEpoch
= xlogctl
->ckptXidEpoch
;
5656 ckptXid
= xlogctl
->ckptXid
;
5657 SpinLockRelease(&xlogctl
->info_lck
);
5660 /* Now fetch current nextXid */
5661 nextXid
= ReadNewTransactionId();
5664 * nextXid is certainly logically later than ckptXid. So if it's
5665 * numerically less, it must have wrapped into the next epoch.
5667 if (nextXid
< ckptXid
)
5671 *epoch
= ckptXidEpoch
;
5675 * This must be called ONCE during postmaster or standalone-backend shutdown
5678 ShutdownXLOG(int code
, Datum arg
)
5681 (errmsg("shutting down")));
5683 CreateCheckPoint(CHECKPOINT_IS_SHUTDOWN
| CHECKPOINT_IMMEDIATE
);
5686 ShutdownMultiXact();
5689 (errmsg("database system is shut down")));
5693 * Log start of a checkpoint.
5696 LogCheckpointStart(int flags
)
5698 elog(LOG
, "checkpoint starting:%s%s%s%s%s%s",
5699 (flags
& CHECKPOINT_IS_SHUTDOWN
) ? " shutdown" : "",
5700 (flags
& CHECKPOINT_IMMEDIATE
) ? " immediate" : "",
5701 (flags
& CHECKPOINT_FORCE
) ? " force" : "",
5702 (flags
& CHECKPOINT_WAIT
) ? " wait" : "",
5703 (flags
& CHECKPOINT_CAUSE_XLOG
) ? " xlog" : "",
5704 (flags
& CHECKPOINT_CAUSE_TIME
) ? " time" : "");
5708 * Log end of a checkpoint.
5711 LogCheckpointEnd(void)
5720 CheckpointStats
.ckpt_end_t
= GetCurrentTimestamp();
5722 TimestampDifference(CheckpointStats
.ckpt_start_t
,
5723 CheckpointStats
.ckpt_end_t
,
5724 &total_secs
, &total_usecs
);
5726 TimestampDifference(CheckpointStats
.ckpt_write_t
,
5727 CheckpointStats
.ckpt_sync_t
,
5728 &write_secs
, &write_usecs
);
5730 TimestampDifference(CheckpointStats
.ckpt_sync_t
,
5731 CheckpointStats
.ckpt_sync_end_t
,
5732 &sync_secs
, &sync_usecs
);
5734 elog(LOG
, "checkpoint complete: wrote %d buffers (%.1f%%); "
5735 "%d transaction log file(s) added, %d removed, %d recycled; "
5736 "write=%ld.%03d s, sync=%ld.%03d s, total=%ld.%03d s",
5737 CheckpointStats
.ckpt_bufs_written
,
5738 (double) CheckpointStats
.ckpt_bufs_written
* 100 / NBuffers
,
5739 CheckpointStats
.ckpt_segs_added
,
5740 CheckpointStats
.ckpt_segs_removed
,
5741 CheckpointStats
.ckpt_segs_recycled
,
5742 write_secs
, write_usecs
/ 1000,
5743 sync_secs
, sync_usecs
/ 1000,
5744 total_secs
, total_usecs
/ 1000);
5748 * Perform a checkpoint --- either during shutdown, or on-the-fly
5750 * flags is a bitwise OR of the following:
5751 * CHECKPOINT_IS_SHUTDOWN: checkpoint is for database shutdown.
5752 * CHECKPOINT_IMMEDIATE: finish the checkpoint ASAP,
5753 * ignoring checkpoint_completion_target parameter.
5754 * CHECKPOINT_FORCE: force a checkpoint even if no XLOG activity has occured
5755 * since the last one (implied by CHECKPOINT_IS_SHUTDOWN).
5757 * Note: flags contains other bits, of interest here only for logging purposes.
5758 * In particular note that this routine is synchronous and does not pay
5759 * attention to CHECKPOINT_WAIT.
5762 CreateCheckPoint(int flags
)
5764 bool shutdown
= (flags
& CHECKPOINT_IS_SHUTDOWN
) != 0;
5765 CheckPoint checkPoint
;
5767 XLogCtlInsert
*Insert
= &XLogCtl
->Insert
;
5772 TransactionId
*inCommitXids
;
5776 * Acquire CheckpointLock to ensure only one checkpoint happens at a time.
5777 * (This is just pro forma, since in the present system structure there is
5778 * only one process that is allowed to issue checkpoints at any given
5781 LWLockAcquire(CheckpointLock
, LW_EXCLUSIVE
);
5784 * Prepare to accumulate statistics.
5786 * Note: because it is possible for log_checkpoints to change while a
5787 * checkpoint proceeds, we always accumulate stats, even if
5788 * log_checkpoints is currently off.
5790 MemSet(&CheckpointStats
, 0, sizeof(CheckpointStats
));
5791 CheckpointStats
.ckpt_start_t
= GetCurrentTimestamp();
5794 * Use a critical section to force system panic if we have trouble.
5796 START_CRIT_SECTION();
5800 ControlFile
->state
= DB_SHUTDOWNING
;
5801 ControlFile
->time
= (pg_time_t
) time(NULL
);
5802 UpdateControlFile();
5806 * Let smgr prepare for checkpoint; this has to happen before we determine
5807 * the REDO pointer. Note that smgr must not do anything that'd have to
5808 * be undone if we decide no checkpoint is needed.
5812 /* Begin filling in the checkpoint WAL record */
5813 MemSet(&checkPoint
, 0, sizeof(checkPoint
));
5814 checkPoint
.ThisTimeLineID
= ThisTimeLineID
;
5815 checkPoint
.time
= (pg_time_t
) time(NULL
);
5818 * We must hold WALInsertLock while examining insert state to determine
5819 * the checkpoint REDO pointer.
5821 LWLockAcquire(WALInsertLock
, LW_EXCLUSIVE
);
5824 * If this isn't a shutdown or forced checkpoint, and we have not inserted
5825 * any XLOG records since the start of the last checkpoint, skip the
5826 * checkpoint. The idea here is to avoid inserting duplicate checkpoints
5827 * when the system is idle. That wastes log space, and more importantly it
5828 * exposes us to possible loss of both current and previous checkpoint
5829 * records if the machine crashes just as we're writing the update.
5830 * (Perhaps it'd make even more sense to checkpoint only when the previous
5831 * checkpoint record is in a different xlog page?)
5833 * We have to make two tests to determine that nothing has happened since
5834 * the start of the last checkpoint: current insertion point must match
5835 * the end of the last checkpoint record, and its redo pointer must point
5838 if ((flags
& (CHECKPOINT_IS_SHUTDOWN
| CHECKPOINT_FORCE
)) == 0)
5840 XLogRecPtr curInsert
;
5842 INSERT_RECPTR(curInsert
, Insert
, Insert
->curridx
);
5843 if (curInsert
.xlogid
== ControlFile
->checkPoint
.xlogid
&&
5844 curInsert
.xrecoff
== ControlFile
->checkPoint
.xrecoff
+
5845 MAXALIGN(SizeOfXLogRecord
+ sizeof(CheckPoint
)) &&
5846 ControlFile
->checkPoint
.xlogid
==
5847 ControlFile
->checkPointCopy
.redo
.xlogid
&&
5848 ControlFile
->checkPoint
.xrecoff
==
5849 ControlFile
->checkPointCopy
.redo
.xrecoff
)
5851 LWLockRelease(WALInsertLock
);
5852 LWLockRelease(CheckpointLock
);
5859 * Compute new REDO record ptr = location of next XLOG record.
5861 * NB: this is NOT necessarily where the checkpoint record itself will be,
5862 * since other backends may insert more XLOG records while we're off doing
5863 * the buffer flush work. Those XLOG records are logically after the
5864 * checkpoint, even though physically before it. Got that?
5866 freespace
= INSERT_FREESPACE(Insert
);
5867 if (freespace
< SizeOfXLogRecord
)
5869 (void) AdvanceXLInsertBuffer(false);
5870 /* OK to ignore update return flag, since we will do flush anyway */
5871 freespace
= INSERT_FREESPACE(Insert
);
5873 INSERT_RECPTR(checkPoint
.redo
, Insert
, Insert
->curridx
);
5876 * Here we update the shared RedoRecPtr for future XLogInsert calls; this
5877 * must be done while holding the insert lock AND the info_lck.
5879 * Note: if we fail to complete the checkpoint, RedoRecPtr will be left
5880 * pointing past where it really needs to point. This is okay; the only
5881 * consequence is that XLogInsert might back up whole buffers that it
5882 * didn't really need to. We can't postpone advancing RedoRecPtr because
5883 * XLogInserts that happen while we are dumping buffers must assume that
5884 * their buffer changes are not included in the checkpoint.
5887 /* use volatile pointer to prevent code rearrangement */
5888 volatile XLogCtlData
*xlogctl
= XLogCtl
;
5890 SpinLockAcquire(&xlogctl
->info_lck
);
5891 RedoRecPtr
= xlogctl
->Insert
.RedoRecPtr
= checkPoint
.redo
;
5892 SpinLockRelease(&xlogctl
->info_lck
);
5896 * Now we can release WAL insert lock, allowing other xacts to proceed
5897 * while we are flushing disk buffers.
5899 LWLockRelease(WALInsertLock
);
5902 * If enabled, log checkpoint start. We postpone this until now so as not
5903 * to log anything if we decided to skip the checkpoint.
5905 if (log_checkpoints
)
5906 LogCheckpointStart(flags
);
5908 TRACE_POSTGRESQL_CHECKPOINT_START(flags
);
5911 * Before flushing data, we must wait for any transactions that are
5912 * currently in their commit critical sections. If an xact inserted its
5913 * commit record into XLOG just before the REDO point, then a crash
5914 * restart from the REDO point would not replay that record, which means
5915 * that our flushing had better include the xact's update of pg_clog. So
5916 * we wait till he's out of his commit critical section before proceeding.
5917 * See notes in RecordTransactionCommit().
5919 * Because we've already released WALInsertLock, this test is a bit fuzzy:
5920 * it is possible that we will wait for xacts we didn't really need to
5921 * wait for. But the delay should be short and it seems better to make
5922 * checkpoint take a bit longer than to hold locks longer than necessary.
5923 * (In fact, the whole reason we have this issue is that xact.c does
5924 * commit record XLOG insertion and clog update as two separate steps
5925 * protected by different locks, but again that seems best on grounds of
5926 * minimizing lock contention.)
5928 * A transaction that has not yet set inCommit when we look cannot be at
5929 * risk, since he's not inserted his commit record yet; and one that's
5930 * already cleared it is not at risk either, since he's done fixing clog
5931 * and we will correctly flush the update below. So we cannot miss any
5932 * xacts we need to wait for.
5934 nInCommit
= GetTransactionsInCommit(&inCommitXids
);
5939 pg_usleep(10000L); /* wait for 10 msec */
5940 } while (HaveTransactionsInCommit(inCommitXids
, nInCommit
));
5942 pfree(inCommitXids
);
5945 * Get the other info we need for the checkpoint record.
5947 LWLockAcquire(XidGenLock
, LW_SHARED
);
5948 checkPoint
.nextXid
= ShmemVariableCache
->nextXid
;
5949 LWLockRelease(XidGenLock
);
5951 /* Increase XID epoch if we've wrapped around since last checkpoint */
5952 checkPoint
.nextXidEpoch
= ControlFile
->checkPointCopy
.nextXidEpoch
;
5953 if (checkPoint
.nextXid
< ControlFile
->checkPointCopy
.nextXid
)
5954 checkPoint
.nextXidEpoch
++;
5956 LWLockAcquire(OidGenLock
, LW_SHARED
);
5957 checkPoint
.nextOid
= ShmemVariableCache
->nextOid
;
5959 checkPoint
.nextOid
+= ShmemVariableCache
->oidCount
;
5960 LWLockRelease(OidGenLock
);
5962 MultiXactGetCheckptMulti(shutdown
,
5963 &checkPoint
.nextMulti
,
5964 &checkPoint
.nextMultiOffset
);
5967 * Having constructed the checkpoint record, ensure all shmem disk buffers
5968 * and commit-log buffers are flushed to disk.
5970 * This I/O could fail for various reasons. If so, we will fail to
5971 * complete the checkpoint, but there is no reason to force a system
5972 * panic. Accordingly, exit critical section while doing it.
5976 CheckPointGuts(checkPoint
.redo
, flags
);
5978 START_CRIT_SECTION();
5981 * Now insert the checkpoint record into XLOG.
5983 rdata
.data
= (char *) (&checkPoint
);
5984 rdata
.len
= sizeof(checkPoint
);
5985 rdata
.buffer
= InvalidBuffer
;
5988 recptr
= XLogInsert(RM_XLOG_ID
,
5989 shutdown
? XLOG_CHECKPOINT_SHUTDOWN
:
5990 XLOG_CHECKPOINT_ONLINE
,
5996 * We now have ProcLastRecPtr = start of actual checkpoint record, recptr
5997 * = end of actual checkpoint record.
5999 if (shutdown
&& !XLByteEQ(checkPoint
.redo
, ProcLastRecPtr
))
6001 (errmsg("concurrent transaction log activity while database system is shutting down")));
6004 * Select point at which we can truncate the log, which we base on the
6005 * prior checkpoint's earliest info.
6007 XLByteToSeg(ControlFile
->checkPointCopy
.redo
, _logId
, _logSeg
);
6010 * Update the control file.
6012 LWLockAcquire(ControlFileLock
, LW_EXCLUSIVE
);
6014 ControlFile
->state
= DB_SHUTDOWNED
;
6015 ControlFile
->prevCheckPoint
= ControlFile
->checkPoint
;
6016 ControlFile
->checkPoint
= ProcLastRecPtr
;
6017 ControlFile
->checkPointCopy
= checkPoint
;
6018 ControlFile
->time
= (pg_time_t
) time(NULL
);
6019 UpdateControlFile();
6020 LWLockRelease(ControlFileLock
);
6022 /* Update shared-memory copy of checkpoint XID/epoch */
6024 /* use volatile pointer to prevent code rearrangement */
6025 volatile XLogCtlData
*xlogctl
= XLogCtl
;
6027 SpinLockAcquire(&xlogctl
->info_lck
);
6028 xlogctl
->ckptXidEpoch
= checkPoint
.nextXidEpoch
;
6029 xlogctl
->ckptXid
= checkPoint
.nextXid
;
6030 SpinLockRelease(&xlogctl
->info_lck
);
6034 * We are now done with critical updates; no need for system panic if we
6035 * have trouble while fooling with old log segments.
6040 * Let smgr do post-checkpoint cleanup (eg, deleting old files).
6045 * Delete old log files (those no longer needed even for previous
6048 if (_logId
|| _logSeg
)
6050 PrevLogSeg(_logId
, _logSeg
);
6051 RemoveOldXlogFiles(_logId
, _logSeg
, recptr
);
6055 * Make more log segments if needed. (Do this after recycling old log
6056 * segments, since that may supply some of the needed files.)
6059 PreallocXlogFiles(recptr
);
6062 * Truncate pg_subtrans if possible. We can throw away all data before
6063 * the oldest XMIN of any running transaction. No future transaction will
6064 * attempt to reference any pg_subtrans entry older than that (see Asserts
6065 * in subtrans.c). During recovery, though, we mustn't do this because
6066 * StartupSUBTRANS hasn't been called yet.
6069 TruncateSUBTRANS(GetOldestXmin(true, false));
6071 /* All real work is done, but log before releasing lock. */
6072 if (log_checkpoints
)
6075 TRACE_POSTGRESQL_CHECKPOINT_DONE(CheckpointStats
.ckpt_bufs_written
,
6076 NBuffers
, CheckpointStats
.ckpt_segs_added
,
6077 CheckpointStats
.ckpt_segs_removed
,
6078 CheckpointStats
.ckpt_segs_recycled
);
6080 LWLockRelease(CheckpointLock
);
6084 * Flush all data in shared memory to disk, and fsync
6086 * This is the common code shared between regular checkpoints and
6087 * recovery restartpoints.
6090 CheckPointGuts(XLogRecPtr checkPointRedo
, int flags
)
6093 CheckPointSUBTRANS();
6094 CheckPointMultiXact();
6095 CheckPointBuffers(flags
); /* performs all required fsyncs */
6096 /* We deliberately delay 2PC checkpointing as long as possible */
6097 CheckPointTwoPhase(checkPointRedo
);
6101 * Set a recovery restart point if appropriate
6103 * This is similar to CreateCheckPoint, but is used during WAL recovery
6104 * to establish a point from which recovery can roll forward without
6105 * replaying the entire recovery log. This function is called each time
6106 * a checkpoint record is read from XLOG; it must determine whether a
6107 * restartpoint is needed or not.
6110 RecoveryRestartPoint(const CheckPoint
*checkPoint
)
6116 * Do nothing if the elapsed time since the last restartpoint is less than
6117 * half of checkpoint_timeout. (We use a value less than
6118 * checkpoint_timeout so that variations in the timing of checkpoints on
6119 * the master, or speed of transmission of WAL segments to a slave, won't
6120 * make the slave skip a restartpoint once it's synced with the master.)
6121 * Checking true elapsed time keeps us from doing restartpoints too often
6122 * while rapidly scanning large amounts of WAL.
6124 elapsed_secs
= (pg_time_t
) time(NULL
) - ControlFile
->time
;
6125 if (elapsed_secs
< CheckPointTimeout
/ 2)
6129 * Is it safe to checkpoint? We must ask each of the resource managers
6130 * whether they have any partial state information that might prevent a
6131 * correct restart from this point. If so, we skip this opportunity, but
6132 * return at the next checkpoint record for another try.
6134 for (rmid
= 0; rmid
<= RM_MAX_ID
; rmid
++)
6136 if (RmgrTable
[rmid
].rm_safe_restartpoint
!= NULL
)
6137 if (!(RmgrTable
[rmid
].rm_safe_restartpoint()))
6139 elog(DEBUG2
, "RM %d not safe to record restart point at %X/%X",
6141 checkPoint
->redo
.xlogid
,
6142 checkPoint
->redo
.xrecoff
);
6148 * OK, force data out to disk
6150 CheckPointGuts(checkPoint
->redo
, CHECKPOINT_IMMEDIATE
);
6153 * Update pg_control so that any subsequent crash will restart from this
6154 * checkpoint. Note: ReadRecPtr gives the XLOG address of the checkpoint
6157 ControlFile
->prevCheckPoint
= ControlFile
->checkPoint
;
6158 ControlFile
->checkPoint
= ReadRecPtr
;
6159 ControlFile
->checkPointCopy
= *checkPoint
;
6160 ControlFile
->time
= (pg_time_t
) time(NULL
);
6161 UpdateControlFile();
6163 ereport((recoveryLogRestartpoints
? LOG
: DEBUG2
),
6164 (errmsg("recovery restart point at %X/%X",
6165 checkPoint
->redo
.xlogid
, checkPoint
->redo
.xrecoff
)));
6166 if (recoveryLastXTime
)
6167 ereport((recoveryLogRestartpoints
? LOG
: DEBUG2
),
6168 (errmsg("last completed transaction was at log time %s",
6169 timestamptz_to_str(recoveryLastXTime
))));
6173 * Write a NEXTOID log record
6176 XLogPutNextOid(Oid nextOid
)
6180 rdata
.data
= (char *) (&nextOid
);
6181 rdata
.len
= sizeof(Oid
);
6182 rdata
.buffer
= InvalidBuffer
;
6184 (void) XLogInsert(RM_XLOG_ID
, XLOG_NEXTOID
, &rdata
);
6187 * We need not flush the NEXTOID record immediately, because any of the
6188 * just-allocated OIDs could only reach disk as part of a tuple insert or
6189 * update that would have its own XLOG record that must follow the NEXTOID
6190 * record. Therefore, the standard buffer LSN interlock applied to those
6191 * records will ensure no such OID reaches disk before the NEXTOID record
6194 * Note, however, that the above statement only covers state "within" the
6195 * database. When we use a generated OID as a file or directory name, we
6196 * are in a sense violating the basic WAL rule, because that filesystem
6197 * change may reach disk before the NEXTOID WAL record does. The impact
6198 * of this is that if a database crash occurs immediately afterward, we
6199 * might after restart re-generate the same OID and find that it conflicts
6200 * with the leftover file or directory. But since for safety's sake we
6201 * always loop until finding a nonconflicting filename, this poses no real
6202 * problem in practice. See pgsql-hackers discussion 27-Sep-2006.
6207 * Write an XLOG SWITCH record.
6209 * Here we just blindly issue an XLogInsert request for the record.
6210 * All the magic happens inside XLogInsert.
6212 * The return value is either the end+1 address of the switch record,
6213 * or the end+1 address of the prior segment if we did not need to
6214 * write a switch record because we are already at segment start.
6217 RequestXLogSwitch(void)
6222 /* XLOG SWITCH, alone among xlog record types, has no data */
6223 rdata
.buffer
= InvalidBuffer
;
6228 RecPtr
= XLogInsert(RM_XLOG_ID
, XLOG_SWITCH
, &rdata
);
6234 * XLOG resource manager's routines
6237 xlog_redo(XLogRecPtr lsn
, XLogRecord
*record
)
6239 uint8 info
= record
->xl_info
& ~XLR_INFO_MASK
;
6241 /* Backup blocks are not used in xlog records */
6242 Assert(!(record
->xl_info
& XLR_BKP_BLOCK_MASK
));
6244 if (info
== XLOG_NEXTOID
)
6248 memcpy(&nextOid
, XLogRecGetData(record
), sizeof(Oid
));
6249 if (ShmemVariableCache
->nextOid
< nextOid
)
6251 ShmemVariableCache
->nextOid
= nextOid
;
6252 ShmemVariableCache
->oidCount
= 0;
6255 else if (info
== XLOG_CHECKPOINT_SHUTDOWN
)
6257 CheckPoint checkPoint
;
6259 memcpy(&checkPoint
, XLogRecGetData(record
), sizeof(CheckPoint
));
6260 /* In a SHUTDOWN checkpoint, believe the counters exactly */
6261 ShmemVariableCache
->nextXid
= checkPoint
.nextXid
;
6262 ShmemVariableCache
->nextOid
= checkPoint
.nextOid
;
6263 ShmemVariableCache
->oidCount
= 0;
6264 MultiXactSetNextMXact(checkPoint
.nextMulti
,
6265 checkPoint
.nextMultiOffset
);
6267 /* ControlFile->checkPointCopy always tracks the latest ckpt XID */
6268 ControlFile
->checkPointCopy
.nextXidEpoch
= checkPoint
.nextXidEpoch
;
6269 ControlFile
->checkPointCopy
.nextXid
= checkPoint
.nextXid
;
6272 * TLI may change in a shutdown checkpoint, but it shouldn't decrease
6274 if (checkPoint
.ThisTimeLineID
!= ThisTimeLineID
)
6276 if (checkPoint
.ThisTimeLineID
< ThisTimeLineID
||
6277 !list_member_int(expectedTLIs
,
6278 (int) checkPoint
.ThisTimeLineID
))
6280 (errmsg("unexpected timeline ID %u (after %u) in checkpoint record",
6281 checkPoint
.ThisTimeLineID
, ThisTimeLineID
)));
6282 /* Following WAL records should be run with new TLI */
6283 ThisTimeLineID
= checkPoint
.ThisTimeLineID
;
6286 RecoveryRestartPoint(&checkPoint
);
6288 else if (info
== XLOG_CHECKPOINT_ONLINE
)
6290 CheckPoint checkPoint
;
6292 memcpy(&checkPoint
, XLogRecGetData(record
), sizeof(CheckPoint
));
6293 /* In an ONLINE checkpoint, treat the counters like NEXTOID */
6294 if (TransactionIdPrecedes(ShmemVariableCache
->nextXid
,
6295 checkPoint
.nextXid
))
6296 ShmemVariableCache
->nextXid
= checkPoint
.nextXid
;
6297 if (ShmemVariableCache
->nextOid
< checkPoint
.nextOid
)
6299 ShmemVariableCache
->nextOid
= checkPoint
.nextOid
;
6300 ShmemVariableCache
->oidCount
= 0;
6302 MultiXactAdvanceNextMXact(checkPoint
.nextMulti
,
6303 checkPoint
.nextMultiOffset
);
6305 /* ControlFile->checkPointCopy always tracks the latest ckpt XID */
6306 ControlFile
->checkPointCopy
.nextXidEpoch
= checkPoint
.nextXidEpoch
;
6307 ControlFile
->checkPointCopy
.nextXid
= checkPoint
.nextXid
;
6309 /* TLI should not change in an on-line checkpoint */
6310 if (checkPoint
.ThisTimeLineID
!= ThisTimeLineID
)
6312 (errmsg("unexpected timeline ID %u (should be %u) in checkpoint record",
6313 checkPoint
.ThisTimeLineID
, ThisTimeLineID
)));
6315 RecoveryRestartPoint(&checkPoint
);
6317 else if (info
== XLOG_NOOP
)
6319 /* nothing to do here */
6321 else if (info
== XLOG_SWITCH
)
6323 /* nothing to do here */
6328 xlog_desc(StringInfo buf
, uint8 xl_info
, char *rec
)
6330 uint8 info
= xl_info
& ~XLR_INFO_MASK
;
6332 if (info
== XLOG_CHECKPOINT_SHUTDOWN
||
6333 info
== XLOG_CHECKPOINT_ONLINE
)
6335 CheckPoint
*checkpoint
= (CheckPoint
*) rec
;
6337 appendStringInfo(buf
, "checkpoint: redo %X/%X; "
6338 "tli %u; xid %u/%u; oid %u; multi %u; offset %u; %s",
6339 checkpoint
->redo
.xlogid
, checkpoint
->redo
.xrecoff
,
6340 checkpoint
->ThisTimeLineID
,
6341 checkpoint
->nextXidEpoch
, checkpoint
->nextXid
,
6342 checkpoint
->nextOid
,
6343 checkpoint
->nextMulti
,
6344 checkpoint
->nextMultiOffset
,
6345 (info
== XLOG_CHECKPOINT_SHUTDOWN
) ? "shutdown" : "online");
6347 else if (info
== XLOG_NOOP
)
6349 appendStringInfo(buf
, "xlog no-op");
6351 else if (info
== XLOG_NEXTOID
)
6355 memcpy(&nextOid
, rec
, sizeof(Oid
));
6356 appendStringInfo(buf
, "nextOid: %u", nextOid
);
6358 else if (info
== XLOG_SWITCH
)
6360 appendStringInfo(buf
, "xlog switch");
6363 appendStringInfo(buf
, "UNKNOWN");
6369 xlog_outrec(StringInfo buf
, XLogRecord
*record
)
6373 appendStringInfo(buf
, "prev %X/%X; xid %u",
6374 record
->xl_prev
.xlogid
, record
->xl_prev
.xrecoff
,
6377 for (i
= 0; i
< XLR_MAX_BKP_BLOCKS
; i
++)
6379 if (record
->xl_info
& XLR_SET_BKP_BLOCK(i
))
6380 appendStringInfo(buf
, "; bkpb%d", i
+ 1);
6383 appendStringInfo(buf
, ": %s", RmgrTable
[record
->xl_rmid
].rm_name
);
6385 #endif /* WAL_DEBUG */
6389 * Return the (possible) sync flag used for opening a file, depending on the
6390 * value of the GUC wal_sync_method.
6393 get_sync_bit(int method
)
6395 /* If fsync is disabled, never open in sync mode */
6402 * enum values for all sync options are defined even if they are not
6403 * supported on the current platform. But if not, they are not
6404 * included in the enum option array, and therefore will never be seen
6407 case SYNC_METHOD_FSYNC
:
6408 case SYNC_METHOD_FSYNC_WRITETHROUGH
:
6409 case SYNC_METHOD_FDATASYNC
:
6411 #ifdef OPEN_SYNC_FLAG
6412 case SYNC_METHOD_OPEN
:
6413 return OPEN_SYNC_FLAG
;
6415 #ifdef OPEN_DATASYNC_FLAG
6416 case SYNC_METHOD_OPEN_DSYNC
:
6417 return OPEN_DATASYNC_FLAG
;
6420 /* can't happen (unless we are out of sync with option array) */
6421 elog(ERROR
, "unrecognized wal_sync_method: %d", method
);
6422 return 0; /* silence warning */
6430 assign_xlog_sync_method(int new_sync_method
, bool doit
, GucSource source
)
6435 if (sync_method
!= new_sync_method
)
6438 * To ensure that no blocks escape unsynced, force an fsync on the
6439 * currently open log segment (if any). Also, if the open flag is
6440 * changing, close the log file so it will be reopened (with new flag
6443 if (openLogFile
>= 0)
6445 if (pg_fsync(openLogFile
) != 0)
6447 (errcode_for_file_access(),
6448 errmsg("could not fsync log file %u, segment %u: %m",
6449 openLogId
, openLogSeg
)));
6450 if (get_sync_bit(sync_method
) != get_sync_bit(new_sync_method
))
6460 * Issue appropriate kind of fsync (if any) on the current XLOG output file
6463 issue_xlog_fsync(void)
6465 switch (sync_method
)
6467 case SYNC_METHOD_FSYNC
:
6468 if (pg_fsync_no_writethrough(openLogFile
) != 0)
6470 (errcode_for_file_access(),
6471 errmsg("could not fsync log file %u, segment %u: %m",
6472 openLogId
, openLogSeg
)));
6474 #ifdef HAVE_FSYNC_WRITETHROUGH
6475 case SYNC_METHOD_FSYNC_WRITETHROUGH
:
6476 if (pg_fsync_writethrough(openLogFile
) != 0)
6478 (errcode_for_file_access(),
6479 errmsg("could not fsync write-through log file %u, segment %u: %m",
6480 openLogId
, openLogSeg
)));
6483 #ifdef HAVE_FDATASYNC
6484 case SYNC_METHOD_FDATASYNC
:
6485 if (pg_fdatasync(openLogFile
) != 0)
6487 (errcode_for_file_access(),
6488 errmsg("could not fdatasync log file %u, segment %u: %m",
6489 openLogId
, openLogSeg
)));
6492 case SYNC_METHOD_OPEN
:
6493 case SYNC_METHOD_OPEN_DSYNC
:
6494 /* write synced it already */
6497 elog(PANIC
, "unrecognized wal_sync_method: %d", sync_method
);
6504 * pg_start_backup: set up for taking an on-line backup dump
6506 * Essentially what this does is to create a backup label file in $PGDATA,
6507 * where it will be archived as part of the backup dump. The label file
6508 * contains the user-supplied label string (typically this would be used
6509 * to tell where the backup dump will be stored) and the starting time and
6510 * starting WAL location for the dump.
6513 pg_start_backup(PG_FUNCTION_ARGS
)
6515 text
*backupid
= PG_GETARG_TEXT_P(0);
6517 XLogRecPtr checkpointloc
;
6518 XLogRecPtr startpoint
;
6519 pg_time_t stamp_time
;
6521 char xlogfilename
[MAXFNAMELEN
];
6524 struct stat stat_buf
;
6529 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE
),
6530 errmsg("must be superuser to run a backup")));
6532 if (!XLogArchivingActive())
6534 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE
),
6535 errmsg("WAL archiving is not active"),
6536 errhint("archive_mode must be enabled at server start.")));
6538 if (!XLogArchiveCommandSet())
6540 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE
),
6541 errmsg("WAL archiving is not active"),
6542 errhint("archive_command must be defined before "
6543 "online backups can be made safely.")));
6545 backupidstr
= text_to_cstring(backupid
);
6548 * Mark backup active in shared memory. We must do full-page WAL writes
6549 * during an on-line backup even if not doing so at other times, because
6550 * it's quite possible for the backup dump to obtain a "torn" (partially
6551 * written) copy of a database page if it reads the page concurrently with
6552 * our write to the same page. This can be fixed as long as the first
6553 * write to the page in the WAL sequence is a full-page write. Hence, we
6554 * turn on forcePageWrites and then force a CHECKPOINT, to ensure there
6555 * are no dirty pages in shared memory that might get dumped while the
6556 * backup is in progress without having a corresponding WAL record. (Once
6557 * the backup is complete, we need not force full-page writes anymore,
6558 * since we expect that any pages not modified during the backup interval
6559 * must have been correctly captured by the backup.)
6561 * We must hold WALInsertLock to change the value of forcePageWrites, to
6562 * ensure adequate interlocking against XLogInsert().
6564 LWLockAcquire(WALInsertLock
, LW_EXCLUSIVE
);
6565 if (XLogCtl
->Insert
.forcePageWrites
)
6567 LWLockRelease(WALInsertLock
);
6569 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE
),
6570 errmsg("a backup is already in progress"),
6571 errhint("Run pg_stop_backup() and try again.")));
6573 XLogCtl
->Insert
.forcePageWrites
= true;
6574 LWLockRelease(WALInsertLock
);
6576 /* Ensure we release forcePageWrites if fail below */
6577 PG_ENSURE_ERROR_CLEANUP(pg_start_backup_callback
, (Datum
) 0);
6580 * Force a CHECKPOINT. Aside from being necessary to prevent torn
6581 * page problems, this guarantees that two successive backup runs will
6582 * have different checkpoint positions and hence different history
6583 * file names, even if nothing happened in between.
6585 * We don't use CHECKPOINT_IMMEDIATE, hence this can take awhile.
6587 RequestCheckpoint(CHECKPOINT_FORCE
| CHECKPOINT_WAIT
);
6590 * Now we need to fetch the checkpoint record location, and also its
6591 * REDO pointer. The oldest point in WAL that would be needed to
6592 * restore starting from the checkpoint is precisely the REDO pointer.
6594 LWLockAcquire(ControlFileLock
, LW_EXCLUSIVE
);
6595 checkpointloc
= ControlFile
->checkPoint
;
6596 startpoint
= ControlFile
->checkPointCopy
.redo
;
6597 LWLockRelease(ControlFileLock
);
6599 XLByteToSeg(startpoint
, _logId
, _logSeg
);
6600 XLogFileName(xlogfilename
, ThisTimeLineID
, _logId
, _logSeg
);
6602 /* Use the log timezone here, not the session timezone */
6603 stamp_time
= (pg_time_t
) time(NULL
);
6604 pg_strftime(strfbuf
, sizeof(strfbuf
),
6605 "%Y-%m-%d %H:%M:%S %Z",
6606 pg_localtime(&stamp_time
, log_timezone
));
6609 * Check for existing backup label --- implies a backup is already
6610 * running. (XXX given that we checked forcePageWrites above, maybe
6611 * it would be OK to just unlink any such label file?)
6613 if (stat(BACKUP_LABEL_FILE
, &stat_buf
) != 0)
6615 if (errno
!= ENOENT
)
6617 (errcode_for_file_access(),
6618 errmsg("could not stat file \"%s\": %m",
6619 BACKUP_LABEL_FILE
)));
6623 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE
),
6624 errmsg("a backup is already in progress"),
6625 errhint("If you're sure there is no backup in progress, remove file \"%s\" and try again.",
6626 BACKUP_LABEL_FILE
)));
6629 * Okay, write the file
6631 fp
= AllocateFile(BACKUP_LABEL_FILE
, "w");
6634 (errcode_for_file_access(),
6635 errmsg("could not create file \"%s\": %m",
6636 BACKUP_LABEL_FILE
)));
6637 fprintf(fp
, "START WAL LOCATION: %X/%X (file %s)\n",
6638 startpoint
.xlogid
, startpoint
.xrecoff
, xlogfilename
);
6639 fprintf(fp
, "CHECKPOINT LOCATION: %X/%X\n",
6640 checkpointloc
.xlogid
, checkpointloc
.xrecoff
);
6641 fprintf(fp
, "START TIME: %s\n", strfbuf
);
6642 fprintf(fp
, "LABEL: %s\n", backupidstr
);
6643 if (fflush(fp
) || ferror(fp
) || FreeFile(fp
))
6645 (errcode_for_file_access(),
6646 errmsg("could not write file \"%s\": %m",
6647 BACKUP_LABEL_FILE
)));
6649 PG_END_ENSURE_ERROR_CLEANUP(pg_start_backup_callback
, (Datum
) 0);
6652 * We're done. As a convenience, return the starting WAL location.
6654 snprintf(xlogfilename
, sizeof(xlogfilename
), "%X/%X",
6655 startpoint
.xlogid
, startpoint
.xrecoff
);
6656 PG_RETURN_TEXT_P(cstring_to_text(xlogfilename
));
6659 /* Error cleanup callback for pg_start_backup */
6661 pg_start_backup_callback(int code
, Datum arg
)
6663 /* Turn off forcePageWrites on failure */
6664 LWLockAcquire(WALInsertLock
, LW_EXCLUSIVE
);
6665 XLogCtl
->Insert
.forcePageWrites
= false;
6666 LWLockRelease(WALInsertLock
);
6670 * pg_stop_backup: finish taking an on-line backup dump
6672 * We remove the backup label file created by pg_start_backup, and instead
6673 * create a backup history file in pg_xlog (whence it will immediately be
6674 * archived). The backup history file contains the same info found in
6675 * the label file, plus the backup-end time and WAL location.
6676 * Note: different from CancelBackup which just cancels online backup mode.
6679 pg_stop_backup(PG_FUNCTION_ARGS
)
6681 XLogRecPtr startpoint
;
6682 XLogRecPtr stoppoint
;
6683 pg_time_t stamp_time
;
6685 char histfilepath
[MAXPGPATH
];
6686 char startxlogfilename
[MAXFNAMELEN
];
6687 char stopxlogfilename
[MAXFNAMELEN
];
6688 char lastxlogfilename
[MAXFNAMELEN
];
6689 char histfilename
[MAXFNAMELEN
];
6696 int seconds_before_warning
;
6701 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE
),
6702 (errmsg("must be superuser to run a backup"))));
6704 if (!XLogArchivingActive())
6706 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE
),
6707 errmsg("WAL archiving is not active"),
6708 errhint("archive_mode must be enabled at server start.")));
6711 * OK to clear forcePageWrites
6713 LWLockAcquire(WALInsertLock
, LW_EXCLUSIVE
);
6714 XLogCtl
->Insert
.forcePageWrites
= false;
6715 LWLockRelease(WALInsertLock
);
6718 * Force a switch to a new xlog segment file, so that the backup is valid
6719 * as soon as archiver moves out the current segment file. We'll report
6720 * the end address of the XLOG SWITCH record as the backup stopping point.
6722 stoppoint
= RequestXLogSwitch();
6724 XLByteToSeg(stoppoint
, _logId
, _logSeg
);
6725 XLogFileName(stopxlogfilename
, ThisTimeLineID
, _logId
, _logSeg
);
6727 /* Use the log timezone here, not the session timezone */
6728 stamp_time
= (pg_time_t
) time(NULL
);
6729 pg_strftime(strfbuf
, sizeof(strfbuf
),
6730 "%Y-%m-%d %H:%M:%S %Z",
6731 pg_localtime(&stamp_time
, log_timezone
));
6734 * Open the existing label file
6736 lfp
= AllocateFile(BACKUP_LABEL_FILE
, "r");
6739 if (errno
!= ENOENT
)
6741 (errcode_for_file_access(),
6742 errmsg("could not read file \"%s\": %m",
6743 BACKUP_LABEL_FILE
)));
6745 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE
),
6746 errmsg("a backup is not in progress")));
6750 * Read and parse the START WAL LOCATION line (this code is pretty crude,
6751 * but we are not expecting any variability in the file format).
6753 if (fscanf(lfp
, "START WAL LOCATION: %X/%X (file %24s)%c",
6754 &startpoint
.xlogid
, &startpoint
.xrecoff
, startxlogfilename
,
6755 &ch
) != 4 || ch
!= '\n')
6757 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE
),
6758 errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE
)));
6761 * Write the backup history file
6763 XLByteToSeg(startpoint
, _logId
, _logSeg
);
6764 BackupHistoryFilePath(histfilepath
, ThisTimeLineID
, _logId
, _logSeg
,
6765 startpoint
.xrecoff
% XLogSegSize
);
6766 fp
= AllocateFile(histfilepath
, "w");
6769 (errcode_for_file_access(),
6770 errmsg("could not create file \"%s\": %m",
6772 fprintf(fp
, "START WAL LOCATION: %X/%X (file %s)\n",
6773 startpoint
.xlogid
, startpoint
.xrecoff
, startxlogfilename
);
6774 fprintf(fp
, "STOP WAL LOCATION: %X/%X (file %s)\n",
6775 stoppoint
.xlogid
, stoppoint
.xrecoff
, stopxlogfilename
);
6776 /* transfer remaining lines from label to history file */
6777 while ((ich
= fgetc(lfp
)) != EOF
)
6779 fprintf(fp
, "STOP TIME: %s\n", strfbuf
);
6780 if (fflush(fp
) || ferror(fp
) || FreeFile(fp
))
6782 (errcode_for_file_access(),
6783 errmsg("could not write file \"%s\": %m",
6787 * Close and remove the backup label file
6789 if (ferror(lfp
) || FreeFile(lfp
))
6791 (errcode_for_file_access(),
6792 errmsg("could not read file \"%s\": %m",
6793 BACKUP_LABEL_FILE
)));
6794 if (unlink(BACKUP_LABEL_FILE
) != 0)
6796 (errcode_for_file_access(),
6797 errmsg("could not remove file \"%s\": %m",
6798 BACKUP_LABEL_FILE
)));
6801 * Clean out any no-longer-needed history files. As a side effect, this
6802 * will post a .ready file for the newly created history file, notifying
6803 * the archiver that history file may be archived immediately.
6805 CleanupBackupHistory();
6808 * Wait until both the last WAL file filled during backup and the history
6809 * file have been archived. We assume that the alphabetic sorting
6810 * property of the WAL files ensures any earlier WAL files are safely
6813 * We wait forever, since archive_command is supposed to work and
6814 * we assume the admin wanted his backup to work completely. If you
6815 * don't wish to wait, you can set statement_timeout.
6817 XLByteToPrevSeg(stoppoint
, _logId
, _logSeg
);
6818 XLogFileName(lastxlogfilename
, ThisTimeLineID
, _logId
, _logSeg
);
6820 XLByteToSeg(startpoint
, _logId
, _logSeg
);
6821 BackupHistoryFileName(histfilename
, ThisTimeLineID
, _logId
, _logSeg
,
6822 startpoint
.xrecoff
% XLogSegSize
);
6824 seconds_before_warning
= 60;
6827 while (XLogArchiveIsBusy(lastxlogfilename
) ||
6828 XLogArchiveIsBusy(histfilename
))
6830 CHECK_FOR_INTERRUPTS();
6832 pg_usleep(1000000L);
6834 if (++waits
>= seconds_before_warning
)
6836 seconds_before_warning
*= 2; /* This wraps in >10 years... */
6838 (errmsg("pg_stop_backup still waiting for archive to complete (%d seconds elapsed)",
6844 * We're done. As a convenience, return the ending WAL location.
6846 snprintf(stopxlogfilename
, sizeof(stopxlogfilename
), "%X/%X",
6847 stoppoint
.xlogid
, stoppoint
.xrecoff
);
6848 PG_RETURN_TEXT_P(cstring_to_text(stopxlogfilename
));
6852 * pg_switch_xlog: switch to next xlog file
6855 pg_switch_xlog(PG_FUNCTION_ARGS
)
6857 XLogRecPtr switchpoint
;
6858 char location
[MAXFNAMELEN
];
6862 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE
),
6863 (errmsg("must be superuser to switch transaction log files"))));
6865 switchpoint
= RequestXLogSwitch();
6868 * As a convenience, return the WAL location of the switch record
6870 snprintf(location
, sizeof(location
), "%X/%X",
6871 switchpoint
.xlogid
, switchpoint
.xrecoff
);
6872 PG_RETURN_TEXT_P(cstring_to_text(location
));
6876 * Report the current WAL write location (same format as pg_start_backup etc)
6878 * This is useful for determining how much of WAL is visible to an external
6879 * archiving process. Note that the data before this point is written out
6880 * to the kernel, but is not necessarily synced to disk.
6883 pg_current_xlog_location(PG_FUNCTION_ARGS
)
6885 char location
[MAXFNAMELEN
];
6887 /* Make sure we have an up-to-date local LogwrtResult */
6889 /* use volatile pointer to prevent code rearrangement */
6890 volatile XLogCtlData
*xlogctl
= XLogCtl
;
6892 SpinLockAcquire(&xlogctl
->info_lck
);
6893 LogwrtResult
= xlogctl
->LogwrtResult
;
6894 SpinLockRelease(&xlogctl
->info_lck
);
6897 snprintf(location
, sizeof(location
), "%X/%X",
6898 LogwrtResult
.Write
.xlogid
, LogwrtResult
.Write
.xrecoff
);
6899 PG_RETURN_TEXT_P(cstring_to_text(location
));
6903 * Report the current WAL insert location (same format as pg_start_backup etc)
6905 * This function is mostly for debugging purposes.
6908 pg_current_xlog_insert_location(PG_FUNCTION_ARGS
)
6910 XLogCtlInsert
*Insert
= &XLogCtl
->Insert
;
6911 XLogRecPtr current_recptr
;
6912 char location
[MAXFNAMELEN
];
6915 * Get the current end-of-WAL position ... shared lock is sufficient
6917 LWLockAcquire(WALInsertLock
, LW_SHARED
);
6918 INSERT_RECPTR(current_recptr
, Insert
, Insert
->curridx
);
6919 LWLockRelease(WALInsertLock
);
6921 snprintf(location
, sizeof(location
), "%X/%X",
6922 current_recptr
.xlogid
, current_recptr
.xrecoff
);
6923 PG_RETURN_TEXT_P(cstring_to_text(location
));
6927 * Compute an xlog file name and decimal byte offset given a WAL location,
6928 * such as is returned by pg_stop_backup() or pg_xlog_switch().
6930 * Note that a location exactly at a segment boundary is taken to be in
6931 * the previous segment. This is usually the right thing, since the
6932 * expected usage is to determine which xlog file(s) are ready to archive.
6935 pg_xlogfile_name_offset(PG_FUNCTION_ARGS
)
6937 text
*location
= PG_GETARG_TEXT_P(0);
6939 unsigned int uxlogid
;
6940 unsigned int uxrecoff
;
6944 XLogRecPtr locationpoint
;
6945 char xlogfilename
[MAXFNAMELEN
];
6948 TupleDesc resultTupleDesc
;
6949 HeapTuple resultHeapTuple
;
6953 * Read input and parse
6955 locationstr
= text_to_cstring(location
);
6957 if (sscanf(locationstr
, "%X/%X", &uxlogid
, &uxrecoff
) != 2)
6959 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
6960 errmsg("could not parse transaction log location \"%s\"",
6963 locationpoint
.xlogid
= uxlogid
;
6964 locationpoint
.xrecoff
= uxrecoff
;
6967 * Construct a tuple descriptor for the result row. This must match this
6968 * function's pg_proc entry!
6970 resultTupleDesc
= CreateTemplateTupleDesc(2, false);
6971 TupleDescInitEntry(resultTupleDesc
, (AttrNumber
) 1, "file_name",
6973 TupleDescInitEntry(resultTupleDesc
, (AttrNumber
) 2, "file_offset",
6976 resultTupleDesc
= BlessTupleDesc(resultTupleDesc
);
6981 XLByteToPrevSeg(locationpoint
, xlogid
, xlogseg
);
6982 XLogFileName(xlogfilename
, ThisTimeLineID
, xlogid
, xlogseg
);
6984 values
[0] = CStringGetTextDatum(xlogfilename
);
6990 xrecoff
= locationpoint
.xrecoff
- xlogseg
* XLogSegSize
;
6992 values
[1] = UInt32GetDatum(xrecoff
);
6996 * Tuple jam: Having first prepared your Datums, then squash together
6998 resultHeapTuple
= heap_form_tuple(resultTupleDesc
, values
, isnull
);
7000 result
= HeapTupleGetDatum(resultHeapTuple
);
7002 PG_RETURN_DATUM(result
);
7006 * Compute an xlog file name given a WAL location,
7007 * such as is returned by pg_stop_backup() or pg_xlog_switch().
7010 pg_xlogfile_name(PG_FUNCTION_ARGS
)
7012 text
*location
= PG_GETARG_TEXT_P(0);
7014 unsigned int uxlogid
;
7015 unsigned int uxrecoff
;
7018 XLogRecPtr locationpoint
;
7019 char xlogfilename
[MAXFNAMELEN
];
7021 locationstr
= text_to_cstring(location
);
7023 if (sscanf(locationstr
, "%X/%X", &uxlogid
, &uxrecoff
) != 2)
7025 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
7026 errmsg("could not parse transaction log location \"%s\"",
7029 locationpoint
.xlogid
= uxlogid
;
7030 locationpoint
.xrecoff
= uxrecoff
;
7032 XLByteToPrevSeg(locationpoint
, xlogid
, xlogseg
);
7033 XLogFileName(xlogfilename
, ThisTimeLineID
, xlogid
, xlogseg
);
7035 PG_RETURN_TEXT_P(cstring_to_text(xlogfilename
));
7039 * read_backup_label: check to see if a backup_label file is present
7041 * If we see a backup_label during recovery, we assume that we are recovering
7042 * from a backup dump file, and we therefore roll forward from the checkpoint
7043 * identified by the label file, NOT what pg_control says. This avoids the
7044 * problem that pg_control might have been archived one or more checkpoints
7045 * later than the start of the dump, and so if we rely on it as the start
7046 * point, we will fail to restore a consistent database state.
7048 * We also attempt to retrieve the corresponding backup history file.
7049 * If successful, set *minRecoveryLoc to constrain valid PITR stopping
7052 * Returns TRUE if a backup_label was found (and fills the checkpoint
7053 * location into *checkPointLoc); returns FALSE if not.
7056 read_backup_label(XLogRecPtr
*checkPointLoc
, XLogRecPtr
*minRecoveryLoc
)
7058 XLogRecPtr startpoint
;
7059 XLogRecPtr stoppoint
;
7060 char histfilename
[MAXFNAMELEN
];
7061 char histfilepath
[MAXPGPATH
];
7062 char startxlogfilename
[MAXFNAMELEN
];
7063 char stopxlogfilename
[MAXFNAMELEN
];
7071 /* Default is to not constrain recovery stop point */
7072 minRecoveryLoc
->xlogid
= 0;
7073 minRecoveryLoc
->xrecoff
= 0;
7076 * See if label file is present
7078 lfp
= AllocateFile(BACKUP_LABEL_FILE
, "r");
7081 if (errno
!= ENOENT
)
7083 (errcode_for_file_access(),
7084 errmsg("could not read file \"%s\": %m",
7085 BACKUP_LABEL_FILE
)));
7086 return false; /* it's not there, all is fine */
7090 * Read and parse the START WAL LOCATION and CHECKPOINT lines (this code
7091 * is pretty crude, but we are not expecting any variability in the file
7094 if (fscanf(lfp
, "START WAL LOCATION: %X/%X (file %08X%16s)%c",
7095 &startpoint
.xlogid
, &startpoint
.xrecoff
, &tli
,
7096 startxlogfilename
, &ch
) != 5 || ch
!= '\n')
7098 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE
),
7099 errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE
)));
7100 if (fscanf(lfp
, "CHECKPOINT LOCATION: %X/%X%c",
7101 &checkPointLoc
->xlogid
, &checkPointLoc
->xrecoff
,
7102 &ch
) != 3 || ch
!= '\n')
7104 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE
),
7105 errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE
)));
7106 if (ferror(lfp
) || FreeFile(lfp
))
7108 (errcode_for_file_access(),
7109 errmsg("could not read file \"%s\": %m",
7110 BACKUP_LABEL_FILE
)));
7113 * Try to retrieve the backup history file (no error if we can't)
7115 XLByteToSeg(startpoint
, _logId
, _logSeg
);
7116 BackupHistoryFileName(histfilename
, tli
, _logId
, _logSeg
,
7117 startpoint
.xrecoff
% XLogSegSize
);
7119 if (InArchiveRecovery
)
7120 RestoreArchivedFile(histfilepath
, histfilename
, "RECOVERYHISTORY", 0);
7122 BackupHistoryFilePath(histfilepath
, tli
, _logId
, _logSeg
,
7123 startpoint
.xrecoff
% XLogSegSize
);
7125 fp
= AllocateFile(histfilepath
, "r");
7129 * Parse history file to identify stop point.
7131 if (fscanf(fp
, "START WAL LOCATION: %X/%X (file %24s)%c",
7132 &startpoint
.xlogid
, &startpoint
.xrecoff
, startxlogfilename
,
7133 &ch
) != 4 || ch
!= '\n')
7135 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE
),
7136 errmsg("invalid data in file \"%s\"", histfilename
)));
7137 if (fscanf(fp
, "STOP WAL LOCATION: %X/%X (file %24s)%c",
7138 &stoppoint
.xlogid
, &stoppoint
.xrecoff
, stopxlogfilename
,
7139 &ch
) != 4 || ch
!= '\n')
7141 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE
),
7142 errmsg("invalid data in file \"%s\"", histfilename
)));
7143 *minRecoveryLoc
= stoppoint
;
7144 if (ferror(fp
) || FreeFile(fp
))
7146 (errcode_for_file_access(),
7147 errmsg("could not read file \"%s\": %m",
7155 * Error context callback for errors occurring during rm_redo().
7158 rm_redo_error_callback(void *arg
)
7160 XLogRecord
*record
= (XLogRecord
*) arg
;
7163 initStringInfo(&buf
);
7164 RmgrTable
[record
->xl_rmid
].rm_desc(&buf
,
7166 XLogRecGetData(record
));
7168 /* don't bother emitting empty description */
7170 errcontext("xlog redo %s", buf
.data
);
7176 * BackupInProgress: check if online backup mode is active
7178 * This is done by checking for existence of the "backup_label" file.
7181 BackupInProgress(void)
7183 struct stat stat_buf
;
7185 return (stat(BACKUP_LABEL_FILE
, &stat_buf
) == 0);
7189 * CancelBackup: rename the "backup_label" file to cancel backup mode
7191 * If the "backup_label" file exists, it will be renamed to "backup_label.old".
7192 * Note that this will render an online backup in progress useless.
7193 * To correctly finish an online backup, pg_stop_backup must be called.
7198 struct stat stat_buf
;
7200 /* if the file is not there, return */
7201 if (stat(BACKUP_LABEL_FILE
, &stat_buf
) < 0)
7204 /* remove leftover file from previously cancelled backup if it exists */
7205 unlink(BACKUP_LABEL_OLD
);
7207 if (rename(BACKUP_LABEL_FILE
, BACKUP_LABEL_OLD
) == 0)
7210 (errmsg("online backup mode cancelled"),
7211 errdetail("\"%s\" was renamed to \"%s\".",
7212 BACKUP_LABEL_FILE
, BACKUP_LABEL_OLD
)));
7217 (errcode_for_file_access(),
7218 errmsg("online backup mode was not cancelled"),
7219 errdetail("Could not rename \"%s\" to \"%s\": %m.",
7220 BACKUP_LABEL_FILE
, BACKUP_LABEL_OLD
)));