xlog.c 196.5 KB
Newer Older
1
/*-------------------------------------------------------------------------
2 3
 *
 * xlog.c
4
 *		PostgreSQL transaction log manager
5 6
 *
 *
7
 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
8
 * Portions Copyright (c) 1994, Regents of the University of California
9
 *
10
 * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.260 2007/01/05 22:19:23 momjian Exp $
11 12 13
 *
 *-------------------------------------------------------------------------
 */
14

15 16
#include "postgres.h"

17
#include <ctype.h>
18
#include <fcntl.h>
T
Tom Lane 已提交
19
#include <signal.h>
20
#include <time.h>
21
#include <sys/stat.h>
22
#include <sys/time.h>
23 24
#include <sys/wait.h>
#include <unistd.h>
25

26
#include "access/clog.h"
27
#include "access/heapam.h"
28
#include "access/multixact.h"
29
#include "access/subtrans.h"
30
#include "access/transam.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"
T
Tom Lane 已提交
36
#include "catalog/pg_control.h"
37 38
#include "catalog/pg_type.h"
#include "funcapi.h"
39
#include "miscadmin.h"
40
#include "pgstat.h"
41
#include "postmaster/bgwriter.h"
42
#include "storage/bufpage.h"
43
#include "storage/fd.h"
44
#include "storage/pmsignal.h"
45
#include "storage/procarray.h"
46
#include "storage/spin.h"
47
#include "utils/builtins.h"
48
#include "utils/nabstime.h"
49
#include "utils/pg_locale.h"
V
WAL  
Vadim B. Mikheev 已提交
50

51

B
 
Bruce Momjian 已提交
52
/*
53
 *	Because O_DIRECT bypasses the kernel buffers, and because we never
B
 
Bruce Momjian 已提交
54
 *	read those buffers except during crash recovery, it is a win to use
B
Bruce Momjian 已提交
55
 *	it in all cases where we sync on each write().	We could allow O_DIRECT
B
 
Bruce Momjian 已提交
56 57 58
 *	with fsync(), but because skipping the kernel buffer forces writes out
 *	quickly, it seems best just to use it for O_SYNC.  It is hard to imagine
 *	how fsync() could be a win for O_DIRECT compared to O_SYNC and O_DIRECT.
B
Bruce Momjian 已提交
59 60
 *	Also, O_DIRECT is never enough to force data to the drives, it merely
 *	tries to bypass the kernel cache, so we still need O_SYNC or fsync().
B
 
Bruce Momjian 已提交
61 62 63 64 65 66 67
 */
#ifdef O_DIRECT
#define PG_O_DIRECT				O_DIRECT
#else
#define PG_O_DIRECT				0
#endif

68 69 70
/*
 * This chunk of hackery attempts to determine which file sync methods
 * are available on the current platform, and to choose an appropriate
B
Bruce Momjian 已提交
71
 * default method.	We assume that fsync() is always available, and that
72 73
 * configure determined whether fdatasync() is.
 */
74 75
#if defined(O_SYNC)
#define BARE_OPEN_SYNC_FLAG		O_SYNC
76
#elif defined(O_FSYNC)
77
#define BARE_OPEN_SYNC_FLAG		O_FSYNC
B
Bruce Momjian 已提交
78
#endif
79 80
#ifdef BARE_OPEN_SYNC_FLAG
#define OPEN_SYNC_FLAG			(BARE_OPEN_SYNC_FLAG | PG_O_DIRECT)
81
#endif
82

83 84
#if defined(O_DSYNC)
#if defined(OPEN_SYNC_FLAG)
85
/* O_DSYNC is distinct? */
86
#if O_DSYNC != BARE_OPEN_SYNC_FLAG
B
 
Bruce Momjian 已提交
87
#define OPEN_DATASYNC_FLAG		(O_DSYNC | PG_O_DIRECT)
88
#endif
B
Bruce Momjian 已提交
89
#else							/* !defined(OPEN_SYNC_FLAG) */
90
/* Win32 only has O_DSYNC */
B
 
Bruce Momjian 已提交
91
#define OPEN_DATASYNC_FLAG		(O_DSYNC | PG_O_DIRECT)
B
Bruce Momjian 已提交
92
#endif
93 94
#endif

95
#if defined(OPEN_DATASYNC_FLAG)
B
Bruce Momjian 已提交
96
#define DEFAULT_SYNC_METHOD_STR "open_datasync"
B
Bruce Momjian 已提交
97 98
#define DEFAULT_SYNC_METHOD		SYNC_METHOD_OPEN
#define DEFAULT_SYNC_FLAGBIT	OPEN_DATASYNC_FLAG
99
#elif defined(HAVE_FDATASYNC)
B
Bruce Momjian 已提交
100 101 102
#define DEFAULT_SYNC_METHOD_STR "fdatasync"
#define DEFAULT_SYNC_METHOD		SYNC_METHOD_FDATASYNC
#define DEFAULT_SYNC_FLAGBIT	0
103
#elif defined(HAVE_FSYNC_WRITETHROUGH_ONLY)
B
Bruce Momjian 已提交
104 105 106
#define DEFAULT_SYNC_METHOD_STR "fsync_writethrough"
#define DEFAULT_SYNC_METHOD		SYNC_METHOD_FSYNC_WRITETHROUGH
#define DEFAULT_SYNC_FLAGBIT	0
107 108 109 110
#else
#define DEFAULT_SYNC_METHOD_STR "fsync"
#define DEFAULT_SYNC_METHOD		SYNC_METHOD_FSYNC
#define DEFAULT_SYNC_FLAGBIT	0
B
Bruce Momjian 已提交
111
#endif
112 113


114 115
/*
 * Limitation of buffer-alignment for direct IO depends on OS and filesystem,
116
 * but XLOG_BLCKSZ is assumed to be enough for it.
117 118
 */
#ifdef O_DIRECT
119
#define ALIGNOF_XLOG_BUFFER		XLOG_BLCKSZ
120 121 122 123 124
#else
#define ALIGNOF_XLOG_BUFFER		ALIGNOF_BUFFER
#endif


125 126
/* File path names (all relative to $PGDATA) */
#define BACKUP_LABEL_FILE		"backup_label"
127
#define BACKUP_LABEL_OLD		"backup_label.old"
128 129 130 131
#define RECOVERY_COMMAND_FILE	"recovery.conf"
#define RECOVERY_COMMAND_DONE	"recovery.done"


T
Tom Lane 已提交
132 133
/* User-settable parameters */
int			CheckPointSegments = 3;
V
Vadim B. Mikheev 已提交
134
int			XLOGbuffers = 8;
135
int			XLogArchiveTimeout = 0;
136
char	   *XLogArchiveCommand = NULL;
137 138
char	   *XLOG_sync_method = NULL;
const char	XLOG_sync_method_default[] = DEFAULT_SYNC_METHOD_STR;
139
bool		fullPageWrites = true;
T
Tom Lane 已提交
140

141 142 143 144
#ifdef WAL_DEBUG
bool		XLOG_DEBUG = false;
#endif

145
/*
146
 * XLOGfileslop is used in the code as the allowed "fuzz" in the number of
147
 * preallocated XLOG segments --- we try to have at least XLOGfiles advance
B
Bruce Momjian 已提交
148
 * segments but no more than XLOGfileslop segments.  This could
149 150 151 152 153 154 155 156 157 158
 * be made a separate GUC variable, but at present I think it's sufficient
 * to hardwire it as 2*CheckPointSegments+1.  Under normal conditions, a
 * checkpoint will free no more than 2*CheckPointSegments log segments, and
 * we want to recycle all of them; the +1 allows boundary cases to happen
 * without wasting a delete/create-segment cycle.
 */

#define XLOGfileslop	(2*CheckPointSegments + 1)


159
/* these are derived from XLOG_sync_method by assign_xlog_sync_method */
B
Bruce Momjian 已提交
160
int			sync_method = DEFAULT_SYNC_METHOD;
161 162 163 164
static int	open_sync_bit = DEFAULT_SYNC_FLAGBIT;

#define XLOG_SYNC_BIT  (enableFsync ? open_sync_bit : 0)

T
Tom Lane 已提交
165 166

/*
167 168
 * ThisTimeLineID will be same in all backends --- it identifies current
 * WAL timeline for the database system.
T
Tom Lane 已提交
169
 */
170
TimeLineID	ThisTimeLineID = 0;
V
WAL  
Vadim B. Mikheev 已提交
171

172
/* Are we doing recovery from XLOG? */
T
Tom Lane 已提交
173
bool		InRecovery = false;
B
Bruce Momjian 已提交
174

175
/* Are we recovering using offline XLOG archives? */
B
Bruce Momjian 已提交
176 177
static bool InArchiveRecovery = false;

178
/* Was the last xlog file restored from archive, or local? */
B
Bruce Momjian 已提交
179
static bool restoredFromArchive = false;
180

181 182
/* options taken from recovery.conf */
static char *recoveryRestoreCommand = NULL;
183 184 185
static bool recoveryTarget = false;
static bool recoveryTargetExact = false;
static bool recoveryTargetInclusive = true;
B
Bruce Momjian 已提交
186 187
static TransactionId recoveryTargetXid;
static time_t recoveryTargetTime;
188

189
/* if recoveryStopsHere returns true, it saves actual stop xid/time here */
B
Bruce Momjian 已提交
190 191 192
static TransactionId recoveryStopXid;
static time_t recoveryStopTime;
static bool recoveryStopAfter;
193 194 195 196 197 198 199 200 201 202 203 204 205

/*
 * During normal operation, the only timeline we care about is ThisTimeLineID.
 * During recovery, however, things are more complicated.  To simplify life
 * for rmgr code, we keep ThisTimeLineID set to the "current" timeline as we
 * scan through the WAL history (that is, it is the line that was active when
 * the currently-scanned WAL record was generated).  We also need these
 * timeline values:
 *
 * recoveryTargetTLI: the desired timeline that we want to end in.
 *
 * expectedTLIs: an integer list of recoveryTargetTLI and the TLIs of
 * its known parents, newest first (so recoveryTargetTLI is always the
B
Bruce Momjian 已提交
206
 * first list member).	Only these TLIs are expected to be seen in the WAL
207 208 209 210 211 212 213 214 215
 * segments we read, and indeed only these TLIs will be considered as
 * candidate WAL files to open at all.
 *
 * curFileTLI: the TLI appearing in the name of the current input WAL file.
 * (This is not necessarily the same as ThisTimeLineID, because we could
 * be scanning data that was copied from an ancestor timeline when the current
 * file was created.)  During a sequential scan we do not allow this value
 * to decrease.
 */
B
Bruce Momjian 已提交
216 217 218
static TimeLineID recoveryTargetTLI;
static List *expectedTLIs;
static TimeLineID curFileTLI;
219

T
Tom Lane 已提交
220 221
/*
 * MyLastRecPtr points to the start of the last XLOG record inserted by the
222 223
 * current transaction.  If MyLastRecPtr.xrecoff == 0, then the current
 * xact hasn't yet inserted any transaction-controlled XLOG records.
T
Tom Lane 已提交
224 225
 *
 * Note that XLOG records inserted outside transaction control are not
226
 * reflected into MyLastRecPtr.  They do, however, cause MyXactMadeXLogEntry
B
Bruce Momjian 已提交
227
 * to be set true.	The latter can be used to test whether the current xact
228 229
 * made any loggable changes (including out-of-xact changes, such as
 * sequence updates).
230 231 232
 *
 * When we insert/update/delete a tuple in a temporary relation, we do not
 * make any XLOG record, since we don't care about recovering the state of
B
Bruce Momjian 已提交
233
 * the temp rel after a crash.	However, we will still need to remember
234 235 236
 * whether our transaction committed or aborted in that case.  So, we must
 * set MyXactMadeTempRelUpdate true to indicate that the XID will be of
 * interest later.
T
Tom Lane 已提交
237 238
 */
XLogRecPtr	MyLastRecPtr = {0, 0};
V
Vadim B. Mikheev 已提交
239

240 241
bool		MyXactMadeXLogEntry = false;

242 243
bool		MyXactMadeTempRelUpdate = false;

T
Tom Lane 已提交
244 245 246
/*
 * ProcLastRecPtr points to the start of the last XLOG record inserted by the
 * current backend.  It is updated for all inserts, transaction-controlled
B
Bruce Momjian 已提交
247
 * or not.	ProcLastRecEnd is similar but points to end+1 of last record.
T
Tom Lane 已提交
248 249
 */
static XLogRecPtr ProcLastRecPtr = {0, 0};
250

251 252
XLogRecPtr	ProcLastRecEnd = {0, 0};

T
Tom Lane 已提交
253 254 255
/*
 * RedoRecPtr is this backend's local copy of the REDO record pointer
 * (which is almost but not quite the same as a pointer to the most recent
B
Bruce Momjian 已提交
256
 * CHECKPOINT record).	We update this from the shared-memory copy,
T
Tom Lane 已提交
257
 * XLogCtl->Insert.RedoRecPtr, whenever we can safely do so (ie, when we
B
Bruce Momjian 已提交
258
 * hold the Insert lock).  See XLogInsert for details.	We are also allowed
259
 * to update from XLogCtl->Insert.RedoRecPtr if we hold the info_lck;
260 261
 * see GetRedoRecPtr.  A freshly spawned backend obtains the value during
 * InitXLOGAccess.
T
Tom Lane 已提交
262
 */
263
static XLogRecPtr RedoRecPtr;
264

T
Tom Lane 已提交
265 266 267 268 269 270 271 272 273
/*----------
 * Shared-memory data structures for XLOG control
 *
 * LogwrtRqst indicates a byte position that we need to write and/or fsync
 * the log up to (all records before that point must be written or fsynced).
 * LogwrtResult indicates the byte positions we have already written/fsynced.
 * These structs are identical but are declared separately to indicate their
 * slightly different functions.
 *
274
 * We do a lot of pushups to minimize the amount of access to lockable
T
Tom Lane 已提交
275 276 277
 * shared memory values.  There are actually three shared-memory copies of
 * LogwrtResult, plus one unshared copy in each backend.  Here's how it works:
 *		XLogCtl->LogwrtResult is protected by info_lck
278 279 280 281
 *		XLogCtl->Write.LogwrtResult is protected by WALWriteLock
 *		XLogCtl->Insert.LogwrtResult is protected by WALInsertLock
 * One must hold the associated lock to read or write any of these, but
 * of course no lock is needed to read/write the unshared LogwrtResult.
T
Tom Lane 已提交
282 283 284
 *
 * XLogCtl->LogwrtResult and XLogCtl->Write.LogwrtResult are both "always
 * right", since both are updated by a write or flush operation before
285 286
 * it releases WALWriteLock.  The point of keeping XLogCtl->Write.LogwrtResult
 * is that it can be examined/modified by code that already holds WALWriteLock
T
Tom Lane 已提交
287 288 289
 * without needing to grab info_lck as well.
 *
 * XLogCtl->Insert.LogwrtResult may lag behind the reality of the other two,
B
Bruce Momjian 已提交
290
 * but is updated when convenient.	Again, it exists for the convenience of
291
 * code that is already holding WALInsertLock but not the other locks.
T
Tom Lane 已提交
292 293 294 295 296 297 298 299 300 301
 *
 * The unshared LogwrtResult may lag behind any or all of these, and again
 * is updated when convenient.
 *
 * The request bookkeeping is simpler: there is a shared XLogCtl->LogwrtRqst
 * (protected by info_lck), but we don't need to cache any copies of it.
 *
 * Note that this all works because the request and result positions can only
 * advance forward, never back up, and so we can easily determine which of two
 * values is "more up to date".
302 303 304 305 306 307 308 309 310 311 312 313 314 315
 *
 * info_lck is only held long enough to read/update the protected variables,
 * so it's a plain spinlock.  The other locks are held longer (potentially
 * over I/O operations), so we use LWLocks for them.  These locks are:
 *
 * WALInsertLock: must be held to insert a record into the WAL buffers.
 *
 * WALWriteLock: must be held to write WAL buffers to disk (XLogWrite or
 * XLogFlush).
 *
 * ControlFileLock: must be held to read/update control file or create
 * new log file.
 *
 * CheckpointLock: must be held to do a checkpoint (ensures only one
316 317
 * checkpointer at a time; currently, with all checkpoints done by the
 * bgwriter, this is just pro forma).
318
 *
T
Tom Lane 已提交
319 320
 *----------
 */
321

T
Tom Lane 已提交
322
typedef struct XLogwrtRqst
323
{
T
Tom Lane 已提交
324 325
	XLogRecPtr	Write;			/* last byte + 1 to write out */
	XLogRecPtr	Flush;			/* last byte + 1 to flush */
326
} XLogwrtRqst;
327

328 329 330 331 332 333
typedef struct XLogwrtResult
{
	XLogRecPtr	Write;			/* last byte + 1 written out */
	XLogRecPtr	Flush;			/* last byte + 1 flushed */
} XLogwrtResult;

T
Tom Lane 已提交
334 335 336
/*
 * Shared state data for XLogInsert.
 */
337 338
typedef struct XLogCtlInsert
{
B
Bruce Momjian 已提交
339 340
	XLogwrtResult LogwrtResult; /* a recent value of LogwrtResult */
	XLogRecPtr	PrevRecord;		/* start of previously-inserted record */
341
	int			curridx;		/* current block index in cache */
B
Bruce Momjian 已提交
342 343 344
	XLogPageHeader currpage;	/* points to header of block in cache */
	char	   *currpos;		/* current insertion point in cache */
	XLogRecPtr	RedoRecPtr;		/* current redo point for insertions */
345
	bool		forcePageWrites;	/* forcing full-page writes for PITR? */
346 347
} XLogCtlInsert;

T
Tom Lane 已提交
348 349 350
/*
 * Shared state data for XLogWrite/XLogFlush.
 */
351 352
typedef struct XLogCtlWrite
{
B
Bruce Momjian 已提交
353 354 355
	XLogwrtResult LogwrtResult; /* current value of LogwrtResult */
	int			curridx;		/* cache index of next block to write */
	time_t		lastSegSwitchTime;		/* time of last xlog segment switch */
356 357
} XLogCtlWrite;

T
Tom Lane 已提交
358 359 360
/*
 * Total shared-memory state for XLOG.
 */
361 362
typedef struct XLogCtlData
{
363
	/* Protected by WALInsertLock: */
B
Bruce Momjian 已提交
364
	XLogCtlInsert Insert;
365

T
Tom Lane 已提交
366
	/* Protected by info_lck: */
B
Bruce Momjian 已提交
367 368
	XLogwrtRqst LogwrtRqst;
	XLogwrtResult LogwrtResult;
369 370 371
	uint32		ckptXidEpoch;	/* nextXID & epoch of latest checkpoint */
	TransactionId ckptXid;

372
	/* Protected by WALWriteLock: */
B
Bruce Momjian 已提交
373 374
	XLogCtlWrite Write;

T
Tom Lane 已提交
375
	/*
B
Bruce Momjian 已提交
376 377 378
	 * These values do not change after startup, although the pointed-to pages
	 * and xlblocks values certainly do.  Permission to read/write the pages
	 * and xlblocks values depends on WALInsertLock and WALWriteLock.
T
Tom Lane 已提交
379
	 */
B
Bruce Momjian 已提交
380
	char	   *pages;			/* buffers for unwritten XLOG pages */
381
	XLogRecPtr *xlblocks;		/* 1st byte ptr-s + XLOG_BLCKSZ */
382 383
	Size		XLogCacheByte;	/* # bytes in xlog buffers */
	int			XLogCacheBlck;	/* highest allocated xlog buffer index */
384
	TimeLineID	ThisTimeLineID;
T
Tom Lane 已提交
385

386
	slock_t		info_lck;		/* locks shared variables shown above */
387 388
} XLogCtlData;

389
static XLogCtlData *XLogCtl = NULL;
390

391
/*
T
Tom Lane 已提交
392
 * We maintain an image of pg_control in shared memory.
393
 */
394
static ControlFileData *ControlFile = NULL;
395

T
Tom Lane 已提交
396 397 398 399 400
/*
 * Macros for managing XLogInsert state.  In most cases, the calling routine
 * has local copies of XLogCtl->Insert and/or XLogCtl->Insert->curridx,
 * so these are passed as parameters instead of being fetched via XLogCtl.
 */
401

T
Tom Lane 已提交
402 403
/* Free space remaining in the current xlog page buffer */
#define INSERT_FREESPACE(Insert)  \
404
	(XLOG_BLCKSZ - ((Insert)->currpos - (char *) (Insert)->currpage))
T
Tom Lane 已提交
405 406 407 408 409 410

/* Construct XLogRecPtr value for current insertion point */
#define INSERT_RECPTR(recptr,Insert,curridx)  \
	( \
	  (recptr).xlogid = XLogCtl->xlblocks[curridx].xlogid, \
	  (recptr).xrecoff = \
B
Bruce Momjian 已提交
411
		XLogCtl->xlblocks[curridx].xrecoff - INSERT_FREESPACE(Insert) \
T
Tom Lane 已提交
412 413 414 415 416 417 418
	)

#define PrevBufIdx(idx)		\
		(((idx) == 0) ? XLogCtl->XLogCacheBlck : ((idx) - 1))

#define NextBufIdx(idx)		\
		(((idx) == XLogCtl->XLogCacheBlck) ? 0 : ((idx) + 1))
419

T
Tom Lane 已提交
420 421 422 423
/*
 * Private, possibly out-of-date copy of shared LogwrtResult.
 * See discussion above.
 */
424
static XLogwrtResult LogwrtResult = {{0, 0}, {0, 0}};
425

T
Tom Lane 已提交
426 427 428 429 430 431 432 433 434 435
/*
 * openLogFile is -1 or a kernel FD for an open log file segment.
 * When it's open, openLogOff is the current seek offset in the file.
 * openLogId/openLogSeg identify the segment.  These variables are only
 * used to write the XLOG, and so will normally refer to the active segment.
 */
static int	openLogFile = -1;
static uint32 openLogId = 0;
static uint32 openLogSeg = 0;
static uint32 openLogOff = 0;
436

T
Tom Lane 已提交
437 438 439 440 441 442
/*
 * These variables are used similarly to the ones above, but for reading
 * the XLOG.  Note, however, that readOff generally represents the offset
 * of the page just read, not the seek position of the FD itself, which
 * will be just past that page.
 */
443 444 445 446
static int	readFile = -1;
static uint32 readId = 0;
static uint32 readSeg = 0;
static uint32 readOff = 0;
B
Bruce Momjian 已提交
447

448
/* Buffer for currently read page (XLOG_BLCKSZ bytes) */
T
Tom Lane 已提交
449
static char *readBuf = NULL;
B
Bruce Momjian 已提交
450

451 452 453 454
/* Buffer for current ReadRecord result (expandable) */
static char *readRecordBuf = NULL;
static uint32 readRecordBufSize = 0;

T
Tom Lane 已提交
455
/* State information for XLOG reading */
B
Bruce Momjian 已提交
456 457
static XLogRecPtr ReadRecPtr;	/* start of last record read */
static XLogRecPtr EndRecPtr;	/* end+1 of last record read */
458
static XLogRecord *nextRecord = NULL;
459
static TimeLineID lastPageTLI = 0;
460

V
WAL  
Vadim B. Mikheev 已提交
461 462
static bool InRedo = false;

463

464 465
static void XLogArchiveNotify(const char *xlog);
static void XLogArchiveNotifySeg(uint32 log, uint32 seg);
466
static bool XLogArchiveCheckDone(const char *xlog);
467 468
static void XLogArchiveCleanup(const char *xlog);
static void readRecoveryCommandFile(void);
469
static void exitArchiveRecovery(TimeLineID endTLI,
B
Bruce Momjian 已提交
470
					uint32 endLogId, uint32 endLogSeg);
471
static bool recoveryStopsHere(XLogRecord *record, bool *includeThis);
472
static void CheckPointGuts(XLogRecPtr checkPointRedo);
T
Tom Lane 已提交
473

474
static bool XLogCheckBuffer(XLogRecData *rdata, bool doPageWrites,
B
Bruce Momjian 已提交
475
				XLogRecPtr *lsn, BkpBlock *bkpb);
476 477
static bool AdvanceXLInsertBuffer(bool new_segment);
static void XLogWrite(XLogwrtRqst WriteRqst, bool flexible, bool xlog_switch);
B
Bruce Momjian 已提交
478 479
static int XLogFileInit(uint32 log, uint32 seg,
			 bool *use_existent, bool use_lock);
480 481
static bool InstallXLogFileSegment(uint32 *log, uint32 *seg, char *tmppath,
					   bool find_free, int *max_advance,
482
					   bool use_lock);
483 484
static int	XLogFileOpen(uint32 log, uint32 seg);
static int	XLogFileRead(uint32 log, uint32 seg, int emode);
B
Bruce Momjian 已提交
485
static void XLogFileClose(void);
486
static bool RestoreArchivedFile(char *path, const char *xlogfname,
B
Bruce Momjian 已提交
487
					const char *recovername, off_t expectedSize);
488 489
static int	PreallocXlogFiles(XLogRecPtr endptr);
static void MoveOfflineLogs(uint32 log, uint32 seg, XLogRecPtr endptr,
B
Bruce Momjian 已提交
490
				int *nsegsremoved, int *nsegsrecycled);
491
static void CleanupBackupHistory(void);
492
static XLogRecord *ReadRecord(XLogRecPtr *RecPtr, int emode);
493
static bool ValidXLOGHeader(XLogPageHeader hdr, int emode);
494
static XLogRecord *ReadCheckpointRecord(XLogRecPtr RecPtr, int whichChkpt);
495 496 497 498
static List *readTimeLineHistory(TimeLineID targetTLI);
static bool existsTimeLineHistory(TimeLineID probeTLI);
static TimeLineID findNewestTimeLine(TimeLineID startTLI);
static void writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI,
B
Bruce Momjian 已提交
499 500
					 TimeLineID endTLI,
					 uint32 endLogId, uint32 endLogSeg);
T
Tom Lane 已提交
501 502 503
static void WriteControlFile(void);
static void ReadControlFile(void);
static char *str_time(time_t tnow);
504
static void issue_xlog_fsync(void);
B
Bruce Momjian 已提交
505

506
#ifdef WAL_DEBUG
507
static void xlog_outrec(StringInfo buf, XLogRecord *record);
508
#endif
509
static bool read_backup_label(XLogRecPtr *checkPointLoc,
B
Bruce Momjian 已提交
510
				  XLogRecPtr *minRecoveryLoc);
511
static void rm_redo_error_callback(void *arg);
T
Tom Lane 已提交
512 513 514 515 516


/*
 * Insert an XLOG record having the specified RMID and info bytes,
 * with the body of the record being the data chunk(s) described by
517
 * the rdata chain (see xlog.h for notes about rdata).
T
Tom Lane 已提交
518 519 520 521 522 523 524 525 526 527 528
 *
 * Returns XLOG pointer to end of record (beginning of next record).
 * This can be used as LSN for data pages affected by the logged action.
 * (LSN is the XLOG point up to which the XLOG must be flushed to disk
 * before the data page can be written out.  This implements the basic
 * WAL rule "write the log before the data".)
 *
 * NB: this routine feels free to scribble on the XLogRecData structs,
 * though not on the data they reference.  This is OK since the XLogRecData
 * structs are always just temporaries in the calling code.
 */
529
XLogRecPtr
530
XLogInsert(RmgrId rmid, uint8 info, XLogRecData *rdata)
531
{
B
Bruce Momjian 已提交
532 533
	XLogCtlInsert *Insert = &XLogCtl->Insert;
	XLogRecord *record;
T
Tom Lane 已提交
534
	XLogContRecord *contrecord;
B
Bruce Momjian 已提交
535 536 537
	XLogRecPtr	RecPtr;
	XLogRecPtr	WriteRqst;
	uint32		freespace;
538
	int			curridx;
B
Bruce Momjian 已提交
539 540 541 542 543
	XLogRecData *rdt;
	Buffer		dtbuf[XLR_MAX_BKP_BLOCKS];
	bool		dtbuf_bkp[XLR_MAX_BKP_BLOCKS];
	BkpBlock	dtbuf_xlg[XLR_MAX_BKP_BLOCKS];
	XLogRecPtr	dtbuf_lsn[XLR_MAX_BKP_BLOCKS];
544 545 546 547
	XLogRecData dtbuf_rdt1[XLR_MAX_BKP_BLOCKS];
	XLogRecData dtbuf_rdt2[XLR_MAX_BKP_BLOCKS];
	XLogRecData dtbuf_rdt3[XLR_MAX_BKP_BLOCKS];
	pg_crc32	rdata_crc;
B
Bruce Momjian 已提交
548 549 550
	uint32		len,
				write_len;
	unsigned	i;
551
	XLogwrtRqst LogwrtRqst;
B
Bruce Momjian 已提交
552
	bool		updrqst;
553
	bool		doPageWrites;
554 555
	bool		isLogSwitch = (rmid == RM_XLOG_ID && info == XLOG_SWITCH);
	bool		no_tran = (rmid == RM_XLOG_ID);
V
Vadim B. Mikheev 已提交
556 557 558 559

	if (info & XLR_INFO_MASK)
	{
		if ((info & XLR_INFO_MASK) != XLOG_NO_TRAN)
560
			elog(PANIC, "invalid xlog info mask %02X", (info & XLR_INFO_MASK));
V
Vadim B. Mikheev 已提交
561 562 563 564
		no_tran = true;
		info &= ~XLR_INFO_MASK;
	}

T
Tom Lane 已提交
565
	/*
B
Bruce Momjian 已提交
566 567
	 * In bootstrap mode, we don't actually log anything but XLOG resources;
	 * return a phony record pointer.
T
Tom Lane 已提交
568
	 */
V
Vadim B. Mikheev 已提交
569
	if (IsBootstrapProcessingMode() && rmid != RM_XLOG_ID)
V
WAL  
Vadim B. Mikheev 已提交
570 571
	{
		RecPtr.xlogid = 0;
B
Bruce Momjian 已提交
572
		RecPtr.xrecoff = SizeOfXLogLongPHD;		/* start of 1st chkpt record */
573
		return RecPtr;
V
WAL  
Vadim B. Mikheev 已提交
574 575
	}

T
Tom Lane 已提交
576
	/*
577
	 * Here we scan the rdata chain, determine which buffers must be backed
T
Tom Lane 已提交
578
	 * up, and compute the CRC values for the data.  Note that the record
B
Bruce Momjian 已提交
579 580 581 582
	 * header isn't added into the CRC initially since we don't know the final
	 * length or info bits quite yet.  Thus, the CRC will represent the CRC of
	 * the whole record in the order "rdata, then backup blocks, then record
	 * header".
T
Tom Lane 已提交
583
	 *
584 585 586 587 588
	 * We may have to loop back to here if a race condition is detected below.
	 * We could prevent the race by doing all this work while holding the
	 * insert lock, but it seems better to avoid doing CRC calculations while
	 * holding the lock.  This means we have to be careful about modifying the
	 * rdata chain until we know we aren't going to loop back again.  The only
B
Bruce Momjian 已提交
589 590 591 592 593
	 * change we allow ourselves to make earlier is to set rdt->data = NULL in
	 * chain items we have decided we will have to back up the whole buffer
	 * for.  This is OK because we will certainly decide the same thing again
	 * for those items if we do it over; doing it here saves an extra pass
	 * over the chain later.
T
Tom Lane 已提交
594
	 */
595
begin:;
T
Tom Lane 已提交
596 597 598 599 600 601
	for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
	{
		dtbuf[i] = InvalidBuffer;
		dtbuf_bkp[i] = false;
	}

602 603 604 605 606 607 608 609
	/*
	 * Decide if we need to do full-page writes in this XLOG record: true if
	 * full_page_writes is on or we have a PITR request for it.  Since we
	 * don't yet have the insert lock, forcePageWrites could change under us,
	 * but we'll recheck it once we have the lock.
	 */
	doPageWrites = fullPageWrites || Insert->forcePageWrites;

610
	INIT_CRC32(rdata_crc);
T
Tom Lane 已提交
611
	len = 0;
B
Bruce Momjian 已提交
612
	for (rdt = rdata;;)
613 614 615
	{
		if (rdt->buffer == InvalidBuffer)
		{
T
Tom Lane 已提交
616
			/* Simple data, just include it */
617
			len += rdt->len;
618
			COMP_CRC32(rdata_crc, rdt->data, rdt->len);
619
		}
T
Tom Lane 已提交
620
		else
621
		{
T
Tom Lane 已提交
622 623
			/* Find info for buffer */
			for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
624
			{
T
Tom Lane 已提交
625
				if (rdt->buffer == dtbuf[i])
626
				{
627
					/* Buffer already referenced by earlier chain item */
T
Tom Lane 已提交
628 629 630 631 632
					if (dtbuf_bkp[i])
						rdt->data = NULL;
					else if (rdt->data)
					{
						len += rdt->len;
633
						COMP_CRC32(rdata_crc, rdt->data, rdt->len);
T
Tom Lane 已提交
634 635
					}
					break;
636
				}
T
Tom Lane 已提交
637
				if (dtbuf[i] == InvalidBuffer)
638
				{
T
Tom Lane 已提交
639 640
					/* OK, put it in this slot */
					dtbuf[i] = rdt->buffer;
641 642
					if (XLogCheckBuffer(rdt, doPageWrites,
										&(dtbuf_lsn[i]), &(dtbuf_xlg[i])))
T
Tom Lane 已提交
643 644 645 646 647 648 649
					{
						dtbuf_bkp[i] = true;
						rdt->data = NULL;
					}
					else if (rdt->data)
					{
						len += rdt->len;
650
						COMP_CRC32(rdata_crc, rdt->data, rdt->len);
T
Tom Lane 已提交
651 652
					}
					break;
653 654
				}
			}
T
Tom Lane 已提交
655
			if (i >= XLR_MAX_BKP_BLOCKS)
656
				elog(PANIC, "can backup at most %d blocks per xlog record",
T
Tom Lane 已提交
657
					 XLR_MAX_BKP_BLOCKS);
658
		}
659
		/* Break out of loop when rdt points to last chain item */
660 661 662 663 664
		if (rdt->next == NULL)
			break;
		rdt = rdt->next;
	}

665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
	/*
	 * Now add the backup block headers and data into the CRC
	 */
	for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
	{
		if (dtbuf_bkp[i])
		{
			BkpBlock   *bkpb = &(dtbuf_xlg[i]);
			char	   *page;

			COMP_CRC32(rdata_crc,
					   (char *) bkpb,
					   sizeof(BkpBlock));
			page = (char *) BufferGetBlock(dtbuf[i]);
			if (bkpb->hole_length == 0)
			{
				COMP_CRC32(rdata_crc,
						   page,
						   BLCKSZ);
			}
			else
			{
				/* must skip the hole */
				COMP_CRC32(rdata_crc,
						   page,
						   bkpb->hole_offset);
				COMP_CRC32(rdata_crc,
						   page + (bkpb->hole_offset + bkpb->hole_length),
						   BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
			}
		}
	}

T
Tom Lane 已提交
698
	/*
699 700
	 * NOTE: We disallow len == 0 because it provides a useful bit of extra
	 * error checking in ReadRecord.  This means that all callers of
B
Bruce Momjian 已提交
701 702 703
	 * XLogInsert must supply at least some not-in-a-buffer data.  However, we
	 * make an exception for XLOG SWITCH records because we don't want them to
	 * ever cross a segment boundary.
T
Tom Lane 已提交
704
	 */
705
	if (len == 0 && !isLogSwitch)
706
		elog(PANIC, "invalid xlog record length %u", len);
707

708
	START_CRIT_SECTION();
709

710
	/* update LogwrtResult before doing cache fill check */
711 712 713 714
	{
		/* use volatile pointer to prevent code rearrangement */
		volatile XLogCtlData *xlogctl = XLogCtl;

715
		SpinLockAcquire(&xlogctl->info_lck);
716 717
		LogwrtRqst = xlogctl->LogwrtRqst;
		LogwrtResult = xlogctl->LogwrtResult;
718
		SpinLockRelease(&xlogctl->info_lck);
719
	}
720

721
	/*
722 723
	 * If cache is half filled then try to acquire write lock and do
	 * XLogWrite. Ignore any fractional blocks in performing this check.
724
	 */
725
	LogwrtRqst.Write.xrecoff -= LogwrtRqst.Write.xrecoff % XLOG_BLCKSZ;
726 727 728
	if (LogwrtRqst.Write.xlogid != LogwrtResult.Write.xlogid ||
		(LogwrtRqst.Write.xrecoff >= LogwrtResult.Write.xrecoff +
		 XLogCtl->XLogCacheByte / 2))
T
Tom Lane 已提交
729
	{
730
		if (LWLockConditionalAcquire(WALWriteLock, LW_EXCLUSIVE))
731
		{
732 733 734 735
			/*
			 * Since the amount of data we write here is completely optional
			 * anyway, tell XLogWrite it can be "flexible" and stop at a
			 * convenient boundary.  This allows writes triggered by this
B
Bruce Momjian 已提交
736 737 738
			 * mechanism to synchronize with the cache boundaries, so that in
			 * a long transaction we'll basically dump alternating halves of
			 * the buffer array.
739
			 */
740 741
			LogwrtResult = XLogCtl->Write.LogwrtResult;
			if (XLByteLT(LogwrtResult.Write, LogwrtRqst.Write))
742
				XLogWrite(LogwrtRqst, true, false);
743
			LWLockRelease(WALWriteLock);
744 745 746
		}
	}

747 748 749
	/* Now wait to get insert lock */
	LWLockAcquire(WALInsertLock, LW_EXCLUSIVE);

T
Tom Lane 已提交
750
	/*
B
Bruce Momjian 已提交
751 752 753
	 * Check to see if my RedoRecPtr is out of date.  If so, may have to go
	 * back and recompute everything.  This can only happen just after a
	 * checkpoint, so it's better to be slow in this case and fast otherwise.
754 755
	 *
	 * If we aren't doing full-page writes then RedoRecPtr doesn't actually
B
Bruce Momjian 已提交
756 757
	 * affect the contents of the XLOG record, so we'll update our local copy
	 * but not force a recomputation.
T
Tom Lane 已提交
758 759
	 */
	if (!XLByteEQ(RedoRecPtr, Insert->RedoRecPtr))
760
	{
T
Tom Lane 已提交
761 762 763
		Assert(XLByteLT(RedoRecPtr, Insert->RedoRecPtr));
		RedoRecPtr = Insert->RedoRecPtr;

764
		if (doPageWrites)
765
		{
766
			for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
T
Tom Lane 已提交
767
			{
768 769 770 771 772 773 774 775 776 777 778 779 780
				if (dtbuf[i] == InvalidBuffer)
					continue;
				if (dtbuf_bkp[i] == false &&
					XLByteLE(dtbuf_lsn[i], RedoRecPtr))
				{
					/*
					 * Oops, this buffer now needs to be backed up, but we
					 * didn't think so above.  Start over.
					 */
					LWLockRelease(WALInsertLock);
					END_CRIT_SECTION();
					goto begin;
				}
T
Tom Lane 已提交
781
			}
782 783 784
		}
	}

785
	/*
B
Bruce Momjian 已提交
786 787 788 789
	 * Also check to see if forcePageWrites was just turned on; if we weren't
	 * already doing full-page writes then go back and recompute. (If it was
	 * just turned off, we could recompute the record without full pages, but
	 * we choose not to bother.)
790 791 792 793 794 795 796 797 798
	 */
	if (Insert->forcePageWrites && !doPageWrites)
	{
		/* Oops, must redo it with full-page data */
		LWLockRelease(WALInsertLock);
		END_CRIT_SECTION();
		goto begin;
	}

T
Tom Lane 已提交
799
	/*
B
Bruce Momjian 已提交
800 801 802 803
	 * Make additional rdata chain entries for the backup blocks, so that we
	 * don't need to special-case them in the write loop.  Note that we have
	 * now irrevocably changed the input rdata chain.  At the exit of this
	 * loop, write_len includes the backup block data.
T
Tom Lane 已提交
804
	 *
805 806 807
	 * Also set the appropriate info bits to show which buffers were backed
	 * up. The i'th XLR_SET_BKP_BLOCK bit corresponds to the i'th distinct
	 * buffer value (ignoring InvalidBuffer) appearing in the rdata chain.
T
Tom Lane 已提交
808 809 810
	 */
	write_len = len;
	for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
811
	{
812 813 814
		BkpBlock   *bkpb;
		char	   *page;

815
		if (!dtbuf_bkp[i])
816 817
			continue;

T
Tom Lane 已提交
818
		info |= XLR_SET_BKP_BLOCK(i);
819

820 821 822 823 824
		bkpb = &(dtbuf_xlg[i]);
		page = (char *) BufferGetBlock(dtbuf[i]);

		rdt->next = &(dtbuf_rdt1[i]);
		rdt = rdt->next;
825

826 827
		rdt->data = (char *) bkpb;
		rdt->len = sizeof(BkpBlock);
T
Tom Lane 已提交
828
		write_len += sizeof(BkpBlock);
829

830 831
		rdt->next = &(dtbuf_rdt2[i]);
		rdt = rdt->next;
832

833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
		if (bkpb->hole_length == 0)
		{
			rdt->data = page;
			rdt->len = BLCKSZ;
			write_len += BLCKSZ;
			rdt->next = NULL;
		}
		else
		{
			/* must skip the hole */
			rdt->data = page;
			rdt->len = bkpb->hole_offset;
			write_len += bkpb->hole_offset;

			rdt->next = &(dtbuf_rdt3[i]);
			rdt = rdt->next;

			rdt->data = page + (bkpb->hole_offset + bkpb->hole_length);
			rdt->len = BLCKSZ - (bkpb->hole_offset + bkpb->hole_length);
			write_len += rdt->len;
			rdt->next = NULL;
		}
855 856
	}

857
	/*
858
	 * If there isn't enough space on the current XLOG page for a record
B
Bruce Momjian 已提交
859
	 * header, advance to the next page (leaving the unused space as zeroes).
860
	 */
T
Tom Lane 已提交
861 862
	updrqst = false;
	freespace = INSERT_FREESPACE(Insert);
863 864
	if (freespace < SizeOfXLogRecord)
	{
865
		updrqst = AdvanceXLInsertBuffer(false);
866 867 868
		freespace = INSERT_FREESPACE(Insert);
	}

869
	/* Compute record's XLOG location */
T
Tom Lane 已提交
870
	curridx = Insert->curridx;
871 872 873
	INSERT_RECPTR(RecPtr, Insert, curridx);

	/*
B
Bruce Momjian 已提交
874 875 876 877 878
	 * If the record is an XLOG_SWITCH, and we are exactly at the start of a
	 * segment, we need not insert it (and don't want to because we'd like
	 * consecutive switch requests to be no-ops).  Instead, make sure
	 * everything is written and flushed through the end of the prior segment,
	 * and return the prior segment's end address.
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
	 */
	if (isLogSwitch &&
		(RecPtr.xrecoff % XLogSegSize) == SizeOfXLogLongPHD)
	{
		/* We can release insert lock immediately */
		LWLockRelease(WALInsertLock);

		RecPtr.xrecoff -= SizeOfXLogLongPHD;
		if (RecPtr.xrecoff == 0)
		{
			/* crossing a logid boundary */
			RecPtr.xlogid -= 1;
			RecPtr.xrecoff = XLogFileSize;
		}

		LWLockAcquire(WALWriteLock, LW_EXCLUSIVE);
		LogwrtResult = XLogCtl->Write.LogwrtResult;
		if (!XLByteLE(RecPtr, LogwrtResult.Flush))
		{
			XLogwrtRqst FlushRqst;

			FlushRqst.Write = RecPtr;
			FlushRqst.Flush = RecPtr;
			XLogWrite(FlushRqst, false, false);
		}
		LWLockRelease(WALWriteLock);

		END_CRIT_SECTION();

		return RecPtr;
	}
T
Tom Lane 已提交
910

911 912
	/* Insert record header */

913
	record = (XLogRecord *) Insert->currpos;
914
	record->xl_prev = Insert->PrevRecord;
915
	record->xl_xid = GetCurrentTransactionIdIfAny();
916
	record->xl_tot_len = SizeOfXLogRecord + write_len;
T
Tom Lane 已提交
917
	record->xl_len = len;		/* doesn't include backup blocks */
918
	record->xl_info = info;
919
	record->xl_rmid = rmid;
920

921 922 923 924
	/* Now we can finish computing the record's CRC */
	COMP_CRC32(rdata_crc, (char *) record + sizeof(pg_crc32),
			   SizeOfXLogRecord - sizeof(pg_crc32));
	FIN_CRC32(rdata_crc);
925 926
	record->xl_crc = rdata_crc;

927
#ifdef WAL_DEBUG
V
WAL  
Vadim B. Mikheev 已提交
928 929
	if (XLOG_DEBUG)
	{
B
Bruce Momjian 已提交
930
		StringInfoData buf;
V
WAL  
Vadim B. Mikheev 已提交
931

932
		initStringInfo(&buf);
933 934
		appendStringInfo(&buf, "INSERT @ %X/%X: ",
						 RecPtr.xlogid, RecPtr.xrecoff);
935
		xlog_outrec(&buf, record);
936
		if (rdata->data != NULL)
V
WAL  
Vadim B. Mikheev 已提交
937
		{
938 939
			appendStringInfo(&buf, " - ");
			RmgrTable[record->xl_rmid].rm_desc(&buf, record->xl_info, rdata->data);
V
WAL  
Vadim B. Mikheev 已提交
940
		}
941 942
		elog(LOG, "%s", buf.data);
		pfree(buf.data);
V
WAL  
Vadim B. Mikheev 已提交
943
	}
944
#endif
V
WAL  
Vadim B. Mikheev 已提交
945

T
Tom Lane 已提交
946 947 948 949 950
	/* Record begin of record in appropriate places */
	if (!no_tran)
		MyLastRecPtr = RecPtr;
	ProcLastRecPtr = RecPtr;
	Insert->PrevRecord = RecPtr;
951
	MyXactMadeXLogEntry = true;
T
Tom Lane 已提交
952

953
	Insert->currpos += SizeOfXLogRecord;
T
Tom Lane 已提交
954
	freespace -= SizeOfXLogRecord;
955

T
Tom Lane 已提交
956 957 958 959
	/*
	 * Append the data, including backup blocks if any
	 */
	while (write_len)
960
	{
961 962 963 964
		while (rdata->data == NULL)
			rdata = rdata->next;

		if (freespace > 0)
965
		{
966 967 968 969 970
			if (rdata->len > freespace)
			{
				memcpy(Insert->currpos, rdata->data, freespace);
				rdata->data += freespace;
				rdata->len -= freespace;
T
Tom Lane 已提交
971
				write_len -= freespace;
972 973 974 975 976
			}
			else
			{
				memcpy(Insert->currpos, rdata->data, rdata->len);
				freespace -= rdata->len;
T
Tom Lane 已提交
977
				write_len -= rdata->len;
978 979 980 981
				Insert->currpos += rdata->len;
				rdata = rdata->next;
				continue;
			}
982 983
		}

984
		/* Use next buffer */
985
		updrqst = AdvanceXLInsertBuffer(false);
T
Tom Lane 已提交
986 987 988 989 990 991
		curridx = Insert->curridx;
		/* Insert cont-record header */
		Insert->currpage->xlp_info |= XLP_FIRST_IS_CONTRECORD;
		contrecord = (XLogContRecord *) Insert->currpos;
		contrecord->xl_rem_len = write_len;
		Insert->currpos += SizeOfXLogContRecord;
992
		freespace = INSERT_FREESPACE(Insert);
993
	}
994

T
Tom Lane 已提交
995 996
	/* Ensure next record will be properly aligned */
	Insert->currpos = (char *) Insert->currpage +
B
Bruce Momjian 已提交
997
		MAXALIGN(Insert->currpos - (char *) Insert->currpage);
T
Tom Lane 已提交
998
	freespace = INSERT_FREESPACE(Insert);
999

V
Vadim B. Mikheev 已提交
1000
	/*
B
Bruce Momjian 已提交
1001 1002
	 * The recptr I return is the beginning of the *next* record. This will be
	 * stored as LSN for changed data pages...
V
Vadim B. Mikheev 已提交
1003
	 */
T
Tom Lane 已提交
1004
	INSERT_RECPTR(RecPtr, Insert, curridx);
V
Vadim B. Mikheev 已提交
1005

1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
	/*
	 * If the record is an XLOG_SWITCH, we must now write and flush all the
	 * existing data, and then forcibly advance to the start of the next
	 * segment.  It's not good to do this I/O while holding the insert lock,
	 * but there seems too much risk of confusion if we try to release the
	 * lock sooner.  Fortunately xlog switch needn't be a high-performance
	 * operation anyway...
	 */
	if (isLogSwitch)
	{
		XLogCtlWrite *Write = &XLogCtl->Write;
		XLogwrtRqst FlushRqst;
		XLogRecPtr	OldSegEnd;

		LWLockAcquire(WALWriteLock, LW_EXCLUSIVE);

		/*
B
Bruce Momjian 已提交
1023 1024
		 * Flush through the end of the page containing XLOG_SWITCH, and
		 * perform end-of-segment actions (eg, notifying archiver).
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
		 */
		WriteRqst = XLogCtl->xlblocks[curridx];
		FlushRqst.Write = WriteRqst;
		FlushRqst.Flush = WriteRqst;
		XLogWrite(FlushRqst, false, true);

		/* Set up the next buffer as first page of next segment */
		/* Note: AdvanceXLInsertBuffer cannot need to do I/O here */
		(void) AdvanceXLInsertBuffer(true);

		/* There should be no unwritten data */
		curridx = Insert->curridx;
		Assert(curridx == Write->curridx);

		/* Compute end address of old segment */
		OldSegEnd = XLogCtl->xlblocks[curridx];
		OldSegEnd.xrecoff -= XLOG_BLCKSZ;
		if (OldSegEnd.xrecoff == 0)
		{
			/* crossing a logid boundary */
			OldSegEnd.xlogid -= 1;
			OldSegEnd.xrecoff = XLogFileSize;
		}

		/* Make it look like we've written and synced all of old segment */
		LogwrtResult.Write = OldSegEnd;
		LogwrtResult.Flush = OldSegEnd;

		/*
		 * Update shared-memory status --- this code should match XLogWrite
		 */
		{
			/* use volatile pointer to prevent code rearrangement */
			volatile XLogCtlData *xlogctl = XLogCtl;

			SpinLockAcquire(&xlogctl->info_lck);
			xlogctl->LogwrtResult = LogwrtResult;
			if (XLByteLT(xlogctl->LogwrtRqst.Write, LogwrtResult.Write))
				xlogctl->LogwrtRqst.Write = LogwrtResult.Write;
			if (XLByteLT(xlogctl->LogwrtRqst.Flush, LogwrtResult.Flush))
				xlogctl->LogwrtRqst.Flush = LogwrtResult.Flush;
			SpinLockRelease(&xlogctl->info_lck);
		}

		Write->LogwrtResult = LogwrtResult;

		LWLockRelease(WALWriteLock);

		updrqst = false;		/* done already */
	}
1075
	else
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
	{
		/* normal case, ie not xlog switch */

		/* Need to update shared LogwrtRqst if some block was filled up */
		if (freespace < SizeOfXLogRecord)
		{
			/* curridx is filled and available for writing out */
			updrqst = true;
		}
		else
		{
			/* if updrqst already set, write through end of previous buf */
			curridx = PrevBufIdx(curridx);
		}
		WriteRqst = XLogCtl->xlblocks[curridx];
	}
1092

1093
	LWLockRelease(WALInsertLock);
1094 1095 1096

	if (updrqst)
	{
1097 1098 1099
		/* use volatile pointer to prevent code rearrangement */
		volatile XLogCtlData *xlogctl = XLogCtl;

1100
		SpinLockAcquire(&xlogctl->info_lck);
T
Tom Lane 已提交
1101
		/* advance global request to include new block(s) */
1102 1103
		if (XLByteLT(xlogctl->LogwrtRqst.Write, WriteRqst))
			xlogctl->LogwrtRqst.Write = WriteRqst;
T
Tom Lane 已提交
1104
		/* update local result copy while I have the chance */
1105
		LogwrtResult = xlogctl->LogwrtResult;
1106
		SpinLockRelease(&xlogctl->info_lck);
1107 1108
	}

1109 1110
	ProcLastRecEnd = RecPtr;

1111
	END_CRIT_SECTION();
1112

1113
	return RecPtr;
1114
}
1115

1116
/*
1117 1118 1119
 * Determine whether the buffer referenced by an XLogRecData item has to
 * be backed up, and if so fill a BkpBlock struct for it.  In any case
 * save the buffer's LSN at *lsn.
1120
 */
1121
static bool
1122
XLogCheckBuffer(XLogRecData *rdata, bool doPageWrites,
1123
				XLogRecPtr *lsn, BkpBlock *bkpb)
1124 1125
{
	PageHeader	page;
1126 1127 1128 1129

	page = (PageHeader) BufferGetBlock(rdata->buffer);

	/*
B
Bruce Momjian 已提交
1130 1131 1132
	 * XXX We assume page LSN is first data on *every* page that can be passed
	 * to XLogInsert, whether it otherwise has the standard page layout or
	 * not.
1133 1134 1135
	 */
	*lsn = page->pd_lsn;

1136
	if (doPageWrites &&
1137
		XLByteLE(page->pd_lsn, RedoRecPtr))
1138
	{
1139 1140 1141 1142 1143
		/*
		 * The page needs to be backed up, so set up *bkpb
		 */
		bkpb->node = BufferGetFileNode(rdata->buffer);
		bkpb->block = BufferGetBlockNumber(rdata->buffer);
1144

1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
		if (rdata->buffer_std)
		{
			/* Assume we can omit data between pd_lower and pd_upper */
			uint16		lower = page->pd_lower;
			uint16		upper = page->pd_upper;

			if (lower >= SizeOfPageHeaderData &&
				upper > lower &&
				upper <= BLCKSZ)
			{
				bkpb->hole_offset = lower;
				bkpb->hole_length = upper - lower;
			}
			else
			{
				/* No "hole" to compress out */
				bkpb->hole_offset = 0;
				bkpb->hole_length = 0;
			}
		}
		else
		{
			/* Not a standard page header, don't try to eliminate "hole" */
			bkpb->hole_offset = 0;
			bkpb->hole_length = 0;
		}
1171

1172
		return true;			/* buffer requires backup */
1173
	}
1174 1175

	return false;				/* buffer does not need to be backed up */
1176 1177
}

1178 1179 1180 1181 1182 1183
/*
 * XLogArchiveNotify
 *
 * Create an archive notification file
 *
 * The name of the notification file is the message that will be picked up
1184
 * by the archiver, e.g. we write 0000000100000001000000C6.ready
1185
 * and the archiver then knows to archive XLOGDIR/0000000100000001000000C6,
1186
 * then when complete, rename it to 0000000100000001000000C6.done
1187 1188 1189 1190 1191
 */
static void
XLogArchiveNotify(const char *xlog)
{
	char		archiveStatusPath[MAXPGPATH];
B
Bruce Momjian 已提交
1192
	FILE	   *fd;
1193 1194 1195 1196

	/* insert an otherwise empty file called <XLOG>.ready */
	StatusFilePath(archiveStatusPath, xlog, ".ready");
	fd = AllocateFile(archiveStatusPath, "w");
B
Bruce Momjian 已提交
1197 1198
	if (fd == NULL)
	{
1199 1200 1201 1202 1203 1204
		ereport(LOG,
				(errcode_for_file_access(),
				 errmsg("could not create archive status file \"%s\": %m",
						archiveStatusPath)));
		return;
	}
B
Bruce Momjian 已提交
1205 1206
	if (FreeFile(fd))
	{
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
		ereport(LOG,
				(errcode_for_file_access(),
				 errmsg("could not write archive status file \"%s\": %m",
						archiveStatusPath)));
		return;
	}

	/* Notify archiver that it's got something to do */
	if (IsUnderPostmaster)
		SendPostmasterSignal(PMSIGNAL_WAKEN_ARCHIVER);
}

/*
 * Convenience routine to notify using log/seg representation of filename
 */
static void
XLogArchiveNotifySeg(uint32 log, uint32 seg)
{
	char		xlog[MAXFNAMELEN];

1227
	XLogFileName(xlog, ThisTimeLineID, log, seg);
1228 1229 1230 1231
	XLogArchiveNotify(xlog);
}

/*
1232
 * XLogArchiveCheckDone
1233
 *
1234 1235 1236 1237
 * This is called when we are ready to delete or recycle an old XLOG segment
 * file or backup history file.  If it is okay to delete it then return true.
 * If it is not time to delete it, make sure a .ready file exists, and return
 * false.
1238 1239
 *
 * If <XLOG>.done exists, then return true; else if <XLOG>.ready exists,
1240 1241 1242 1243
 * then return false; else create <XLOG>.ready and return false.
 *
 * The reason we do things this way is so that if the original attempt to
 * create <XLOG>.ready fails, we'll retry during subsequent checkpoints.
1244 1245
 */
static bool
1246
XLogArchiveCheckDone(const char *xlog)
1247 1248 1249 1250
{
	char		archiveStatusPath[MAXPGPATH];
	struct stat stat_buf;

1251 1252 1253 1254 1255
	/* Always deletable if archiving is off */
	if (!XLogArchivingActive())
		return true;

	/* First check for .done --- this means archiver is done with it */
1256 1257 1258 1259 1260 1261 1262
	StatusFilePath(archiveStatusPath, xlog, ".done");
	if (stat(archiveStatusPath, &stat_buf) == 0)
		return true;

	/* check for .ready --- this means archiver is still busy with it */
	StatusFilePath(archiveStatusPath, xlog, ".ready");
	if (stat(archiveStatusPath, &stat_buf) == 0)
B
Bruce Momjian 已提交
1263
		return false;
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277

	/* Race condition --- maybe archiver just finished, so recheck */
	StatusFilePath(archiveStatusPath, xlog, ".done");
	if (stat(archiveStatusPath, &stat_buf) == 0)
		return true;

	/* Retry creation of the .ready file */
	XLogArchiveNotify(xlog);
	return false;
}

/*
 * XLogArchiveCleanup
 *
1278
 * Cleanup archive notification file(s) for a particular xlog segment
1279 1280 1281 1282
 */
static void
XLogArchiveCleanup(const char *xlog)
{
B
Bruce Momjian 已提交
1283
	char		archiveStatusPath[MAXPGPATH];
1284

1285
	/* Remove the .done file */
1286 1287 1288
	StatusFilePath(archiveStatusPath, xlog, ".done");
	unlink(archiveStatusPath);
	/* should we complain about failure? */
1289 1290 1291 1292 1293

	/* Remove the .ready file if present --- normally it shouldn't be */
	StatusFilePath(archiveStatusPath, xlog, ".ready");
	unlink(archiveStatusPath);
	/* should we complain about failure? */
1294 1295
}

T
Tom Lane 已提交
1296 1297 1298 1299
/*
 * Advance the Insert state to the next buffer page, writing out the next
 * buffer if it still contains unwritten data.
 *
1300 1301 1302 1303
 * If new_segment is TRUE then we set up the next buffer page as the first
 * page of the next xlog segment file, possibly but not usually the next
 * consecutive file page.
 *
T
Tom Lane 已提交
1304
 * The global LogwrtRqst.Write pointer needs to be advanced to include the
1305
 * just-filled page.  If we can do this for free (without an extra lock),
T
Tom Lane 已提交
1306 1307 1308
 * we do so here.  Otherwise the caller must do it.  We return TRUE if the
 * request update still needs to be done, FALSE if we did it internally.
 *
1309
 * Must be called with WALInsertLock held.
T
Tom Lane 已提交
1310 1311
 */
static bool
1312
AdvanceXLInsertBuffer(bool new_segment)
1313
{
T
Tom Lane 已提交
1314 1315
	XLogCtlInsert *Insert = &XLogCtl->Insert;
	XLogCtlWrite *Write = &XLogCtl->Write;
1316
	int			nextidx = NextBufIdx(Insert->curridx);
T
Tom Lane 已提交
1317 1318 1319
	bool		update_needed = true;
	XLogRecPtr	OldPageRqstPtr;
	XLogwrtRqst WriteRqst;
1320 1321
	XLogRecPtr	NewPageEndPtr;
	XLogPageHeader NewPage;
1322

T
Tom Lane 已提交
1323 1324 1325
	/* Use Insert->LogwrtResult copy if it's more fresh */
	if (XLByteLT(LogwrtResult.Write, Insert->LogwrtResult.Write))
		LogwrtResult = Insert->LogwrtResult;
V
WAL  
Vadim B. Mikheev 已提交
1326

T
Tom Lane 已提交
1327
	/*
B
Bruce Momjian 已提交
1328 1329 1330
	 * Get ending-offset of the buffer page we need to replace (this may be
	 * zero if the buffer hasn't been used yet).  Fall through if it's already
	 * written out.
T
Tom Lane 已提交
1331 1332 1333 1334 1335 1336
	 */
	OldPageRqstPtr = XLogCtl->xlblocks[nextidx];
	if (!XLByteLE(OldPageRqstPtr, LogwrtResult.Write))
	{
		/* nope, got work to do... */
		XLogRecPtr	FinishedPageRqstPtr;
1337

T
Tom Lane 已提交
1338
		FinishedPageRqstPtr = XLogCtl->xlblocks[Insert->curridx];
1339

1340
		/* Before waiting, get info_lck and update LogwrtResult */
1341 1342 1343 1344
		{
			/* use volatile pointer to prevent code rearrangement */
			volatile XLogCtlData *xlogctl = XLogCtl;

1345
			SpinLockAcquire(&xlogctl->info_lck);
1346 1347 1348
			if (XLByteLT(xlogctl->LogwrtRqst.Write, FinishedPageRqstPtr))
				xlogctl->LogwrtRqst.Write = FinishedPageRqstPtr;
			LogwrtResult = xlogctl->LogwrtResult;
1349
			SpinLockRelease(&xlogctl->info_lck);
1350
		}
1351 1352 1353 1354 1355 1356 1357 1358 1359

		update_needed = false;	/* Did the shared-request update */

		if (XLByteLE(OldPageRqstPtr, LogwrtResult.Write))
		{
			/* OK, someone wrote it already */
			Insert->LogwrtResult = LogwrtResult;
		}
		else
1360
		{
1361 1362 1363 1364
			/* Must acquire write lock */
			LWLockAcquire(WALWriteLock, LW_EXCLUSIVE);
			LogwrtResult = Write->LogwrtResult;
			if (XLByteLE(OldPageRqstPtr, LogwrtResult.Write))
1365
			{
1366 1367 1368
				/* OK, someone wrote it already */
				LWLockRelease(WALWriteLock);
				Insert->LogwrtResult = LogwrtResult;
T
Tom Lane 已提交
1369
			}
1370
			else
T
Tom Lane 已提交
1371 1372
			{
				/*
B
Bruce Momjian 已提交
1373 1374
				 * Have to write buffers while holding insert lock. This is
				 * not good, so only write as much as we absolutely must.
T
Tom Lane 已提交
1375 1376 1377 1378
				 */
				WriteRqst.Write = OldPageRqstPtr;
				WriteRqst.Flush.xlogid = 0;
				WriteRqst.Flush.xrecoff = 0;
1379
				XLogWrite(WriteRqst, false, false);
1380
				LWLockRelease(WALWriteLock);
T
Tom Lane 已提交
1381
				Insert->LogwrtResult = LogwrtResult;
1382 1383 1384 1385
			}
		}
	}

T
Tom Lane 已提交
1386
	/*
B
Bruce Momjian 已提交
1387 1388
	 * Now the next buffer slot is free and we can set it up to be the next
	 * output page.
T
Tom Lane 已提交
1389
	 */
1390
	NewPageEndPtr = XLogCtl->xlblocks[Insert->curridx];
1391 1392 1393 1394 1395 1396 1397 1398

	if (new_segment)
	{
		/* force it to a segment start point */
		NewPageEndPtr.xrecoff += XLogSegSize - 1;
		NewPageEndPtr.xrecoff -= NewPageEndPtr.xrecoff % XLogSegSize;
	}

1399
	if (NewPageEndPtr.xrecoff >= XLogFileSize)
1400
	{
T
Tom Lane 已提交
1401
		/* crossing a logid boundary */
1402
		NewPageEndPtr.xlogid += 1;
1403
		NewPageEndPtr.xrecoff = XLOG_BLCKSZ;
1404
	}
T
Tom Lane 已提交
1405
	else
1406
		NewPageEndPtr.xrecoff += XLOG_BLCKSZ;
1407
	XLogCtl->xlblocks[nextidx] = NewPageEndPtr;
1408
	NewPage = (XLogPageHeader) (XLogCtl->pages + nextidx * (Size) XLOG_BLCKSZ);
B
Bruce Momjian 已提交
1409

T
Tom Lane 已提交
1410
	Insert->curridx = nextidx;
1411
	Insert->currpage = NewPage;
B
Bruce Momjian 已提交
1412 1413

	Insert->currpos = ((char *) NewPage) +SizeOfXLogShortPHD;
B
Bruce Momjian 已提交
1414

T
Tom Lane 已提交
1415
	/*
B
Bruce Momjian 已提交
1416 1417
	 * Be sure to re-zero the buffer so that bytes beyond what we've written
	 * will look like zeroes and not valid XLOG records...
T
Tom Lane 已提交
1418
	 */
1419
	MemSet((char *) NewPage, 0, XLOG_BLCKSZ);
1420

1421 1422 1423
	/*
	 * Fill the new page's header
	 */
B
Bruce Momjian 已提交
1424 1425
	NewPage   ->xlp_magic = XLOG_PAGE_MAGIC;

1426
	/* NewPage->xlp_info = 0; */	/* done by memset */
B
Bruce Momjian 已提交
1427 1428
	NewPage   ->xlp_tli = ThisTimeLineID;
	NewPage   ->xlp_pageaddr.xlogid = NewPageEndPtr.xlogid;
1429
	NewPage   ->xlp_pageaddr.xrecoff = NewPageEndPtr.xrecoff - XLOG_BLCKSZ;
T
Tom Lane 已提交
1430

1431
	/*
1432
	 * If first page of an XLOG segment file, make it a long header.
1433 1434 1435
	 */
	if ((NewPage->xlp_pageaddr.xrecoff % XLogSegSize) == 0)
	{
1436
		XLogLongPageHeader NewLongPage = (XLogLongPageHeader) NewPage;
1437

1438 1439
		NewLongPage->xlp_sysid = ControlFile->system_identifier;
		NewLongPage->xlp_seg_size = XLogSegSize;
1440
		NewLongPage->xlp_xlog_blcksz = XLOG_BLCKSZ;
B
Bruce Momjian 已提交
1441 1442 1443
		NewPage   ->xlp_info |= XLP_LONG_HEADER;

		Insert->currpos = ((char *) NewPage) +SizeOfXLogLongPHD;
1444 1445
	}

T
Tom Lane 已提交
1446
	return update_needed;
1447 1448
}

T
Tom Lane 已提交
1449 1450 1451
/*
 * Write and/or fsync the log at least as far as WriteRqst indicates.
 *
1452 1453 1454 1455 1456
 * If flexible == TRUE, we don't have to write as far as WriteRqst, but
 * may stop at any convenient boundary (such as a cache or logfile boundary).
 * This option allows us to avoid uselessly issuing multiple writes when a
 * single one would do.
 *
1457 1458 1459 1460 1461 1462
 * If xlog_switch == TRUE, we are intending an xlog segment switch, so
 * perform end-of-segment actions after writing the last page, even if
 * it's not physically the end of its segment.  (NB: this will work properly
 * only if caller specifies WriteRqst == page-end and flexible == false,
 * and there is some data to write.)
 *
1463
 * Must be called with WALWriteLock held.
T
Tom Lane 已提交
1464
 */
1465
static void
1466
XLogWrite(XLogwrtRqst WriteRqst, bool flexible, bool xlog_switch)
1467
{
1468
	XLogCtlWrite *Write = &XLogCtl->Write;
T
Tom Lane 已提交
1469
	bool		ispartialpage;
1470
	bool		last_iteration;
1471
	bool		finishing_seg;
1472
	bool		use_existent;
1473 1474 1475 1476
	int			curridx;
	int			npages;
	int			startidx;
	uint32		startoffset;
1477

1478 1479 1480
	/* We should always be inside a critical section here */
	Assert(CritSectionCount > 0);

B
Bruce Momjian 已提交
1481
	/*
B
Bruce Momjian 已提交
1482
	 * Update local LogwrtResult (caller probably did this already, but...)
B
Bruce Momjian 已提交
1483
	 */
T
Tom Lane 已提交
1484 1485
	LogwrtResult = Write->LogwrtResult;

1486 1487 1488
	/*
	 * Since successive pages in the xlog cache are consecutively allocated,
	 * we can usually gather multiple pages together and issue just one
B
Bruce Momjian 已提交
1489 1490 1491 1492 1493
	 * write() call.  npages is the number of pages we have determined can be
	 * written together; startidx is the cache block index of the first one,
	 * and startoffset is the file offset at which it should go. The latter
	 * two variables are only valid when npages > 0, but we must initialize
	 * all of them to keep the compiler quiet.
1494 1495 1496 1497 1498 1499 1500 1501 1502
	 */
	npages = 0;
	startidx = 0;
	startoffset = 0;

	/*
	 * Within the loop, curridx is the cache block index of the page to
	 * consider writing.  We advance Write->curridx only after successfully
	 * writing pages.  (Right now, this refinement is useless since we are
B
Bruce Momjian 已提交
1503 1504
	 * going to PANIC if any error occurs anyway; but someday it may come in
	 * useful.)
1505 1506
	 */
	curridx = Write->curridx;
B
 
Bruce Momjian 已提交
1507

T
Tom Lane 已提交
1508
	while (XLByteLT(LogwrtResult.Write, WriteRqst.Write))
1509
	{
1510
		/*
B
Bruce Momjian 已提交
1511 1512 1513
		 * Make sure we're not ahead of the insert process.  This could happen
		 * if we're passed a bogus WriteRqst.Write that is past the end of the
		 * last page that's been initialized by AdvanceXLInsertBuffer.
1514
		 */
1515
		if (!XLByteLT(LogwrtResult.Write, XLogCtl->xlblocks[curridx]))
1516
			elog(PANIC, "xlog write request %X/%X is past end of log %X/%X",
1517
				 LogwrtResult.Write.xlogid, LogwrtResult.Write.xrecoff,
1518 1519
				 XLogCtl->xlblocks[curridx].xlogid,
				 XLogCtl->xlblocks[curridx].xrecoff);
1520

T
Tom Lane 已提交
1521
		/* Advance LogwrtResult.Write to end of current buffer page */
1522
		LogwrtResult.Write = XLogCtl->xlblocks[curridx];
T
Tom Lane 已提交
1523 1524 1525
		ispartialpage = XLByteLT(WriteRqst.Write, LogwrtResult.Write);

		if (!XLByteInPrevSeg(LogwrtResult.Write, openLogId, openLogSeg))
1526
		{
T
Tom Lane 已提交
1527
			/*
1528 1529
			 * Switch to new logfile segment.  We cannot have any pending
			 * pages here (since we dump what we have at segment end).
T
Tom Lane 已提交
1530
			 */
1531
			Assert(npages == 0);
T
Tom Lane 已提交
1532
			if (openLogFile >= 0)
1533
				XLogFileClose();
T
Tom Lane 已提交
1534 1535
			XLByteToPrevSeg(LogwrtResult.Write, openLogId, openLogSeg);

1536 1537 1538 1539
			/* create/use new log file */
			use_existent = true;
			openLogFile = XLogFileInit(openLogId, openLogSeg,
									   &use_existent, true);
T
Tom Lane 已提交
1540
			openLogOff = 0;
1541 1542
		}

1543
		/* Make sure we have the current logfile open */
T
Tom Lane 已提交
1544
		if (openLogFile < 0)
1545
		{
T
Tom Lane 已提交
1546
			XLByteToPrevSeg(LogwrtResult.Write, openLogId, openLogSeg);
1547
			openLogFile = XLogFileOpen(openLogId, openLogSeg);
T
Tom Lane 已提交
1548
			openLogOff = 0;
1549 1550
		}

1551 1552 1553 1554 1555
		/* Add current page to the set of pending pages-to-dump */
		if (npages == 0)
		{
			/* first of group */
			startidx = curridx;
1556
			startoffset = (LogwrtResult.Write.xrecoff - XLOG_BLCKSZ) % XLogSegSize;
1557 1558
		}
		npages++;
1559

T
Tom Lane 已提交
1560
		/*
B
Bruce Momjian 已提交
1561 1562 1563 1564
		 * Dump the set if this will be the last loop iteration, or if we are
		 * at the last page of the cache area (since the next page won't be
		 * contiguous in memory), or if we are at the end of the logfile
		 * segment.
T
Tom Lane 已提交
1565
		 */
1566 1567
		last_iteration = !XLByteLT(LogwrtResult.Write, WriteRqst.Write);

1568
		finishing_seg = !ispartialpage &&
1569
			(startoffset + npages * XLOG_BLCKSZ) >= XLogSegSize;
1570

1571
		if (last_iteration ||
1572 1573
			curridx == XLogCtl->XLogCacheBlck ||
			finishing_seg)
T
Tom Lane 已提交
1574
		{
1575 1576
			char	   *from;
			Size		nbytes;
1577

1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
			/* Need to seek in the file? */
			if (openLogOff != startoffset)
			{
				if (lseek(openLogFile, (off_t) startoffset, SEEK_SET) < 0)
					ereport(PANIC,
							(errcode_for_file_access(),
							 errmsg("could not seek in log file %u, "
									"segment %u to offset %u: %m",
									openLogId, openLogSeg, startoffset)));
				openLogOff = startoffset;
			}

			/* OK to write the page(s) */
1591 1592
			from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
			nbytes = npages * (Size) XLOG_BLCKSZ;
1593 1594 1595 1596 1597 1598 1599 1600 1601
			errno = 0;
			if (write(openLogFile, from, nbytes) != nbytes)
			{
				/* if write didn't set errno, assume no disk space */
				if (errno == 0)
					errno = ENOSPC;
				ereport(PANIC,
						(errcode_for_file_access(),
						 errmsg("could not write to log file %u, segment %u "
P
Peter Eisentraut 已提交
1602
								"at offset %u, length %lu: %m",
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
								openLogId, openLogSeg,
								openLogOff, (unsigned long) nbytes)));
			}

			/* Update state for write */
			openLogOff += nbytes;
			Write->curridx = ispartialpage ? curridx : NextBufIdx(curridx);
			npages = 0;

			/*
			 * If we just wrote the whole last page of a logfile segment,
			 * fsync the segment immediately.  This avoids having to go back
			 * and re-open prior segments when an fsync request comes along
			 * later. Doing it here ensures that one and only one backend will
			 * perform this fsync.
			 *
1619 1620 1621
			 * We also do this if this is the last page written for an xlog
			 * switch.
			 *
1622
			 * This is also the right place to notify the Archiver that the
B
Bruce Momjian 已提交
1623
			 * segment is ready to copy to archival storage, and to update the
1624 1625 1626
			 * timer for archive_timeout, and to signal for a checkpoint if
			 * too many logfile segments have been used since the last
			 * checkpoint.
1627
			 */
1628
			if (finishing_seg || (xlog_switch && last_iteration))
1629 1630
			{
				issue_xlog_fsync();
B
Bruce Momjian 已提交
1631
				LogwrtResult.Flush = LogwrtResult.Write;		/* end of page */
1632 1633 1634

				if (XLogArchivingActive())
					XLogArchiveNotifySeg(openLogId, openLogSeg);
1635 1636

				Write->lastSegSwitchTime = time(NULL);
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671

				/*
				 * Signal bgwriter to start a checkpoint if it's been too long
				 * since the last one.	(We look at local copy of RedoRecPtr
				 * which might be a little out of date, but should be close
				 * enough for this purpose.)
				 *
				 * A straight computation of segment number could overflow 32
				 * bits.  Rather than assuming we have working 64-bit
				 * arithmetic, we compare the highest-order bits separately,
				 * and force a checkpoint immediately when they change.
				 */
				if (IsUnderPostmaster)
				{
					uint32		old_segno,
								new_segno;
					uint32		old_highbits,
								new_highbits;

					old_segno = (RedoRecPtr.xlogid % XLogSegSize) * XLogSegsPerFile +
						(RedoRecPtr.xrecoff / XLogSegSize);
					old_highbits = RedoRecPtr.xlogid / XLogSegSize;
					new_segno = (openLogId % XLogSegSize) * XLogSegsPerFile +
						openLogSeg;
					new_highbits = openLogId / XLogSegSize;
					if (new_highbits != old_highbits ||
						new_segno >= old_segno + (uint32) (CheckPointSegments-1))
					{
#ifdef WAL_DEBUG
						if (XLOG_DEBUG)
							elog(LOG, "time for a checkpoint, signaling bgwriter");
#endif
						RequestCheckpoint(false, true);
					}
				}
1672
			}
T
Tom Lane 已提交
1673
		}
1674

T
Tom Lane 已提交
1675 1676 1677 1678 1679 1680
		if (ispartialpage)
		{
			/* Only asked to write a partial page */
			LogwrtResult.Write = WriteRqst.Write;
			break;
		}
1681 1682 1683 1684 1685
		curridx = NextBufIdx(curridx);

		/* If flexible, break out of loop as soon as we wrote something */
		if (flexible && npages == 0)
			break;
1686
	}
1687 1688 1689

	Assert(npages == 0);
	Assert(curridx == Write->curridx);
1690

T
Tom Lane 已提交
1691 1692 1693 1694 1695
	/*
	 * If asked to flush, do so
	 */
	if (XLByteLT(LogwrtResult.Flush, WriteRqst.Flush) &&
		XLByteLT(LogwrtResult.Flush, LogwrtResult.Write))
1696
	{
T
Tom Lane 已提交
1697
		/*
B
Bruce Momjian 已提交
1698 1699 1700
		 * Could get here without iterating above loop, in which case we might
		 * have no open file or the wrong one.	However, we do not need to
		 * fsync more than one file.
T
Tom Lane 已提交
1701
		 */
1702
		if (sync_method != SYNC_METHOD_OPEN)
T
Tom Lane 已提交
1703
		{
1704
			if (openLogFile >= 0 &&
B
Bruce Momjian 已提交
1705
				!XLByteInPrevSeg(LogwrtResult.Write, openLogId, openLogSeg))
1706
				XLogFileClose();
1707 1708 1709
			if (openLogFile < 0)
			{
				XLByteToPrevSeg(LogwrtResult.Write, openLogId, openLogSeg);
1710
				openLogFile = XLogFileOpen(openLogId, openLogSeg);
1711 1712 1713
				openLogOff = 0;
			}
			issue_xlog_fsync();
T
Tom Lane 已提交
1714 1715
		}
		LogwrtResult.Flush = LogwrtResult.Write;
1716 1717
	}

T
Tom Lane 已提交
1718 1719 1720
	/*
	 * Update shared-memory status
	 *
B
Bruce Momjian 已提交
1721
	 * We make sure that the shared 'request' values do not fall behind the
B
Bruce Momjian 已提交
1722 1723
	 * 'result' values.  This is not absolutely essential, but it saves some
	 * code in a couple of places.
T
Tom Lane 已提交
1724
	 */
1725 1726 1727 1728
	{
		/* use volatile pointer to prevent code rearrangement */
		volatile XLogCtlData *xlogctl = XLogCtl;

1729
		SpinLockAcquire(&xlogctl->info_lck);
1730 1731 1732 1733 1734
		xlogctl->LogwrtResult = LogwrtResult;
		if (XLByteLT(xlogctl->LogwrtRqst.Write, LogwrtResult.Write))
			xlogctl->LogwrtRqst.Write = LogwrtResult.Write;
		if (XLByteLT(xlogctl->LogwrtRqst.Flush, LogwrtResult.Flush))
			xlogctl->LogwrtRqst.Flush = LogwrtResult.Flush;
1735
		SpinLockRelease(&xlogctl->info_lck);
1736
	}
1737

T
Tom Lane 已提交
1738 1739 1740 1741 1742 1743
	Write->LogwrtResult = LogwrtResult;
}

/*
 * Ensure that all XLOG data through the given position is flushed to disk.
 *
1744
 * NOTE: this differs from XLogWrite mainly in that the WALWriteLock is not
T
Tom Lane 已提交
1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
 * already held, and we try to avoid acquiring it if possible.
 */
void
XLogFlush(XLogRecPtr record)
{
	XLogRecPtr	WriteRqstPtr;
	XLogwrtRqst WriteRqst;

	/* Disabled during REDO */
	if (InRedo)
		return;

	/* Quick exit if already known flushed */
	if (XLByteLE(record, LogwrtResult.Flush))
		return;

1761
#ifdef WAL_DEBUG
1762
	if (XLOG_DEBUG)
1763
		elog(LOG, "xlog flush request %X/%X; write %X/%X; flush %X/%X",
1764 1765 1766
			 record.xlogid, record.xrecoff,
			 LogwrtResult.Write.xlogid, LogwrtResult.Write.xrecoff,
			 LogwrtResult.Flush.xlogid, LogwrtResult.Flush.xrecoff);
1767
#endif
1768

T
Tom Lane 已提交
1769 1770 1771 1772
	START_CRIT_SECTION();

	/*
	 * Since fsync is usually a horribly expensive operation, we try to
B
Bruce Momjian 已提交
1773 1774 1775 1776
	 * piggyback as much data as we can on each fsync: if we see any more data
	 * entered into the xlog buffer, we'll write and fsync that too, so that
	 * the final value of LogwrtResult.Flush is as large as possible. This
	 * gives us some chance of avoiding another fsync immediately after.
T
Tom Lane 已提交
1777 1778 1779 1780 1781
	 */

	/* initialize to given target; may increase below */
	WriteRqstPtr = record;

1782
	/* read LogwrtResult and update local state */
1783 1784 1785 1786
	{
		/* use volatile pointer to prevent code rearrangement */
		volatile XLogCtlData *xlogctl = XLogCtl;

1787
		SpinLockAcquire(&xlogctl->info_lck);
1788 1789 1790
		if (XLByteLT(WriteRqstPtr, xlogctl->LogwrtRqst.Write))
			WriteRqstPtr = xlogctl->LogwrtRqst.Write;
		LogwrtResult = xlogctl->LogwrtResult;
1791
		SpinLockRelease(&xlogctl->info_lck);
1792
	}
1793 1794 1795

	/* done already? */
	if (!XLByteLE(record, LogwrtResult.Flush))
T
Tom Lane 已提交
1796
	{
1797 1798 1799 1800
		/* now wait for the write lock */
		LWLockAcquire(WALWriteLock, LW_EXCLUSIVE);
		LogwrtResult = XLogCtl->Write.LogwrtResult;
		if (!XLByteLE(record, LogwrtResult.Flush))
T
Tom Lane 已提交
1801
		{
1802 1803 1804 1805 1806 1807
			/* try to write/flush later additions to XLOG as well */
			if (LWLockConditionalAcquire(WALInsertLock, LW_EXCLUSIVE))
			{
				XLogCtlInsert *Insert = &XLogCtl->Insert;
				uint32		freespace = INSERT_FREESPACE(Insert);

B
Bruce Momjian 已提交
1808
				if (freespace < SizeOfXLogRecord)		/* buffer is full */
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
					WriteRqstPtr = XLogCtl->xlblocks[Insert->curridx];
				else
				{
					WriteRqstPtr = XLogCtl->xlblocks[Insert->curridx];
					WriteRqstPtr.xrecoff -= freespace;
				}
				LWLockRelease(WALInsertLock);
				WriteRqst.Write = WriteRqstPtr;
				WriteRqst.Flush = WriteRqstPtr;
			}
			else
			{
				WriteRqst.Write = WriteRqstPtr;
				WriteRqst.Flush = record;
			}
1824
			XLogWrite(WriteRqst, false, false);
T
Tom Lane 已提交
1825
		}
1826
		LWLockRelease(WALWriteLock);
T
Tom Lane 已提交
1827 1828 1829
	}

	END_CRIT_SECTION();
1830 1831 1832

	/*
	 * If we still haven't flushed to the request point then we have a
B
Bruce Momjian 已提交
1833 1834
	 * problem; most likely, the requested flush point is past end of XLOG.
	 * This has been seen to occur when a disk page has a corrupted LSN.
1835
	 *
1836 1837 1838 1839 1840 1841 1842 1843 1844
	 * Formerly we treated this as a PANIC condition, but that hurts the
	 * system's robustness rather than helping it: we do not want to take down
	 * the whole system due to corruption on one data page.  In particular, if
	 * the bad page is encountered again during recovery then we would be
	 * unable to restart the database at all!  (This scenario has actually
	 * happened in the field several times with 7.1 releases. Note that we
	 * cannot get here while InRedo is true, but if the bad page is brought in
	 * and marked dirty during recovery then CreateCheckPoint will try to
	 * flush it at the end of recovery.)
1845
	 *
1846 1847 1848 1849
	 * The current approach is to ERROR under normal conditions, but only
	 * WARNING during recovery, so that the system can be brought up even if
	 * there's a corrupt LSN.  Note that for calls from xact.c, the ERROR will
	 * be promoted to PANIC since xact.c calls this routine inside a critical
B
Bruce Momjian 已提交
1850 1851
	 * section.  However, calls from bufmgr.c are not within critical sections
	 * and so we will not force a restart for a bad LSN on a data page.
1852 1853
	 */
	if (XLByteLT(LogwrtResult.Flush, record))
B
Bruce Momjian 已提交
1854
		elog(InRecovery ? WARNING : ERROR,
B
Bruce Momjian 已提交
1855
		"xlog flush request %X/%X is not satisfied --- flushed only to %X/%X",
1856 1857
			 record.xlogid, record.xrecoff,
			 LogwrtResult.Flush.xlogid, LogwrtResult.Flush.xrecoff);
1858 1859
}

T
Tom Lane 已提交
1860 1861 1862
/*
 * Create a new XLOG file segment, or open a pre-existing one.
 *
1863 1864 1865
 * log, seg: identify segment to be created/opened.
 *
 * *use_existent: if TRUE, OK to use a pre-existing file (else, any
B
Bruce Momjian 已提交
1866
 * pre-existing file will be deleted).	On return, TRUE if a pre-existing
1867 1868
 * file was used.
 *
1869
 * use_lock: if TRUE, acquire ControlFileLock while moving file into
1870
 * place.  This should be TRUE except during bootstrap log creation.  The
1871
 * caller must *not* hold the lock at call.
1872
 *
T
Tom Lane 已提交
1873
 * Returns FD of opened file.
1874 1875 1876 1877 1878
 *
 * Note: errors here are ERROR not PANIC because we might or might not be
 * inside a critical section (eg, during checkpoint there is no reason to
 * take down the system on failure).  They will promote to PANIC if we are
 * in a critical section.
T
Tom Lane 已提交
1879
 */
1880
static int
1881 1882
XLogFileInit(uint32 log, uint32 seg,
			 bool *use_existent, bool use_lock)
1883
{
1884
	char		path[MAXPGPATH];
1885
	char		tmppath[MAXPGPATH];
1886
	char		zbuffer[XLOG_BLCKSZ];
1887 1888 1889
	uint32		installed_log;
	uint32		installed_seg;
	int			max_advance;
1890
	int			fd;
1891
	int			nbytes;
1892

1893
	XLogFilePath(path, ThisTimeLineID, log, seg);
V
Vadim B. Mikheev 已提交
1894 1895

	/*
B
Bruce Momjian 已提交
1896
	 * Try to use existent file (checkpoint maker may have created it already)
V
Vadim B. Mikheev 已提交
1897
	 */
1898
	if (*use_existent)
V
Vadim B. Mikheev 已提交
1899
	{
1900 1901
		fd = BasicOpenFile(path, O_RDWR | PG_BINARY | XLOG_SYNC_BIT,
						   S_IRUSR | S_IWUSR);
V
Vadim B. Mikheev 已提交
1902 1903 1904
		if (fd < 0)
		{
			if (errno != ENOENT)
1905
				ereport(ERROR,
1906
						(errcode_for_file_access(),
1907
						 errmsg("could not open file \"%s\" (log file %u, segment %u): %m",
1908
								path, log, seg)));
V
Vadim B. Mikheev 已提交
1909 1910
		}
		else
1911
			return fd;
V
Vadim B. Mikheev 已提交
1912 1913
	}

1914
	/*
B
Bruce Momjian 已提交
1915 1916 1917 1918
	 * Initialize an empty (all zeroes) segment.  NOTE: it is possible that
	 * another process is doing the same thing.  If so, we will end up
	 * pre-creating an extra log segment.  That seems OK, and better than
	 * holding the lock throughout this lengthy process.
1919
	 */
1920
	snprintf(tmppath, MAXPGPATH, XLOGDIR "/xlogtemp.%d", (int) getpid());
1921 1922

	unlink(tmppath);
1923

1924
	/* do not use XLOG_SYNC_BIT here --- want to fsync only at end of fill */
1925
	fd = BasicOpenFile(tmppath, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
T
Tom Lane 已提交
1926
					   S_IRUSR | S_IWUSR);
1927
	if (fd < 0)
1928
		ereport(ERROR,
1929
				(errcode_for_file_access(),
1930
				 errmsg("could not create file \"%s\": %m", tmppath)));
1931

1932
	/*
B
Bruce Momjian 已提交
1933 1934 1935 1936 1937 1938 1939
	 * Zero-fill the file.	We have to do this the hard way to ensure that all
	 * the file space has really been allocated --- on platforms that allow
	 * "holes" in files, just seeking to the end doesn't allocate intermediate
	 * space.  This way, we know that we have all the space and (after the
	 * fsync below) that all the indirect blocks are down on disk.	Therefore,
	 * fdatasync(2) or O_DSYNC will be sufficient to sync future writes to the
	 * log file.
1940 1941 1942 1943
	 */
	MemSet(zbuffer, 0, sizeof(zbuffer));
	for (nbytes = 0; nbytes < XLogSegSize; nbytes += sizeof(zbuffer))
	{
1944
		errno = 0;
1945
		if ((int) write(fd, zbuffer, sizeof(zbuffer)) != (int) sizeof(zbuffer))
T
Tom Lane 已提交
1946
		{
B
Bruce Momjian 已提交
1947
			int			save_errno = errno;
T
Tom Lane 已提交
1948

B
Bruce Momjian 已提交
1949
			/*
B
Bruce Momjian 已提交
1950
			 * If we fail to make the file, delete it to release disk space
B
Bruce Momjian 已提交
1951
			 */
1952
			unlink(tmppath);
1953 1954
			/* if write didn't set errno, assume problem is no disk space */
			errno = save_errno ? save_errno : ENOSPC;
T
Tom Lane 已提交
1955

1956
			ereport(ERROR,
1957
					(errcode_for_file_access(),
B
Bruce Momjian 已提交
1958
					 errmsg("could not write to file \"%s\": %m", tmppath)));
T
Tom Lane 已提交
1959
		}
1960
	}
1961

1962
	if (pg_fsync(fd) != 0)
1963
		ereport(ERROR,
1964
				(errcode_for_file_access(),
1965
				 errmsg("could not fsync file \"%s\": %m", tmppath)));
1966

1967
	if (close(fd))
1968
		ereport(ERROR,
1969 1970
				(errcode_for_file_access(),
				 errmsg("could not close file \"%s\": %m", tmppath)));
T
Tom Lane 已提交
1971

1972
	/*
1973 1974
	 * Now move the segment into place with its final name.
	 *
1975
	 * If caller didn't want to use a pre-existing file, get rid of any
B
Bruce Momjian 已提交
1976 1977 1978
	 * pre-existing file.  Otherwise, cope with possibility that someone else
	 * has created the file while we were filling ours: if so, use ours to
	 * pre-create a future log segment.
1979
	 */
1980 1981 1982 1983 1984
	installed_log = log;
	installed_seg = seg;
	max_advance = XLOGfileslop;
	if (!InstallXLogFileSegment(&installed_log, &installed_seg, tmppath,
								*use_existent, &max_advance,
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
								use_lock))
	{
		/* No need for any more future segments... */
		unlink(tmppath);
	}

	/* Set flag to tell caller there was no existent file */
	*use_existent = false;

	/* Now open original target segment (might not be file I just made) */
	fd = BasicOpenFile(path, O_RDWR | PG_BINARY | XLOG_SYNC_BIT,
					   S_IRUSR | S_IWUSR);
	if (fd < 0)
1998
		ereport(ERROR,
1999
				(errcode_for_file_access(),
B
Bruce Momjian 已提交
2000 2001
		   errmsg("could not open file \"%s\" (log file %u, segment %u): %m",
				  path, log, seg)));
2002

2003
	return fd;
2004 2005
}

2006 2007 2008 2009 2010 2011 2012 2013 2014
/*
 * Create a new XLOG file segment by copying a pre-existing one.
 *
 * log, seg: identify segment to be created.
 *
 * srcTLI, srclog, srcseg: identify segment to be copied (could be from
 *		a different timeline)
 *
 * Currently this is only used during recovery, and so there are no locking
B
Bruce Momjian 已提交
2015
 * considerations.	But we should be just as tense as XLogFileInit to avoid
2016 2017 2018 2019 2020 2021 2022 2023
 * emplacing a bogus file.
 */
static void
XLogFileCopy(uint32 log, uint32 seg,
			 TimeLineID srcTLI, uint32 srclog, uint32 srcseg)
{
	char		path[MAXPGPATH];
	char		tmppath[MAXPGPATH];
2024
	char		buffer[XLOG_BLCKSZ];
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
	int			srcfd;
	int			fd;
	int			nbytes;

	/*
	 * Open the source file
	 */
	XLogFilePath(path, srcTLI, srclog, srcseg);
	srcfd = BasicOpenFile(path, O_RDONLY | PG_BINARY, 0);
	if (srcfd < 0)
2035
		ereport(ERROR,
2036 2037 2038 2039 2040 2041
				(errcode_for_file_access(),
				 errmsg("could not open file \"%s\": %m", path)));

	/*
	 * Copy into a temp file name.
	 */
2042
	snprintf(tmppath, MAXPGPATH, XLOGDIR "/xlogtemp.%d", (int) getpid());
2043 2044 2045 2046 2047 2048 2049

	unlink(tmppath);

	/* do not use XLOG_SYNC_BIT here --- want to fsync only at end of fill */
	fd = BasicOpenFile(tmppath, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
					   S_IRUSR | S_IWUSR);
	if (fd < 0)
2050
		ereport(ERROR,
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
				(errcode_for_file_access(),
				 errmsg("could not create file \"%s\": %m", tmppath)));

	/*
	 * Do the data copying.
	 */
	for (nbytes = 0; nbytes < XLogSegSize; nbytes += sizeof(buffer))
	{
		errno = 0;
		if ((int) read(srcfd, buffer, sizeof(buffer)) != (int) sizeof(buffer))
		{
			if (errno != 0)
2063
				ereport(ERROR,
2064 2065 2066
						(errcode_for_file_access(),
						 errmsg("could not read file \"%s\": %m", path)));
			else
2067
				ereport(ERROR,
B
Bruce Momjian 已提交
2068
						(errmsg("not enough data in file \"%s\"", path)));
2069 2070 2071 2072 2073 2074 2075
		}
		errno = 0;
		if ((int) write(fd, buffer, sizeof(buffer)) != (int) sizeof(buffer))
		{
			int			save_errno = errno;

			/*
B
Bruce Momjian 已提交
2076
			 * If we fail to make the file, delete it to release disk space
2077 2078 2079 2080 2081
			 */
			unlink(tmppath);
			/* if write didn't set errno, assume problem is no disk space */
			errno = save_errno ? save_errno : ENOSPC;

2082
			ereport(ERROR,
2083
					(errcode_for_file_access(),
B
Bruce Momjian 已提交
2084
					 errmsg("could not write to file \"%s\": %m", tmppath)));
2085 2086 2087 2088
		}
	}

	if (pg_fsync(fd) != 0)
2089
		ereport(ERROR,
2090 2091 2092 2093
				(errcode_for_file_access(),
				 errmsg("could not fsync file \"%s\": %m", tmppath)));

	if (close(fd))
2094
		ereport(ERROR,
2095 2096 2097 2098 2099 2100 2101 2102
				(errcode_for_file_access(),
				 errmsg("could not close file \"%s\": %m", tmppath)));

	close(srcfd);

	/*
	 * Now move the segment into place with its final name.
	 */
2103
	if (!InstallXLogFileSegment(&log, &seg, tmppath, false, NULL, false))
2104
		elog(ERROR, "InstallXLogFileSegment should not have failed");
2105 2106
}

2107 2108 2109 2110 2111 2112
/*
 * Install a new XLOG segment file as a current or future log segment.
 *
 * This is used both to install a newly-created segment (which has a temp
 * filename while it's being created) and to recycle an old segment.
 *
2113 2114 2115
 * *log, *seg: identify segment to install as (or first possible target).
 * When find_free is TRUE, these are modified on return to indicate the
 * actual installation location or last segment searched.
2116 2117 2118 2119 2120 2121 2122
 *
 * tmppath: initial name of file to install.  It will be renamed into place.
 *
 * find_free: if TRUE, install the new segment at the first empty log/seg
 * number at or after the passed numbers.  If FALSE, install the new segment
 * exactly where specified, deleting any existing segment file there.
 *
2123 2124 2125 2126
 * *max_advance: maximum number of log/seg slots to advance past the starting
 * point.  Fail if no free slot is found in this range.  On return, reduced
 * by the number of slots skipped over.  (Irrelevant, and may be NULL,
 * when find_free is FALSE.)
2127
 *
2128
 * use_lock: if TRUE, acquire ControlFileLock while moving file into
2129
 * place.  This should be TRUE except during bootstrap log creation.  The
2130
 * caller must *not* hold the lock at call.
2131 2132
 *
 * Returns TRUE if file installed, FALSE if not installed because of
2133 2134 2135
 * exceeding max_advance limit.  On Windows, we also return FALSE if we
 * can't rename the file into place because someone's got it open.
 * (Any other kind of failure causes ereport().)
2136 2137
 */
static bool
2138 2139
InstallXLogFileSegment(uint32 *log, uint32 *seg, char *tmppath,
					   bool find_free, int *max_advance,
2140 2141 2142
					   bool use_lock)
{
	char		path[MAXPGPATH];
2143
	struct stat stat_buf;
2144

2145
	XLogFilePath(path, ThisTimeLineID, *log, *seg);
2146 2147 2148 2149 2150

	/*
	 * We want to be sure that only one process does this at a time.
	 */
	if (use_lock)
2151
		LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
2152

2153 2154 2155 2156 2157
	if (!find_free)
	{
		/* Force installation: get rid of any pre-existing segment file */
		unlink(path);
	}
2158 2159
	else
	{
2160
		/* Find a free slot to put it in */
2161
		while (stat(path, &stat_buf) == 0)
2162
		{
2163
			if (*max_advance <= 0)
2164 2165 2166
			{
				/* Failed to find a free slot within specified range */
				if (use_lock)
2167
					LWLockRelease(ControlFileLock);
2168 2169
				return false;
			}
2170 2171 2172
			NextLogSeg(*log, *seg);
			(*max_advance)--;
			XLogFilePath(path, ThisTimeLineID, *log, *seg);
2173 2174 2175 2176 2177 2178 2179
		}
	}

	/*
	 * Prefer link() to rename() here just to be really sure that we don't
	 * overwrite an existing logfile.  However, there shouldn't be one, so
	 * rename() is an acceptable substitute except for the truly paranoid.
2180
	 */
2181
#if HAVE_WORKING_LINK
2182
	if (link(tmppath, path) < 0)
2183
		ereport(ERROR,
2184
				(errcode_for_file_access(),
2185
				 errmsg("could not link file \"%s\" to \"%s\" (initialization of log file %u, segment %u): %m",
2186
						tmppath, path, *log, *seg)));
2187
	unlink(tmppath);
2188
#else
2189
	if (rename(tmppath, path) < 0)
2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203
	{
#ifdef WIN32
#if !defined(__CYGWIN__)
		if (GetLastError() == ERROR_ACCESS_DENIED)
#else
		if (errno == EACCES)
#endif
		{
			if (use_lock)
				LWLockRelease(ControlFileLock);
			return false;
		}
#endif /* WIN32 */

2204
		ereport(ERROR,
2205
				(errcode_for_file_access(),
2206
				 errmsg("could not rename file \"%s\" to \"%s\" (initialization of log file %u, segment %u): %m",
2207
						tmppath, path, *log, *seg)));
2208
	}
2209
#endif
V
Vadim B. Mikheev 已提交
2210

2211
	if (use_lock)
2212
		LWLockRelease(ControlFileLock);
2213

2214
	return true;
2215 2216
}

T
Tom Lane 已提交
2217
/*
2218
 * Open a pre-existing logfile segment for writing.
T
Tom Lane 已提交
2219
 */
2220
static int
2221
XLogFileOpen(uint32 log, uint32 seg)
2222
{
2223 2224
	char		path[MAXPGPATH];
	int			fd;
2225

2226
	XLogFilePath(path, ThisTimeLineID, log, seg);
2227

2228 2229
	fd = BasicOpenFile(path, O_RDWR | PG_BINARY | XLOG_SYNC_BIT,
					   S_IRUSR | S_IWUSR);
2230
	if (fd < 0)
2231 2232
		ereport(PANIC,
				(errcode_for_file_access(),
B
Bruce Momjian 已提交
2233 2234
		   errmsg("could not open file \"%s\" (log file %u, segment %u): %m",
				  path, log, seg)));
2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248

	return fd;
}

/*
 * Open a logfile segment for reading (during recovery).
 */
static int
XLogFileRead(uint32 log, uint32 seg, int emode)
{
	char		path[MAXPGPATH];
	char		xlogfname[MAXFNAMELEN];
	ListCell   *cell;
	int			fd;
2249

2250
	/*
B
Bruce Momjian 已提交
2251 2252
	 * Loop looking for a suitable timeline ID: we might need to read any of
	 * the timelines listed in expectedTLIs.
2253
	 *
B
Bruce Momjian 已提交
2254
	 * We expect curFileTLI on entry to be the TLI of the preceding file in
B
Bruce Momjian 已提交
2255 2256 2257 2258
	 * sequence, or 0 if there was no predecessor.	We do not allow curFileTLI
	 * to go backwards; this prevents us from picking up the wrong file when a
	 * parent timeline extends to higher segment numbers than the child we
	 * want to read.
2259
	 */
2260 2261 2262
	foreach(cell, expectedTLIs)
	{
		TimeLineID	tli = (TimeLineID) lfirst_int(cell);
2263

2264 2265 2266 2267 2268 2269 2270
		if (tli < curFileTLI)
			break;				/* don't bother looking at too-old TLIs */

		if (InArchiveRecovery)
		{
			XLogFileName(xlogfname, tli, log, seg);
			restoredFromArchive = RestoreArchivedFile(path, xlogfname,
2271 2272
													  "RECOVERYXLOG",
													  XLogSegSize);
2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286
		}
		else
			XLogFilePath(path, tli, log, seg);

		fd = BasicOpenFile(path, O_RDONLY | PG_BINARY, 0);
		if (fd >= 0)
		{
			/* Success! */
			curFileTLI = tli;
			return fd;
		}
		if (errno != ENOENT)	/* unexpected failure? */
			ereport(PANIC,
					(errcode_for_file_access(),
B
Bruce Momjian 已提交
2287 2288
			errmsg("could not open file \"%s\" (log file %u, segment %u): %m",
				   path, log, seg)));
2289 2290 2291 2292 2293 2294 2295
	}

	/* Couldn't find it.  For simplicity, complain about front timeline */
	XLogFilePath(path, recoveryTargetTLI, log, seg);
	errno = ENOENT;
	ereport(emode,
			(errcode_for_file_access(),
B
Bruce Momjian 已提交
2296 2297
		   errmsg("could not open file \"%s\" (log file %u, segment %u): %m",
				  path, log, seg)));
2298
	return -1;
2299 2300
}

2301 2302 2303 2304 2305 2306 2307 2308
/*
 * Close the current logfile segment for writing.
 */
static void
XLogFileClose(void)
{
	Assert(openLogFile >= 0);

2309
	/*
B
Bruce Momjian 已提交
2310 2311 2312 2313 2314 2315
	 * posix_fadvise is problematic on many platforms: on older x86 Linux it
	 * just dumps core, and there are reports of problems on PPC platforms as
	 * well.  The following is therefore disabled for the time being. We could
	 * consider some kind of configure test to see if it's safe to use, but
	 * since we lack hard evidence that there's any useful performance gain to
	 * be had, spending time on that seems unprofitable for now.
2316 2317 2318
	 */
#ifdef NOT_USED

2319
	/*
2320
	 * WAL segment files will not be re-read in normal operation, so we advise
B
Bruce Momjian 已提交
2321
	 * OS to release any cached pages.	But do not do so if WAL archiving is
2322 2323 2324
	 * active, because archiver process could use the cache to read the WAL
	 * segment.
	 *
B
Bruce Momjian 已提交
2325 2326
	 * While O_DIRECT works for O_SYNC, posix_fadvise() works for fsync() and
	 * O_SYNC, and some platforms only have posix_fadvise().
2327
	 */
2328
#if defined(HAVE_DECL_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED)
2329 2330 2331
	if (!XLogArchivingActive())
		posix_fadvise(openLogFile, 0, 0, POSIX_FADV_DONTNEED);
#endif
B
Bruce Momjian 已提交
2332
#endif   /* NOT_USED */
2333

2334 2335
	if (close(openLogFile))
		ereport(PANIC,
B
Bruce Momjian 已提交
2336 2337 2338
				(errcode_for_file_access(),
				 errmsg("could not close log file %u, segment %u: %m",
						openLogId, openLogSeg)));
2339 2340 2341
	openLogFile = -1;
}

2342
/*
2343
 * Attempt to retrieve the specified file from off-line archival storage.
2344
 * If successful, fill "path" with its complete path (note that this will be
2345 2346
 * a temp file name that doesn't follow the normal naming convention), and
 * return TRUE.
2347
 *
2348 2349 2350
 * If not successful, fill "path" with the name of the normal on-line file
 * (which may or may not actually exist, but we'll try to use it), and return
 * FALSE.
2351 2352 2353 2354
 *
 * For fixed-size files, the caller may pass the expected size as an
 * additional crosscheck on successful recovery.  If the file size is not
 * known, set expectedSize = 0.
2355
 */
2356 2357
static bool
RestoreArchivedFile(char *path, const char *xlogfname,
2358
					const char *recovername, off_t expectedSize)
2359
{
B
Bruce Momjian 已提交
2360 2361 2362 2363
	char		xlogpath[MAXPGPATH];
	char		xlogRestoreCmd[MAXPGPATH];
	char	   *dp;
	char	   *endp;
2364
	const char *sp;
B
Bruce Momjian 已提交
2365
	int			rc;
2366
	bool		signaled;
2367 2368 2369
	struct stat stat_buf;

	/*
B
Bruce Momjian 已提交
2370 2371 2372 2373
	 * When doing archive recovery, we always prefer an archived log file even
	 * if a file of the same name exists in XLOGDIR.  The reason is that the
	 * file in XLOGDIR could be an old, un-filled or partly-filled version
	 * that was copied and restored as part of backing up $PGDATA.
2374
	 *
B
Bruce Momjian 已提交
2375
	 * We could try to optimize this slightly by checking the local copy
B
Bruce Momjian 已提交
2376 2377 2378 2379
	 * lastchange timestamp against the archived copy, but we have no API to
	 * do this, nor can we guarantee that the lastchange timestamp was
	 * preserved correctly when we copied to archive. Our aim is robustness,
	 * so we elect not to do this.
2380
	 *
2381 2382 2383
	 * If we cannot obtain the log file from the archive, however, we will try
	 * to use the XLOGDIR file if it exists.  This is so that we can make use
	 * of log segments that weren't yet transferred to the archive.
2384
	 *
2385 2386 2387 2388
	 * Notice that we don't actually overwrite any files when we copy back
	 * from archive because the recoveryRestoreCommand may inadvertently
	 * restore inappropriate xlogs, or they may be corrupt, so we may wish to
	 * fallback to the segments remaining in current XLOGDIR later. The
B
Bruce Momjian 已提交
2389 2390
	 * copy-from-archive filename is always the same, ensuring that we don't
	 * run out of disk space on long recoveries.
2391
	 */
2392
	snprintf(xlogpath, MAXPGPATH, XLOGDIR "/%s", recovername);
2393 2394

	/*
2395
	 * Make sure there is no existing file named recovername.
2396 2397 2398 2399 2400 2401
	 */
	if (stat(xlogpath, &stat_buf) != 0)
	{
		if (errno != ENOENT)
			ereport(FATAL,
					(errcode_for_file_access(),
P
Peter Eisentraut 已提交
2402
					 errmsg("could not stat file \"%s\": %m",
2403 2404 2405 2406 2407 2408 2409
							xlogpath)));
	}
	else
	{
		if (unlink(xlogpath) != 0)
			ereport(FATAL,
					(errcode_for_file_access(),
2410
					 errmsg("could not remove file \"%s\": %m",
2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427
							xlogpath)));
	}

	/*
	 * construct the command to be executed
	 */
	dp = xlogRestoreCmd;
	endp = xlogRestoreCmd + MAXPGPATH - 1;
	*endp = '\0';

	for (sp = recoveryRestoreCommand; *sp; sp++)
	{
		if (*sp == '%')
		{
			switch (sp[1])
			{
				case 'p':
2428
					/* %p: relative path of target file */
2429
					sp++;
B
Bruce Momjian 已提交
2430
					StrNCpy(dp, xlogpath, endp - dp);
2431
					make_native_path(dp);
2432 2433 2434 2435 2436
					dp += strlen(dp);
					break;
				case 'f':
					/* %f: filename of desired file */
					sp++;
B
Bruce Momjian 已提交
2437
					StrNCpy(dp, xlogfname, endp - dp);
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461
					dp += strlen(dp);
					break;
				case '%':
					/* convert %% to a single % */
					sp++;
					if (dp < endp)
						*dp++ = *sp;
					break;
				default:
					/* otherwise treat the % as not special */
					if (dp < endp)
						*dp++ = *sp;
					break;
			}
		}
		else
		{
			if (dp < endp)
				*dp++ = *sp;
		}
	}
	*dp = '\0';

	ereport(DEBUG3,
B
Bruce Momjian 已提交
2462
			(errmsg_internal("executing restore command \"%s\"",
2463 2464 2465
							 xlogRestoreCmd)));

	/*
2466
	 * Copy xlog from archival storage to XLOGDIR
2467 2468 2469 2470
	 */
	rc = system(xlogRestoreCmd);
	if (rc == 0)
	{
2471 2472 2473 2474
		/*
		 * command apparently succeeded, but let's make sure the file is
		 * really there now and has the correct size.
		 *
2475 2476 2477 2478 2479
		 * XXX I made wrong-size a fatal error to ensure the DBA would notice
		 * it, but is that too strong?	We could try to plow ahead with a
		 * local copy of the file ... but the problem is that there probably
		 * isn't one, and we'd incorrectly conclude we've reached the end of
		 * WAL and we're done recovering ...
2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503
		 */
		if (stat(xlogpath, &stat_buf) == 0)
		{
			if (expectedSize > 0 && stat_buf.st_size != expectedSize)
				ereport(FATAL,
						(errmsg("archive file \"%s\" has wrong size: %lu instead of %lu",
								xlogfname,
								(unsigned long) stat_buf.st_size,
								(unsigned long) expectedSize)));
			else
			{
				ereport(LOG,
						(errmsg("restored log file \"%s\" from archive",
								xlogfname)));
				strcpy(path, xlogpath);
				return true;
			}
		}
		else
		{
			/* stat failed */
			if (errno != ENOENT)
				ereport(FATAL,
						(errcode_for_file_access(),
P
Peter Eisentraut 已提交
2504
						 errmsg("could not stat file \"%s\": %m",
2505
								xlogpath)));
2506 2507 2508 2509
		}
	}

	/*
2510
	 * Remember, we rollforward UNTIL the restore fails so failure here is
B
Bruce Momjian 已提交
2511
	 * just part of the process... that makes it difficult to determine
B
Bruce Momjian 已提交
2512 2513 2514
	 * whether the restore failed because there isn't an archive to restore,
	 * or because the administrator has specified the restore program
	 * incorrectly.  We have to assume the former.
2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527
	 *
	 * However, if the failure was due to any sort of signal, it's best to
	 * punt and abort recovery.  (If we "return false" here, upper levels
	 * will assume that recovery is complete and start up the database!)
	 * It's essential to abort on child SIGINT and SIGQUIT, because per spec
	 * system() ignores SIGINT and SIGQUIT while waiting; if we see one of
	 * those it's a good bet we should have gotten it too.  Aborting on other
	 * signals such as SIGTERM seems a good idea as well.
	 *
	 * Per the Single Unix Spec, shells report exit status > 128 when
	 * a called command died on a signal.  Also, 126 and 127 are used to
	 * report problems such as an unfindable command; treat those as fatal
	 * errors too.
2528
	 */
2529 2530 2531
	signaled = WIFSIGNALED(rc) || WEXITSTATUS(rc) > 125;

	ereport(signaled ? FATAL : DEBUG2,
B
Bruce Momjian 已提交
2532 2533
		(errmsg("could not restore file \"%s\" from archive: return code %d",
				xlogfname, rc)));
2534 2535

	/*
B
Bruce Momjian 已提交
2536 2537
	 * if an archived file is not available, there might still be a version of
	 * this file in XLOGDIR, so return that as the filename to open.
2538
	 *
B
Bruce Momjian 已提交
2539 2540
	 * In many recovery scenarios we expect this to fail also, but if so that
	 * just means we've reached the end of WAL.
2541
	 */
2542
	snprintf(path, MAXPGPATH, XLOGDIR "/%s", xlogfname);
2543
	return false;
2544 2545
}

V
Vadim B. Mikheev 已提交
2546
/*
T
Tom Lane 已提交
2547 2548 2549
 * Preallocate log files beyond the specified log endpoint, according to
 * the XLOGfile user parameter.
 */
2550
static int
T
Tom Lane 已提交
2551 2552
PreallocXlogFiles(XLogRecPtr endptr)
{
2553
	int			nsegsadded = 0;
T
Tom Lane 已提交
2554 2555 2556
	uint32		_logId;
	uint32		_logSeg;
	int			lf;
2557
	bool		use_existent;
T
Tom Lane 已提交
2558 2559

	XLByteToPrevSeg(endptr, _logId, _logSeg);
B
Bruce Momjian 已提交
2560
	if ((endptr.xrecoff - 1) % XLogSegSize >=
B
Bruce Momjian 已提交
2561
		(uint32) (0.75 * XLogSegSize))
T
Tom Lane 已提交
2562 2563
	{
		NextLogSeg(_logId, _logSeg);
2564 2565
		use_existent = true;
		lf = XLogFileInit(_logId, _logSeg, &use_existent, true);
T
Tom Lane 已提交
2566
		close(lf);
2567 2568
		if (!use_existent)
			nsegsadded++;
T
Tom Lane 已提交
2569
	}
2570
	return nsegsadded;
T
Tom Lane 已提交
2571 2572 2573 2574
}

/*
 * Remove or move offline all log files older or equal to passed log/seg#
2575 2576 2577
 *
 * endptr is current (or recent) end of xlog; this is used to determine
 * whether we want to recycle rather than delete no-longer-wanted log files.
V
Vadim B. Mikheev 已提交
2578 2579
 */
static void
2580 2581
MoveOfflineLogs(uint32 log, uint32 seg, XLogRecPtr endptr,
				int *nsegsremoved, int *nsegsrecycled)
V
Vadim B. Mikheev 已提交
2582
{
2583 2584
	uint32		endlogId;
	uint32		endlogSeg;
2585
	int			max_advance;
B
Bruce Momjian 已提交
2586 2587
	DIR		   *xldir;
	struct dirent *xlde;
2588
	char		lastoff[MAXFNAMELEN];
B
Bruce Momjian 已提交
2589
	char		path[MAXPGPATH];
V
Vadim B. Mikheev 已提交
2590

2591 2592 2593
	*nsegsremoved = 0;
	*nsegsrecycled = 0;

2594 2595 2596 2597
	/*
	 * Initialize info about where to try to recycle to.  We allow recycling
	 * segments up to XLOGfileslop segments beyond the current XLOG location.
	 */
2598
	XLByteToPrevSeg(endptr, endlogId, endlogSeg);
2599
	max_advance = XLOGfileslop;
V
Vadim B. Mikheev 已提交
2600

2601
	xldir = AllocateDir(XLOGDIR);
V
Vadim B. Mikheev 已提交
2602
	if (xldir == NULL)
2603
		ereport(ERROR,
2604
				(errcode_for_file_access(),
B
Bruce Momjian 已提交
2605 2606
				 errmsg("could not open transaction log directory \"%s\": %m",
						XLOGDIR)));
V
Vadim B. Mikheev 已提交
2607

2608
	XLogFileName(lastoff, ThisTimeLineID, log, seg);
V
Vadim B. Mikheev 已提交
2609

2610
	while ((xlde = ReadDir(xldir, XLOGDIR)) != NULL)
V
Vadim B. Mikheev 已提交
2611
	{
2612
		/*
2613
		 * We ignore the timeline part of the XLOG segment identifiers in
B
Bruce Momjian 已提交
2614 2615 2616 2617 2618
		 * deciding whether a segment is still needed.	This ensures that we
		 * won't prematurely remove a segment from a parent timeline. We could
		 * probably be a little more proactive about removing segments of
		 * non-parent timelines, but that would be a whole lot more
		 * complicated.
2619
		 *
B
Bruce Momjian 已提交
2620 2621
		 * We use the alphanumeric sorting property of the filenames to decide
		 * which ones are earlier than the lastoff segment.
2622
		 */
2623 2624 2625
		if (strlen(xlde->d_name) == 24 &&
			strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
			strcmp(xlde->d_name + 8, lastoff + 8) <= 0)
V
Vadim B. Mikheev 已提交
2626
		{
2627
			if (XLogArchiveCheckDone(xlde->d_name))
2628
			{
2629
				snprintf(path, MAXPGPATH, XLOGDIR "/%s", xlde->d_name);
2630

2631
				/*
B
Bruce Momjian 已提交
2632 2633
				 * Before deleting the file, see if it can be recycled as a
				 * future log segment.
2634
				 */
2635 2636
				if (InstallXLogFileSegment(&endlogId, &endlogSeg, path,
										   true, &max_advance,
2637 2638
										   true))
				{
2639
					ereport(DEBUG2,
B
Bruce Momjian 已提交
2640 2641
							(errmsg("recycled transaction log file \"%s\"",
									xlde->d_name)));
2642
					(*nsegsrecycled)++;
2643 2644 2645 2646 2647 2648
					/* Needn't recheck that slot on future iterations */
					if (max_advance > 0)
					{
						NextLogSeg(endlogId, endlogSeg);
						max_advance--;
					}
2649 2650 2651 2652
				}
				else
				{
					/* No need for any more future segments... */
2653
					ereport(DEBUG2,
B
Bruce Momjian 已提交
2654 2655
							(errmsg("removing transaction log file \"%s\"",
									xlde->d_name)));
2656
					unlink(path);
2657
					(*nsegsremoved)++;
2658
				}
2659 2660

				XLogArchiveCleanup(xlde->d_name);
2661
			}
V
Vadim B. Mikheev 已提交
2662 2663
		}
	}
B
Bruce Momjian 已提交
2664

2665
	FreeDir(xldir);
V
Vadim B. Mikheev 已提交
2666 2667
}

2668
/*
2669 2670 2671
 * Remove previous backup history files.  This also retries creation of
 * .ready files for any backup history files for which XLogArchiveNotify
 * failed earlier.
2672 2673
 */
static void
2674
CleanupBackupHistory(void)
2675 2676 2677 2678 2679
{
	DIR		   *xldir;
	struct dirent *xlde;
	char		path[MAXPGPATH];

2680
	xldir = AllocateDir(XLOGDIR);
2681 2682 2683
	if (xldir == NULL)
		ereport(ERROR,
				(errcode_for_file_access(),
B
Bruce Momjian 已提交
2684 2685
				 errmsg("could not open transaction log directory \"%s\": %m",
						XLOGDIR)));
2686

2687
	while ((xlde = ReadDir(xldir, XLOGDIR)) != NULL)
2688 2689 2690 2691 2692 2693
	{
		if (strlen(xlde->d_name) > 24 &&
			strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
			strcmp(xlde->d_name + strlen(xlde->d_name) - strlen(".backup"),
				   ".backup") == 0)
		{
2694
			if (XLogArchiveCheckDone(xlde->d_name))
2695 2696
			{
				ereport(DEBUG2,
B
Bruce Momjian 已提交
2697 2698
				(errmsg("removing transaction log backup history file \"%s\"",
						xlde->d_name)));
2699
				snprintf(path, MAXPGPATH, XLOGDIR "/%s", xlde->d_name);
2700 2701 2702 2703 2704 2705 2706 2707 2708
				unlink(path);
				XLogArchiveCleanup(xlde->d_name);
			}
		}
	}

	FreeDir(xldir);
}

T
Tom Lane 已提交
2709 2710 2711 2712
/*
 * Restore the backup blocks present in an XLOG record, if any.
 *
 * We assume all of the record has been read into memory at *record.
2713 2714 2715 2716 2717 2718 2719 2720 2721
 *
 * Note: when a backup block is available in XLOG, we restore it
 * unconditionally, even if the page in the database appears newer.
 * This is to protect ourselves against database pages that were partially
 * or incorrectly written during a crash.  We assume that the XLOG data
 * must be good because it has passed a CRC check, while the database
 * page might not be.  This will force us to replay all subsequent
 * modifications of the page that appear in XLOG, rather than possibly
 * ignoring them as already applied, but that's not a huge drawback.
T
Tom Lane 已提交
2722
 */
2723 2724 2725 2726 2727 2728 2729 2730 2731 2732
static void
RestoreBkpBlocks(XLogRecord *record, XLogRecPtr lsn)
{
	Relation	reln;
	Buffer		buffer;
	Page		page;
	BkpBlock	bkpb;
	char	   *blk;
	int			i;

B
Bruce Momjian 已提交
2733
	blk = (char *) XLogRecGetData(record) + record->xl_len;
T
Tom Lane 已提交
2734
	for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
2735
	{
T
Tom Lane 已提交
2736
		if (!(record->xl_info & XLR_SET_BKP_BLOCK(i)))
2737 2738
			continue;

2739
		memcpy(&bkpb, blk, sizeof(BkpBlock));
2740 2741
		blk += sizeof(BkpBlock);

2742
		reln = XLogOpenRelation(bkpb.node);
2743 2744 2745
		buffer = XLogReadBuffer(reln, bkpb.block, true);
		Assert(BufferIsValid(buffer));
		page = (Page) BufferGetPage(buffer);
2746

2747
		if (bkpb.hole_length == 0)
2748
		{
2749 2750 2751 2752 2753 2754 2755 2756 2757 2758
			memcpy((char *) page, blk, BLCKSZ);
		}
		else
		{
			/* must zero-fill the hole */
			MemSet((char *) page, 0, BLCKSZ);
			memcpy((char *) page, blk, bkpb.hole_offset);
			memcpy((char *) page + (bkpb.hole_offset + bkpb.hole_length),
				   blk + bkpb.hole_offset,
				   BLCKSZ - (bkpb.hole_offset + bkpb.hole_length));
2759 2760
		}

2761 2762
		PageSetLSN(page, lsn);
		PageSetTLI(page, ThisTimeLineID);
2763 2764
		MarkBufferDirty(buffer);
		UnlockReleaseBuffer(buffer);
2765

2766
		blk += BLCKSZ - bkpb.hole_length;
2767 2768 2769
	}
}

T
Tom Lane 已提交
2770 2771 2772 2773 2774 2775 2776
/*
 * CRC-check an XLOG record.  We do not believe the contents of an XLOG
 * record (other than to the minimal extent of computing the amount of
 * data to read in) until we've checked the CRCs.
 *
 * We assume all of the record has been read into memory at *record.
 */
2777 2778 2779
static bool
RecordIsValid(XLogRecord *record, XLogRecPtr recptr, int emode)
{
2780
	pg_crc32	crc;
2781 2782
	int			i;
	uint32		len = record->xl_len;
2783
	BkpBlock	bkpb;
2784 2785
	char	   *blk;

2786 2787 2788
	/* First the rmgr data */
	INIT_CRC32(crc);
	COMP_CRC32(crc, XLogRecGetData(record), len);
2789

2790
	/* Add in the backup blocks, if any */
B
Bruce Momjian 已提交
2791
	blk = (char *) XLogRecGetData(record) + len;
T
Tom Lane 已提交
2792
	for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
2793
	{
B
Bruce Momjian 已提交
2794
		uint32		blen;
2795

T
Tom Lane 已提交
2796
		if (!(record->xl_info & XLR_SET_BKP_BLOCK(i)))
2797 2798
			continue;

2799 2800
		memcpy(&bkpb, blk, sizeof(BkpBlock));
		if (bkpb.hole_offset + bkpb.hole_length > BLCKSZ)
2801
		{
2802
			ereport(emode,
2803 2804 2805
					(errmsg("incorrect hole size in record at %X/%X",
							recptr.xlogid, recptr.xrecoff)));
			return false;
2806
		}
2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828
		blen = sizeof(BkpBlock) + BLCKSZ - bkpb.hole_length;
		COMP_CRC32(crc, blk, blen);
		blk += blen;
	}

	/* Check that xl_tot_len agrees with our calculation */
	if (blk != (char *) record + record->xl_tot_len)
	{
		ereport(emode,
				(errmsg("incorrect total length in record at %X/%X",
						recptr.xlogid, recptr.xrecoff)));
		return false;
	}

	/* Finally include the record header */
	COMP_CRC32(crc, (char *) record + sizeof(pg_crc32),
			   SizeOfXLogRecord - sizeof(pg_crc32));
	FIN_CRC32(crc);

	if (!EQ_CRC32(record->xl_crc, crc))
	{
		ereport(emode,
B
Bruce Momjian 已提交
2829 2830
		(errmsg("incorrect resource manager data checksum in record at %X/%X",
				recptr.xlogid, recptr.xrecoff)));
2831
		return false;
2832 2833
	}

2834
	return true;
2835 2836
}

T
Tom Lane 已提交
2837 2838 2839 2840 2841 2842
/*
 * Attempt to read an XLOG record.
 *
 * If RecPtr is not NULL, try to read a record at that position.  Otherwise
 * try to read a record just after the last one previously read.
 *
2843 2844
 * If no valid record is available, returns NULL, or fails if emode is PANIC.
 * (emode must be either PANIC or LOG.)
T
Tom Lane 已提交
2845
 *
2846 2847
 * The record is copied into readRecordBuf, so that on successful return,
 * the returned record pointer always points there.
T
Tom Lane 已提交
2848
 */
2849
static XLogRecord *
2850
ReadRecord(XLogRecPtr *RecPtr, int emode)
2851
{
2852
	XLogRecord *record;
2853
	char	   *buffer;
2854
	XLogRecPtr	tmpRecPtr = EndRecPtr;
2855
	bool		randAccess = false;
T
Tom Lane 已提交
2856 2857 2858
	uint32		len,
				total_len;
	uint32		targetPageOff;
2859 2860
	uint32		targetRecOff;
	uint32		pageHeaderSize;
T
Tom Lane 已提交
2861 2862 2863 2864

	if (readBuf == NULL)
	{
		/*
B
Bruce Momjian 已提交
2865 2866 2867 2868 2869
		 * First time through, permanently allocate readBuf.  We do it this
		 * way, rather than just making a static array, for two reasons: (1)
		 * no need to waste the storage in most instantiations of the backend;
		 * (2) a static char array isn't guaranteed to have any particular
		 * alignment, whereas malloc() will provide MAXALIGN'd storage.
T
Tom Lane 已提交
2870
		 */
2871
		readBuf = (char *) malloc(XLOG_BLCKSZ);
T
Tom Lane 已提交
2872 2873
		Assert(readBuf != NULL);
	}
2874

T
Tom Lane 已提交
2875
	if (RecPtr == NULL)
2876
	{
2877
		RecPtr = &tmpRecPtr;
T
Tom Lane 已提交
2878
		/* fast case if next record is on same page */
2879 2880 2881 2882 2883
		if (nextRecord != NULL)
		{
			record = nextRecord;
			goto got_record;
		}
T
Tom Lane 已提交
2884
		/* align old recptr to next page */
2885 2886
		if (tmpRecPtr.xrecoff % XLOG_BLCKSZ != 0)
			tmpRecPtr.xrecoff += (XLOG_BLCKSZ - tmpRecPtr.xrecoff % XLOG_BLCKSZ);
2887 2888 2889 2890 2891
		if (tmpRecPtr.xrecoff >= XLogFileSize)
		{
			(tmpRecPtr.xlogid)++;
			tmpRecPtr.xrecoff = 0;
		}
2892 2893 2894 2895 2896 2897 2898 2899
		/* We will account for page header size below */
	}
	else
	{
		if (!XRecOffIsValid(RecPtr->xrecoff))
			ereport(PANIC,
					(errmsg("invalid record offset at %X/%X",
							RecPtr->xlogid, RecPtr->xrecoff)));
B
Bruce Momjian 已提交
2900

2901
		/*
B
Bruce Momjian 已提交
2902 2903 2904 2905 2906
		 * Since we are going to a random position in WAL, forget any prior
		 * state about what timeline we were in, and allow it to be any
		 * timeline in expectedTLIs.  We also set a flag to allow curFileTLI
		 * to go backwards (but we can't reset that variable right here, since
		 * we might not change files at all).
2907 2908 2909
		 */
		lastPageTLI = 0;		/* see comment in ValidXLOGHeader */
		randAccess = true;		/* allow curFileTLI to go backwards too */
2910 2911
	}

T
Tom Lane 已提交
2912
	if (readFile >= 0 && !XLByteInSeg(*RecPtr, readId, readSeg))
2913
	{
2914 2915
		close(readFile);
		readFile = -1;
2916
	}
T
Tom Lane 已提交
2917
	XLByteToSeg(*RecPtr, readId, readSeg);
2918
	if (readFile < 0)
2919
	{
2920 2921 2922 2923 2924
		/* Now it's okay to reset curFileTLI if random fetch */
		if (randAccess)
			curFileTLI = 0;

		readFile = XLogFileRead(readId, readSeg, emode);
2925 2926
		if (readFile < 0)
			goto next_record_is_invalid;
2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945

		/*
		 * Whenever switching to a new WAL segment, we read the first page of
		 * the file and validate its header, even if that's not where the
		 * target record is.  This is so that we can check the additional
		 * identification info that is present in the first page's "long"
		 * header.
		 */
		readOff = 0;
		if (read(readFile, readBuf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
		{
			ereport(emode,
					(errcode_for_file_access(),
					 errmsg("could not read from log file %u, segment %u, offset %u: %m",
							readId, readSeg, readOff)));
			goto next_record_is_invalid;
		}
		if (!ValidXLOGHeader((XLogPageHeader) readBuf, emode))
			goto next_record_is_invalid;
2946 2947
	}

2948
	targetPageOff = ((RecPtr->xrecoff % XLogSegSize) / XLOG_BLCKSZ) * XLOG_BLCKSZ;
T
Tom Lane 已提交
2949
	if (readOff != targetPageOff)
2950
	{
T
Tom Lane 已提交
2951 2952 2953
		readOff = targetPageOff;
		if (lseek(readFile, (off_t) readOff, SEEK_SET) < 0)
		{
2954 2955
			ereport(emode,
					(errcode_for_file_access(),
2956
					 errmsg("could not seek in log file %u, segment %u to offset %u: %m",
2957
							readId, readSeg, readOff)));
T
Tom Lane 已提交
2958 2959
			goto next_record_is_invalid;
		}
2960
		if (read(readFile, readBuf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
T
Tom Lane 已提交
2961
		{
2962 2963
			ereport(emode,
					(errcode_for_file_access(),
2964
					 errmsg("could not read from log file %u, segment %u at offset %u: %m",
2965
							readId, readSeg, readOff)));
T
Tom Lane 已提交
2966 2967
			goto next_record_is_invalid;
		}
2968
		if (!ValidXLOGHeader((XLogPageHeader) readBuf, emode))
2969 2970
			goto next_record_is_invalid;
	}
2971
	pageHeaderSize = XLogPageHeaderSize((XLogPageHeader) readBuf);
2972
	targetRecOff = RecPtr->xrecoff % XLOG_BLCKSZ;
2973 2974 2975
	if (targetRecOff == 0)
	{
		/*
B
Bruce Momjian 已提交
2976 2977 2978
		 * Can only get here in the continuing-from-prev-page case, because
		 * XRecOffIsValid eliminated the zero-page-offset case otherwise. Need
		 * to skip over the new page's header.
2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989
		 */
		tmpRecPtr.xrecoff += pageHeaderSize;
		targetRecOff = pageHeaderSize;
	}
	else if (targetRecOff < pageHeaderSize)
	{
		ereport(emode,
				(errmsg("invalid record offset at %X/%X",
						RecPtr->xlogid, RecPtr->xrecoff)));
		goto next_record_is_invalid;
	}
T
Tom Lane 已提交
2990
	if ((((XLogPageHeader) readBuf)->xlp_info & XLP_FIRST_IS_CONTRECORD) &&
2991
		targetRecOff == pageHeaderSize)
2992
	{
2993 2994 2995
		ereport(emode,
				(errmsg("contrecord is requested by %X/%X",
						RecPtr->xlogid, RecPtr->xrecoff)));
2996 2997
		goto next_record_is_invalid;
	}
2998
	record = (XLogRecord *) ((char *) readBuf + RecPtr->xrecoff % XLOG_BLCKSZ);
2999 3000

got_record:;
B
Bruce Momjian 已提交
3001

T
Tom Lane 已提交
3002
	/*
B
Bruce Momjian 已提交
3003 3004
	 * xl_len == 0 is bad data for everything except XLOG SWITCH, where it is
	 * required.
T
Tom Lane 已提交
3005
	 */
3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016
	if (record->xl_rmid == RM_XLOG_ID && record->xl_info == XLOG_SWITCH)
	{
		if (record->xl_len != 0)
		{
			ereport(emode,
					(errmsg("invalid xlog switch record at %X/%X",
							RecPtr->xlogid, RecPtr->xrecoff)));
			goto next_record_is_invalid;
		}
	}
	else if (record->xl_len == 0)
3017
	{
3018 3019 3020
		ereport(emode,
				(errmsg("record with zero length at %X/%X",
						RecPtr->xlogid, RecPtr->xrecoff)));
3021 3022
		goto next_record_is_invalid;
	}
3023 3024 3025 3026 3027 3028 3029 3030 3031
	if (record->xl_tot_len < SizeOfXLogRecord + record->xl_len ||
		record->xl_tot_len > SizeOfXLogRecord + record->xl_len +
		XLR_MAX_BKP_BLOCKS * (sizeof(BkpBlock) + BLCKSZ))
	{
		ereport(emode,
				(errmsg("invalid record length at %X/%X",
						RecPtr->xlogid, RecPtr->xrecoff)));
		goto next_record_is_invalid;
	}
3032 3033 3034 3035
	if (record->xl_rmid > RM_MAX_ID)
	{
		ereport(emode,
				(errmsg("invalid resource manager ID %u at %X/%X",
B
Bruce Momjian 已提交
3036
						record->xl_rmid, RecPtr->xlogid, RecPtr->xrecoff)));
3037 3038
		goto next_record_is_invalid;
	}
3039 3040 3041
	if (randAccess)
	{
		/*
B
Bruce Momjian 已提交
3042 3043
		 * We can't exactly verify the prev-link, but surely it should be less
		 * than the record's own address.
3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056
		 */
		if (!XLByteLT(record->xl_prev, *RecPtr))
		{
			ereport(emode,
					(errmsg("record with incorrect prev-link %X/%X at %X/%X",
							record->xl_prev.xlogid, record->xl_prev.xrecoff,
							RecPtr->xlogid, RecPtr->xrecoff)));
			goto next_record_is_invalid;
		}
	}
	else
	{
		/*
B
Bruce Momjian 已提交
3057 3058 3059
		 * Record's prev-link should exactly match our previous location. This
		 * check guards against torn WAL pages where a stale but valid-looking
		 * WAL record starts on a sector boundary.
3060 3061 3062 3063 3064 3065 3066 3067 3068 3069
		 */
		if (!XLByteEQ(record->xl_prev, ReadRecPtr))
		{
			ereport(emode,
					(errmsg("record with incorrect prev-link %X/%X at %X/%X",
							record->xl_prev.xlogid, record->xl_prev.xrecoff,
							RecPtr->xlogid, RecPtr->xrecoff)));
			goto next_record_is_invalid;
		}
	}
B
Bruce Momjian 已提交
3070

T
Tom Lane 已提交
3071
	/*
B
Bruce Momjian 已提交
3072
	 * Allocate or enlarge readRecordBuf as needed.  To avoid useless small
3073 3074 3075 3076
	 * increases, round its size to a multiple of XLOG_BLCKSZ, and make sure
	 * it's at least 4*Max(BLCKSZ, XLOG_BLCKSZ) to start with.  (That is
	 * enough for all "normal" records, but very large commit or abort records
	 * might need more space.)
T
Tom Lane 已提交
3077
	 */
3078
	total_len = record->xl_tot_len;
3079
	if (total_len > readRecordBufSize)
3080
	{
3081 3082
		uint32		newSize = total_len;

3083 3084
		newSize += XLOG_BLCKSZ - (newSize % XLOG_BLCKSZ);
		newSize = Max(newSize, 4 * Max(BLCKSZ, XLOG_BLCKSZ));
3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097
		if (readRecordBuf)
			free(readRecordBuf);
		readRecordBuf = (char *) malloc(newSize);
		if (!readRecordBuf)
		{
			readRecordBufSize = 0;
			/* We treat this as a "bogus data" condition */
			ereport(emode,
					(errmsg("record length %u at %X/%X too long",
							total_len, RecPtr->xlogid, RecPtr->xrecoff)));
			goto next_record_is_invalid;
		}
		readRecordBufSize = newSize;
3098
	}
3099 3100

	buffer = readRecordBuf;
3101
	nextRecord = NULL;
3102
	len = XLOG_BLCKSZ - RecPtr->xrecoff % XLOG_BLCKSZ;
T
Tom Lane 已提交
3103
	if (total_len > len)
3104
	{
T
Tom Lane 已提交
3105 3106
		/* Need to reassemble record */
		XLogContRecord *contrecord;
B
Bruce Momjian 已提交
3107
		uint32		gotlen = len;
3108

T
Tom Lane 已提交
3109
		memcpy(buffer, record, len);
3110
		record = (XLogRecord *) buffer;
T
Tom Lane 已提交
3111
		buffer += len;
3112
		for (;;)
3113
		{
3114
			readOff += XLOG_BLCKSZ;
T
Tom Lane 已提交
3115
			if (readOff >= XLogSegSize)
3116 3117
			{
				close(readFile);
T
Tom Lane 已提交
3118 3119
				readFile = -1;
				NextLogSeg(readId, readSeg);
3120
				readFile = XLogFileRead(readId, readSeg, emode);
3121 3122
				if (readFile < 0)
					goto next_record_is_invalid;
T
Tom Lane 已提交
3123
				readOff = 0;
3124
			}
3125
			if (read(readFile, readBuf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
T
Tom Lane 已提交
3126
			{
3127 3128
				ereport(emode,
						(errcode_for_file_access(),
T
Tom Lane 已提交
3129
						 errmsg("could not read from log file %u, segment %u, offset %u: %m",
3130
								readId, readSeg, readOff)));
T
Tom Lane 已提交
3131 3132
				goto next_record_is_invalid;
			}
3133
			if (!ValidXLOGHeader((XLogPageHeader) readBuf, emode))
3134
				goto next_record_is_invalid;
T
Tom Lane 已提交
3135
			if (!(((XLogPageHeader) readBuf)->xlp_info & XLP_FIRST_IS_CONTRECORD))
3136
			{
3137 3138 3139
				ereport(emode,
						(errmsg("there is no contrecord flag in log file %u, segment %u, offset %u",
								readId, readSeg, readOff)));
3140 3141
				goto next_record_is_invalid;
			}
3142 3143
			pageHeaderSize = XLogPageHeaderSize((XLogPageHeader) readBuf);
			contrecord = (XLogContRecord *) ((char *) readBuf + pageHeaderSize);
B
Bruce Momjian 已提交
3144
			if (contrecord->xl_rem_len == 0 ||
T
Tom Lane 已提交
3145
				total_len != (contrecord->xl_rem_len + gotlen))
3146
			{
3147 3148 3149 3150
				ereport(emode,
						(errmsg("invalid contrecord length %u in log file %u, segment %u, offset %u",
								contrecord->xl_rem_len,
								readId, readSeg, readOff)));
3151 3152
				goto next_record_is_invalid;
			}
3153
			len = XLOG_BLCKSZ - pageHeaderSize - SizeOfXLogContRecord;
T
Tom Lane 已提交
3154
			if (contrecord->xl_rem_len > len)
3155
			{
B
Bruce Momjian 已提交
3156
				memcpy(buffer, (char *) contrecord + SizeOfXLogContRecord, len);
T
Tom Lane 已提交
3157 3158 3159 3160 3161 3162 3163 3164 3165 3166
				gotlen += len;
				buffer += len;
				continue;
			}
			memcpy(buffer, (char *) contrecord + SizeOfXLogContRecord,
				   contrecord->xl_rem_len);
			break;
		}
		if (!RecordIsValid(record, *RecPtr, emode))
			goto next_record_is_invalid;
3167
		pageHeaderSize = XLogPageHeaderSize((XLogPageHeader) readBuf);
3168
		if (XLOG_BLCKSZ - SizeOfXLogRecord >= pageHeaderSize +
3169
			MAXALIGN(SizeOfXLogContRecord + contrecord->xl_rem_len))
T
Tom Lane 已提交
3170
		{
B
Bruce Momjian 已提交
3171
			nextRecord = (XLogRecord *) ((char *) contrecord +
B
Bruce Momjian 已提交
3172
					MAXALIGN(SizeOfXLogContRecord + contrecord->xl_rem_len));
T
Tom Lane 已提交
3173 3174 3175
		}
		EndRecPtr.xlogid = readId;
		EndRecPtr.xrecoff = readSeg * XLogSegSize + readOff +
3176 3177
			pageHeaderSize +
			MAXALIGN(SizeOfXLogContRecord + contrecord->xl_rem_len);
T
Tom Lane 已提交
3178
		ReadRecPtr = *RecPtr;
3179
		/* needn't worry about XLOG SWITCH, it can't cross page boundaries */
T
Tom Lane 已提交
3180
		return record;
3181 3182
	}

T
Tom Lane 已提交
3183 3184 3185
	/* Record does not cross a page boundary */
	if (!RecordIsValid(record, *RecPtr, emode))
		goto next_record_is_invalid;
3186
	if (XLOG_BLCKSZ - SizeOfXLogRecord >= RecPtr->xrecoff % XLOG_BLCKSZ +
T
Tom Lane 已提交
3187 3188 3189 3190 3191 3192
		MAXALIGN(total_len))
		nextRecord = (XLogRecord *) ((char *) record + MAXALIGN(total_len));
	EndRecPtr.xlogid = RecPtr->xlogid;
	EndRecPtr.xrecoff = RecPtr->xrecoff + MAXALIGN(total_len);
	ReadRecPtr = *RecPtr;
	memcpy(buffer, record, total_len);
B
Bruce Momjian 已提交
3193

3194 3195 3196 3197 3198 3199 3200 3201 3202
	/*
	 * Special processing if it's an XLOG SWITCH record
	 */
	if (record->xl_rmid == RM_XLOG_ID && record->xl_info == XLOG_SWITCH)
	{
		/* Pretend it extends to end of segment */
		EndRecPtr.xrecoff += XLogSegSize - 1;
		EndRecPtr.xrecoff -= EndRecPtr.xrecoff % XLogSegSize;
		nextRecord = NULL;		/* definitely not on same page */
B
Bruce Momjian 已提交
3203

3204
		/*
B
Bruce Momjian 已提交
3205 3206 3207
		 * Pretend that readBuf contains the last page of the segment. This is
		 * just to avoid Assert failure in StartupXLOG if XLOG ends with this
		 * segment.
3208 3209 3210
		 */
		readOff = XLogSegSize - XLOG_BLCKSZ;
	}
T
Tom Lane 已提交
3211
	return (XLogRecord *) buffer;
3212

T
Tom Lane 已提交
3213 3214 3215 3216 3217
next_record_is_invalid:;
	close(readFile);
	readFile = -1;
	nextRecord = NULL;
	return NULL;
3218 3219
}

3220 3221 3222 3223
/*
 * Check whether the xlog header of a page just read in looks valid.
 *
 * This is just a convenience subroutine to avoid duplicated code in
B
Bruce Momjian 已提交
3224
 * ReadRecord.	It's not intended for use from anywhere else.
3225 3226
 */
static bool
3227
ValidXLOGHeader(XLogPageHeader hdr, int emode)
3228
{
3229 3230
	XLogRecPtr	recaddr;

3231 3232
	if (hdr->xlp_magic != XLOG_PAGE_MAGIC)
	{
3233 3234 3235
		ereport(emode,
				(errmsg("invalid magic number %04X in log file %u, segment %u, offset %u",
						hdr->xlp_magic, readId, readSeg, readOff)));
3236 3237 3238 3239
		return false;
	}
	if ((hdr->xlp_info & ~XLP_ALL_FLAGS) != 0)
	{
3240 3241 3242
		ereport(emode,
				(errmsg("invalid info bits %04X in log file %u, segment %u, offset %u",
						hdr->xlp_info, readId, readSeg, readOff)));
3243 3244
		return false;
	}
3245
	if (hdr->xlp_info & XLP_LONG_HEADER)
3246
	{
3247
		XLogLongPageHeader longhdr = (XLogLongPageHeader) hdr;
B
Bruce Momjian 已提交
3248

3249
		if (longhdr->xlp_sysid != ControlFile->system_identifier)
3250
		{
3251 3252
			char		fhdrident_str[32];
			char		sysident_str[32];
3253

3254
			/*
B
Bruce Momjian 已提交
3255 3256
			 * Format sysids separately to keep platform-dependent format code
			 * out of the translatable message string.
3257 3258 3259 3260 3261 3262 3263
			 */
			snprintf(fhdrident_str, sizeof(fhdrident_str), UINT64_FORMAT,
					 longhdr->xlp_sysid);
			snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
					 ControlFile->system_identifier);
			ereport(emode,
					(errmsg("WAL file is from different system"),
B
Bruce Momjian 已提交
3264 3265
					 errdetail("WAL file SYSID is %s, pg_control SYSID is %s",
							   fhdrident_str, sysident_str)));
3266 3267 3268 3269 3270 3271
			return false;
		}
		if (longhdr->xlp_seg_size != XLogSegSize)
		{
			ereport(emode,
					(errmsg("WAL file is from different system"),
B
Bruce Momjian 已提交
3272
					 errdetail("Incorrect XLOG_SEG_SIZE in page header.")));
3273 3274
			return false;
		}
3275 3276 3277 3278 3279 3280 3281
		if (longhdr->xlp_xlog_blcksz != XLOG_BLCKSZ)
		{
			ereport(emode,
					(errmsg("WAL file is from different system"),
					 errdetail("Incorrect XLOG_BLCKSZ in page header.")));
			return false;
		}
3282
	}
3283 3284 3285 3286 3287 3288 3289 3290 3291
	else if (readOff == 0)
	{
		/* hmm, first page of file doesn't have a long header? */
		ereport(emode,
				(errmsg("invalid info bits %04X in log file %u, segment %u, offset %u",
						hdr->xlp_info, readId, readSeg, readOff)));
		return false;
	}

3292 3293 3294 3295 3296 3297
	recaddr.xlogid = readId;
	recaddr.xrecoff = readSeg * XLogSegSize + readOff;
	if (!XLByteEQ(hdr->xlp_pageaddr, recaddr))
	{
		ereport(emode,
				(errmsg("unexpected pageaddr %X/%X in log file %u, segment %u, offset %u",
B
Bruce Momjian 已提交
3298
						hdr->xlp_pageaddr.xlogid, hdr->xlp_pageaddr.xrecoff,
3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319
						readId, readSeg, readOff)));
		return false;
	}

	/*
	 * Check page TLI is one of the expected values.
	 */
	if (!list_member_int(expectedTLIs, (int) hdr->xlp_tli))
	{
		ereport(emode,
				(errmsg("unexpected timeline ID %u in log file %u, segment %u, offset %u",
						hdr->xlp_tli,
						readId, readSeg, readOff)));
		return false;
	}

	/*
	 * Since child timelines are always assigned a TLI greater than their
	 * immediate parent's TLI, we should never see TLI go backwards across
	 * successive pages of a consistent WAL sequence.
	 *
B
Bruce Momjian 已提交
3320 3321 3322
	 * Of course this check should only be applied when advancing sequentially
	 * across pages; therefore ReadRecord resets lastPageTLI to zero when
	 * going to a random page.
3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339
	 */
	if (hdr->xlp_tli < lastPageTLI)
	{
		ereport(emode,
				(errmsg("out-of-sequence timeline ID %u (after %u) in log file %u, segment %u, offset %u",
						hdr->xlp_tli, lastPageTLI,
						readId, readSeg, readOff)));
		return false;
	}
	lastPageTLI = hdr->xlp_tli;
	return true;
}

/*
 * Try to read a timeline's history file.
 *
 * If successful, return the list of component TLIs (the given TLI followed by
B
Bruce Momjian 已提交
3340
 * its ancestor TLIs).	If we can't find the history file, assume that the
3341 3342 3343 3344 3345 3346 3347 3348 3349 3350
 * timeline has no parents, and return a list of just the specified timeline
 * ID.
 */
static List *
readTimeLineHistory(TimeLineID targetTLI)
{
	List	   *result;
	char		path[MAXPGPATH];
	char		histfname[MAXFNAMELEN];
	char		fline[MAXPGPATH];
B
Bruce Momjian 已提交
3351
	FILE	   *fd;
3352 3353 3354 3355

	if (InArchiveRecovery)
	{
		TLHistoryFileName(histfname, targetTLI);
3356
		RestoreArchivedFile(path, histfname, "RECOVERYHISTORY", 0);
3357 3358 3359 3360
	}
	else
		TLHistoryFilePath(path, targetTLI);

B
Bruce Momjian 已提交
3361
	fd = AllocateFile(path, "r");
3362 3363 3364 3365 3366
	if (fd == NULL)
	{
		if (errno != ENOENT)
			ereport(FATAL,
					(errcode_for_file_access(),
P
Peter Eisentraut 已提交
3367
					 errmsg("could not open file \"%s\": %m", path)));
3368 3369 3370 3371 3372 3373
		/* Not there, so assume no parents */
		return list_make1_int((int) targetTLI);
	}

	result = NIL;

B
Bruce Momjian 已提交
3374 3375 3376 3377
	/*
	 * Parse the file...
	 */
	while (fgets(fline, MAXPGPATH, fd) != NULL)
3378 3379
	{
		/* skip leading whitespace and check for # comment */
B
Bruce Momjian 已提交
3380 3381 3382
		char	   *ptr;
		char	   *endptr;
		TimeLineID	tli;
3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402

		for (ptr = fline; *ptr; ptr++)
		{
			if (!isspace((unsigned char) *ptr))
				break;
		}
		if (*ptr == '\0' || *ptr == '#')
			continue;

		/* expect a numeric timeline ID as first field of line */
		tli = (TimeLineID) strtoul(ptr, &endptr, 0);
		if (endptr == ptr)
			ereport(FATAL,
					(errmsg("syntax error in history file: %s", fline),
					 errhint("Expected a numeric timeline ID.")));

		if (result &&
			tli <= (TimeLineID) linitial_int(result))
			ereport(FATAL,
					(errmsg("invalid data in history file: %s", fline),
B
Bruce Momjian 已提交
3403
				   errhint("Timeline IDs must be in increasing sequence.")));
3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416

		/* Build list with newest item first */
		result = lcons_int((int) tli, result);

		/* we ignore the remainder of each line */
	}

	FreeFile(fd);

	if (result &&
		targetTLI <= (TimeLineID) linitial_int(result))
		ereport(FATAL,
				(errmsg("invalid data in history file \"%s\"", path),
B
Bruce Momjian 已提交
3417
			errhint("Timeline IDs must be less than child timeline's ID.")));
3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435

	result = lcons_int((int) targetTLI, result);

	ereport(DEBUG3,
			(errmsg_internal("history of timeline %u is %s",
							 targetTLI, nodeToString(result))));

	return result;
}

/*
 * Probe whether a timeline history file exists for the given timeline ID
 */
static bool
existsTimeLineHistory(TimeLineID probeTLI)
{
	char		path[MAXPGPATH];
	char		histfname[MAXFNAMELEN];
B
Bruce Momjian 已提交
3436
	FILE	   *fd;
3437 3438 3439 3440

	if (InArchiveRecovery)
	{
		TLHistoryFileName(histfname, probeTLI);
3441
		RestoreArchivedFile(path, histfname, "RECOVERYHISTORY", 0);
3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456
	}
	else
		TLHistoryFilePath(path, probeTLI);

	fd = AllocateFile(path, "r");
	if (fd != NULL)
	{
		FreeFile(fd);
		return true;
	}
	else
	{
		if (errno != ENOENT)
			ereport(FATAL,
					(errcode_for_file_access(),
P
Peter Eisentraut 已提交
3457
					 errmsg("could not open file \"%s\": %m", path)));
3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475
		return false;
	}
}

/*
 * Find the newest existing timeline, assuming that startTLI exists.
 *
 * Note: while this is somewhat heuristic, it does positively guarantee
 * that (result + 1) is not a known timeline, and therefore it should
 * be safe to assign that ID to a new timeline.
 */
static TimeLineID
findNewestTimeLine(TimeLineID startTLI)
{
	TimeLineID	newestTLI;
	TimeLineID	probeTLI;

	/*
B
Bruce Momjian 已提交
3476 3477
	 * The algorithm is just to probe for the existence of timeline history
	 * files.  XXX is it useful to allow gaps in the sequence?
3478 3479 3480
	 */
	newestTLI = startTLI;

B
Bruce Momjian 已提交
3481
	for (probeTLI = startTLI + 1;; probeTLI++)
3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504
	{
		if (existsTimeLineHistory(probeTLI))
		{
			newestTLI = probeTLI;		/* probeTLI exists */
		}
		else
		{
			/* doesn't exist, assume we're done */
			break;
		}
	}

	return newestTLI;
}

/*
 * Create a new timeline history file.
 *
 *	newTLI: ID of the new timeline
 *	parentTLI: ID of its immediate parent
 *	endTLI et al: ID of the last used WAL file, for annotation purposes
 *
 * Currently this is only used during recovery, and so there are no locking
B
Bruce Momjian 已提交
3505
 * considerations.	But we should be just as tense as XLogFileInit to avoid
3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520
 * emplacing a bogus file.
 */
static void
writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI,
					 TimeLineID endTLI, uint32 endLogId, uint32 endLogSeg)
{
	char		path[MAXPGPATH];
	char		tmppath[MAXPGPATH];
	char		histfname[MAXFNAMELEN];
	char		xlogfname[MAXFNAMELEN];
	char		buffer[BLCKSZ];
	int			srcfd;
	int			fd;
	int			nbytes;

B
Bruce Momjian 已提交
3521
	Assert(newTLI > parentTLI); /* else bad selection of newTLI */
3522 3523 3524 3525

	/*
	 * Write into a temp file name.
	 */
3526
	snprintf(tmppath, MAXPGPATH, XLOGDIR "/xlogtemp.%d", (int) getpid());
3527 3528 3529 3530 3531 3532 3533

	unlink(tmppath);

	/* do not use XLOG_SYNC_BIT here --- want to fsync only at end of fill */
	fd = BasicOpenFile(tmppath, O_RDWR | O_CREAT | O_EXCL,
					   S_IRUSR | S_IWUSR);
	if (fd < 0)
3534
		ereport(ERROR,
3535 3536 3537 3538 3539 3540 3541 3542 3543
				(errcode_for_file_access(),
				 errmsg("could not create file \"%s\": %m", tmppath)));

	/*
	 * If a history file exists for the parent, copy it verbatim
	 */
	if (InArchiveRecovery)
	{
		TLHistoryFileName(histfname, parentTLI);
3544
		RestoreArchivedFile(path, histfname, "RECOVERYHISTORY", 0);
3545 3546 3547 3548 3549 3550 3551 3552
	}
	else
		TLHistoryFilePath(path, parentTLI);

	srcfd = BasicOpenFile(path, O_RDONLY, 0);
	if (srcfd < 0)
	{
		if (errno != ENOENT)
3553
			ereport(ERROR,
3554
					(errcode_for_file_access(),
P
Peter Eisentraut 已提交
3555
					 errmsg("could not open file \"%s\": %m", path)));
3556 3557 3558 3559 3560 3561 3562 3563 3564
		/* Not there, so assume parent has no parents */
	}
	else
	{
		for (;;)
		{
			errno = 0;
			nbytes = (int) read(srcfd, buffer, sizeof(buffer));
			if (nbytes < 0 || errno != 0)
3565
				ereport(ERROR,
3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579
						(errcode_for_file_access(),
						 errmsg("could not read file \"%s\": %m", path)));
			if (nbytes == 0)
				break;
			errno = 0;
			if ((int) write(fd, buffer, nbytes) != nbytes)
			{
				int			save_errno = errno;

				/*
				 * If we fail to make the file, delete it to release disk
				 * space
				 */
				unlink(tmppath);
B
Bruce Momjian 已提交
3580 3581

				/*
B
Bruce Momjian 已提交
3582
				 * if write didn't set errno, assume problem is no disk space
B
Bruce Momjian 已提交
3583
				 */
3584 3585
				errno = save_errno ? save_errno : ENOSPC;

3586
				ereport(ERROR,
3587
						(errcode_for_file_access(),
B
Bruce Momjian 已提交
3588
					 errmsg("could not write to file \"%s\": %m", tmppath)));
3589 3590 3591 3592 3593 3594 3595 3596
			}
		}
		close(srcfd);
	}

	/*
	 * Append one line with the details of this timeline split.
	 *
B
Bruce Momjian 已提交
3597 3598
	 * If we did have a parent file, insert an extra newline just in case the
	 * parent file failed to end with one.
3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617
	 */
	XLogFileName(xlogfname, endTLI, endLogId, endLogSeg);

	snprintf(buffer, sizeof(buffer),
			 "%s%u\t%s\t%s transaction %u at %s\n",
			 (srcfd < 0) ? "" : "\n",
			 parentTLI,
			 xlogfname,
			 recoveryStopAfter ? "after" : "before",
			 recoveryStopXid,
			 str_time(recoveryStopTime));

	nbytes = strlen(buffer);
	errno = 0;
	if ((int) write(fd, buffer, nbytes) != nbytes)
	{
		int			save_errno = errno;

		/*
B
Bruce Momjian 已提交
3618
		 * If we fail to make the file, delete it to release disk space
3619 3620 3621 3622 3623
		 */
		unlink(tmppath);
		/* if write didn't set errno, assume problem is no disk space */
		errno = save_errno ? save_errno : ENOSPC;

3624
		ereport(ERROR,
3625 3626 3627 3628 3629
				(errcode_for_file_access(),
				 errmsg("could not write to file \"%s\": %m", tmppath)));
	}

	if (pg_fsync(fd) != 0)
3630
		ereport(ERROR,
3631 3632 3633 3634
				(errcode_for_file_access(),
				 errmsg("could not fsync file \"%s\": %m", tmppath)));

	if (close(fd))
3635
		ereport(ERROR,
3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651
				(errcode_for_file_access(),
				 errmsg("could not close file \"%s\": %m", tmppath)));


	/*
	 * Now move the completed history file into place with its final name.
	 */
	TLHistoryFilePath(path, newTLI);

	/*
	 * Prefer link() to rename() here just to be really sure that we don't
	 * overwrite an existing logfile.  However, there shouldn't be one, so
	 * rename() is an acceptable substitute except for the truly paranoid.
	 */
#if HAVE_WORKING_LINK
	if (link(tmppath, path) < 0)
3652
		ereport(ERROR,
3653 3654 3655 3656 3657 3658
				(errcode_for_file_access(),
				 errmsg("could not link file \"%s\" to \"%s\": %m",
						tmppath, path)));
	unlink(tmppath);
#else
	if (rename(tmppath, path) < 0)
3659
		ereport(ERROR,
3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671
				(errcode_for_file_access(),
				 errmsg("could not rename file \"%s\" to \"%s\": %m",
						tmppath, path)));
#endif

	/* The history file can be archived immediately. */
	TLHistoryFileName(histfname, newTLI);
	XLogArchiveNotify(histfname);
}

/*
 * I/O routines for pg_control
3672 3673
 *
 * *ControlFile is a buffer in shared memory that holds an image of the
B
Bruce Momjian 已提交
3674
 * contents of pg_control.	WriteControlFile() initializes pg_control
3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687
 * given a preloaded buffer, ReadControlFile() loads the buffer from
 * the pg_control file (during postmaster or standalone-backend startup),
 * and UpdateControlFile() rewrites pg_control after we modify xlog state.
 *
 * For simplicity, WriteControlFile() initializes the fields of pg_control
 * that are related to checking backend/database compatibility, and
 * ReadControlFile() verifies they are correct.  We could split out the
 * I/O and compatibility-check functions, but there seems no need currently.
 */
static void
WriteControlFile(void)
{
	int			fd;
B
Bruce Momjian 已提交
3688
	char		buffer[PG_CONTROL_SIZE];		/* need not be aligned */
3689 3690 3691
	char	   *localeptr;

	/*
T
Tom Lane 已提交
3692
	 * Initialize version and compatibility-check fields
3693
	 */
T
Tom Lane 已提交
3694 3695
	ControlFile->pg_control_version = PG_CONTROL_VERSION;
	ControlFile->catalog_version_no = CATALOG_VERSION_NO;
3696 3697 3698 3699

	ControlFile->maxAlign = MAXIMUM_ALIGNOF;
	ControlFile->floatFormat = FLOATFORMAT_VALUE;

3700 3701
	ControlFile->blcksz = BLCKSZ;
	ControlFile->relseg_size = RELSEG_SIZE;
3702
	ControlFile->xlog_blcksz = XLOG_BLCKSZ;
3703
	ControlFile->xlog_seg_size = XLOG_SEG_SIZE;
3704 3705

	ControlFile->nameDataLen = NAMEDATALEN;
3706
	ControlFile->indexMaxKeys = INDEX_MAX_KEYS;
3707 3708 3709 3710 3711 3712 3713 3714

#ifdef HAVE_INT64_TIMESTAMP
	ControlFile->enableIntTimes = TRUE;
#else
	ControlFile->enableIntTimes = FALSE;
#endif

	ControlFile->localeBuflen = LOCALE_NAME_BUFLEN;
3715 3716
	localeptr = setlocale(LC_COLLATE, NULL);
	if (!localeptr)
3717 3718
		ereport(PANIC,
				(errmsg("invalid LC_COLLATE setting")));
3719 3720 3721
	StrNCpy(ControlFile->lc_collate, localeptr, LOCALE_NAME_BUFLEN);
	localeptr = setlocale(LC_CTYPE, NULL);
	if (!localeptr)
3722 3723
		ereport(PANIC,
				(errmsg("invalid LC_CTYPE setting")));
3724
	StrNCpy(ControlFile->lc_ctype, localeptr, LOCALE_NAME_BUFLEN);
B
Bruce Momjian 已提交
3725

T
Tom Lane 已提交
3726
	/* Contents are protected with a CRC */
3727 3728 3729 3730 3731
	INIT_CRC32(ControlFile->crc);
	COMP_CRC32(ControlFile->crc,
			   (char *) ControlFile,
			   offsetof(ControlFileData, crc));
	FIN_CRC32(ControlFile->crc);
T
Tom Lane 已提交
3732

3733
	/*
3734 3735 3736 3737 3738
	 * We write out PG_CONTROL_SIZE bytes into pg_control, zero-padding the
	 * excess over sizeof(ControlFileData).  This reduces the odds of
	 * premature-EOF errors when reading pg_control.  We'll still fail when we
	 * check the contents of the file, but hopefully with a more specific
	 * error than "couldn't read pg_control".
3739
	 */
3740 3741
	if (sizeof(ControlFileData) > PG_CONTROL_SIZE)
		elog(PANIC, "sizeof(ControlFileData) is larger than PG_CONTROL_SIZE; fix either one");
3742

3743
	memset(buffer, 0, PG_CONTROL_SIZE);
3744 3745
	memcpy(buffer, ControlFile, sizeof(ControlFileData));

3746 3747
	fd = BasicOpenFile(XLOG_CONTROL_FILE,
					   O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
3748
					   S_IRUSR | S_IWUSR);
3749
	if (fd < 0)
3750 3751 3752
		ereport(PANIC,
				(errcode_for_file_access(),
				 errmsg("could not create control file \"%s\": %m",
3753
						XLOG_CONTROL_FILE)));
3754

3755
	errno = 0;
3756
	if (write(fd, buffer, PG_CONTROL_SIZE) != PG_CONTROL_SIZE)
3757 3758 3759 3760
	{
		/* if write didn't set errno, assume problem is no disk space */
		if (errno == 0)
			errno = ENOSPC;
3761 3762
		ereport(PANIC,
				(errcode_for_file_access(),
3763
				 errmsg("could not write to control file: %m")));
3764
	}
3765

3766
	if (pg_fsync(fd) != 0)
3767 3768
		ereport(PANIC,
				(errcode_for_file_access(),
3769
				 errmsg("could not fsync control file: %m")));
3770

3771 3772 3773 3774
	if (close(fd))
		ereport(PANIC,
				(errcode_for_file_access(),
				 errmsg("could not close control file: %m")));
3775 3776 3777 3778 3779
}

static void
ReadControlFile(void)
{
3780
	pg_crc32	crc;
3781 3782 3783 3784 3785
	int			fd;

	/*
	 * Read data...
	 */
3786 3787 3788
	fd = BasicOpenFile(XLOG_CONTROL_FILE,
					   O_RDWR | PG_BINARY,
					   S_IRUSR | S_IWUSR);
3789
	if (fd < 0)
3790 3791 3792
		ereport(PANIC,
				(errcode_for_file_access(),
				 errmsg("could not open control file \"%s\": %m",
3793
						XLOG_CONTROL_FILE)));
3794 3795

	if (read(fd, ControlFile, sizeof(ControlFileData)) != sizeof(ControlFileData))
3796 3797
		ereport(PANIC,
				(errcode_for_file_access(),
3798
				 errmsg("could not read from control file: %m")));
3799 3800 3801

	close(fd);

T
Tom Lane 已提交
3802
	/*
B
Bruce Momjian 已提交
3803 3804 3805 3806
	 * Check for expected pg_control format version.  If this is wrong, the
	 * CRC check will likely fail because we'll be checking the wrong number
	 * of bytes.  Complaining about wrong version will probably be more
	 * enlightening than complaining about wrong CRC.
T
Tom Lane 已提交
3807 3808
	 */
	if (ControlFile->pg_control_version != PG_CONTROL_VERSION)
3809 3810 3811
		ereport(FATAL,
				(errmsg("database files are incompatible with server"),
				 errdetail("The database cluster was initialized with PG_CONTROL_VERSION %d,"
B
Bruce Momjian 已提交
3812 3813
				  " but the server was compiled with PG_CONTROL_VERSION %d.",
						ControlFile->pg_control_version, PG_CONTROL_VERSION),
3814
				 errhint("It looks like you need to initdb.")));
T
Tom Lane 已提交
3815
	/* Now check the CRC. */
3816 3817 3818 3819 3820
	INIT_CRC32(crc);
	COMP_CRC32(crc,
			   (char *) ControlFile,
			   offsetof(ControlFileData, crc));
	FIN_CRC32(crc);
3821

3822
	if (!EQ_CRC32(crc, ControlFile->crc))
3823
		ereport(FATAL,
3824
				(errmsg("incorrect checksum in control file")));
3825

3826
	/*
B
Bruce Momjian 已提交
3827
	 * Do compatibility checking immediately.  We do this here for 2 reasons:
3828
	 *
3829 3830
	 * (1) if the database isn't compatible with the backend executable, we
	 * want to abort before we can possibly do any damage;
3831 3832
	 *
	 * (2) this code is executed in the postmaster, so the setlocale() will
B
Bruce Momjian 已提交
3833 3834
	 * propagate to forked backends, which aren't going to read this file for
	 * themselves.	(These locale settings are considered critical
3835 3836
	 * compatibility items because they can affect sort order of indexes.)
	 */
T
Tom Lane 已提交
3837
	if (ControlFile->catalog_version_no != CATALOG_VERSION_NO)
3838 3839 3840
		ereport(FATAL,
				(errmsg("database files are incompatible with server"),
				 errdetail("The database cluster was initialized with CATALOG_VERSION_NO %d,"
B
Bruce Momjian 已提交
3841 3842
				  " but the server was compiled with CATALOG_VERSION_NO %d.",
						ControlFile->catalog_version_no, CATALOG_VERSION_NO),
3843
				 errhint("It looks like you need to initdb.")));
3844 3845 3846
	if (ControlFile->maxAlign != MAXIMUM_ALIGNOF)
		ereport(FATAL,
				(errmsg("database files are incompatible with server"),
B
Bruce Momjian 已提交
3847 3848 3849 3850
		   errdetail("The database cluster was initialized with MAXALIGN %d,"
					 " but the server was compiled with MAXALIGN %d.",
					 ControlFile->maxAlign, MAXIMUM_ALIGNOF),
				 errhint("It looks like you need to initdb.")));
3851 3852 3853
	if (ControlFile->floatFormat != FLOATFORMAT_VALUE)
		ereport(FATAL,
				(errmsg("database files are incompatible with server"),
P
Peter Eisentraut 已提交
3854
				 errdetail("The database cluster appears to use a different floating-point number format than the server executable."),
3855
				 errhint("It looks like you need to initdb.")));
3856
	if (ControlFile->blcksz != BLCKSZ)
3857 3858
		ereport(FATAL,
				(errmsg("database files are incompatible with server"),
B
Bruce Momjian 已提交
3859 3860 3861 3862
			 errdetail("The database cluster was initialized with BLCKSZ %d,"
					   " but the server was compiled with BLCKSZ %d.",
					   ControlFile->blcksz, BLCKSZ),
				 errhint("It looks like you need to recompile or initdb.")));
3863
	if (ControlFile->relseg_size != RELSEG_SIZE)
3864 3865
		ereport(FATAL,
				(errmsg("database files are incompatible with server"),
B
Bruce Momjian 已提交
3866 3867 3868 3869
		errdetail("The database cluster was initialized with RELSEG_SIZE %d,"
				  " but the server was compiled with RELSEG_SIZE %d.",
				  ControlFile->relseg_size, RELSEG_SIZE),
				 errhint("It looks like you need to recompile or initdb.")));
3870 3871 3872
	if (ControlFile->xlog_blcksz != XLOG_BLCKSZ)
		ereport(FATAL,
				(errmsg("database files are incompatible with server"),
B
Bruce Momjian 已提交
3873 3874 3875
		errdetail("The database cluster was initialized with XLOG_BLCKSZ %d,"
				  " but the server was compiled with XLOG_BLCKSZ %d.",
				  ControlFile->xlog_blcksz, XLOG_BLCKSZ),
3876
				 errhint("It looks like you need to recompile or initdb.")));
3877 3878 3879 3880
	if (ControlFile->xlog_seg_size != XLOG_SEG_SIZE)
		ereport(FATAL,
				(errmsg("database files are incompatible with server"),
				 errdetail("The database cluster was initialized with XLOG_SEG_SIZE %d,"
B
Bruce Momjian 已提交
3881
					   " but the server was compiled with XLOG_SEG_SIZE %d.",
3882
						   ControlFile->xlog_seg_size, XLOG_SEG_SIZE),
B
Bruce Momjian 已提交
3883
				 errhint("It looks like you need to recompile or initdb.")));
3884
	if (ControlFile->nameDataLen != NAMEDATALEN)
3885 3886
		ereport(FATAL,
				(errmsg("database files are incompatible with server"),
B
Bruce Momjian 已提交
3887 3888 3889 3890
		errdetail("The database cluster was initialized with NAMEDATALEN %d,"
				  " but the server was compiled with NAMEDATALEN %d.",
				  ControlFile->nameDataLen, NAMEDATALEN),
				 errhint("It looks like you need to recompile or initdb.")));
3891
	if (ControlFile->indexMaxKeys != INDEX_MAX_KEYS)
3892 3893
		ereport(FATAL,
				(errmsg("database files are incompatible with server"),
3894
				 errdetail("The database cluster was initialized with INDEX_MAX_KEYS %d,"
B
Bruce Momjian 已提交
3895
					  " but the server was compiled with INDEX_MAX_KEYS %d.",
3896
						   ControlFile->indexMaxKeys, INDEX_MAX_KEYS),
B
Bruce Momjian 已提交
3897
				 errhint("It looks like you need to recompile or initdb.")));
3898 3899 3900

#ifdef HAVE_INT64_TIMESTAMP
	if (ControlFile->enableIntTimes != TRUE)
3901 3902 3903
		ereport(FATAL,
				(errmsg("database files are incompatible with server"),
				 errdetail("The database cluster was initialized without HAVE_INT64_TIMESTAMP"
B
Bruce Momjian 已提交
3904 3905
				  " but the server was compiled with HAVE_INT64_TIMESTAMP."),
				 errhint("It looks like you need to recompile or initdb.")));
3906 3907
#else
	if (ControlFile->enableIntTimes != FALSE)
3908 3909 3910
		ereport(FATAL,
				(errmsg("database files are incompatible with server"),
				 errdetail("The database cluster was initialized with HAVE_INT64_TIMESTAMP"
B
Bruce Momjian 已提交
3911 3912
			   " but the server was compiled without HAVE_INT64_TIMESTAMP."),
				 errhint("It looks like you need to recompile or initdb.")));
3913 3914 3915
#endif

	if (ControlFile->localeBuflen != LOCALE_NAME_BUFLEN)
3916 3917 3918
		ereport(FATAL,
				(errmsg("database files are incompatible with server"),
				 errdetail("The database cluster was initialized with LOCALE_NAME_BUFLEN %d,"
B
Bruce Momjian 已提交
3919
				  " but the server was compiled with LOCALE_NAME_BUFLEN %d.",
3920
						   ControlFile->localeBuflen, LOCALE_NAME_BUFLEN),
B
Bruce Momjian 已提交
3921
				 errhint("It looks like you need to recompile or initdb.")));
3922
	if (pg_perm_setlocale(LC_COLLATE, ControlFile->lc_collate) == NULL)
3923
		ereport(FATAL,
B
Bruce Momjian 已提交
3924 3925 3926 3927 3928
			(errmsg("database files are incompatible with operating system"),
			 errdetail("The database cluster was initialized with LC_COLLATE \"%s\","
					   " which is not recognized by setlocale().",
					   ControlFile->lc_collate),
			 errhint("It looks like you need to initdb or install locale support.")));
3929
	if (pg_perm_setlocale(LC_CTYPE, ControlFile->lc_ctype) == NULL)
3930
		ereport(FATAL,
B
Bruce Momjian 已提交
3931 3932 3933 3934 3935
			(errmsg("database files are incompatible with operating system"),
		errdetail("The database cluster was initialized with LC_CTYPE \"%s\","
				  " which is not recognized by setlocale().",
				  ControlFile->lc_ctype),
			 errhint("It looks like you need to initdb or install locale support.")));
3936 3937 3938 3939 3940 3941

	/* Make the fixed locale settings visible as GUC variables, too */
	SetConfigOption("lc_collate", ControlFile->lc_collate,
					PGC_INTERNAL, PGC_S_OVERRIDE);
	SetConfigOption("lc_ctype", ControlFile->lc_ctype,
					PGC_INTERNAL, PGC_S_OVERRIDE);
3942 3943
}

3944
void
3945
UpdateControlFile(void)
3946
{
3947
	int			fd;
3948

3949 3950 3951 3952 3953
	INIT_CRC32(ControlFile->crc);
	COMP_CRC32(ControlFile->crc,
			   (char *) ControlFile,
			   offsetof(ControlFileData, crc));
	FIN_CRC32(ControlFile->crc);
3954

3955 3956 3957
	fd = BasicOpenFile(XLOG_CONTROL_FILE,
					   O_RDWR | PG_BINARY,
					   S_IRUSR | S_IWUSR);
3958
	if (fd < 0)
3959 3960 3961
		ereport(PANIC,
				(errcode_for_file_access(),
				 errmsg("could not open control file \"%s\": %m",
3962
						XLOG_CONTROL_FILE)));
3963

3964
	errno = 0;
3965
	if (write(fd, ControlFile, sizeof(ControlFileData)) != sizeof(ControlFileData))
3966 3967 3968 3969
	{
		/* if write didn't set errno, assume problem is no disk space */
		if (errno == 0)
			errno = ENOSPC;
3970 3971
		ereport(PANIC,
				(errcode_for_file_access(),
3972
				 errmsg("could not write to control file: %m")));
3973
	}
3974

3975
	if (pg_fsync(fd) != 0)
3976 3977
		ereport(PANIC,
				(errcode_for_file_access(),
3978
				 errmsg("could not fsync control file: %m")));
3979

3980 3981 3982 3983
	if (close(fd))
		ereport(PANIC,
				(errcode_for_file_access(),
				 errmsg("could not close control file: %m")));
3984 3985
}

3986
/*
T
Tom Lane 已提交
3987
 * Initialization of shared memory for XLOG
3988
 */
3989
Size
3990
XLOGShmemSize(void)
3991
{
3992
	Size		size;
3993

3994 3995 3996 3997 3998 3999 4000
	/* XLogCtl */
	size = sizeof(XLogCtlData);
	/* xlblocks array */
	size = add_size(size, mul_size(sizeof(XLogRecPtr), XLOGbuffers));
	/* extra alignment padding for XLOG I/O buffers */
	size = add_size(size, ALIGNOF_XLOG_BUFFER);
	/* and the buffers themselves */
4001
	size = add_size(size, mul_size(XLOG_BLCKSZ, XLOGbuffers));
4002 4003

	/*
B
Bruce Momjian 已提交
4004 4005 4006
	 * Note: we don't count ControlFileData, it comes out of the "slop factor"
	 * added by CreateSharedMemoryAndSemaphores.  This lets us use this
	 * routine again below to compute the actual allocation size.
4007 4008 4009
	 */

	return size;
4010 4011 4012 4013 4014
}

void
XLOGShmemInit(void)
{
4015 4016
	bool		foundCFile,
				foundXLog;
4017
	char	   *allocptr;
4018

4019
	ControlFile = (ControlFileData *)
4020
		ShmemInitStruct("Control File", sizeof(ControlFileData), &foundCFile);
4021 4022
	XLogCtl = (XLogCtlData *)
		ShmemInitStruct("XLOG Ctl", XLOGShmemSize(), &foundXLog);
4023

4024
	if (foundCFile || foundXLog)
4025 4026
	{
		/* both should be present or neither */
4027
		Assert(foundCFile && foundXLog);
4028 4029
		return;
	}
4030

T
Tom Lane 已提交
4031
	memset(XLogCtl, 0, sizeof(XLogCtlData));
B
Bruce Momjian 已提交
4032

T
Tom Lane 已提交
4033
	/*
B
Bruce Momjian 已提交
4034 4035 4036
	 * Since XLogCtlData contains XLogRecPtr fields, its sizeof should be a
	 * multiple of the alignment for same, so no extra alignment padding is
	 * needed here.
T
Tom Lane 已提交
4037
	 */
4038 4039
	allocptr = ((char *) XLogCtl) + sizeof(XLogCtlData);
	XLogCtl->xlblocks = (XLogRecPtr *) allocptr;
T
Tom Lane 已提交
4040
	memset(XLogCtl->xlblocks, 0, sizeof(XLogRecPtr) * XLOGbuffers);
4041
	allocptr += sizeof(XLogRecPtr) * XLOGbuffers;
B
Bruce Momjian 已提交
4042

T
Tom Lane 已提交
4043
	/*
4044
	 * Align the start of the page buffers to an ALIGNOF_XLOG_BUFFER boundary.
T
Tom Lane 已提交
4045
	 */
4046 4047
	allocptr = (char *) TYPEALIGN(ALIGNOF_XLOG_BUFFER, allocptr);
	XLogCtl->pages = allocptr;
4048
	memset(XLogCtl->pages, 0, (Size) XLOG_BLCKSZ * XLOGbuffers);
T
Tom Lane 已提交
4049 4050

	/*
B
Bruce Momjian 已提交
4051 4052
	 * Do basic initialization of XLogCtl shared data. (StartupXLOG will fill
	 * in additional info.)
T
Tom Lane 已提交
4053
	 */
B
Bruce Momjian 已提交
4054
	XLogCtl->XLogCacheByte = (Size) XLOG_BLCKSZ *XLOGbuffers;
B
Bruce Momjian 已提交
4055

T
Tom Lane 已提交
4056 4057
	XLogCtl->XLogCacheBlck = XLOGbuffers - 1;
	XLogCtl->Insert.currpage = (XLogPageHeader) (XLogCtl->pages);
4058
	SpinLockInit(&XLogCtl->info_lck);
T
Tom Lane 已提交
4059

4060
	/*
B
Bruce Momjian 已提交
4061 4062 4063
	 * If we are not in bootstrap mode, pg_control should already exist. Read
	 * and validate it immediately (see comments in ReadControlFile() for the
	 * reasons why).
4064 4065 4066
	 */
	if (!IsBootstrapProcessingMode())
		ReadControlFile();
4067 4068 4069
}

/*
T
Tom Lane 已提交
4070 4071
 * This func must be called ONCE on system install.  It creates pg_control
 * and the initial XLOG segment.
4072 4073
 */
void
T
Tom Lane 已提交
4074
BootStrapXLOG(void)
4075
{
4076
	CheckPoint	checkPoint;
T
Tom Lane 已提交
4077 4078
	char	   *buffer;
	XLogPageHeader page;
4079
	XLogLongPageHeader longpage;
4080
	XLogRecord *record;
B
Bruce Momjian 已提交
4081
	bool		use_existent;
4082 4083
	uint64		sysidentifier;
	struct timeval tv;
4084
	pg_crc32	crc;
4085

4086
	/*
B
Bruce Momjian 已提交
4087 4088 4089 4090 4091 4092 4093 4094 4095 4096
	 * Select a hopefully-unique system identifier code for this installation.
	 * We use the result of gettimeofday(), including the fractional seconds
	 * field, as being about as unique as we can easily get.  (Think not to
	 * use random(), since it hasn't been seeded and there's no portable way
	 * to seed it other than the system clock value...)  The upper half of the
	 * uint64 value is just the tv_sec part, while the lower half is the XOR
	 * of tv_sec and tv_usec.  This is to ensure that we don't lose uniqueness
	 * unnecessarily if "uint64" is really only 32 bits wide.  A person
	 * knowing this encoding can determine the initialization time of the
	 * installation, which could perhaps be useful sometimes.
4097 4098 4099 4100 4101
	 */
	gettimeofday(&tv, NULL);
	sysidentifier = ((uint64) tv.tv_sec) << 32;
	sysidentifier |= (uint32) (tv.tv_sec | tv.tv_usec);

4102 4103 4104
	/* First timeline ID is always 1 */
	ThisTimeLineID = 1;

4105
	/* page buffer must be aligned suitably for O_DIRECT */
4106
	buffer = (char *) palloc(XLOG_BLCKSZ + ALIGNOF_XLOG_BUFFER);
4107
	page = (XLogPageHeader) TYPEALIGN(ALIGNOF_XLOG_BUFFER, buffer);
4108
	memset(page, 0, XLOG_BLCKSZ);
T
Tom Lane 已提交
4109

4110
	/* Set up information for the initial checkpoint record */
4111
	checkPoint.redo.xlogid = 0;
4112
	checkPoint.redo.xrecoff = SizeOfXLogLongPHD;
4113
	checkPoint.undo = checkPoint.redo;
4114
	checkPoint.ThisTimeLineID = ThisTimeLineID;
4115
	checkPoint.nextXidEpoch = 0;
4116
	checkPoint.nextXid = FirstNormalTransactionId;
4117
	checkPoint.nextOid = FirstBootstrapObjectId;
4118
	checkPoint.nextMulti = FirstMultiXactId;
4119
	checkPoint.nextMultiOffset = 0;
T
Tom Lane 已提交
4120
	checkPoint.time = time(NULL);
4121

4122 4123 4124
	ShmemVariableCache->nextXid = checkPoint.nextXid;
	ShmemVariableCache->nextOid = checkPoint.nextOid;
	ShmemVariableCache->oidCount = 0;
4125
	MultiXactSetNextMXact(checkPoint.nextMulti, checkPoint.nextMultiOffset);
4126

4127
	/* Set up the XLOG page header */
4128
	page->xlp_magic = XLOG_PAGE_MAGIC;
4129 4130
	page->xlp_info = XLP_LONG_HEADER;
	page->xlp_tli = ThisTimeLineID;
4131 4132
	page->xlp_pageaddr.xlogid = 0;
	page->xlp_pageaddr.xrecoff = 0;
4133 4134 4135
	longpage = (XLogLongPageHeader) page;
	longpage->xlp_sysid = sysidentifier;
	longpage->xlp_seg_size = XLogSegSize;
4136
	longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
4137 4138

	/* Insert the initial checkpoint record */
4139
	record = (XLogRecord *) ((char *) page + SizeOfXLogLongPHD);
4140
	record->xl_prev.xlogid = 0;
4141
	record->xl_prev.xrecoff = 0;
4142
	record->xl_xid = InvalidTransactionId;
4143
	record->xl_tot_len = SizeOfXLogRecord + sizeof(checkPoint);
4144
	record->xl_len = sizeof(checkPoint);
T
Tom Lane 已提交
4145
	record->xl_info = XLOG_CHECKPOINT_SHUTDOWN;
4146
	record->xl_rmid = RM_XLOG_ID;
T
Tom Lane 已提交
4147
	memcpy(XLogRecGetData(record), &checkPoint, sizeof(checkPoint));
4148

4149 4150 4151 4152 4153
	INIT_CRC32(crc);
	COMP_CRC32(crc, &checkPoint, sizeof(checkPoint));
	COMP_CRC32(crc, (char *) record + sizeof(pg_crc32),
			   SizeOfXLogRecord - sizeof(pg_crc32));
	FIN_CRC32(crc);
4154 4155
	record->xl_crc = crc;

4156
	/* Create first XLOG segment file */
4157 4158
	use_existent = false;
	openLogFile = XLogFileInit(0, 0, &use_existent, false);
4159

4160
	/* Write the first page with the initial record */
4161
	errno = 0;
4162
	if (write(openLogFile, page, XLOG_BLCKSZ) != XLOG_BLCKSZ)
4163 4164 4165 4166
	{
		/* if write didn't set errno, assume problem is no disk space */
		if (errno == 0)
			errno = ENOSPC;
4167 4168
		ereport(PANIC,
				(errcode_for_file_access(),
B
Bruce Momjian 已提交
4169
			  errmsg("could not write bootstrap transaction log file: %m")));
4170
	}
4171

T
Tom Lane 已提交
4172
	if (pg_fsync(openLogFile) != 0)
4173 4174
		ereport(PANIC,
				(errcode_for_file_access(),
B
Bruce Momjian 已提交
4175
			  errmsg("could not fsync bootstrap transaction log file: %m")));
4176

4177 4178 4179
	if (close(openLogFile))
		ereport(PANIC,
				(errcode_for_file_access(),
B
Bruce Momjian 已提交
4180
			  errmsg("could not close bootstrap transaction log file: %m")));
4181

T
Tom Lane 已提交
4182
	openLogFile = -1;
4183

4184 4185
	/* Now create pg_control */

4186
	memset(ControlFile, 0, sizeof(ControlFileData));
T
Tom Lane 已提交
4187
	/* Initialize pg_control status fields */
4188
	ControlFile->system_identifier = sysidentifier;
T
Tom Lane 已提交
4189 4190
	ControlFile->state = DB_SHUTDOWNED;
	ControlFile->time = checkPoint.time;
4191
	ControlFile->checkPoint = checkPoint.redo;
T
Tom Lane 已提交
4192
	ControlFile->checkPointCopy = checkPoint;
4193
	/* some additional ControlFile fields are set in WriteControlFile() */
4194

4195
	WriteControlFile();
4196 4197 4198

	/* Bootstrap the commit log, too */
	BootStrapCLOG();
4199
	BootStrapSUBTRANS();
4200
	BootStrapMultiXact();
4201

4202
	pfree(buffer);
4203 4204
}

4205
static char *
4206 4207
str_time(time_t tnow)
{
4208
	static char buf[128];
4209

4210
	strftime(buf, sizeof(buf),
T
Tom Lane 已提交
4211
			 "%Y-%m-%d %H:%M:%S %Z",
4212
			 localtime(&tnow));
4213

4214
	return buf;
4215 4216
}

4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227
/*
 * See if there is a recovery command file (recovery.conf), and if so
 * read in parameters for archive recovery.
 *
 * XXX longer term intention is to expand this to
 * cater for additional parameters and controls
 * possibly use a flex lexer similar to the GUC one
 */
static void
readRecoveryCommandFile(void)
{
B
Bruce Momjian 已提交
4228 4229 4230 4231 4232 4233
	FILE	   *fd;
	char		cmdline[MAXPGPATH];
	TimeLineID	rtli = 0;
	bool		rtliGiven = false;
	bool		syntaxError = false;

4234
	fd = AllocateFile(RECOVERY_COMMAND_FILE, "r");
4235 4236 4237 4238 4239
	if (fd == NULL)
	{
		if (errno == ENOENT)
			return;				/* not there, so no archive recovery */
		ereport(FATAL,
B
Bruce Momjian 已提交
4240
				(errcode_for_file_access(),
4241
				 errmsg("could not open recovery command file \"%s\": %m",
4242
						RECOVERY_COMMAND_FILE)));
4243 4244 4245
	}

	ereport(LOG,
B
Bruce Momjian 已提交
4246
			(errmsg("starting archive recovery")));
4247

B
Bruce Momjian 已提交
4248 4249 4250 4251
	/*
	 * Parse the file...
	 */
	while (fgets(cmdline, MAXPGPATH, fd) != NULL)
4252 4253
	{
		/* skip leading whitespace and check for # comment */
B
Bruce Momjian 已提交
4254 4255 4256
		char	   *ptr;
		char	   *tok1;
		char	   *tok2;
4257 4258 4259 4260 4261 4262 4263 4264 4265 4266

		for (ptr = cmdline; *ptr; ptr++)
		{
			if (!isspace((unsigned char) *ptr))
				break;
		}
		if (*ptr == '\0' || *ptr == '#')
			continue;

		/* identify the quoted parameter value */
B
Bruce Momjian 已提交
4267
		tok1 = strtok(ptr, "'");
4268 4269 4270 4271 4272
		if (!tok1)
		{
			syntaxError = true;
			break;
		}
B
Bruce Momjian 已提交
4273
		tok2 = strtok(NULL, "'");
4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286
		if (!tok2)
		{
			syntaxError = true;
			break;
		}
		/* reparse to get just the parameter name */
		tok1 = strtok(ptr, " \t=");
		if (!tok1)
		{
			syntaxError = true;
			break;
		}

B
Bruce Momjian 已提交
4287 4288
		if (strcmp(tok1, "restore_command") == 0)
		{
4289
			recoveryRestoreCommand = pstrdup(tok2);
4290 4291 4292 4293
			ereport(LOG,
					(errmsg("restore_command = \"%s\"",
							recoveryRestoreCommand)));
		}
B
Bruce Momjian 已提交
4294 4295
		else if (strcmp(tok1, "recovery_target_timeline") == 0)
		{
4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314
			rtliGiven = true;
			if (strcmp(tok2, "latest") == 0)
				rtli = 0;
			else
			{
				errno = 0;
				rtli = (TimeLineID) strtoul(tok2, NULL, 0);
				if (errno == EINVAL || errno == ERANGE)
					ereport(FATAL,
							(errmsg("recovery_target_timeline is not a valid number: \"%s\"",
									tok2)));
			}
			if (rtli)
				ereport(LOG,
						(errmsg("recovery_target_timeline = %u", rtli)));
			else
				ereport(LOG,
						(errmsg("recovery_target_timeline = latest")));
		}
B
Bruce Momjian 已提交
4315 4316
		else if (strcmp(tok1, "recovery_target_xid") == 0)
		{
4317 4318 4319 4320
			errno = 0;
			recoveryTargetXid = (TransactionId) strtoul(tok2, NULL, 0);
			if (errno == EINVAL || errno == ERANGE)
				ereport(FATAL,
B
Bruce Momjian 已提交
4321 4322
				 (errmsg("recovery_target_xid is not a valid number: \"%s\"",
						 tok2)));
4323 4324 4325 4326 4327 4328
			ereport(LOG,
					(errmsg("recovery_target_xid = %u",
							recoveryTargetXid)));
			recoveryTarget = true;
			recoveryTargetExact = true;
		}
B
Bruce Momjian 已提交
4329 4330
		else if (strcmp(tok1, "recovery_target_time") == 0)
		{
4331 4332 4333 4334 4335 4336 4337 4338
			/*
			 * if recovery_target_xid specified, then this overrides
			 * recovery_target_time
			 */
			if (recoveryTargetExact)
				continue;
			recoveryTarget = true;
			recoveryTargetExact = false;
B
Bruce Momjian 已提交
4339

4340
			/*
B
Bruce Momjian 已提交
4341 4342 4343
			 * Convert the time string given by the user to the time_t format.
			 * We use type abstime's input converter because we know abstime
			 * has the same representation as time_t.
4344
			 */
4345 4346
			recoveryTargetTime = (time_t)
				DatumGetAbsoluteTime(DirectFunctionCall1(abstimein,
B
Bruce Momjian 已提交
4347
													 CStringGetDatum(tok2)));
4348 4349
			ereport(LOG,
					(errmsg("recovery_target_time = %s",
B
Bruce Momjian 已提交
4350 4351
							DatumGetCString(DirectFunctionCall1(abstimeout,
				AbsoluteTimeGetDatum((AbsoluteTime) recoveryTargetTime))))));
4352
		}
B
Bruce Momjian 已提交
4353 4354
		else if (strcmp(tok1, "recovery_target_inclusive") == 0)
		{
4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375
			/*
			 * does nothing if a recovery_target is not also set
			 */
			if (strcmp(tok2, "true") == 0)
				recoveryTargetInclusive = true;
			else
			{
				recoveryTargetInclusive = false;
				tok2 = "false";
			}
			ereport(LOG,
					(errmsg("recovery_target_inclusive = %s", tok2)));
		}
		else
			ereport(FATAL,
					(errmsg("unrecognized recovery parameter \"%s\"",
							tok1)));
	}

	FreeFile(fd);

B
Bruce Momjian 已提交
4376 4377
	if (syntaxError)
		ereport(FATAL,
4378 4379
				(errmsg("syntax error in recovery command file: %s",
						cmdline),
B
Bruce Momjian 已提交
4380
			  errhint("Lines should have the format parameter = 'value'.")));
4381 4382

	/* Check that required parameters were supplied */
4383
	if (recoveryRestoreCommand == NULL)
4384 4385
		ereport(FATAL,
				(errmsg("recovery command file \"%s\" did not specify restore_command",
4386
						RECOVERY_COMMAND_FILE)));
4387

4388 4389 4390
	/* Enable fetching from archive recovery area */
	InArchiveRecovery = true;

4391
	/*
B
Bruce Momjian 已提交
4392 4393 4394 4395
	 * If user specified recovery_target_timeline, validate it or compute the
	 * "latest" value.	We can't do this until after we've gotten the restore
	 * command and set InArchiveRecovery, because we need to fetch timeline
	 * history files from the archive.
4396
	 */
4397 4398 4399 4400 4401 4402 4403
	if (rtliGiven)
	{
		if (rtli)
		{
			/* Timeline 1 does not have a history file, all else should */
			if (rtli != 1 && !existsTimeLineHistory(rtli))
				ereport(FATAL,
B
Bruce Momjian 已提交
4404 4405
						(errmsg("recovery_target_timeline %u does not exist",
								rtli)));
4406 4407 4408 4409 4410 4411 4412 4413
			recoveryTargetTLI = rtli;
		}
		else
		{
			/* We start the "latest" search from pg_control's timeline */
			recoveryTargetTLI = findNewestTimeLine(recoveryTargetTLI);
		}
	}
4414 4415 4416 4417 4418 4419
}

/*
 * Exit archive-recovery state
 */
static void
4420
exitArchiveRecovery(TimeLineID endTLI, uint32 endLogId, uint32 endLogSeg)
4421
{
B
Bruce Momjian 已提交
4422 4423
	char		recoveryPath[MAXPGPATH];
	char		xlogpath[MAXPGPATH];
4424 4425

	/*
4426
	 * We are no longer in archive recovery state.
4427 4428 4429 4430
	 */
	InArchiveRecovery = false;

	/*
B
Bruce Momjian 已提交
4431 4432 4433
	 * We should have the ending log segment currently open.  Verify, and then
	 * close it (to avoid problems on Windows with trying to rename or delete
	 * an open file).
4434 4435 4436 4437 4438 4439 4440 4441 4442
	 */
	Assert(readFile >= 0);
	Assert(readId == endLogId);
	Assert(readSeg == endLogSeg);

	close(readFile);
	readFile = -1;

	/*
B
Bruce Momjian 已提交
4443 4444 4445 4446 4447 4448 4449
	 * If the segment was fetched from archival storage, we want to replace
	 * the existing xlog segment (if any) with the archival version.  This is
	 * because whatever is in XLOGDIR is very possibly older than what we have
	 * from the archives, since it could have come from restoring a PGDATA
	 * backup.	In any case, the archival version certainly is more
	 * descriptive of what our current database state is, because that is what
	 * we replayed from.
4450
	 *
4451 4452 4453
	 * Note that if we are establishing a new timeline, ThisTimeLineID is
	 * already set to the new value, and so we will create a new file instead
	 * of overwriting any existing file.
4454
	 */
4455
	snprintf(recoveryPath, MAXPGPATH, XLOGDIR "/RECOVERYXLOG");
4456
	XLogFilePath(xlogpath, ThisTimeLineID, endLogId, endLogSeg);
4457 4458 4459 4460 4461 4462 4463 4464 4465 4466

	if (restoredFromArchive)
	{
		ereport(DEBUG3,
				(errmsg_internal("moving last restored xlog to \"%s\"",
								 xlogpath)));
		unlink(xlogpath);		/* might or might not exist */
		if (rename(recoveryPath, xlogpath) != 0)
			ereport(FATAL,
					(errcode_for_file_access(),
4467
					 errmsg("could not rename file \"%s\" to \"%s\": %m",
4468 4469 4470 4471 4472 4473 4474 4475 4476 4477
							recoveryPath, xlogpath)));
		/* XXX might we need to fix permissions on the file? */
	}
	else
	{
		/*
		 * If the latest segment is not archival, but there's still a
		 * RECOVERYXLOG laying about, get rid of it.
		 */
		unlink(recoveryPath);	/* ignore any error */
B
Bruce Momjian 已提交
4478

4479
		/*
B
Bruce Momjian 已提交
4480 4481 4482
		 * If we are establishing a new timeline, we have to copy data from
		 * the last WAL segment of the old timeline to create a starting WAL
		 * segment for the new timeline.
4483 4484 4485 4486
		 */
		if (endTLI != ThisTimeLineID)
			XLogFileCopy(endLogId, endLogSeg,
						 endTLI, endLogId, endLogSeg);
4487 4488 4489
	}

	/*
B
Bruce Momjian 已提交
4490 4491
	 * Let's just make real sure there are not .ready or .done flags posted
	 * for the new segment.
4492
	 */
4493 4494
	XLogFileName(xlogpath, ThisTimeLineID, endLogId, endLogSeg);
	XLogArchiveCleanup(xlogpath);
4495

4496
	/* Get rid of any remaining recovered timeline-history file, too */
4497
	snprintf(recoveryPath, MAXPGPATH, XLOGDIR "/RECOVERYHISTORY");
B
Bruce Momjian 已提交
4498
	unlink(recoveryPath);		/* ignore any error */
4499 4500

	/*
B
Bruce Momjian 已提交
4501 4502
	 * Rename the config file out of the way, so that we don't accidentally
	 * re-enter archive recovery mode in a subsequent crash.
4503
	 */
4504 4505
	unlink(RECOVERY_COMMAND_DONE);
	if (rename(RECOVERY_COMMAND_FILE, RECOVERY_COMMAND_DONE) != 0)
4506 4507
		ereport(FATAL,
				(errcode_for_file_access(),
4508
				 errmsg("could not rename file \"%s\" to \"%s\": %m",
4509
						RECOVERY_COMMAND_FILE, RECOVERY_COMMAND_DONE)));
4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520

	ereport(LOG,
			(errmsg("archive recovery complete")));
}

/*
 * For point-in-time recovery, this function decides whether we want to
 * stop applying the XLOG at or after the current record.
 *
 * Returns TRUE if we are stopping, FALSE otherwise.  On TRUE return,
 * *includeThis is set TRUE if we should apply this record before stopping.
4521 4522
 * Also, some information is saved in recoveryStopXid et al for use in
 * annotating the new timeline's history file.
4523 4524 4525 4526 4527
 */
static bool
recoveryStopsHere(XLogRecord *record, bool *includeThis)
{
	bool		stopsHere;
B
Bruce Momjian 已提交
4528 4529
	uint8		record_info;
	time_t		recordXtime;
4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540

	/* Do we have a PITR target at all? */
	if (!recoveryTarget)
		return false;

	/* We only consider stopping at COMMIT or ABORT records */
	if (record->xl_rmid != RM_XACT_ID)
		return false;
	record_info = record->xl_info & ~XLR_INFO_MASK;
	if (record_info == XLOG_XACT_COMMIT)
	{
B
Bruce Momjian 已提交
4541
		xl_xact_commit *recordXactCommitData;
4542 4543 4544 4545 4546 4547

		recordXactCommitData = (xl_xact_commit *) XLogRecGetData(record);
		recordXtime = recordXactCommitData->xtime;
	}
	else if (record_info == XLOG_XACT_ABORT)
	{
B
Bruce Momjian 已提交
4548
		xl_xact_abort *recordXactAbortData;
4549 4550 4551 4552 4553 4554 4555 4556 4557 4558

		recordXactAbortData = (xl_xact_abort *) XLogRecGetData(record);
		recordXtime = recordXactAbortData->xtime;
	}
	else
		return false;

	if (recoveryTargetExact)
	{
		/*
B
Bruce Momjian 已提交
4559 4560
		 * there can be only one transaction end record with this exact
		 * transactionid
4561
		 *
B
Bruce Momjian 已提交
4562
		 * when testing for an xid, we MUST test for equality only, since
B
Bruce Momjian 已提交
4563 4564 4565
		 * transactions are numbered in the order they start, not the order
		 * they complete. A higher numbered xid will complete before you about
		 * 50% of the time...
4566 4567 4568 4569 4570 4571 4572 4573
		 */
		stopsHere = (record->xl_xid == recoveryTargetXid);
		if (stopsHere)
			*includeThis = recoveryTargetInclusive;
	}
	else
	{
		/*
B
Bruce Momjian 已提交
4574 4575 4576
		 * there can be many transactions that share the same commit time, so
		 * we stop after the last one, if we are inclusive, or stop at the
		 * first one if we are exclusive
4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587
		 */
		if (recoveryTargetInclusive)
			stopsHere = (recordXtime > recoveryTargetTime);
		else
			stopsHere = (recordXtime >= recoveryTargetTime);
		if (stopsHere)
			*includeThis = false;
	}

	if (stopsHere)
	{
4588 4589 4590 4591
		recoveryStopXid = record->xl_xid;
		recoveryStopTime = recordXtime;
		recoveryStopAfter = *includeThis;

4592 4593
		if (record_info == XLOG_XACT_COMMIT)
		{
4594
			if (recoveryStopAfter)
4595 4596
				ereport(LOG,
						(errmsg("recovery stopping after commit of transaction %u, time %s",
B
Bruce Momjian 已提交
4597
							  recoveryStopXid, str_time(recoveryStopTime))));
4598 4599 4600
			else
				ereport(LOG,
						(errmsg("recovery stopping before commit of transaction %u, time %s",
B
Bruce Momjian 已提交
4601
							  recoveryStopXid, str_time(recoveryStopTime))));
4602 4603 4604
		}
		else
		{
4605
			if (recoveryStopAfter)
4606 4607
				ereport(LOG,
						(errmsg("recovery stopping after abort of transaction %u, time %s",
B
Bruce Momjian 已提交
4608
							  recoveryStopXid, str_time(recoveryStopTime))));
4609 4610 4611
			else
				ereport(LOG,
						(errmsg("recovery stopping before abort of transaction %u, time %s",
B
Bruce Momjian 已提交
4612
							  recoveryStopXid, str_time(recoveryStopTime))));
4613 4614 4615 4616 4617 4618
		}
	}

	return stopsHere;
}

4619
/*
T
Tom Lane 已提交
4620
 * This must be called ONCE during postmaster or standalone-backend startup
4621 4622
 */
void
T
Tom Lane 已提交
4623
StartupXLOG(void)
4624
{
4625 4626
	XLogCtlInsert *Insert;
	CheckPoint	checkPoint;
T
Tom Lane 已提交
4627
	bool		wasShutdown;
4628
	bool		needNewTimeLine = false;
4629
	bool		haveBackupLabel = false;
4630
	XLogRecPtr	RecPtr,
T
Tom Lane 已提交
4631 4632
				LastRec,
				checkPointLoc,
4633
				minRecoveryLoc,
T
Tom Lane 已提交
4634
				EndOfLog;
4635 4636
	uint32		endLogId;
	uint32		endLogSeg;
4637
	XLogRecord *record;
4638
	uint32		freespace;
4639
	TransactionId oldestActiveXID;
4640

4641
	/*
4642 4643
	 * Read control file and check XLOG status looks valid.
	 *
4644 4645
	 * Note: in most control paths, *ControlFile is already valid and we need
	 * not do ReadControlFile() here, but might as well do it to be sure.
4646
	 */
4647
	ReadControlFile();
4648

4649
	if (ControlFile->state < DB_SHUTDOWNED ||
4650
		ControlFile->state > DB_IN_PRODUCTION ||
4651
		!XRecOffIsValid(ControlFile->checkPoint.xrecoff))
4652 4653
		ereport(FATAL,
				(errmsg("control file contains invalid data")));
4654 4655

	if (ControlFile->state == DB_SHUTDOWNED)
4656 4657 4658
		ereport(LOG,
				(errmsg("database system was shut down at %s",
						str_time(ControlFile->time))));
4659
	else if (ControlFile->state == DB_SHUTDOWNING)
4660
		ereport(LOG,
4661
				(errmsg("database system shutdown was interrupted; last known up at %s",
4662
						str_time(ControlFile->time))));
4663
	else if (ControlFile->state == DB_IN_CRASH_RECOVERY)
4664
		ereport(LOG,
B
Bruce Momjian 已提交
4665 4666 4667 4668
		   (errmsg("database system was interrupted while in recovery at %s",
				   str_time(ControlFile->time)),
			errhint("This probably means that some data is corrupted and"
					" you will have to use the last backup for recovery.")));
4669 4670
	else if (ControlFile->state == DB_IN_ARCHIVE_RECOVERY)
		ereport(LOG,
B
Bruce Momjian 已提交
4671 4672 4673 4674
				(errmsg("database system was interrupted while in recovery at log time %s",
						str_time(ControlFile->checkPointCopy.time)),
				 errhint("If this has occurred more than once some data may be corrupted"
				" and you may need to choose an earlier recovery target.")));
4675
	else if (ControlFile->state == DB_IN_PRODUCTION)
4676
		ereport(LOG,
4677
				(errmsg("database system was interrupted; last known up at %s",
4678
						str_time(ControlFile->time))));
4679

4680 4681
	/* This is just to allow attaching to startup process with a debugger */
#ifdef XLOG_REPLAY_DELAY
4682
	if (ControlFile->state != DB_SHUTDOWNED)
4683
		pg_usleep(60000000L);
4684 4685
#endif

4686
	/*
B
Bruce Momjian 已提交
4687 4688
	 * Initialize on the assumption we want to recover to the same timeline
	 * that's active according to pg_control.
4689 4690 4691
	 */
	recoveryTargetTLI = ControlFile->checkPointCopy.ThisTimeLineID;

4692
	/*
B
Bruce Momjian 已提交
4693 4694
	 * Check for recovery control file, and if so set up state for offline
	 * recovery
4695 4696 4697
	 */
	readRecoveryCommandFile();

4698 4699 4700
	/* Now we can determine the list of expected TLIs */
	expectedTLIs = readTimeLineHistory(recoveryTargetTLI);

4701 4702 4703 4704 4705 4706
	/*
	 * If pg_control's timeline is not in expectedTLIs, then we cannot
	 * proceed: the backup is not part of the history of the requested
	 * timeline.
	 */
	if (!list_member_int(expectedTLIs,
B
Bruce Momjian 已提交
4707
						 (int) ControlFile->checkPointCopy.ThisTimeLineID))
4708 4709 4710 4711 4712
		ereport(FATAL,
				(errmsg("requested timeline %u is not a child of database system timeline %u",
						recoveryTargetTLI,
						ControlFile->checkPointCopy.ThisTimeLineID)));

4713
	if (read_backup_label(&checkPointLoc, &minRecoveryLoc))
T
Tom Lane 已提交
4714
	{
4715
		/*
B
Bruce Momjian 已提交
4716 4717
		 * When a backup_label file is present, we want to roll forward from
		 * the checkpoint it identifies, rather than using pg_control.
4718
		 */
4719
		record = ReadCheckpointRecord(checkPointLoc, 0);
4720 4721 4722 4723
		if (record != NULL)
		{
			ereport(LOG,
					(errmsg("checkpoint record is at %X/%X",
B
Bruce Momjian 已提交
4724
							checkPointLoc.xlogid, checkPointLoc.xrecoff)));
4725 4726 4727 4728 4729
			InRecovery = true;	/* force recovery even if SHUTDOWNED */
		}
		else
		{
			ereport(PANIC,
B
Bruce Momjian 已提交
4730 4731
					(errmsg("could not locate required checkpoint record"),
					 errhint("If you are not restoring from a backup, try removing the file \"%s/backup_label\".", DataDir)));
4732
		}
4733 4734
		/* set flag to delete it later */
		haveBackupLabel = true;
T
Tom Lane 已提交
4735 4736 4737
	}
	else
	{
4738
		/*
B
Bruce Momjian 已提交
4739 4740
		 * Get the last valid checkpoint record.  If the latest one according
		 * to pg_control is broken, try the next-to-last one.
4741 4742
		 */
		checkPointLoc = ControlFile->checkPoint;
4743
		record = ReadCheckpointRecord(checkPointLoc, 1);
T
Tom Lane 已提交
4744 4745
		if (record != NULL)
		{
4746
			ereport(LOG,
4747
					(errmsg("checkpoint record is at %X/%X",
B
Bruce Momjian 已提交
4748
							checkPointLoc.xlogid, checkPointLoc.xrecoff)));
T
Tom Lane 已提交
4749 4750
		}
		else
4751 4752
		{
			checkPointLoc = ControlFile->prevCheckPoint;
4753
			record = ReadCheckpointRecord(checkPointLoc, 2);
4754 4755 4756
			if (record != NULL)
			{
				ereport(LOG,
B
Bruce Momjian 已提交
4757 4758 4759
						(errmsg("using previous checkpoint record at %X/%X",
							  checkPointLoc.xlogid, checkPointLoc.xrecoff)));
				InRecovery = true;		/* force recovery even if SHUTDOWNED */
4760 4761 4762
			}
			else
				ereport(PANIC,
B
Bruce Momjian 已提交
4763
					 (errmsg("could not locate a valid checkpoint record")));
4764
		}
T
Tom Lane 已提交
4765
	}
4766

T
Tom Lane 已提交
4767 4768 4769
	LastRec = RecPtr = checkPointLoc;
	memcpy(&checkPoint, XLogRecGetData(record), sizeof(CheckPoint));
	wasShutdown = (record->xl_info == XLOG_CHECKPOINT_SHUTDOWN);
4770

4771
	ereport(LOG,
B
Bruce Momjian 已提交
4772 4773 4774 4775
	 (errmsg("redo record is at %X/%X; undo record is at %X/%X; shutdown %s",
			 checkPoint.redo.xlogid, checkPoint.redo.xrecoff,
			 checkPoint.undo.xlogid, checkPoint.undo.xrecoff,
			 wasShutdown ? "TRUE" : "FALSE")));
4776
	ereport(LOG,
4777 4778 4779
			(errmsg("next transaction ID: %u/%u; next OID: %u",
					checkPoint.nextXidEpoch, checkPoint.nextXid,
					checkPoint.nextOid)));
4780 4781 4782
	ereport(LOG,
			(errmsg("next MultiXactId: %u; next MultiXactOffset: %u",
					checkPoint.nextMulti, checkPoint.nextMultiOffset)));
4783
	if (!TransactionIdIsNormal(checkPoint.nextXid))
4784
		ereport(PANIC,
4785
				(errmsg("invalid next transaction ID")));
4786 4787 4788

	ShmemVariableCache->nextXid = checkPoint.nextXid;
	ShmemVariableCache->nextOid = checkPoint.nextOid;
4789
	ShmemVariableCache->oidCount = 0;
4790
	MultiXactSetNextMXact(checkPoint.nextMulti, checkPoint.nextMultiOffset);
4791

4792
	/*
B
Bruce Momjian 已提交
4793 4794 4795
	 * We must replay WAL entries using the same TimeLineID they were created
	 * under, so temporarily adopt the TLI indicated by the checkpoint (see
	 * also xlog_redo()).
4796
	 */
4797
	ThisTimeLineID = checkPoint.ThisTimeLineID;
4798

4799
	RedoRecPtr = XLogCtl->Insert.RedoRecPtr = checkPoint.redo;
V
WAL  
Vadim B. Mikheev 已提交
4800

4801
	if (XLByteLT(RecPtr, checkPoint.redo))
4802 4803
		ereport(PANIC,
				(errmsg("invalid redo in checkpoint record")));
4804 4805 4806
	if (checkPoint.undo.xrecoff == 0)
		checkPoint.undo = RecPtr;

4807
	/*
B
Bruce Momjian 已提交
4808
	 * Check whether we need to force recovery from WAL.  If it appears to
B
Bruce Momjian 已提交
4809 4810
	 * have been a clean shutdown and we did not have a recovery.conf file,
	 * then assume no recovery needed.
4811
	 */
B
Bruce Momjian 已提交
4812
	if (XLByteLT(checkPoint.undo, RecPtr) ||
V
Vadim B. Mikheev 已提交
4813
		XLByteLT(checkPoint.redo, RecPtr))
4814
	{
T
Tom Lane 已提交
4815
		if (wasShutdown)
4816
			ereport(PANIC,
B
Bruce Momjian 已提交
4817
				(errmsg("invalid redo/undo record in shutdown checkpoint")));
V
WAL  
Vadim B. Mikheev 已提交
4818
		InRecovery = true;
4819 4820
	}
	else if (ControlFile->state != DB_SHUTDOWNED)
V
WAL  
Vadim B. Mikheev 已提交
4821
		InRecovery = true;
4822 4823 4824 4825 4826
	else if (InArchiveRecovery)
	{
		/* force recovery due to presence of recovery.conf */
		InRecovery = true;
	}
4827

V
WAL  
Vadim B. Mikheev 已提交
4828
	/* REDO */
4829
	if (InRecovery)
4830
	{
B
Bruce Momjian 已提交
4831
		int			rmid;
4832

4833
		/*
B
Bruce Momjian 已提交
4834 4835 4836 4837
		 * Update pg_control to show that we are recovering and to show the
		 * selected checkpoint as the place we are starting from. We also mark
		 * pg_control with any minimum recovery stop point obtained from a
		 * backup history file.
4838
		 */
4839
		if (InArchiveRecovery)
4840
		{
4841
			ereport(LOG,
4842
					(errmsg("automatic recovery in progress")));
4843 4844
			ControlFile->state = DB_IN_ARCHIVE_RECOVERY;
		}
4845
		else
4846
		{
4847
			ereport(LOG,
4848 4849
					(errmsg("database system was not properly shut down; "
							"automatic recovery in progress")));
4850 4851 4852 4853 4854 4855 4856
			ControlFile->state = DB_IN_CRASH_RECOVERY;
		}
		ControlFile->prevCheckPoint = ControlFile->checkPoint;
		ControlFile->checkPoint = checkPointLoc;
		ControlFile->checkPointCopy = checkPoint;
		if (minRecoveryLoc.xlogid != 0 || minRecoveryLoc.xrecoff != 0)
			ControlFile->minRecoveryPoint = minRecoveryLoc;
4857 4858 4859
		ControlFile->time = time(NULL);
		UpdateControlFile();

4860
		/*
B
Bruce Momjian 已提交
4861 4862 4863 4864 4865 4866
		 * If there was a backup label file, it's done its job and the info
		 * has now been propagated into pg_control.  We must get rid of the
		 * label file so that if we crash during recovery, we'll pick up at
		 * the latest recovery restartpoint instead of going all the way back
		 * to the backup start point.  It seems prudent though to just rename
		 * the file out of the way rather than delete it completely.
4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877
		 */
		if (haveBackupLabel)
		{
			unlink(BACKUP_LABEL_OLD);
			if (rename(BACKUP_LABEL_FILE, BACKUP_LABEL_OLD) != 0)
				ereport(FATAL,
						(errcode_for_file_access(),
						 errmsg("could not rename file \"%s\" to \"%s\": %m",
								BACKUP_LABEL_FILE, BACKUP_LABEL_OLD)));
		}

4878
		/* Start up the recovery environment */
V
WAL  
Vadim B. Mikheev 已提交
4879
		XLogInitRelationCache();
V
Vadim B. Mikheev 已提交
4880

4881 4882 4883 4884 4885 4886
		for (rmid = 0; rmid <= RM_MAX_ID; rmid++)
		{
			if (RmgrTable[rmid].rm_startup != NULL)
				RmgrTable[rmid].rm_startup();
		}

4887
		/*
B
Bruce Momjian 已提交
4888 4889
		 * Find the first record that logically follows the checkpoint --- it
		 * might physically precede it, though.
4890
		 */
4891
		if (XLByteLT(checkPoint.redo, RecPtr))
4892 4893
		{
			/* back up to find the record */
4894
			record = ReadRecord(&(checkPoint.redo), PANIC);
4895
		}
B
Bruce Momjian 已提交
4896
		else
4897
		{
4898
			/* just have to read next record after CheckPoint */
4899
			record = ReadRecord(NULL, LOG);
4900
		}
4901

T
Tom Lane 已提交
4902
		if (record != NULL)
4903
		{
4904 4905
			bool		recoveryContinue = true;
			bool		recoveryApply = true;
B
Bruce Momjian 已提交
4906
			ErrorContextCallback errcontext;
4907

V
WAL  
Vadim B. Mikheev 已提交
4908
			InRedo = true;
4909 4910 4911
			ereport(LOG,
					(errmsg("redo starts at %X/%X",
							ReadRecPtr.xlogid, ReadRecPtr.xrecoff)));
4912 4913 4914 4915

			/*
			 * main redo apply loop
			 */
4916 4917
			do
			{
4918
#ifdef WAL_DEBUG
V
WAL  
Vadim B. Mikheev 已提交
4919 4920
				if (XLOG_DEBUG)
				{
B
Bruce Momjian 已提交
4921
					StringInfoData buf;
V
WAL  
Vadim B. Mikheev 已提交
4922

4923 4924
					initStringInfo(&buf);
					appendStringInfo(&buf, "REDO @ %X/%X; LSN %X/%X: ",
B
Bruce Momjian 已提交
4925 4926
									 ReadRecPtr.xlogid, ReadRecPtr.xrecoff,
									 EndRecPtr.xlogid, EndRecPtr.xrecoff);
4927 4928 4929 4930
					xlog_outrec(&buf, record);
					appendStringInfo(&buf, " - ");
					RmgrTable[record->xl_rmid].rm_desc(&buf,
													   record->xl_info,
B
Bruce Momjian 已提交
4931
													 XLogRecGetData(record));
4932 4933
					elog(LOG, "%s", buf.data);
					pfree(buf.data);
V
WAL  
Vadim B. Mikheev 已提交
4934
				}
4935
#endif
V
WAL  
Vadim B. Mikheev 已提交
4936

4937 4938 4939 4940 4941
				/*
				 * Have we reached our recovery target?
				 */
				if (recoveryStopsHere(record, &recoveryApply))
				{
B
Bruce Momjian 已提交
4942
					needNewTimeLine = true;		/* see below */
4943 4944 4945 4946 4947
					recoveryContinue = false;
					if (!recoveryApply)
						break;
				}

4948 4949 4950 4951 4952 4953
				/* Setup error traceback support for ereport() */
				errcontext.callback = rm_redo_error_callback;
				errcontext.arg = (void *) record;
				errcontext.previous = error_context_stack;
				error_context_stack = &errcontext;

4954 4955
				/* nextXid must be beyond record's xid */
				if (TransactionIdFollowsOrEquals(record->xl_xid,
B
Bruce Momjian 已提交
4956
												 ShmemVariableCache->nextXid))
4957 4958 4959 4960 4961
				{
					ShmemVariableCache->nextXid = record->xl_xid;
					TransactionIdAdvance(ShmemVariableCache->nextXid);
				}

T
Tom Lane 已提交
4962
				if (record->xl_info & XLR_BKP_BLOCK_MASK)
4963 4964
					RestoreBkpBlocks(record, EndRecPtr);

4965
				RmgrTable[record->xl_rmid].rm_redo(EndRecPtr, record);
4966

4967 4968 4969
				/* Pop the error context stack */
				error_context_stack = errcontext.previous;

4970 4971
				LastRec = ReadRecPtr;

4972
				record = ReadRecord(NULL, LOG);
4973
			} while (record != NULL && recoveryContinue);
B
Bruce Momjian 已提交
4974

4975 4976 4977 4978
			/*
			 * end of main redo apply loop
			 */

4979 4980 4981
			ereport(LOG,
					(errmsg("redo done at %X/%X",
							ReadRecPtr.xlogid, ReadRecPtr.xrecoff)));
V
WAL  
Vadim B. Mikheev 已提交
4982
			InRedo = false;
4983 4984
		}
		else
4985 4986
		{
			/* there are no WAL records following the checkpoint */
4987 4988
			ereport(LOG,
					(errmsg("redo is not required")));
4989
		}
V
WAL  
Vadim B. Mikheev 已提交
4990 4991
	}

T
Tom Lane 已提交
4992
	/*
B
Bruce Momjian 已提交
4993 4994
	 * Re-fetch the last valid or last applied record, so we can identify the
	 * exact endpoint of what we consider the valid portion of WAL.
T
Tom Lane 已提交
4995
	 */
4996
	record = ReadRecord(&LastRec, PANIC);
T
Tom Lane 已提交
4997
	EndOfLog = EndRecPtr;
4998 4999
	XLByteToPrevSeg(EndOfLog, endLogId, endLogSeg);

5000 5001 5002 5003
	/*
	 * Complain if we did not roll forward far enough to render the backup
	 * dump consistent.
	 */
5004
	if (XLByteLT(EndOfLog, ControlFile->minRecoveryPoint))
5005 5006 5007 5008
	{
		if (needNewTimeLine)	/* stopped because of stop request */
			ereport(FATAL,
					(errmsg("requested recovery stop point is before end time of backup dump")));
B
Bruce Momjian 已提交
5009
		else
5010
			/* ran off end of WAL */
5011 5012 5013 5014
			ereport(FATAL,
					(errmsg("WAL ends before end time of backup dump")));
	}

5015 5016 5017
	/*
	 * Consider whether we need to assign a new timeline ID.
	 *
B
Bruce Momjian 已提交
5018 5019
	 * If we stopped short of the end of WAL during recovery, then we are
	 * generating a new timeline and must assign it a unique new ID.
B
Bruce Momjian 已提交
5020 5021
	 * Otherwise, we can just extend the timeline we were in when we ran out
	 * of WAL.
5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034
	 */
	if (needNewTimeLine)
	{
		ThisTimeLineID = findNewestTimeLine(recoveryTargetTLI) + 1;
		ereport(LOG,
				(errmsg("selected new timeline ID: %u", ThisTimeLineID)));
		writeTimeLineHistory(ThisTimeLineID, recoveryTargetTLI,
							 curFileTLI, endLogId, endLogSeg);
	}

	/* Save the selected TimeLineID in shared memory, too */
	XLogCtl->ThisTimeLineID = ThisTimeLineID;

5035
	/*
B
Bruce Momjian 已提交
5036 5037 5038 5039
	 * We are now done reading the old WAL.  Turn off archive fetching if it
	 * was active, and make a writable copy of the last WAL segment. (Note
	 * that we also have a copy of the last block of the old WAL in readBuf;
	 * we will use that below.)
5040 5041
	 */
	if (InArchiveRecovery)
5042
		exitArchiveRecovery(curFileTLI, endLogId, endLogSeg);
5043 5044 5045 5046 5047 5048 5049 5050

	/*
	 * Prepare to write WAL starting at EndOfLog position, and init xlog
	 * buffer cache using the block containing the last record from the
	 * previous incarnation.
	 */
	openLogId = endLogId;
	openLogSeg = endLogSeg;
5051
	openLogFile = XLogFileOpen(openLogId, openLogSeg);
T
Tom Lane 已提交
5052
	openLogOff = 0;
V
WAL  
Vadim B. Mikheev 已提交
5053
	Insert = &XLogCtl->Insert;
5054
	Insert->PrevRecord = LastRec;
5055 5056
	XLogCtl->xlblocks[0].xlogid = openLogId;
	XLogCtl->xlblocks[0].xrecoff =
5057
		((EndOfLog.xrecoff - 1) / XLOG_BLCKSZ + 1) * XLOG_BLCKSZ;
B
Bruce Momjian 已提交
5058 5059

	/*
B
Bruce Momjian 已提交
5060 5061 5062
	 * Tricky point here: readBuf contains the *last* block that the LastRec
	 * record spans, not the one it starts in.	The last block is indeed the
	 * one we want to use.
T
Tom Lane 已提交
5063
	 */
5064 5065
	Assert(readOff == (XLogCtl->xlblocks[0].xrecoff - XLOG_BLCKSZ) % XLogSegSize);
	memcpy((char *) Insert->currpage, readBuf, XLOG_BLCKSZ);
5066
	Insert->currpos = (char *) Insert->currpage +
5067
		(EndOfLog.xrecoff + XLOG_BLCKSZ - XLogCtl->xlblocks[0].xrecoff);
V
WAL  
Vadim B. Mikheev 已提交
5068

T
Tom Lane 已提交
5069
	LogwrtResult.Write = LogwrtResult.Flush = EndOfLog;
V
WAL  
Vadim B. Mikheev 已提交
5070

T
Tom Lane 已提交
5071 5072 5073
	XLogCtl->Write.LogwrtResult = LogwrtResult;
	Insert->LogwrtResult = LogwrtResult;
	XLogCtl->LogwrtResult = LogwrtResult;
V
WAL  
Vadim B. Mikheev 已提交
5074

T
Tom Lane 已提交
5075 5076
	XLogCtl->LogwrtRqst.Write = EndOfLog;
	XLogCtl->LogwrtRqst.Flush = EndOfLog;
5077

5078 5079 5080 5081 5082 5083 5084 5085 5086 5087
	freespace = INSERT_FREESPACE(Insert);
	if (freespace > 0)
	{
		/* Make sure rest of page is zero */
		MemSet(Insert->currpos, 0, freespace);
		XLogCtl->Write.curridx = 0;
	}
	else
	{
		/*
B
Bruce Momjian 已提交
5088 5089
		 * Whenever Write.LogwrtResult points to exactly the end of a page,
		 * Write.curridx must point to the *next* page (see XLogWrite()).
5090
		 *
B
Bruce Momjian 已提交
5091
		 * Note: it might seem we should do AdvanceXLInsertBuffer() here, but
B
Bruce Momjian 已提交
5092
		 * this is sufficient.	The first actual attempt to insert a log
5093
		 * record will advance the insert state.
5094 5095 5096 5097
		 */
		XLogCtl->Write.curridx = NextBufIdx(0);
	}

5098 5099
	/* Pre-scan prepared transactions to find out the range of XIDs present */
	oldestActiveXID = PrescanPreparedTransactions();
5100

V
WAL  
Vadim B. Mikheev 已提交
5101
	if (InRecovery)
5102
	{
B
Bruce Momjian 已提交
5103
		int			rmid;
5104 5105 5106 5107 5108 5109 5110 5111 5112 5113

		/*
		 * Allow resource managers to do any required cleanup.
		 */
		for (rmid = 0; rmid <= RM_MAX_ID; rmid++)
		{
			if (RmgrTable[rmid].rm_cleanup != NULL)
				RmgrTable[rmid].rm_cleanup();
		}

5114 5115 5116 5117 5118 5119
		/*
		 * Check to see if the XLOG sequence contained any unresolved
		 * references to uninitialized pages.
		 */
		XLogCheckInvalidPages();

5120 5121 5122 5123 5124
		/*
		 * Reset pgstat data, because it may be invalid after recovery.
		 */
		pgstat_reset_all();

T
Tom Lane 已提交
5125
		/*
5126
		 * Perform a checkpoint to update all our recovery activity to disk.
5127
		 *
5128 5129 5130 5131 5132
		 * Note that we write a shutdown checkpoint rather than an on-line
		 * one. This is not particularly critical, but since we may be
		 * assigning a new TLI, using a shutdown checkpoint allows us to have
		 * the rule that TLI only changes in shutdown checkpoints, which
		 * allows some extra error checking in xlog_redo.
T
Tom Lane 已提交
5133
		 */
5134
		CreateCheckPoint(true, true);
5135 5136 5137 5138

		/*
		 * Close down recovery environment
		 */
V
WAL  
Vadim B. Mikheev 已提交
5139
		XLogCloseRelationCache();
5140
	}
5141

T
Tom Lane 已提交
5142 5143 5144
	/*
	 * Preallocate additional log files, if wanted.
	 */
5145
	(void) PreallocXlogFiles(EndOfLog);
5146

5147 5148 5149
	/*
	 * Okay, we're officially UP.
	 */
V
WAL  
Vadim B. Mikheev 已提交
5150
	InRecovery = false;
5151 5152 5153 5154 5155

	ControlFile->state = DB_IN_PRODUCTION;
	ControlFile->time = time(NULL);
	UpdateControlFile();

5156 5157 5158
	/* start the archive_timeout timer running */
	XLogCtl->Write.lastSegSwitchTime = ControlFile->time;

5159 5160 5161 5162
	/* initialize shared-memory copy of latest checkpoint XID/epoch */
	XLogCtl->ckptXidEpoch = ControlFile->checkPointCopy.nextXidEpoch;
	XLogCtl->ckptXid = ControlFile->checkPointCopy.nextXid;

5163
	/* Start up the commit log and related stuff, too */
5164
	StartupCLOG();
5165
	StartupSUBTRANS(oldestActiveXID);
5166
	StartupMultiXact();
5167

5168 5169 5170
	/* Reload shared-memory state for prepared transactions */
	RecoverPreparedTransactions();

5171 5172
	ereport(LOG,
			(errmsg("database system is ready")));
5173

T
Tom Lane 已提交
5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184
	/* Shut down readFile facility, free space */
	if (readFile >= 0)
	{
		close(readFile);
		readFile = -1;
	}
	if (readBuf)
	{
		free(readBuf);
		readBuf = NULL;
	}
5185 5186 5187 5188 5189 5190
	if (readRecordBuf)
	{
		free(readRecordBuf);
		readRecordBuf = NULL;
		readRecordBufSize = 0;
	}
T
Tom Lane 已提交
5191 5192
}

5193 5194
/*
 * Subroutine to try to fetch and validate a prior checkpoint record.
5195 5196 5197
 *
 * whichChkpt identifies the checkpoint (merely for reporting purposes).
 * 1 for "primary", 2 for "secondary", 0 for "other" (backup_label)
5198
 */
T
Tom Lane 已提交
5199
static XLogRecord *
5200
ReadCheckpointRecord(XLogRecPtr RecPtr, int whichChkpt)
T
Tom Lane 已提交
5201 5202 5203 5204 5205
{
	XLogRecord *record;

	if (!XRecOffIsValid(RecPtr.xrecoff))
	{
5206 5207 5208 5209
		switch (whichChkpt)
		{
			case 1:
				ereport(LOG,
B
Bruce Momjian 已提交
5210
				(errmsg("invalid primary checkpoint link in control file")));
5211 5212 5213 5214 5215 5216 5217
				break;
			case 2:
				ereport(LOG,
						(errmsg("invalid secondary checkpoint link in control file")));
				break;
			default:
				ereport(LOG,
B
Bruce Momjian 已提交
5218
				   (errmsg("invalid checkpoint link in backup_label file")));
5219 5220
				break;
		}
T
Tom Lane 已提交
5221 5222 5223
		return NULL;
	}

5224
	record = ReadRecord(&RecPtr, LOG);
T
Tom Lane 已提交
5225 5226 5227

	if (record == NULL)
	{
5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242
		switch (whichChkpt)
		{
			case 1:
				ereport(LOG,
						(errmsg("invalid primary checkpoint record")));
				break;
			case 2:
				ereport(LOG,
						(errmsg("invalid secondary checkpoint record")));
				break;
			default:
				ereport(LOG,
						(errmsg("invalid checkpoint record")));
				break;
		}
T
Tom Lane 已提交
5243 5244 5245 5246
		return NULL;
	}
	if (record->xl_rmid != RM_XLOG_ID)
	{
5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258
		switch (whichChkpt)
		{
			case 1:
				ereport(LOG,
						(errmsg("invalid resource manager ID in primary checkpoint record")));
				break;
			case 2:
				ereport(LOG,
						(errmsg("invalid resource manager ID in secondary checkpoint record")));
				break;
			default:
				ereport(LOG,
B
Bruce Momjian 已提交
5259
				(errmsg("invalid resource manager ID in checkpoint record")));
5260 5261
				break;
		}
T
Tom Lane 已提交
5262 5263 5264 5265 5266
		return NULL;
	}
	if (record->xl_info != XLOG_CHECKPOINT_SHUTDOWN &&
		record->xl_info != XLOG_CHECKPOINT_ONLINE)
	{
5267 5268 5269 5270
		switch (whichChkpt)
		{
			case 1:
				ereport(LOG,
B
Bruce Momjian 已提交
5271
				   (errmsg("invalid xl_info in primary checkpoint record")));
5272 5273 5274
				break;
			case 2:
				ereport(LOG,
B
Bruce Momjian 已提交
5275
				 (errmsg("invalid xl_info in secondary checkpoint record")));
5276 5277 5278 5279 5280 5281
				break;
			default:
				ereport(LOG,
						(errmsg("invalid xl_info in checkpoint record")));
				break;
		}
T
Tom Lane 已提交
5282 5283
		return NULL;
	}
5284 5285
	if (record->xl_len != sizeof(CheckPoint) ||
		record->xl_tot_len != SizeOfXLogRecord + sizeof(CheckPoint))
T
Tom Lane 已提交
5286
	{
5287 5288 5289 5290
		switch (whichChkpt)
		{
			case 1:
				ereport(LOG,
B
Bruce Momjian 已提交
5291
					(errmsg("invalid length of primary checkpoint record")));
5292 5293 5294
				break;
			case 2:
				ereport(LOG,
B
Bruce Momjian 已提交
5295
				  (errmsg("invalid length of secondary checkpoint record")));
5296 5297 5298 5299 5300 5301
				break;
			default:
				ereport(LOG,
						(errmsg("invalid length of checkpoint record")));
				break;
		}
T
Tom Lane 已提交
5302 5303 5304
		return NULL;
	}
	return record;
5305 5306
}

V
WAL  
Vadim B. Mikheev 已提交
5307
/*
5308 5309
 * This must be called during startup of a backend process, except that
 * it need not be called in a standalone backend (which does StartupXLOG
5310
 * instead).  We need to initialize the local copies of ThisTimeLineID and
5311 5312
 * RedoRecPtr.
 *
5313
 * Note: before Postgres 8.0, we went to some effort to keep the postmaster
5314
 * process's copies of ThisTimeLineID and RedoRecPtr valid too.  This was
5315
 * unnecessary however, since the postmaster itself never touches XLOG anyway.
V
WAL  
Vadim B. Mikheev 已提交
5316 5317
 */
void
5318
InitXLOGAccess(void)
V
WAL  
Vadim B. Mikheev 已提交
5319
{
5320 5321
	/* ThisTimeLineID doesn't change so we need no lock to copy it */
	ThisTimeLineID = XLogCtl->ThisTimeLineID;
5322 5323
	/* Use GetRedoRecPtr to copy the RedoRecPtr safely */
	(void) GetRedoRecPtr();
5324 5325 5326 5327 5328 5329 5330 5331
}

/*
 * Once spawned, a backend may update its local RedoRecPtr from
 * XLogCtl->Insert.RedoRecPtr; it must hold the insert lock or info_lck
 * to do so.  This is done in XLogInsert() or GetRedoRecPtr().
 */
XLogRecPtr
5332 5333
GetRedoRecPtr(void)
{
5334 5335 5336
	/* use volatile pointer to prevent code rearrangement */
	volatile XLogCtlData *xlogctl = XLogCtl;

5337
	SpinLockAcquire(&xlogctl->info_lck);
5338 5339
	Assert(XLByteLE(RedoRecPtr, xlogctl->Insert.RedoRecPtr));
	RedoRecPtr = xlogctl->Insert.RedoRecPtr;
5340
	SpinLockRelease(&xlogctl->info_lck);
5341 5342

	return RedoRecPtr;
V
WAL  
Vadim B. Mikheev 已提交
5343 5344
}

5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360
/*
 * Get the time of the last xlog segment switch
 */
time_t
GetLastSegSwitchTime(void)
{
	time_t		result;

	/* Need WALWriteLock, but shared lock is sufficient */
	LWLockAcquire(WALWriteLock, LW_SHARED);
	result = XLogCtl->Write.lastSegSwitchTime;
	LWLockRelease(WALWriteLock);

	return result;
}

5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371
/*
 * GetNextXidAndEpoch - get the current nextXid value and associated epoch
 *
 * This is exported for use by code that would like to have 64-bit XIDs.
 * We don't really support such things, but all XIDs within the system
 * can be presumed "close to" the result, and thus the epoch associated
 * with them can be determined.
 */
void
GetNextXidAndEpoch(TransactionId *xid, uint32 *epoch)
{
B
Bruce Momjian 已提交
5372 5373 5374
	uint32		ckptXidEpoch;
	TransactionId ckptXid;
	TransactionId nextXid;
5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400

	/* Must read checkpoint info first, else have race condition */
	{
		/* use volatile pointer to prevent code rearrangement */
		volatile XLogCtlData *xlogctl = XLogCtl;

		SpinLockAcquire(&xlogctl->info_lck);
		ckptXidEpoch = xlogctl->ckptXidEpoch;
		ckptXid = xlogctl->ckptXid;
		SpinLockRelease(&xlogctl->info_lck);
	}

	/* Now fetch current nextXid */
	nextXid = ReadNewTransactionId();

	/*
	 * nextXid is certainly logically later than ckptXid.  So if it's
	 * numerically less, it must have wrapped into the next epoch.
	 */
	if (nextXid < ckptXid)
		ckptXidEpoch++;

	*xid = nextXid;
	*epoch = ckptXidEpoch;
}

5401
/*
T
Tom Lane 已提交
5402
 * This must be called ONCE during postmaster or standalone-backend shutdown
5403 5404
 */
void
5405
ShutdownXLOG(int code, Datum arg)
5406
{
5407 5408
	ereport(LOG,
			(errmsg("shutting down")));
5409

5410
	CreateCheckPoint(true, true);
5411
	ShutdownCLOG();
5412
	ShutdownSUBTRANS();
5413
	ShutdownMultiXact();
5414

5415 5416
	ereport(LOG,
			(errmsg("database system is shut down")));
5417 5418
}

T
Tom Lane 已提交
5419 5420
/*
 * Perform a checkpoint --- either during shutdown, or on-the-fly
5421 5422 5423
 *
 * If force is true, we force a checkpoint regardless of whether any XLOG
 * activity has occurred since the last one.
T
Tom Lane 已提交
5424
 */
5425
void
5426
CreateCheckPoint(bool shutdown, bool force)
5427
{
5428 5429 5430
	CheckPoint	checkPoint;
	XLogRecPtr	recptr;
	XLogCtlInsert *Insert = &XLogCtl->Insert;
B
Bruce Momjian 已提交
5431
	XLogRecData rdata;
5432
	uint32		freespace;
V
Vadim B. Mikheev 已提交
5433 5434
	uint32		_logId;
	uint32		_logSeg;
5435 5436 5437
	int			nsegsadded = 0;
	int			nsegsremoved = 0;
	int			nsegsrecycled = 0;
V
Vadim B. Mikheev 已提交
5438

5439
	/*
B
Bruce Momjian 已提交
5440 5441 5442 5443
	 * Acquire CheckpointLock to ensure only one checkpoint happens at a time.
	 * (This is just pro forma, since in the present system structure there is
	 * only one process that is allowed to issue checkpoints at any given
	 * time.)
5444
	 */
5445
	LWLockAcquire(CheckpointLock, LW_EXCLUSIVE);
5446

5447 5448 5449
	/*
	 * Use a critical section to force system panic if we have trouble.
	 */
5450 5451
	START_CRIT_SECTION();

5452 5453 5454 5455 5456 5457
	if (shutdown)
	{
		ControlFile->state = DB_SHUTDOWNING;
		ControlFile->time = time(NULL);
		UpdateControlFile();
	}
T
Tom Lane 已提交
5458

5459
	MemSet(&checkPoint, 0, sizeof(checkPoint));
5460
	checkPoint.ThisTimeLineID = ThisTimeLineID;
T
Tom Lane 已提交
5461
	checkPoint.time = time(NULL);
5462

5463
	/*
B
Bruce Momjian 已提交
5464 5465 5466 5467
	 * We must hold CheckpointStartLock while determining the checkpoint REDO
	 * pointer.  This ensures that any concurrent transaction commits will be
	 * either not yet logged, or logged and recorded in pg_clog. See notes in
	 * RecordTransactionCommit().
5468 5469 5470 5471
	 */
	LWLockAcquire(CheckpointStartLock, LW_EXCLUSIVE);

	/* And we need WALInsertLock too */
5472
	LWLockAcquire(WALInsertLock, LW_EXCLUSIVE);
T
Tom Lane 已提交
5473 5474

	/*
B
Bruce Momjian 已提交
5475 5476 5477 5478 5479 5480 5481 5482
	 * If this isn't a shutdown or forced checkpoint, and we have not inserted
	 * any XLOG records since the start of the last checkpoint, skip the
	 * checkpoint.	The idea here is to avoid inserting duplicate checkpoints
	 * when the system is idle. That wastes log space, and more importantly it
	 * exposes us to possible loss of both current and previous checkpoint
	 * records if the machine crashes just as we're writing the update.
	 * (Perhaps it'd make even more sense to checkpoint only when the previous
	 * checkpoint record is in a different xlog page?)
T
Tom Lane 已提交
5483
	 *
5484 5485 5486 5487
	 * We have to make two tests to determine that nothing has happened since
	 * the start of the last checkpoint: current insertion point must match
	 * the end of the last checkpoint record, and its redo pointer must point
	 * to itself.
T
Tom Lane 已提交
5488
	 */
5489
	if (!shutdown && !force)
T
Tom Lane 已提交
5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501
	{
		XLogRecPtr	curInsert;

		INSERT_RECPTR(curInsert, Insert, Insert->curridx);
		if (curInsert.xlogid == ControlFile->checkPoint.xlogid &&
			curInsert.xrecoff == ControlFile->checkPoint.xrecoff +
			MAXALIGN(SizeOfXLogRecord + sizeof(CheckPoint)) &&
			ControlFile->checkPoint.xlogid ==
			ControlFile->checkPointCopy.redo.xlogid &&
			ControlFile->checkPoint.xrecoff ==
			ControlFile->checkPointCopy.redo.xrecoff)
		{
5502
			LWLockRelease(WALInsertLock);
5503
			LWLockRelease(CheckpointStartLock);
5504
			LWLockRelease(CheckpointLock);
T
Tom Lane 已提交
5505 5506 5507 5508 5509 5510 5511 5512
			END_CRIT_SECTION();
			return;
		}
	}

	/*
	 * Compute new REDO record ptr = location of next XLOG record.
	 *
B
Bruce Momjian 已提交
5513 5514 5515 5516
	 * NB: this is NOT necessarily where the checkpoint record itself will be,
	 * since other backends may insert more XLOG records while we're off doing
	 * the buffer flush work.  Those XLOG records are logically after the
	 * checkpoint, even though physically before it.  Got that?
T
Tom Lane 已提交
5517 5518
	 */
	freespace = INSERT_FREESPACE(Insert);
5519 5520
	if (freespace < SizeOfXLogRecord)
	{
5521
		(void) AdvanceXLInsertBuffer(false);
T
Tom Lane 已提交
5522
		/* OK to ignore update return flag, since we will do flush anyway */
5523
		freespace = INSERT_FREESPACE(Insert);
5524
	}
T
Tom Lane 已提交
5525
	INSERT_RECPTR(checkPoint.redo, Insert, Insert->curridx);
B
Bruce Momjian 已提交
5526

T
Tom Lane 已提交
5527
	/*
B
Bruce Momjian 已提交
5528 5529
	 * Here we update the shared RedoRecPtr for future XLogInsert calls; this
	 * must be done while holding the insert lock AND the info_lck.
5530
	 *
B
Bruce Momjian 已提交
5531
	 * Note: if we fail to complete the checkpoint, RedoRecPtr will be left
B
Bruce Momjian 已提交
5532 5533 5534 5535 5536
	 * pointing past where it really needs to point.  This is okay; the only
	 * consequence is that XLogInsert might back up whole buffers that it
	 * didn't really need to.  We can't postpone advancing RedoRecPtr because
	 * XLogInserts that happen while we are dumping buffers must assume that
	 * their buffer changes are not included in the checkpoint.
T
Tom Lane 已提交
5537
	 */
5538 5539 5540 5541
	{
		/* use volatile pointer to prevent code rearrangement */
		volatile XLogCtlData *xlogctl = XLogCtl;

5542
		SpinLockAcquire(&xlogctl->info_lck);
5543
		RedoRecPtr = xlogctl->Insert.RedoRecPtr = checkPoint.redo;
5544
		SpinLockRelease(&xlogctl->info_lck);
5545
	}
B
Bruce Momjian 已提交
5546

T
Tom Lane 已提交
5547
	/*
5548 5549
	 * Now we can release insert lock and checkpoint start lock, allowing
	 * other xacts to proceed even while we are flushing disk buffers.
T
Tom Lane 已提交
5550
	 */
5551
	LWLockRelease(WALInsertLock);
5552

5553 5554
	LWLockRelease(CheckpointStartLock);

5555 5556 5557 5558
	if (!shutdown)
		ereport(DEBUG2,
				(errmsg("checkpoint starting")));

5559 5560 5561
	/*
	 * Get the other info we need for the checkpoint record.
	 */
5562
	LWLockAcquire(XidGenLock, LW_SHARED);
5563
	checkPoint.nextXid = ShmemVariableCache->nextXid;
5564
	LWLockRelease(XidGenLock);
T
Tom Lane 已提交
5565

5566 5567 5568 5569 5570
	/* Increase XID epoch if we've wrapped around since last checkpoint */
	checkPoint.nextXidEpoch = ControlFile->checkPointCopy.nextXidEpoch;
	if (checkPoint.nextXid < ControlFile->checkPointCopy.nextXid)
		checkPoint.nextXidEpoch++;

5571
	LWLockAcquire(OidGenLock, LW_SHARED);
5572
	checkPoint.nextOid = ShmemVariableCache->nextOid;
5573 5574
	if (!shutdown)
		checkPoint.nextOid += ShmemVariableCache->oidCount;
5575
	LWLockRelease(OidGenLock);
5576

5577 5578 5579
	MultiXactGetCheckptMulti(shutdown,
							 &checkPoint.nextMulti,
							 &checkPoint.nextMultiOffset);
5580

T
Tom Lane 已提交
5581
	/*
B
Bruce Momjian 已提交
5582 5583
	 * Having constructed the checkpoint record, ensure all shmem disk buffers
	 * and commit-log buffers are flushed to disk.
5584
	 *
5585 5586
	 * This I/O could fail for various reasons.  If so, we will fail to
	 * complete the checkpoint, but there is no reason to force a system
5587
	 * panic. Accordingly, exit critical section while doing it.
T
Tom Lane 已提交
5588
	 */
5589 5590
	END_CRIT_SECTION();

5591
	CheckPointGuts(checkPoint.redo);
5592

5593 5594
	START_CRIT_SECTION();

T
Tom Lane 已提交
5595 5596 5597
	/*
	 * Now insert the checkpoint record into XLOG.
	 */
B
Bruce Momjian 已提交
5598
	rdata.data = (char *) (&checkPoint);
5599
	rdata.len = sizeof(checkPoint);
5600
	rdata.buffer = InvalidBuffer;
5601 5602
	rdata.next = NULL;

T
Tom Lane 已提交
5603 5604 5605 5606 5607 5608
	recptr = XLogInsert(RM_XLOG_ID,
						shutdown ? XLOG_CHECKPOINT_SHUTDOWN :
						XLOG_CHECKPOINT_ONLINE,
						&rdata);

	XLogFlush(recptr);
5609

T
Tom Lane 已提交
5610
	/*
B
Bruce Momjian 已提交
5611 5612
	 * We now have ProcLastRecPtr = start of actual checkpoint record, recptr
	 * = end of actual checkpoint record.
T
Tom Lane 已提交
5613 5614
	 */
	if (shutdown && !XLByteEQ(checkPoint.redo, ProcLastRecPtr))
5615 5616
		ereport(PANIC,
				(errmsg("concurrent transaction log activity while database system is shutting down")));
5617

T
Tom Lane 已提交
5618
	/*
5619 5620
	 * Select point at which we can truncate the log, which we base on the
	 * prior checkpoint's earliest info.
T
Tom Lane 已提交
5621
	 */
5622
	XLByteToSeg(ControlFile->checkPointCopy.redo, _logId, _logSeg);
5623

T
Tom Lane 已提交
5624 5625 5626
	/*
	 * Update the control file.
	 */
5627
	LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
5628 5629
	if (shutdown)
		ControlFile->state = DB_SHUTDOWNED;
T
Tom Lane 已提交
5630 5631 5632
	ControlFile->prevCheckPoint = ControlFile->checkPoint;
	ControlFile->checkPoint = ProcLastRecPtr;
	ControlFile->checkPointCopy = checkPoint;
5633 5634
	ControlFile->time = time(NULL);
	UpdateControlFile();
5635
	LWLockRelease(ControlFileLock);
5636

5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647
	/* Update shared-memory copy of checkpoint XID/epoch */
	{
		/* use volatile pointer to prevent code rearrangement */
		volatile XLogCtlData *xlogctl = XLogCtl;

		SpinLockAcquire(&xlogctl->info_lck);
		xlogctl->ckptXidEpoch = checkPoint.nextXidEpoch;
		xlogctl->ckptXid = checkPoint.nextXid;
		SpinLockRelease(&xlogctl->info_lck);
	}

5648
	/*
B
Bruce Momjian 已提交
5649 5650
	 * We are now done with critical updates; no need for system panic if we
	 * have trouble while fooling with offline log segments.
5651 5652 5653
	 */
	END_CRIT_SECTION();

V
Vadim B. Mikheev 已提交
5654
	/*
T
Tom Lane 已提交
5655 5656
	 * Delete offline log files (those no longer needed even for previous
	 * checkpoint).
V
Vadim B. Mikheev 已提交
5657 5658 5659
	 */
	if (_logId || _logSeg)
	{
T
Tom Lane 已提交
5660
		PrevLogSeg(_logId, _logSeg);
5661 5662
		MoveOfflineLogs(_logId, _logSeg, recptr,
						&nsegsremoved, &nsegsrecycled);
V
Vadim B. Mikheev 已提交
5663 5664
	}

T
Tom Lane 已提交
5665
	/*
B
Bruce Momjian 已提交
5666 5667
	 * Make more log segments if needed.  (Do this after deleting offline log
	 * segments, to avoid having peak disk space usage higher than necessary.)
T
Tom Lane 已提交
5668 5669
	 */
	if (!shutdown)
5670
		nsegsadded = PreallocXlogFiles(recptr);
T
Tom Lane 已提交
5671

5672
	/*
B
Bruce Momjian 已提交
5673 5674 5675 5676 5677
	 * Truncate pg_subtrans if possible.  We can throw away all data before
	 * the oldest XMIN of any running transaction.	No future transaction will
	 * attempt to reference any pg_subtrans entry older than that (see Asserts
	 * in subtrans.c).	During recovery, though, we mustn't do this because
	 * StartupSUBTRANS hasn't been called yet.
5678
	 */
5679
	if (!InRecovery)
5680
		TruncateSUBTRANS(GetOldestXmin(true, false));
5681

5682
	if (!shutdown)
5683
		ereport(DEBUG2,
5684 5685 5686
				(errmsg("checkpoint complete; %d transaction log file(s) added, %d removed, %d recycled",
						nsegsadded, nsegsremoved, nsegsrecycled)));

5687
	LWLockRelease(CheckpointLock);
5688
}
V
WAL  
Vadim B. Mikheev 已提交
5689

5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701
/*
 * Flush all data in shared memory to disk, and fsync
 *
 * This is the common code shared between regular checkpoints and
 * recovery restartpoints.
 */
static void
CheckPointGuts(XLogRecPtr checkPointRedo)
{
	CheckPointCLOG();
	CheckPointSUBTRANS();
	CheckPointMultiXact();
B
Bruce Momjian 已提交
5702
	FlushBufferPool();			/* performs all required fsyncs */
5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718
	/* We deliberately delay 2PC checkpointing as long as possible */
	CheckPointTwoPhase(checkPointRedo);
}

/*
 * Set a recovery restart point if appropriate
 *
 * This is similar to CreateCheckpoint, but is used during WAL recovery
 * to establish a point from which recovery can roll forward without
 * replaying the entire recovery log.  This function is called each time
 * a checkpoint record is read from XLOG; it must determine whether a
 * restartpoint is needed or not.
 */
static void
RecoveryRestartPoint(const CheckPoint *checkPoint)
{
B
Bruce Momjian 已提交
5719 5720
	int			elapsed_secs;
	int			rmid;
5721 5722

	/*
B
Bruce Momjian 已提交
5723 5724
	 * Do nothing if the elapsed time since the last restartpoint is less than
	 * half of checkpoint_timeout.	(We use a value less than
5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753
	 * checkpoint_timeout so that variations in the timing of checkpoints on
	 * the master, or speed of transmission of WAL segments to a slave, won't
	 * make the slave skip a restartpoint once it's synced with the master.)
	 * Checking true elapsed time keeps us from doing restartpoints too often
	 * while rapidly scanning large amounts of WAL.
	 */
	elapsed_secs = time(NULL) - ControlFile->time;
	if (elapsed_secs < CheckPointTimeout / 2)
		return;

	/*
	 * Is it safe to checkpoint?  We must ask each of the resource managers
	 * whether they have any partial state information that might prevent a
	 * correct restart from this point.  If so, we skip this opportunity, but
	 * return at the next checkpoint record for another try.
	 */
	for (rmid = 0; rmid <= RM_MAX_ID; rmid++)
	{
		if (RmgrTable[rmid].rm_safe_restartpoint != NULL)
			if (!(RmgrTable[rmid].rm_safe_restartpoint()))
				return;
	}

	/*
	 * OK, force data out to disk
	 */
	CheckPointGuts(checkPoint->redo);

	/*
B
Bruce Momjian 已提交
5754 5755 5756
	 * Update pg_control so that any subsequent crash will restart from this
	 * checkpoint.	Note: ReadRecPtr gives the XLOG address of the checkpoint
	 * record itself.
5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768
	 */
	ControlFile->prevCheckPoint = ControlFile->checkPoint;
	ControlFile->checkPoint = ReadRecPtr;
	ControlFile->checkPointCopy = *checkPoint;
	ControlFile->time = time(NULL);
	UpdateControlFile();

	ereport(DEBUG2,
			(errmsg("recovery restart point at %X/%X",
					checkPoint->redo.xlogid, checkPoint->redo.xrecoff)));
}

T
Tom Lane 已提交
5769 5770 5771
/*
 * Write a NEXTOID log record
 */
5772 5773 5774
void
XLogPutNextOid(Oid nextOid)
{
B
Bruce Momjian 已提交
5775
	XLogRecData rdata;
5776

B
Bruce Momjian 已提交
5777
	rdata.data = (char *) (&nextOid);
5778
	rdata.len = sizeof(Oid);
5779
	rdata.buffer = InvalidBuffer;
5780 5781
	rdata.next = NULL;
	(void) XLogInsert(RM_XLOG_ID, XLOG_NEXTOID, &rdata);
B
Bruce Momjian 已提交
5782

5783 5784
	/*
	 * We need not flush the NEXTOID record immediately, because any of the
B
Bruce Momjian 已提交
5785 5786 5787 5788 5789
	 * just-allocated OIDs could only reach disk as part of a tuple insert or
	 * update that would have its own XLOG record that must follow the NEXTOID
	 * record.	Therefore, the standard buffer LSN interlock applied to those
	 * records will ensure no such OID reaches disk before the NEXTOID record
	 * does.
5790 5791 5792 5793 5794 5795 5796 5797 5798 5799
	 *
	 * Note, however, that the above statement only covers state "within" the
	 * database.  When we use a generated OID as a file or directory name,
	 * we are in a sense violating the basic WAL rule, because that filesystem
	 * change may reach disk before the NEXTOID WAL record does.  The impact
	 * of this is that if a database crash occurs immediately afterward,
	 * we might after restart re-generate the same OID and find that it
	 * conflicts with the leftover file or directory.  But since for safety's
	 * sake we always loop until finding a nonconflicting filename, this poses
	 * no real problem in practice. See pgsql-hackers discussion 27-Sep-2006.
5800 5801 5802
	 */
}

5803 5804 5805 5806 5807 5808 5809 5810 5811 5812
/*
 * Write an XLOG SWITCH record.
 *
 * Here we just blindly issue an XLogInsert request for the record.
 * All the magic happens inside XLogInsert.
 *
 * The return value is either the end+1 address of the switch record,
 * or the end+1 address of the prior segment if we did not need to
 * write a switch record because we are already at segment start.
 */
5813
XLogRecPtr
5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829
RequestXLogSwitch(void)
{
	XLogRecPtr	RecPtr;
	XLogRecData rdata;

	/* XLOG SWITCH, alone among xlog record types, has no data */
	rdata.buffer = InvalidBuffer;
	rdata.data = NULL;
	rdata.len = 0;
	rdata.next = NULL;

	RecPtr = XLogInsert(RM_XLOG_ID, XLOG_SWITCH, &rdata);

	return RecPtr;
}

T
Tom Lane 已提交
5830 5831 5832
/*
 * XLOG resource manager's routines
 */
V
WAL  
Vadim B. Mikheev 已提交
5833 5834 5835
void
xlog_redo(XLogRecPtr lsn, XLogRecord *record)
{
B
Bruce Momjian 已提交
5836
	uint8		info = record->xl_info & ~XLR_INFO_MASK;
5837

5838
	if (info == XLOG_NEXTOID)
5839
	{
B
Bruce Momjian 已提交
5840
		Oid			nextOid;
5841 5842 5843

		memcpy(&nextOid, XLogRecGetData(record), sizeof(Oid));
		if (ShmemVariableCache->nextOid < nextOid)
T
Tom Lane 已提交
5844
		{
5845
			ShmemVariableCache->nextOid = nextOid;
T
Tom Lane 已提交
5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857
			ShmemVariableCache->oidCount = 0;
		}
	}
	else if (info == XLOG_CHECKPOINT_SHUTDOWN)
	{
		CheckPoint	checkPoint;

		memcpy(&checkPoint, XLogRecGetData(record), sizeof(CheckPoint));
		/* In a SHUTDOWN checkpoint, believe the counters exactly */
		ShmemVariableCache->nextXid = checkPoint.nextXid;
		ShmemVariableCache->nextOid = checkPoint.nextOid;
		ShmemVariableCache->oidCount = 0;
5858 5859
		MultiXactSetNextMXact(checkPoint.nextMulti,
							  checkPoint.nextMultiOffset);
B
Bruce Momjian 已提交
5860

5861 5862 5863 5864
		/* ControlFile->checkPointCopy always tracks the latest ckpt XID */
		ControlFile->checkPointCopy.nextXidEpoch = checkPoint.nextXidEpoch;
		ControlFile->checkPointCopy.nextXid = checkPoint.nextXid;

5865
		/*
B
Bruce Momjian 已提交
5866
		 * TLI may change in a shutdown checkpoint, but it shouldn't decrease
5867 5868 5869 5870 5871 5872 5873 5874
		 */
		if (checkPoint.ThisTimeLineID != ThisTimeLineID)
		{
			if (checkPoint.ThisTimeLineID < ThisTimeLineID ||
				!list_member_int(expectedTLIs,
								 (int) checkPoint.ThisTimeLineID))
				ereport(PANIC,
						(errmsg("unexpected timeline ID %u (after %u) in checkpoint record",
B
Bruce Momjian 已提交
5875
								checkPoint.ThisTimeLineID, ThisTimeLineID)));
5876 5877 5878
			/* Following WAL records should be run with new TLI */
			ThisTimeLineID = checkPoint.ThisTimeLineID;
		}
5879 5880

		RecoveryRestartPoint(&checkPoint);
T
Tom Lane 已提交
5881 5882 5883 5884 5885 5886
	}
	else if (info == XLOG_CHECKPOINT_ONLINE)
	{
		CheckPoint	checkPoint;

		memcpy(&checkPoint, XLogRecGetData(record), sizeof(CheckPoint));
5887
		/* In an ONLINE checkpoint, treat the counters like NEXTOID */
5888 5889
		if (TransactionIdPrecedes(ShmemVariableCache->nextXid,
								  checkPoint.nextXid))
T
Tom Lane 已提交
5890 5891 5892 5893 5894 5895
			ShmemVariableCache->nextXid = checkPoint.nextXid;
		if (ShmemVariableCache->nextOid < checkPoint.nextOid)
		{
			ShmemVariableCache->nextOid = checkPoint.nextOid;
			ShmemVariableCache->oidCount = 0;
		}
5896 5897
		MultiXactAdvanceNextMXact(checkPoint.nextMulti,
								  checkPoint.nextMultiOffset);
5898 5899 5900 5901 5902

		/* ControlFile->checkPointCopy always tracks the latest ckpt XID */
		ControlFile->checkPointCopy.nextXidEpoch = checkPoint.nextXidEpoch;
		ControlFile->checkPointCopy.nextXid = checkPoint.nextXid;

5903 5904
		/* TLI should not change in an on-line checkpoint */
		if (checkPoint.ThisTimeLineID != ThisTimeLineID)
5905
			ereport(PANIC,
5906 5907
					(errmsg("unexpected timeline ID %u (should be %u) in checkpoint record",
							checkPoint.ThisTimeLineID, ThisTimeLineID)));
5908 5909

		RecoveryRestartPoint(&checkPoint);
5910
	}
5911 5912 5913 5914
	else if (info == XLOG_SWITCH)
	{
		/* nothing to do here */
	}
V
WAL  
Vadim B. Mikheev 已提交
5915
}
B
Bruce Momjian 已提交
5916

V
WAL  
Vadim B. Mikheev 已提交
5917
void
5918
xlog_desc(StringInfo buf, uint8 xl_info, char *rec)
V
WAL  
Vadim B. Mikheev 已提交
5919
{
B
Bruce Momjian 已提交
5920
	uint8		info = xl_info & ~XLR_INFO_MASK;
V
WAL  
Vadim B. Mikheev 已提交
5921

T
Tom Lane 已提交
5922 5923
	if (info == XLOG_CHECKPOINT_SHUTDOWN ||
		info == XLOG_CHECKPOINT_ONLINE)
V
WAL  
Vadim B. Mikheev 已提交
5924
	{
B
Bruce Momjian 已提交
5925 5926
		CheckPoint *checkpoint = (CheckPoint *) rec;

5927
		appendStringInfo(buf, "checkpoint: redo %X/%X; undo %X/%X; "
B
Bruce Momjian 已提交
5928 5929 5930 5931 5932 5933 5934 5935 5936
						 "tli %u; xid %u/%u; oid %u; multi %u; offset %u; %s",
						 checkpoint->redo.xlogid, checkpoint->redo.xrecoff,
						 checkpoint->undo.xlogid, checkpoint->undo.xrecoff,
						 checkpoint->ThisTimeLineID,
						 checkpoint->nextXidEpoch, checkpoint->nextXid,
						 checkpoint->nextOid,
						 checkpoint->nextMulti,
						 checkpoint->nextMultiOffset,
				 (info == XLOG_CHECKPOINT_SHUTDOWN) ? "shutdown" : "online");
T
Tom Lane 已提交
5937
	}
5938 5939
	else if (info == XLOG_NEXTOID)
	{
B
Bruce Momjian 已提交
5940
		Oid			nextOid;
5941 5942

		memcpy(&nextOid, rec, sizeof(Oid));
5943
		appendStringInfo(buf, "nextOid: %u", nextOid);
5944
	}
5945 5946 5947 5948
	else if (info == XLOG_SWITCH)
	{
		appendStringInfo(buf, "xlog switch");
	}
V
WAL  
Vadim B. Mikheev 已提交
5949
	else
5950
		appendStringInfo(buf, "UNKNOWN");
V
WAL  
Vadim B. Mikheev 已提交
5951 5952
}

5953
#ifdef WAL_DEBUG
5954

V
WAL  
Vadim B. Mikheev 已提交
5955
static void
5956
xlog_outrec(StringInfo buf, XLogRecord *record)
V
WAL  
Vadim B. Mikheev 已提交
5957
{
B
Bruce Momjian 已提交
5958
	int			i;
5959

5960
	appendStringInfo(buf, "prev %X/%X; xid %u",
5961 5962
					 record->xl_prev.xlogid, record->xl_prev.xrecoff,
					 record->xl_xid);
5963

5964
	for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
5965
	{
5966
		if (record->xl_info & XLR_SET_BKP_BLOCK(i))
B
Bruce Momjian 已提交
5967
			appendStringInfo(buf, "; bkpb%d", i + 1);
5968 5969
	}

5970
	appendStringInfo(buf, ": %s", RmgrTable[record->xl_rmid].rm_name);
V
WAL  
Vadim B. Mikheev 已提交
5971
}
B
Bruce Momjian 已提交
5972
#endif   /* WAL_DEBUG */
5973 5974 5975


/*
5976
 * GUC support
5977
 */
5978
const char *
5979
assign_xlog_sync_method(const char *method, bool doit, GucSource source)
5980
{
B
Bruce Momjian 已提交
5981 5982
	int			new_sync_method;
	int			new_sync_bit;
5983

5984
	if (pg_strcasecmp(method, "fsync") == 0)
5985 5986 5987 5988
	{
		new_sync_method = SYNC_METHOD_FSYNC;
		new_sync_bit = 0;
	}
5989 5990 5991 5992 5993 5994 5995
#ifdef HAVE_FSYNC_WRITETHROUGH
	else if (pg_strcasecmp(method, "fsync_writethrough") == 0)
	{
		new_sync_method = SYNC_METHOD_FSYNC_WRITETHROUGH;
		new_sync_bit = 0;
	}
#endif
5996
#ifdef HAVE_FDATASYNC
5997
	else if (pg_strcasecmp(method, "fdatasync") == 0)
5998 5999 6000 6001 6002 6003
	{
		new_sync_method = SYNC_METHOD_FDATASYNC;
		new_sync_bit = 0;
	}
#endif
#ifdef OPEN_SYNC_FLAG
6004
	else if (pg_strcasecmp(method, "open_sync") == 0)
6005 6006 6007 6008 6009 6010
	{
		new_sync_method = SYNC_METHOD_OPEN;
		new_sync_bit = OPEN_SYNC_FLAG;
	}
#endif
#ifdef OPEN_DATASYNC_FLAG
6011
	else if (pg_strcasecmp(method, "open_datasync") == 0)
6012 6013 6014 6015 6016 6017
	{
		new_sync_method = SYNC_METHOD_OPEN;
		new_sync_bit = OPEN_DATASYNC_FLAG;
	}
#endif
	else
6018
		return NULL;
6019

6020 6021 6022
	if (!doit)
		return method;

6023 6024 6025
	if (sync_method != new_sync_method || open_sync_bit != new_sync_bit)
	{
		/*
B
Bruce Momjian 已提交
6026 6027
		 * To ensure that no blocks escape unsynced, force an fsync on the
		 * currently open log segment (if any).  Also, if the open flag is
B
Bruce Momjian 已提交
6028 6029
		 * changing, close the log file so it will be reopened (with new flag
		 * bit) at next use.
6030 6031 6032 6033
		 */
		if (openLogFile >= 0)
		{
			if (pg_fsync(openLogFile) != 0)
6034 6035
				ereport(PANIC,
						(errcode_for_file_access(),
B
Bruce Momjian 已提交
6036 6037
						 errmsg("could not fsync log file %u, segment %u: %m",
								openLogId, openLogSeg)));
6038
			if (open_sync_bit != new_sync_bit)
6039
				XLogFileClose();
6040 6041 6042 6043
		}
		sync_method = new_sync_method;
		open_sync_bit = new_sync_bit;
	}
6044 6045

	return method;
6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056
}


/*
 * Issue appropriate kind of fsync (if any) on the current XLOG output file
 */
static void
issue_xlog_fsync(void)
{
	switch (sync_method)
	{
6057
		case SYNC_METHOD_FSYNC:
6058
			if (pg_fsync_no_writethrough(openLogFile) != 0)
6059 6060
				ereport(PANIC,
						(errcode_for_file_access(),
B
Bruce Momjian 已提交
6061 6062
						 errmsg("could not fsync log file %u, segment %u: %m",
								openLogId, openLogSeg)));
6063
			break;
6064 6065 6066 6067 6068
#ifdef HAVE_FSYNC_WRITETHROUGH
		case SYNC_METHOD_FSYNC_WRITETHROUGH:
			if (pg_fsync_writethrough(openLogFile) != 0)
				ereport(PANIC,
						(errcode_for_file_access(),
B
Bruce Momjian 已提交
6069 6070
						 errmsg("could not fsync write-through log file %u, segment %u: %m",
								openLogId, openLogSeg)));
6071 6072
			break;
#endif
6073 6074 6075
#ifdef HAVE_FDATASYNC
		case SYNC_METHOD_FDATASYNC:
			if (pg_fdatasync(openLogFile) != 0)
6076 6077
				ereport(PANIC,
						(errcode_for_file_access(),
B
Bruce Momjian 已提交
6078 6079
					errmsg("could not fdatasync log file %u, segment %u: %m",
						   openLogId, openLogSeg)));
6080 6081 6082 6083 6084 6085
			break;
#endif
		case SYNC_METHOD_OPEN:
			/* write synced it already */
			break;
		default:
6086
			elog(PANIC, "unrecognized wal_sync_method: %d", sync_method);
6087 6088 6089
			break;
	}
}
6090 6091 6092 6093 6094 6095 6096 6097 6098


/*
 * pg_start_backup: set up for taking an on-line backup dump
 *
 * Essentially what this does is to create a backup label file in $PGDATA,
 * where it will be archived as part of the backup dump.  The label file
 * contains the user-supplied label string (typically this would be used
 * to tell where the backup dump will be stored) and the starting time and
6099
 * starting WAL location for the dump.
6100 6101 6102 6103 6104 6105 6106
 */
Datum
pg_start_backup(PG_FUNCTION_ARGS)
{
	text	   *backupid = PG_GETARG_TEXT_P(0);
	text	   *result;
	char	   *backupidstr;
6107
	XLogRecPtr	checkpointloc;
6108
	XLogRecPtr	startpoint;
B
Bruce Momjian 已提交
6109
	time_t		stamp_time;
6110 6111 6112 6113 6114 6115 6116
	char		strfbuf[128];
	char		xlogfilename[MAXFNAMELEN];
	uint32		_logId;
	uint32		_logSeg;
	struct stat stat_buf;
	FILE	   *fp;

B
Bruce Momjian 已提交
6117
	if (!superuser())
6118 6119 6120
		ereport(ERROR,
				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
				 (errmsg("must be superuser to run a backup"))));
6121 6122 6123 6124

	if (!XLogArchivingActive())
		ereport(ERROR,
				(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
T
Tom Lane 已提交
6125
				 (errmsg("WAL archiving is not active"),
B
Bruce Momjian 已提交
6126 6127
				  (errhint("archive_command must be defined before "
						   "online backups can be made safely.")))));
6128

6129
	backupidstr = DatumGetCString(DirectFunctionCall1(textout,
B
Bruce Momjian 已提交
6130
												 PointerGetDatum(backupid)));
B
Bruce Momjian 已提交
6131

6132
	/*
6133 6134 6135
	 * Mark backup active in shared memory.  We must do full-page WAL writes
	 * during an on-line backup even if not doing so at other times, because
	 * it's quite possible for the backup dump to obtain a "torn" (partially
B
Bruce Momjian 已提交
6136 6137 6138 6139 6140 6141 6142 6143 6144
	 * written) copy of a database page if it reads the page concurrently with
	 * our write to the same page.	This can be fixed as long as the first
	 * write to the page in the WAL sequence is a full-page write. Hence, we
	 * turn on forcePageWrites and then force a CHECKPOINT, to ensure there
	 * are no dirty pages in shared memory that might get dumped while the
	 * backup is in progress without having a corresponding WAL record.  (Once
	 * the backup is complete, we need not force full-page writes anymore,
	 * since we expect that any pages not modified during the backup interval
	 * must have been correctly captured by the backup.)
6145
	 *
B
Bruce Momjian 已提交
6146 6147
	 * We must hold WALInsertLock to change the value of forcePageWrites, to
	 * ensure adequate interlocking against XLogInsert().
6148
	 */
6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159
	LWLockAcquire(WALInsertLock, LW_EXCLUSIVE);
	if (XLogCtl->Insert.forcePageWrites)
	{
		LWLockRelease(WALInsertLock);
		ereport(ERROR,
				(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
				 errmsg("a backup is already in progress"),
				 errhint("Run pg_stop_backup() and try again.")));
	}
	XLogCtl->Insert.forcePageWrites = true;
	LWLockRelease(WALInsertLock);
B
Bruce Momjian 已提交
6160

6161 6162 6163 6164
	/* Use a TRY block to ensure we release forcePageWrites if fail below */
	PG_TRY();
	{
		/*
B
Bruce Momjian 已提交
6165
		 * Force a CHECKPOINT.	Aside from being necessary to prevent torn
6166 6167 6168 6169 6170
		 * page problems, this guarantees that two successive backup runs will
		 * have different checkpoint positions and hence different history
		 * file names, even if nothing happened in between.
		 */
		RequestCheckpoint(true, false);
6171

6172 6173 6174 6175 6176 6177 6178 6179 6180
		/*
		 * Now we need to fetch the checkpoint record location, and also its
		 * REDO pointer.  The oldest point in WAL that would be needed to
		 * restore starting from the checkpoint is precisely the REDO pointer.
		 */
		LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
		checkpointloc = ControlFile->checkPoint;
		startpoint = ControlFile->checkPointCopy.redo;
		LWLockRelease(ControlFileLock);
B
Bruce Momjian 已提交
6181

6182 6183
		XLByteToSeg(startpoint, _logId, _logSeg);
		XLogFileName(xlogfilename, ThisTimeLineID, _logId, _logSeg);
B
Bruce Momjian 已提交
6184

6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220
		/*
		 * We deliberately use strftime/localtime not the src/timezone
		 * functions, so that backup labels will consistently be recorded in
		 * the same timezone regardless of TimeZone setting.  This matches
		 * elog.c's practice.
		 */
		stamp_time = time(NULL);
		strftime(strfbuf, sizeof(strfbuf),
				 "%Y-%m-%d %H:%M:%S %Z",
				 localtime(&stamp_time));

		/*
		 * Check for existing backup label --- implies a backup is already
		 * running.  (XXX given that we checked forcePageWrites above, maybe
		 * it would be OK to just unlink any such label file?)
		 */
		if (stat(BACKUP_LABEL_FILE, &stat_buf) != 0)
		{
			if (errno != ENOENT)
				ereport(ERROR,
						(errcode_for_file_access(),
						 errmsg("could not stat file \"%s\": %m",
								BACKUP_LABEL_FILE)));
		}
		else
			ereport(ERROR,
					(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
					 errmsg("a backup is already in progress"),
					 errhint("If you're sure there is no backup in progress, remove file \"%s\" and try again.",
							 BACKUP_LABEL_FILE)));

		/*
		 * Okay, write the file
		 */
		fp = AllocateFile(BACKUP_LABEL_FILE, "w");
		if (!fp)
6221 6222
			ereport(ERROR,
					(errcode_for_file_access(),
6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234
					 errmsg("could not create file \"%s\": %m",
							BACKUP_LABEL_FILE)));
		fprintf(fp, "START WAL LOCATION: %X/%X (file %s)\n",
				startpoint.xlogid, startpoint.xrecoff, xlogfilename);
		fprintf(fp, "CHECKPOINT LOCATION: %X/%X\n",
				checkpointloc.xlogid, checkpointloc.xrecoff);
		fprintf(fp, "START TIME: %s\n", strfbuf);
		fprintf(fp, "LABEL: %s\n", backupidstr);
		if (fflush(fp) || ferror(fp) || FreeFile(fp))
			ereport(ERROR,
					(errcode_for_file_access(),
					 errmsg("could not write file \"%s\": %m",
6235
							BACKUP_LABEL_FILE)));
6236
	}
6237 6238 6239 6240 6241 6242
	PG_CATCH();
	{
		/* Turn off forcePageWrites on failure */
		LWLockAcquire(WALInsertLock, LW_EXCLUSIVE);
		XLogCtl->Insert.forcePageWrites = false;
		LWLockRelease(WALInsertLock);
B
Bruce Momjian 已提交
6243

6244 6245 6246
		PG_RE_THROW();
	}
	PG_END_TRY();
B
Bruce Momjian 已提交
6247

6248
	/*
6249
	 * We're done.  As a convenience, return the starting WAL location.
6250 6251 6252 6253
	 */
	snprintf(xlogfilename, sizeof(xlogfilename), "%X/%X",
			 startpoint.xlogid, startpoint.xrecoff);
	result = DatumGetTextP(DirectFunctionCall1(textin,
B
Bruce Momjian 已提交
6254
											 CStringGetDatum(xlogfilename)));
6255 6256 6257 6258 6259 6260 6261 6262 6263
	PG_RETURN_TEXT_P(result);
}

/*
 * pg_stop_backup: finish taking an on-line backup dump
 *
 * We remove the backup label file created by pg_start_backup, and instead
 * create a backup history file in pg_xlog (whence it will immediately be
 * archived).  The backup history file contains the same info found in
6264
 * the label file, plus the backup-end time and WAL location.
6265 6266 6267 6268 6269 6270 6271
 */
Datum
pg_stop_backup(PG_FUNCTION_ARGS)
{
	text	   *result;
	XLogRecPtr	startpoint;
	XLogRecPtr	stoppoint;
B
Bruce Momjian 已提交
6272
	time_t		stamp_time;
6273
	char		strfbuf[128];
6274
	char		histfilepath[MAXPGPATH];
6275 6276 6277 6278 6279 6280 6281 6282 6283
	char		startxlogfilename[MAXFNAMELEN];
	char		stopxlogfilename[MAXFNAMELEN];
	uint32		_logId;
	uint32		_logSeg;
	FILE	   *lfp;
	FILE	   *fp;
	char		ch;
	int			ich;

B
Bruce Momjian 已提交
6284
	if (!superuser())
6285 6286 6287
		ereport(ERROR,
				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
				 (errmsg("must be superuser to run a backup"))));
B
Bruce Momjian 已提交
6288

6289
	/*
6290
	 * OK to clear forcePageWrites
6291 6292
	 */
	LWLockAcquire(WALInsertLock, LW_EXCLUSIVE);
6293
	XLogCtl->Insert.forcePageWrites = false;
6294 6295
	LWLockRelease(WALInsertLock);

6296
	/*
B
Bruce Momjian 已提交
6297 6298 6299
	 * Force a switch to a new xlog segment file, so that the backup is valid
	 * as soon as archiver moves out the current segment file. We'll report
	 * the end address of the XLOG SWITCH record as the backup stopping point.
6300 6301 6302
	 */
	stoppoint = RequestXLogSwitch();

6303 6304
	XLByteToSeg(stoppoint, _logId, _logSeg);
	XLogFileName(stopxlogfilename, ThisTimeLineID, _logId, _logSeg);
B
Bruce Momjian 已提交
6305

6306
	/*
B
Bruce Momjian 已提交
6307 6308 6309 6310
	 * We deliberately use strftime/localtime not the src/timezone functions,
	 * so that backup labels will consistently be recorded in the same
	 * timezone regardless of TimeZone setting.  This matches elog.c's
	 * practice.
6311 6312 6313 6314 6315
	 */
	stamp_time = time(NULL);
	strftime(strfbuf, sizeof(strfbuf),
			 "%Y-%m-%d %H:%M:%S %Z",
			 localtime(&stamp_time));
B
Bruce Momjian 已提交
6316

6317 6318 6319
	/*
	 * Open the existing label file
	 */
6320
	lfp = AllocateFile(BACKUP_LABEL_FILE, "r");
6321 6322 6323 6324 6325 6326
	if (!lfp)
	{
		if (errno != ENOENT)
			ereport(ERROR,
					(errcode_for_file_access(),
					 errmsg("could not read file \"%s\": %m",
6327
							BACKUP_LABEL_FILE)));
6328 6329 6330 6331
		ereport(ERROR,
				(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
				 errmsg("a backup is not in progress")));
	}
B
Bruce Momjian 已提交
6332

6333
	/*
B
Bruce Momjian 已提交
6334 6335
	 * Read and parse the START WAL LOCATION line (this code is pretty crude,
	 * but we are not expecting any variability in the file format).
6336 6337 6338 6339 6340 6341
	 */
	if (fscanf(lfp, "START WAL LOCATION: %X/%X (file %24s)%c",
			   &startpoint.xlogid, &startpoint.xrecoff, startxlogfilename,
			   &ch) != 4 || ch != '\n')
		ereport(ERROR,
				(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
6342
				 errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
B
Bruce Momjian 已提交
6343

6344 6345 6346 6347
	/*
	 * Write the backup history file
	 */
	XLByteToSeg(startpoint, _logId, _logSeg);
6348
	BackupHistoryFilePath(histfilepath, ThisTimeLineID, _logId, _logSeg,
6349
						  startpoint.xrecoff % XLogSegSize);
6350
	fp = AllocateFile(histfilepath, "w");
6351 6352 6353 6354
	if (!fp)
		ereport(ERROR,
				(errcode_for_file_access(),
				 errmsg("could not create file \"%s\": %m",
6355
						histfilepath)));
6356 6357 6358 6359
	fprintf(fp, "START WAL LOCATION: %X/%X (file %s)\n",
			startpoint.xlogid, startpoint.xrecoff, startxlogfilename);
	fprintf(fp, "STOP WAL LOCATION: %X/%X (file %s)\n",
			stoppoint.xlogid, stoppoint.xrecoff, stopxlogfilename);
6360
	/* transfer remaining lines from label to history file */
6361 6362 6363 6364 6365 6366 6367
	while ((ich = fgetc(lfp)) != EOF)
		fputc(ich, fp);
	fprintf(fp, "STOP TIME: %s\n", strfbuf);
	if (fflush(fp) || ferror(fp) || FreeFile(fp))
		ereport(ERROR,
				(errcode_for_file_access(),
				 errmsg("could not write file \"%s\": %m",
6368
						histfilepath)));
B
Bruce Momjian 已提交
6369

6370 6371 6372 6373 6374 6375 6376
	/*
	 * Close and remove the backup label file
	 */
	if (ferror(lfp) || FreeFile(lfp))
		ereport(ERROR,
				(errcode_for_file_access(),
				 errmsg("could not read file \"%s\": %m",
6377 6378
						BACKUP_LABEL_FILE)));
	if (unlink(BACKUP_LABEL_FILE) != 0)
6379 6380 6381
		ereport(ERROR,
				(errcode_for_file_access(),
				 errmsg("could not remove file \"%s\": %m",
6382
						BACKUP_LABEL_FILE)));
B
Bruce Momjian 已提交
6383

6384
	/*
B
Bruce Momjian 已提交
6385 6386 6387
	 * Clean out any no-longer-needed history files.  As a side effect, this
	 * will post a .ready file for the newly created history file, notifying
	 * the archiver that history file may be archived immediately.
6388
	 */
6389
	CleanupBackupHistory();
B
Bruce Momjian 已提交
6390

6391
	/*
6392
	 * We're done.  As a convenience, return the ending WAL location.
6393 6394 6395 6396
	 */
	snprintf(stopxlogfilename, sizeof(stopxlogfilename), "%X/%X",
			 stoppoint.xlogid, stoppoint.xrecoff);
	result = DatumGetTextP(DirectFunctionCall1(textin,
B
Bruce Momjian 已提交
6397
										 CStringGetDatum(stopxlogfilename)));
6398 6399
	PG_RETURN_TEXT_P(result);
}
6400

6401 6402 6403 6404 6405 6406 6407
/*
 * pg_switch_xlog: switch to next xlog file
 */
Datum
pg_switch_xlog(PG_FUNCTION_ARGS)
{
	text	   *result;
B
Bruce Momjian 已提交
6408
	XLogRecPtr	switchpoint;
6409 6410 6411 6412 6413
	char		location[MAXFNAMELEN];

	if (!superuser())
		ereport(ERROR,
				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
P
Peter Eisentraut 已提交
6414
				 (errmsg("must be superuser to switch transaction log files"))));
6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428

	switchpoint = RequestXLogSwitch();

	/*
	 * As a convenience, return the WAL location of the switch record
	 */
	snprintf(location, sizeof(location), "%X/%X",
			 switchpoint.xlogid, switchpoint.xrecoff);
	result = DatumGetTextP(DirectFunctionCall1(textin,
											   CStringGetDatum(location)));
	PG_RETURN_TEXT_P(result);
}

/*
6429 6430 6431 6432 6433
 * Report the current WAL write location (same format as pg_start_backup etc)
 *
 * This is useful for determining how much of WAL is visible to an external
 * archiving process.  Note that the data before this point is written out
 * to the kernel, but is not necessarily synced to disk.
6434 6435 6436
 */
Datum
pg_current_xlog_location(PG_FUNCTION_ARGS)
6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465
{
	text	   *result;
	char		location[MAXFNAMELEN];

	/* Make sure we have an up-to-date local LogwrtResult */
	{
		/* use volatile pointer to prevent code rearrangement */
		volatile XLogCtlData *xlogctl = XLogCtl;

		SpinLockAcquire(&xlogctl->info_lck);
		LogwrtResult = xlogctl->LogwrtResult;
		SpinLockRelease(&xlogctl->info_lck);
	}

	snprintf(location, sizeof(location), "%X/%X",
			 LogwrtResult.Write.xlogid, LogwrtResult.Write.xrecoff);

	result = DatumGetTextP(DirectFunctionCall1(textin,
											   CStringGetDatum(location)));
	PG_RETURN_TEXT_P(result);
}

/*
 * Report the current WAL insert location (same format as pg_start_backup etc)
 *
 * This function is mostly for debugging purposes.
 */
Datum
pg_current_xlog_insert_location(PG_FUNCTION_ARGS)
6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506
{
	text	   *result;
	XLogCtlInsert *Insert = &XLogCtl->Insert;
	XLogRecPtr	current_recptr;
	char		location[MAXFNAMELEN];

	/*
	 * Get the current end-of-WAL position ... shared lock is sufficient
	 */
	LWLockAcquire(WALInsertLock, LW_SHARED);
	INSERT_RECPTR(current_recptr, Insert, Insert->curridx);
	LWLockRelease(WALInsertLock);

	snprintf(location, sizeof(location), "%X/%X",
			 current_recptr.xlogid, current_recptr.xrecoff);

	result = DatumGetTextP(DirectFunctionCall1(textin,
											   CStringGetDatum(location)));
	PG_RETURN_TEXT_P(result);
}

/*
 * Compute an xlog file name and decimal byte offset given a WAL location,
 * such as is returned by pg_stop_backup() or pg_xlog_switch().
 *
 * Note that a location exactly at a segment boundary is taken to be in
 * the previous segment.  This is usually the right thing, since the
 * expected usage is to determine which xlog file(s) are ready to archive.
 */
Datum
pg_xlogfile_name_offset(PG_FUNCTION_ARGS)
{
	text	   *location = PG_GETARG_TEXT_P(0);
	char	   *locationstr;
	unsigned int uxlogid;
	unsigned int uxrecoff;
	uint32		xlogid;
	uint32		xlogseg;
	uint32		xrecoff;
	XLogRecPtr	locationpoint;
	char		xlogfilename[MAXFNAMELEN];
B
Bruce Momjian 已提交
6507 6508 6509 6510 6511
	Datum		values[2];
	bool		isnull[2];
	TupleDesc	resultTupleDesc;
	HeapTuple	resultHeapTuple;
	Datum		result;
6512

6513 6514 6515
	/*
	 * Read input and parse
	 */
6516
	locationstr = DatumGetCString(DirectFunctionCall1(textout,
B
Bruce Momjian 已提交
6517
												 PointerGetDatum(location)));
6518 6519 6520 6521

	if (sscanf(locationstr, "%X/%X", &uxlogid, &uxrecoff) != 2)
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
P
Peter Eisentraut 已提交
6522
				 errmsg("could not parse transaction log location \"%s\"",
6523 6524 6525 6526 6527
						locationstr)));

	locationpoint.xlogid = uxlogid;
	locationpoint.xrecoff = uxrecoff;

6528
	/*
B
Bruce Momjian 已提交
6529 6530
	 * Construct a tuple descriptor for the result row.  This must match this
	 * function's pg_proc entry!
6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542
	 */
	resultTupleDesc = CreateTemplateTupleDesc(2, false);
	TupleDescInitEntry(resultTupleDesc, (AttrNumber) 1, "file_name",
					   TEXTOID, -1, 0);
	TupleDescInitEntry(resultTupleDesc, (AttrNumber) 2, "file_offset",
					   INT4OID, -1, 0);

	resultTupleDesc = BlessTupleDesc(resultTupleDesc);

	/*
	 * xlogfilename
	 */
6543 6544 6545
	XLByteToPrevSeg(locationpoint, xlogid, xlogseg);
	XLogFileName(xlogfilename, ThisTimeLineID, xlogid, xlogseg);

6546 6547 6548 6549 6550 6551 6552
	values[0] = DirectFunctionCall1(textin,
									CStringGetDatum(xlogfilename));
	isnull[0] = false;

	/*
	 * offset
	 */
6553 6554
	xrecoff = locationpoint.xrecoff - xlogseg * XLogSegSize;

6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565
	values[1] = UInt32GetDatum(xrecoff);
	isnull[1] = false;

	/*
	 * Tuple jam: Having first prepared your Datums, then squash together
	 */
	resultHeapTuple = heap_form_tuple(resultTupleDesc, values, isnull);

	result = HeapTupleGetDatum(resultHeapTuple);

	PG_RETURN_DATUM(result);
6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585
}

/*
 * Compute an xlog file name given a WAL location,
 * such as is returned by pg_stop_backup() or pg_xlog_switch().
 */
Datum
pg_xlogfile_name(PG_FUNCTION_ARGS)
{
	text	   *location = PG_GETARG_TEXT_P(0);
	text	   *result;
	char	   *locationstr;
	unsigned int uxlogid;
	unsigned int uxrecoff;
	uint32		xlogid;
	uint32		xlogseg;
	XLogRecPtr	locationpoint;
	char		xlogfilename[MAXFNAMELEN];

	locationstr = DatumGetCString(DirectFunctionCall1(textout,
B
Bruce Momjian 已提交
6586
												 PointerGetDatum(location)));
6587 6588 6589 6590

	if (sscanf(locationstr, "%X/%X", &uxlogid, &uxrecoff) != 2)
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
P
Peter Eisentraut 已提交
6591
				 errmsg("could not parse transaction log location \"%s\"",
6592 6593 6594 6595 6596 6597 6598 6599 6600
						locationstr)));

	locationpoint.xlogid = uxlogid;
	locationpoint.xrecoff = uxrecoff;

	XLByteToPrevSeg(locationpoint, xlogid, xlogseg);
	XLogFileName(xlogfilename, ThisTimeLineID, xlogid, xlogseg);

	result = DatumGetTextP(DirectFunctionCall1(textin,
B
Bruce Momjian 已提交
6601
											 CStringGetDatum(xlogfilename)));
6602 6603 6604
	PG_RETURN_TEXT_P(result);
}

6605 6606 6607 6608 6609
/*
 * read_backup_label: check to see if a backup_label file is present
 *
 * If we see a backup_label during recovery, we assume that we are recovering
 * from a backup dump file, and we therefore roll forward from the checkpoint
B
Bruce Momjian 已提交
6610
 * identified by the label file, NOT what pg_control says.	This avoids the
6611 6612 6613 6614 6615
 * problem that pg_control might have been archived one or more checkpoints
 * later than the start of the dump, and so if we rely on it as the start
 * point, we will fail to restore a consistent database state.
 *
 * We also attempt to retrieve the corresponding backup history file.
6616
 * If successful, set *minRecoveryLoc to constrain valid PITR stopping
6617 6618 6619 6620 6621 6622
 * points.
 *
 * Returns TRUE if a backup_label was found (and fills the checkpoint
 * location into *checkPointLoc); returns FALSE if not.
 */
static bool
6623
read_backup_label(XLogRecPtr *checkPointLoc, XLogRecPtr *minRecoveryLoc)
6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637
{
	XLogRecPtr	startpoint;
	XLogRecPtr	stoppoint;
	char		histfilename[MAXFNAMELEN];
	char		histfilepath[MAXPGPATH];
	char		startxlogfilename[MAXFNAMELEN];
	char		stopxlogfilename[MAXFNAMELEN];
	TimeLineID	tli;
	uint32		_logId;
	uint32		_logSeg;
	FILE	   *lfp;
	FILE	   *fp;
	char		ch;

6638 6639 6640 6641
	/* Default is to not constrain recovery stop point */
	minRecoveryLoc->xlogid = 0;
	minRecoveryLoc->xrecoff = 0;

6642 6643 6644
	/*
	 * See if label file is present
	 */
6645
	lfp = AllocateFile(BACKUP_LABEL_FILE, "r");
6646 6647 6648 6649 6650 6651
	if (!lfp)
	{
		if (errno != ENOENT)
			ereport(FATAL,
					(errcode_for_file_access(),
					 errmsg("could not read file \"%s\": %m",
6652
							BACKUP_LABEL_FILE)));
6653 6654
		return false;			/* it's not there, all is fine */
	}
B
Bruce Momjian 已提交
6655

6656
	/*
B
Bruce Momjian 已提交
6657 6658 6659
	 * Read and parse the START WAL LOCATION and CHECKPOINT lines (this code
	 * is pretty crude, but we are not expecting any variability in the file
	 * format).
6660 6661 6662 6663 6664 6665
	 */
	if (fscanf(lfp, "START WAL LOCATION: %X/%X (file %08X%16s)%c",
			   &startpoint.xlogid, &startpoint.xrecoff, &tli,
			   startxlogfilename, &ch) != 5 || ch != '\n')
		ereport(FATAL,
				(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
6666
				 errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
6667 6668 6669 6670 6671
	if (fscanf(lfp, "CHECKPOINT LOCATION: %X/%X%c",
			   &checkPointLoc->xlogid, &checkPointLoc->xrecoff,
			   &ch) != 3 || ch != '\n')
		ereport(FATAL,
				(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
6672
				 errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
6673 6674 6675 6676
	if (ferror(lfp) || FreeFile(lfp))
		ereport(FATAL,
				(errcode_for_file_access(),
				 errmsg("could not read file \"%s\": %m",
6677
						BACKUP_LABEL_FILE)));
B
Bruce Momjian 已提交
6678

6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691
	/*
	 * Try to retrieve the backup history file (no error if we can't)
	 */
	XLByteToSeg(startpoint, _logId, _logSeg);
	BackupHistoryFileName(histfilename, tli, _logId, _logSeg,
						  startpoint.xrecoff % XLogSegSize);

	if (InArchiveRecovery)
		RestoreArchivedFile(histfilepath, histfilename, "RECOVERYHISTORY", 0);
	else
		BackupHistoryFilePath(histfilepath, tli, _logId, _logSeg,
							  startpoint.xrecoff % XLogSegSize);

B
Bruce Momjian 已提交
6692
	fp = AllocateFile(histfilepath, "r");
6693 6694 6695 6696 6697 6698
	if (fp)
	{
		/*
		 * Parse history file to identify stop point.
		 */
		if (fscanf(fp, "START WAL LOCATION: %X/%X (file %24s)%c",
B
Bruce Momjian 已提交
6699
				   &startpoint.xlogid, &startpoint.xrecoff, startxlogfilename,
6700 6701 6702
				   &ch) != 4 || ch != '\n')
			ereport(FATAL,
					(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
B
Bruce Momjian 已提交
6703
					 errmsg("invalid data in file \"%s\"", histfilename)));
6704
		if (fscanf(fp, "STOP WAL LOCATION: %X/%X (file %24s)%c",
B
Bruce Momjian 已提交
6705
				   &stoppoint.xlogid, &stoppoint.xrecoff, stopxlogfilename,
6706 6707 6708
				   &ch) != 4 || ch != '\n')
			ereport(FATAL,
					(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
B
Bruce Momjian 已提交
6709
					 errmsg("invalid data in file \"%s\"", histfilename)));
6710
		*minRecoveryLoc = stoppoint;
6711 6712 6713 6714 6715 6716 6717 6718 6719 6720
		if (ferror(fp) || FreeFile(fp))
			ereport(FATAL,
					(errcode_for_file_access(),
					 errmsg("could not read file \"%s\": %m",
							histfilepath)));
	}

	return true;
}

6721 6722 6723 6724 6725 6726
/*
 * Error context callback for errors occurring during rm_redo().
 */
static void
rm_redo_error_callback(void *arg)
{
B
Bruce Momjian 已提交
6727 6728
	XLogRecord *record = (XLogRecord *) arg;
	StringInfoData buf;
6729 6730

	initStringInfo(&buf);
6731 6732
	RmgrTable[record->xl_rmid].rm_desc(&buf,
									   record->xl_info,
6733 6734 6735 6736 6737 6738 6739 6740
									   XLogRecGetData(record));

	/* don't bother emitting empty description */
	if (buf.len > 0)
		errcontext("xlog redo %s", buf.data);

	pfree(buf.data);
}