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

15 16
#include "postgres.h"

17
#include <fcntl.h>
T
Tom Lane 已提交
18
#include <signal.h>
19 20 21
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
V
Vadim B. Mikheev 已提交
22
#include <sys/time.h>
V
Vadim B. Mikheev 已提交
23 24
#include <sys/types.h>
#include <dirent.h>
25
#include <locale.h>
26

27
#include "access/clog.h"
28
#include "access/transam.h"
29
#include "access/xact.h"
30 31
#include "access/xlog.h"
#include "access/xlogutils.h"
32
#include "catalog/catversion.h"
T
Tom Lane 已提交
33
#include "catalog/pg_control.h"
34 35
#include "storage/bufpage.h"
#include "storage/lwlock.h"
36
#include "storage/pmsignal.h"
37
#include "storage/proc.h"
38
#include "storage/sinval.h"
39
#include "storage/spin.h"
40
#include "utils/builtins.h"
41
#include "utils/relcache.h"
V
WAL  
Vadim B. Mikheev 已提交
42 43
#include "miscadmin.h"

44

45 46 47
/*
 * 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 已提交
48
 * default method.	We assume that fsync() is always available, and that
49 50 51 52
 * configure determined whether fdatasync() is.
 */
#define SYNC_METHOD_FSYNC		0
#define SYNC_METHOD_FDATASYNC	1
B
Bruce Momjian 已提交
53 54
#define SYNC_METHOD_OPEN		2		/* used for both O_SYNC and
										 * O_DSYNC */
55 56

#if defined(O_SYNC)
B
Bruce Momjian 已提交
57
#define OPEN_SYNC_FLAG	   O_SYNC
58
#else
B
Bruce Momjian 已提交
59 60 61
#if defined(O_FSYNC)
#define OPEN_SYNC_FLAG	  O_FSYNC
#endif
62 63 64
#endif

#if defined(OPEN_SYNC_FLAG)
B
Bruce Momjian 已提交
65 66 67
#if defined(O_DSYNC) && (O_DSYNC != OPEN_SYNC_FLAG)
#define OPEN_DATASYNC_FLAG	  O_DSYNC
#endif
68 69 70
#endif

#if defined(OPEN_DATASYNC_FLAG)
B
Bruce Momjian 已提交
71 72 73
#define DEFAULT_SYNC_METHOD_STR    "open_datasync"
#define DEFAULT_SYNC_METHOD		   SYNC_METHOD_OPEN
#define DEFAULT_SYNC_FLAGBIT	   OPEN_DATASYNC_FLAG
74
#else
B
Bruce Momjian 已提交
75 76 77 78 79 80 81 82 83
#if defined(HAVE_FDATASYNC)
#define DEFAULT_SYNC_METHOD_STR   "fdatasync"
#define DEFAULT_SYNC_METHOD		  SYNC_METHOD_FDATASYNC
#define DEFAULT_SYNC_FLAGBIT	  0
#else
#define DEFAULT_SYNC_METHOD_STR   "fsync"
#define DEFAULT_SYNC_METHOD		  SYNC_METHOD_FSYNC
#define DEFAULT_SYNC_FLAGBIT	  0
#endif
84 85 86
#endif


T
Tom Lane 已提交
87 88
/* User-settable parameters */
int			CheckPointSegments = 3;
V
Vadim B. Mikheev 已提交
89
int			XLOGbuffers = 8;
90
int			XLOGfiles = 0;		/* # of files to preallocate during ckpt */
T
Tom Lane 已提交
91
int			XLOG_DEBUG = 0;
92 93
char	   *XLOG_sync_method = NULL;
const char	XLOG_sync_method_default[] = DEFAULT_SYNC_METHOD_STR;
B
Bruce Momjian 已提交
94 95
char		XLOG_archive_dir[MAXPGPATH];		/* null string means
												 * delete 'em */
T
Tom Lane 已提交
96

97
/*
98
 * XLOGfileslop is used in the code as the allowed "fuzz" in the number of
99 100 101 102 103 104 105 106 107 108 109 110
 * preallocated XLOG segments --- we try to have at least XLOGfiles advance
 * segments but no more than XLOGfiles+XLOGfileslop segments.  This could
 * 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)


111 112 113 114 115 116
/* these are derived from XLOG_sync_method by assign_xlog_sync_method */
static int	sync_method = DEFAULT_SYNC_METHOD;
static int	open_sync_bit = DEFAULT_SYNC_FLAGBIT;

#define XLOG_SYNC_BIT  (enableFsync ? open_sync_bit : 0)

117 118
#define MinXLOGbuffers	4

T
Tom Lane 已提交
119 120 121 122 123

/*
 * ThisStartUpID will be same in all backends --- it identifies current
 * instance of the database system.
 */
V
WAL  
Vadim B. Mikheev 已提交
124 125
StartUpID	ThisStartUpID = 0;

T
Tom Lane 已提交
126 127
/* Are we doing recovery by reading XLOG? */
bool		InRecovery = false;
128

T
Tom Lane 已提交
129 130
/*
 * MyLastRecPtr points to the start of the last XLOG record inserted by the
131 132
 * current transaction.  If MyLastRecPtr.xrecoff == 0, then the current
 * xact hasn't yet inserted any transaction-controlled XLOG records.
T
Tom Lane 已提交
133 134
 *
 * Note that XLOG records inserted outside transaction control are not
135 136 137 138
 * reflected into MyLastRecPtr.  They do, however, cause MyXactMadeXLogEntry
 * to be set true.  The latter can be used to test whether the current xact
 * made any loggable changes (including out-of-xact changes, such as
 * sequence updates).
T
Tom Lane 已提交
139 140
 */
XLogRecPtr	MyLastRecPtr = {0, 0};
V
Vadim B. Mikheev 已提交
141

142 143
bool		MyXactMadeXLogEntry = false;

T
Tom Lane 已提交
144 145 146
/*
 * ProcLastRecPtr points to the start of the last XLOG record inserted by the
 * current backend.  It is updated for all inserts, transaction-controlled
147
 * or not.  ProcLastRecEnd is similar but points to end+1 of last record.
T
Tom Lane 已提交
148 149
 */
static XLogRecPtr ProcLastRecPtr = {0, 0};
150

151 152
XLogRecPtr	ProcLastRecEnd = {0, 0};

T
Tom Lane 已提交
153 154 155
/*
 * 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 已提交
156
 * CHECKPOINT record).	We update this from the shared-memory copy,
T
Tom Lane 已提交
157
 * XLogCtl->Insert.RedoRecPtr, whenever we can safely do so (ie, when we
158 159 160
 * hold the Insert lock).  See XLogInsert for details.  We are also allowed
 * to update from XLogCtl->Insert.RedoRecPtr if we hold the info_lck;
 * see GetRedoRecPtr.
T
Tom Lane 已提交
161 162
 */
static XLogRecPtr RedoRecPtr;
163

T
Tom Lane 已提交
164 165 166 167 168 169 170 171 172
/*----------
 * 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.
 *
173
 * We do a lot of pushups to minimize the amount of access to lockable
T
Tom Lane 已提交
174 175 176
 * 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
177 178 179 180
 *		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 已提交
181 182 183
 *
 * XLogCtl->LogwrtResult and XLogCtl->Write.LogwrtResult are both "always
 * right", since both are updated by a write or flush operation before
184 185
 * 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 已提交
186 187 188
 * without needing to grab info_lck as well.
 *
 * XLogCtl->Insert.LogwrtResult may lag behind the reality of the other two,
B
Bruce Momjian 已提交
189
 * but is updated when convenient.	Again, it exists for the convenience of
190
 * code that is already holding WALInsertLock but not the other locks.
T
Tom Lane 已提交
191 192 193 194 195 196 197 198 199 200
 *
 * 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".
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
 *
 * 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
 * checkpointer at a time; even though the postmaster won't launch
 * parallel checkpoint processes, we need this because manual checkpoints
 * could be launched simultaneously).
 *
T
Tom Lane 已提交
219 220 221
 *----------
 */
typedef struct XLogwrtRqst
222
{
T
Tom Lane 已提交
223 224
	XLogRecPtr	Write;			/* last byte + 1 to write out */
	XLogRecPtr	Flush;			/* last byte + 1 to flush */
225
} XLogwrtRqst;
226

T
Tom Lane 已提交
227
typedef struct XLogwrtResult
228
{
T
Tom Lane 已提交
229 230
	XLogRecPtr	Write;			/* last byte + 1 written out */
	XLogRecPtr	Flush;			/* last byte + 1 flushed */
231
} XLogwrtResult;
232

T
Tom Lane 已提交
233 234 235
/*
 * Shared state data for XLogInsert.
 */
236 237
typedef struct XLogCtlInsert
{
B
Bruce Momjian 已提交
238 239 240 241 242 243
	XLogwrtResult LogwrtResult; /* a recent value of LogwrtResult */
	XLogRecPtr	PrevRecord;		/* start of previously-inserted record */
	uint16		curridx;		/* current block index in cache */
	XLogPageHeader currpage;	/* points to header of block in cache */
	char	   *currpos;		/* current insertion point in cache */
	XLogRecPtr	RedoRecPtr;		/* current redo point for insertions */
244 245
} XLogCtlInsert;

T
Tom Lane 已提交
246 247 248
/*
 * Shared state data for XLogWrite/XLogFlush.
 */
249 250
typedef struct XLogCtlWrite
{
B
Bruce Momjian 已提交
251 252
	XLogwrtResult LogwrtResult; /* current value of LogwrtResult */
	uint16		curridx;		/* cache index of next block to write */
253 254
} XLogCtlWrite;

T
Tom Lane 已提交
255 256 257
/*
 * Total shared-memory state for XLOG.
 */
258 259
typedef struct XLogCtlData
{
260
	/* Protected by WALInsertLock: */
B
Bruce Momjian 已提交
261
	XLogCtlInsert Insert;
T
Tom Lane 已提交
262
	/* Protected by info_lck: */
B
Bruce Momjian 已提交
263 264
	XLogwrtRqst LogwrtRqst;
	XLogwrtResult LogwrtResult;
265
	/* Protected by WALWriteLock: */
B
Bruce Momjian 已提交
266 267
	XLogCtlWrite Write;

T
Tom Lane 已提交
268 269
	/*
	 * These values do not change after startup, although the pointed-to
270 271 272
	 * pages and xlblocks values certainly do.	Permission to read/write
	 * the pages and xlblocks values depends on WALInsertLock and
	 * WALWriteLock.
T
Tom Lane 已提交
273
	 */
B
Bruce Momjian 已提交
274 275 276 277 278
	char	   *pages;			/* buffers for unwritten XLOG pages */
	XLogRecPtr *xlblocks;		/* 1st byte ptr-s + BLCKSZ */
	uint32		XLogCacheByte;	/* # bytes in xlog buffers */
	uint32		XLogCacheBlck;	/* highest allocated xlog buffer index */
	StartUpID	ThisStartUpID;
T
Tom Lane 已提交
279

280
	/* This value is not protected by *any* lock... */
281 282
	/* see SetSavedRedoRecPtr/GetSavedRedoRecPtr */
	XLogRecPtr	SavedRedoRecPtr;
T
Tom Lane 已提交
283

B
Bruce Momjian 已提交
284
	slock_t		info_lck;		/* locks shared LogwrtRqst/LogwrtResult */
285 286
} XLogCtlData;

287
static XLogCtlData *XLogCtl = NULL;
288

289
/*
T
Tom Lane 已提交
290
 * We maintain an image of pg_control in shared memory.
291
 */
292
static ControlFileData *ControlFile = NULL;
293

T
Tom Lane 已提交
294 295 296 297 298
/*
 * 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.
 */
299

T
Tom Lane 已提交
300 301 302 303 304 305 306 307 308
/* Free space remaining in the current xlog page buffer */
#define INSERT_FREESPACE(Insert)  \
	(BLCKSZ - ((Insert)->currpos - (char *) (Insert)->currpage))

/* Construct XLogRecPtr value for current insertion point */
#define INSERT_RECPTR(recptr,Insert,curridx)  \
	( \
	  (recptr).xlogid = XLogCtl->xlblocks[curridx].xlogid, \
	  (recptr).xrecoff = \
B
Bruce Momjian 已提交
309
		XLogCtl->xlblocks[curridx].xrecoff - INSERT_FREESPACE(Insert) \
T
Tom Lane 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	)


/* Increment an xlogid/segment pair */
#define NextLogSeg(logId, logSeg)	\
	do { \
		if ((logSeg) >= XLogSegsPerFile-1) \
		{ \
			(logId)++; \
			(logSeg) = 0; \
		} \
		else \
			(logSeg)++; \
	} while (0)

/* Decrement an xlogid/segment pair (assume it's not 0,0) */
#define PrevLogSeg(logId, logSeg)	\
	do { \
		if (logSeg) \
			(logSeg)--; \
		else \
		{ \
			(logId)--; \
			(logSeg) = XLogSegsPerFile-1; \
		} \
	} while (0)
V
WAL  
Vadim B. Mikheev 已提交
336

T
Tom Lane 已提交
337 338 339 340
/*
 * Compute ID and segment from an XLogRecPtr.
 *
 * For XLByteToSeg, do the computation at face value.  For XLByteToPrevSeg,
B
Bruce Momjian 已提交
341
 * a boundary byte is taken to be in the previous segment.	This is suitable
T
Tom Lane 已提交
342 343 344 345 346 347 348 349 350 351 352
 * for deciding which segment to write given a pointer to a record end,
 * for example.
 */
#define XLByteToSeg(xlrp, logId, logSeg)	\
	( logId = (xlrp).xlogid, \
	  logSeg = (xlrp).xrecoff / XLogSegSize \
	)
#define XLByteToPrevSeg(xlrp, logId, logSeg)	\
	( logId = (xlrp).xlogid, \
	  logSeg = ((xlrp).xrecoff - 1) / XLogSegSize \
	)
353

354
/*
T
Tom Lane 已提交
355 356 357 358
 * Is an XLogRecPtr within a particular XLOG segment?
 *
 * For XLByteInSeg, do the computation at face value.  For XLByteInPrevSeg,
 * a boundary byte is taken to be in the previous segment.
359
 */
T
Tom Lane 已提交
360 361 362 363 364 365 366
#define XLByteInSeg(xlrp, logId, logSeg)	\
	((xlrp).xlogid == (logId) && \
	 (xlrp).xrecoff / XLogSegSize == (logSeg))

#define XLByteInPrevSeg(xlrp, logId, logSeg)	\
	((xlrp).xlogid == (logId) && \
	 ((xlrp).xrecoff - 1) / XLogSegSize == (logSeg))
367 368


369
#define XLogFileName(path, log, seg)	\
370 371
			snprintf(path, MAXPGPATH, "%s/%08X%08X",	\
					 XLogDir, log, seg)
372

T
Tom Lane 已提交
373 374 375 376 377
#define PrevBufIdx(idx)		\
		(((idx) == 0) ? XLogCtl->XLogCacheBlck : ((idx) - 1))

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

379
#define XRecOffIsValid(xrecoff) \
T
Tom Lane 已提交
380 381
		((xrecoff) % BLCKSZ >= SizeOfXLogPHD && \
		(BLCKSZ - (xrecoff) % BLCKSZ) >= SizeOfXLogRecord)
382

T
Tom Lane 已提交
383 384 385 386 387 388
/*
 * _INTL_MAXLOGRECSZ: max space needed for a record including header and
 * any backup-block data.
 */
#define _INTL_MAXLOGRECSZ	(SizeOfXLogRecord + MAXLOGRECSZ + \
							 XLR_MAX_BKP_BLOCKS * (sizeof(BkpBlock) + BLCKSZ))
389

390

T
Tom Lane 已提交
391
/* File path names */
392 393
char *XLogDir = NULL;

B
Bruce Momjian 已提交
394
static char ControlFilePath[MAXPGPATH];
T
Tom Lane 已提交
395 396 397 398 399 400

/*
 * Private, possibly out-of-date copy of shared LogwrtResult.
 * See discussion above.
 */
static XLogwrtResult LogwrtResult = {{0, 0}, {0, 0}};
401

T
Tom Lane 已提交
402 403 404 405 406 407 408 409 410 411
/*
 * 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;
412

T
Tom Lane 已提交
413 414 415 416 417 418
/*
 * 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.
 */
419 420 421 422
static int	readFile = -1;
static uint32 readId = 0;
static uint32 readSeg = 0;
static uint32 readOff = 0;
B
Bruce Momjian 已提交
423

T
Tom Lane 已提交
424 425
/* Buffer for currently read page (BLCKSZ bytes) */
static char *readBuf = NULL;
B
Bruce Momjian 已提交
426

T
Tom Lane 已提交
427 428 429
/* State information for XLOG reading */
static XLogRecPtr ReadRecPtr;
static XLogRecPtr EndRecPtr;
430
static XLogRecord *nextRecord = NULL;
431
static StartUpID lastReadSUI;
432

V
WAL  
Vadim B. Mikheev 已提交
433 434
static bool InRedo = false;

T
Tom Lane 已提交
435 436 437

static bool AdvanceXLInsertBuffer(void);
static void XLogWrite(XLogwrtRqst WriteRqst);
B
Bruce Momjian 已提交
438 439
static int XLogFileInit(uint32 log, uint32 seg,
			 bool *use_existent, bool use_lock);
440
static bool InstallXLogFileSegment(uint32 log, uint32 seg, char *tmppath,
441 442
					   bool find_free, int max_advance,
					   bool use_lock);
T
Tom Lane 已提交
443 444
static int	XLogFileOpen(uint32 log, uint32 seg, bool econt);
static void PreallocXlogFiles(XLogRecPtr endptr);
445
static void MoveOfflineLogs(uint32 log, uint32 seg, XLogRecPtr endptr);
T
Tom Lane 已提交
446
static XLogRecord *ReadRecord(XLogRecPtr *RecPtr, int emode, char *buffer);
447
static bool ValidXLOGHeader(XLogPageHeader hdr, int emode, bool checkSUI);
T
Tom Lane 已提交
448
static XLogRecord *ReadCheckpointRecord(XLogRecPtr RecPtr,
449
					 int whichChkpt,
B
Bruce Momjian 已提交
450
					 char *buffer);
T
Tom Lane 已提交
451 452 453 454
static void WriteControlFile(void);
static void ReadControlFile(void);
static char *str_time(time_t tnow);
static void xlog_outrec(char *buf, XLogRecord *record);
455
static void issue_xlog_fsync(void);
T
Tom Lane 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472


/*
 * Insert an XLOG record having the specified RMID and info bytes,
 * with the body of the record being the data chunk(s) described by
 * the rdata list (see xlog.h for notes about rdata).
 *
 * 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.
 */
473
XLogRecPtr
474
XLogInsert(RmgrId rmid, uint8 info, XLogRecData *rdata)
475
{
B
Bruce Momjian 已提交
476 477
	XLogCtlInsert *Insert = &XLogCtl->Insert;
	XLogRecord *record;
T
Tom Lane 已提交
478
	XLogContRecord *contrecord;
B
Bruce Momjian 已提交
479 480 481 482 483 484 485 486 487 488 489 490 491 492
	XLogRecPtr	RecPtr;
	XLogRecPtr	WriteRqst;
	uint32		freespace;
	uint16		curridx;
	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];
	XLogRecData dtbuf_rdt[2 * XLR_MAX_BKP_BLOCKS];
	crc64		rdata_crc;
	uint32		len,
				write_len;
	unsigned	i;
493
	XLogwrtRqst LogwrtRqst;
B
Bruce Momjian 已提交
494 495
	bool		updrqst;
	bool		no_tran = (rmid == RM_XLOG_ID) ? true : false;
V
Vadim B. Mikheev 已提交
496 497 498 499

	if (info & XLR_INFO_MASK)
	{
		if ((info & XLR_INFO_MASK) != XLOG_NO_TRAN)
500
			elog(PANIC, "XLogInsert: invalid info mask %02X",
T
Tom Lane 已提交
501
				 (info & XLR_INFO_MASK));
V
Vadim B. Mikheev 已提交
502 503 504 505
		no_tran = true;
		info &= ~XLR_INFO_MASK;
	}

T
Tom Lane 已提交
506
	/*
B
Bruce Momjian 已提交
507 508
	 * In bootstrap mode, we don't actually log anything but XLOG
	 * resources; return a phony record pointer.
T
Tom Lane 已提交
509
	 */
V
Vadim B. Mikheev 已提交
510
	if (IsBootstrapProcessingMode() && rmid != RM_XLOG_ID)
V
WAL  
Vadim B. Mikheev 已提交
511 512
	{
		RecPtr.xlogid = 0;
B
Bruce Momjian 已提交
513
		RecPtr.xrecoff = SizeOfXLogPHD; /* start of 1st checkpoint record */
V
WAL  
Vadim B. Mikheev 已提交
514 515 516
		return (RecPtr);
	}

T
Tom Lane 已提交
517 518 519 520 521 522
	/*
	 * Here we scan the rdata list, determine which buffers must be backed
	 * up, and compute the CRC values for the data.  Note that the record
	 * header isn't added into the CRC yet since we don't know the final
	 * length or info bits quite yet.
	 *
B
Bruce Momjian 已提交
523 524
	 * 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
525
	 * holding the insert lock, but it seems better to avoid doing CRC
B
Bruce Momjian 已提交
526 527 528 529 530 531 532 533
	 * calculations while holding the lock.  This means we have to be
	 * careful about modifying the rdata list until we know we aren't
	 * going to loop back again.  The only change we allow ourselves to
	 * make earlier is to set rdt->data = NULL in list 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
	 * list later.
T
Tom Lane 已提交
534
	 */
535
begin:;
T
Tom Lane 已提交
536 537 538 539 540 541
	for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
	{
		dtbuf[i] = InvalidBuffer;
		dtbuf_bkp[i] = false;
	}

542
	INIT_CRC64(rdata_crc);
T
Tom Lane 已提交
543
	len = 0;
B
Bruce Momjian 已提交
544
	for (rdt = rdata;;)
545 546 547
	{
		if (rdt->buffer == InvalidBuffer)
		{
T
Tom Lane 已提交
548
			/* Simple data, just include it */
549 550 551
			len += rdt->len;
			COMP_CRC64(rdata_crc, rdt->data, rdt->len);
		}
T
Tom Lane 已提交
552
		else
553
		{
T
Tom Lane 已提交
554 555
			/* Find info for buffer */
			for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
556
			{
T
Tom Lane 已提交
557
				if (rdt->buffer == dtbuf[i])
558
				{
T
Tom Lane 已提交
559 560 561 562 563 564 565 566 567
					/* Buffer already referenced by earlier list item */
					if (dtbuf_bkp[i])
						rdt->data = NULL;
					else if (rdt->data)
					{
						len += rdt->len;
						COMP_CRC64(rdata_crc, rdt->data, rdt->len);
					}
					break;
568
				}
T
Tom Lane 已提交
569
				if (dtbuf[i] == InvalidBuffer)
570
				{
T
Tom Lane 已提交
571 572
					/* OK, put it in this slot */
					dtbuf[i] = rdt->buffer;
B
Bruce Momjian 已提交
573

T
Tom Lane 已提交
574 575 576
					/*
					 * XXX We assume page LSN is first data on page
					 */
B
Bruce Momjian 已提交
577
					dtbuf_lsn[i] = *((XLogRecPtr *) BufferGetBlock(rdt->buffer));
T
Tom Lane 已提交
578 579
					if (XLByteLE(dtbuf_lsn[i], RedoRecPtr))
					{
B
Bruce Momjian 已提交
580
						crc64		dtcrc;
T
Tom Lane 已提交
581 582 583 584 585 586 587 588 589 590

						dtbuf_bkp[i] = true;
						rdt->data = NULL;
						INIT_CRC64(dtcrc);
						COMP_CRC64(dtcrc,
								   BufferGetBlock(dtbuf[i]),
								   BLCKSZ);
						dtbuf_xlg[i].node = BufferGetFileNode(dtbuf[i]);
						dtbuf_xlg[i].block = BufferGetBlockNumber(dtbuf[i]);
						COMP_CRC64(dtcrc,
B
Bruce Momjian 已提交
591
								(char *) &(dtbuf_xlg[i]) + sizeof(crc64),
T
Tom Lane 已提交
592 593 594 595 596 597 598 599 600 601
								   sizeof(BkpBlock) - sizeof(crc64));
						FIN_CRC64(dtcrc);
						dtbuf_xlg[i].crc = dtcrc;
					}
					else if (rdt->data)
					{
						len += rdt->len;
						COMP_CRC64(rdata_crc, rdt->data, rdt->len);
					}
					break;
602 603
				}
			}
T
Tom Lane 已提交
604
			if (i >= XLR_MAX_BKP_BLOCKS)
605
				elog(PANIC, "XLogInsert: can backup %d blocks at most",
T
Tom Lane 已提交
606
					 XLR_MAX_BKP_BLOCKS);
607
		}
T
Tom Lane 已提交
608
		/* Break out of loop when rdt points to last list item */
609 610 611 612 613
		if (rdt->next == NULL)
			break;
		rdt = rdt->next;
	}

T
Tom Lane 已提交
614 615 616
	/*
	 * NOTE: the test for len == 0 here is somewhat fishy, since in theory
	 * all of the rmgr data might have been suppressed in favor of backup
B
Bruce Momjian 已提交
617
	 * blocks.	Currently, all callers of XLogInsert provide at least some
T
Tom Lane 已提交
618 619 620 621
	 * not-in-a-buffer data and so len == 0 should never happen, but that
	 * may not be true forever.  If you need to remove the len == 0 check,
	 * also remove the check for xl_len == 0 in ReadRecord, below.
	 */
622
	if (len == 0 || len > MAXLOGRECSZ)
623
		elog(PANIC, "XLogInsert: invalid record length %u", len);
624

625
	START_CRIT_SECTION();
626

627
	/* update LogwrtResult before doing cache fill check */
628 629 630 631 632 633 634 635 636
	{
		/* use volatile pointer to prevent code rearrangement */
		volatile XLogCtlData *xlogctl = XLogCtl;

		SpinLockAcquire_NoHoldoff(&xlogctl->info_lck);
		LogwrtRqst = xlogctl->LogwrtRqst;
		LogwrtResult = xlogctl->LogwrtResult;
		SpinLockRelease_NoHoldoff(&xlogctl->info_lck);
	}
637

638
	/*
639 640
	 * If cache is half filled then try to acquire write lock and do
	 * XLogWrite. Ignore any fractional blocks in performing this check.
641 642 643 644 645
	 */
	LogwrtRqst.Write.xrecoff -= LogwrtRqst.Write.xrecoff % BLCKSZ;
	if (LogwrtRqst.Write.xlogid != LogwrtResult.Write.xlogid ||
		(LogwrtRqst.Write.xrecoff >= LogwrtResult.Write.xrecoff +
		 XLogCtl->XLogCacheByte / 2))
T
Tom Lane 已提交
646
	{
647
		if (LWLockConditionalAcquire(WALWriteLock, LW_EXCLUSIVE))
648
		{
649 650 651 652
			LogwrtResult = XLogCtl->Write.LogwrtResult;
			if (XLByteLT(LogwrtResult.Write, LogwrtRqst.Write))
				XLogWrite(LogwrtRqst);
			LWLockRelease(WALWriteLock);
653 654 655
		}
	}

656 657 658
	/* Now wait to get insert lock */
	LWLockAcquire(WALInsertLock, LW_EXCLUSIVE);

T
Tom Lane 已提交
659 660
	/*
	 * Check to see if my RedoRecPtr is out of date.  If so, may have to
B
Bruce Momjian 已提交
661 662 663
	 * 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.
T
Tom Lane 已提交
664 665
	 */
	if (!XLByteEQ(RedoRecPtr, Insert->RedoRecPtr))
666
	{
T
Tom Lane 已提交
667 668 669 670
		Assert(XLByteLT(RedoRecPtr, Insert->RedoRecPtr));
		RedoRecPtr = Insert->RedoRecPtr;

		for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
671
		{
T
Tom Lane 已提交
672 673 674 675 676 677
			if (dtbuf[i] == InvalidBuffer)
				continue;
			if (dtbuf_bkp[i] == false &&
				XLByteLE(dtbuf_lsn[i], RedoRecPtr))
			{
				/*
B
Bruce Momjian 已提交
678 679
				 * Oops, this buffer now needs to be backed up, but we
				 * didn't think so above.  Start over.
T
Tom Lane 已提交
680
				 */
681
				LWLockRelease(WALInsertLock);
T
Tom Lane 已提交
682 683 684
				END_CRIT_SECTION();
				goto begin;
			}
685 686 687
		}
	}

T
Tom Lane 已提交
688 689 690 691 692 693 694
	/*
	 * Make additional rdata list 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 list.  At the exit of
	 * this loop, write_len includes the backup block data.
	 *
	 * Also set the appropriate info bits to show which buffers were backed
B
Bruce Momjian 已提交
695 696 697
	 * up.	The i'th XLR_SET_BKP_BLOCK bit corresponds to the i'th
	 * distinct buffer value (ignoring InvalidBuffer) appearing in the
	 * rdata list.
T
Tom Lane 已提交
698 699 700
	 */
	write_len = len;
	for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
701 702 703 704
	{
		if (dtbuf[i] == InvalidBuffer || !(dtbuf_bkp[i]))
			continue;

T
Tom Lane 已提交
705
		info |= XLR_SET_BKP_BLOCK(i);
706 707 708

		rdt->next = &(dtbuf_rdt[2 * i]);

B
Bruce Momjian 已提交
709
		dtbuf_rdt[2 * i].data = (char *) &(dtbuf_xlg[i]);
710
		dtbuf_rdt[2 * i].len = sizeof(BkpBlock);
T
Tom Lane 已提交
711
		write_len += sizeof(BkpBlock);
712 713 714

		rdt = dtbuf_rdt[2 * i].next = &(dtbuf_rdt[2 * i + 1]);

B
Bruce Momjian 已提交
715
		dtbuf_rdt[2 * i + 1].data = (char *) BufferGetBlock(dtbuf[i]);
716
		dtbuf_rdt[2 * i + 1].len = BLCKSZ;
T
Tom Lane 已提交
717
		write_len += BLCKSZ;
718 719 720
		dtbuf_rdt[2 * i + 1].next = NULL;
	}

T
Tom Lane 已提交
721
	/* Insert record header */
722

T
Tom Lane 已提交
723 724
	updrqst = false;
	freespace = INSERT_FREESPACE(Insert);
725 726
	if (freespace < SizeOfXLogRecord)
	{
T
Tom Lane 已提交
727
		updrqst = AdvanceXLInsertBuffer();
728 729 730
		freespace = BLCKSZ - SizeOfXLogPHD;
	}

T
Tom Lane 已提交
731
	curridx = Insert->curridx;
732
	record = (XLogRecord *) Insert->currpos;
T
Tom Lane 已提交
733

734
	record->xl_prev = Insert->PrevRecord;
V
Vadim B. Mikheev 已提交
735
	if (no_tran)
736 737 738 739
	{
		record->xl_xact_prev.xlogid = 0;
		record->xl_xact_prev.xrecoff = 0;
	}
V
Vadim B. Mikheev 已提交
740 741 742
	else
		record->xl_xact_prev = MyLastRecPtr;

743
	record->xl_xid = GetCurrentTransactionId();
T
Tom Lane 已提交
744
	record->xl_len = len;		/* doesn't include backup blocks */
745
	record->xl_info = info;
746
	record->xl_rmid = rmid;
747

T
Tom Lane 已提交
748
	/* Now we can finish computing the main CRC */
B
Bruce Momjian 已提交
749
	COMP_CRC64(rdata_crc, (char *) record + sizeof(crc64),
T
Tom Lane 已提交
750
			   SizeOfXLogRecord - sizeof(crc64));
751 752 753
	FIN_CRC64(rdata_crc);
	record->xl_crc = rdata_crc;

T
Tom Lane 已提交
754 755 756
	/* Compute record's XLOG location */
	INSERT_RECPTR(RecPtr, Insert, curridx);

J
Jan Wieck 已提交
757
	/* If first XLOG record of transaction, save it in PGPROC array */
V
Vadim B. Mikheev 已提交
758
	if (MyLastRecPtr.xrecoff == 0 && !no_tran)
759
	{
760 761 762 763 764 765
		/*
		 * We do not acquire SInvalLock here because of possible deadlock.
		 * Anyone who wants to inspect other procs' logRec must acquire
		 * WALInsertLock, instead.  A better solution would be a per-PROC
		 * spinlock, but no time for that before 7.2 --- tgl 12/19/01.
		 */
766 767
		MyProc->logRec = RecPtr;
	}
V
WAL  
Vadim B. Mikheev 已提交
768 769 770

	if (XLOG_DEBUG)
	{
B
Bruce Momjian 已提交
771
		char		buf[8192];
V
WAL  
Vadim B. Mikheev 已提交
772

773
		sprintf(buf, "INSERT @ %X/%X: ", RecPtr.xlogid, RecPtr.xrecoff);
V
WAL  
Vadim B. Mikheev 已提交
774
		xlog_outrec(buf, record);
775
		if (rdata->data != NULL)
V
WAL  
Vadim B. Mikheev 已提交
776 777
		{
			strcat(buf, " - ");
778
			RmgrTable[record->xl_rmid].rm_desc(buf, record->xl_info, rdata->data);
V
WAL  
Vadim B. Mikheev 已提交
779
		}
780
		elog(LOG, "%s", buf);
V
WAL  
Vadim B. Mikheev 已提交
781 782
	}

T
Tom Lane 已提交
783 784 785 786 787
	/* Record begin of record in appropriate places */
	if (!no_tran)
		MyLastRecPtr = RecPtr;
	ProcLastRecPtr = RecPtr;
	Insert->PrevRecord = RecPtr;
788
	MyXactMadeXLogEntry = true;
T
Tom Lane 已提交
789

790
	Insert->currpos += SizeOfXLogRecord;
T
Tom Lane 已提交
791
	freespace -= SizeOfXLogRecord;
792

T
Tom Lane 已提交
793 794 795 796
	/*
	 * Append the data, including backup blocks if any
	 */
	while (write_len)
797
	{
798 799 800 801
		while (rdata->data == NULL)
			rdata = rdata->next;

		if (freespace > 0)
802
		{
803 804 805 806 807
			if (rdata->len > freespace)
			{
				memcpy(Insert->currpos, rdata->data, freespace);
				rdata->data += freespace;
				rdata->len -= freespace;
T
Tom Lane 已提交
808
				write_len -= freespace;
809 810 811 812 813
			}
			else
			{
				memcpy(Insert->currpos, rdata->data, rdata->len);
				freespace -= rdata->len;
T
Tom Lane 已提交
814
				write_len -= rdata->len;
815 816 817 818
				Insert->currpos += rdata->len;
				rdata = rdata->next;
				continue;
			}
819 820
		}

821
		/* Use next buffer */
T
Tom Lane 已提交
822 823 824 825 826 827 828 829
		updrqst = AdvanceXLInsertBuffer();
		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;
		freespace = BLCKSZ - SizeOfXLogPHD - SizeOfXLogContRecord;
830
	}
831

T
Tom Lane 已提交
832 833
	/* Ensure next record will be properly aligned */
	Insert->currpos = (char *) Insert->currpage +
B
Bruce Momjian 已提交
834
		MAXALIGN(Insert->currpos - (char *) Insert->currpage);
T
Tom Lane 已提交
835
	freespace = INSERT_FREESPACE(Insert);
836

V
Vadim B. Mikheev 已提交
837
	/*
B
Bruce Momjian 已提交
838 839
	 * 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 已提交
840
	 */
T
Tom Lane 已提交
841
	INSERT_RECPTR(RecPtr, Insert, curridx);
V
Vadim B. Mikheev 已提交
842

T
Tom Lane 已提交
843
	/* Need to update shared LogwrtRqst if some block was filled up */
844
	if (freespace < SizeOfXLogRecord)
B
Bruce Momjian 已提交
845 846
		updrqst = true;			/* curridx is filled and available for
								 * writing out */
847 848
	else
		curridx = PrevBufIdx(curridx);
T
Tom Lane 已提交
849
	WriteRqst = XLogCtl->xlblocks[curridx];
850

851
	LWLockRelease(WALInsertLock);
852 853 854

	if (updrqst)
	{
855 856 857 858
		/* use volatile pointer to prevent code rearrangement */
		volatile XLogCtlData *xlogctl = XLogCtl;

		SpinLockAcquire_NoHoldoff(&xlogctl->info_lck);
T
Tom Lane 已提交
859
		/* advance global request to include new block(s) */
860 861
		if (XLByteLT(xlogctl->LogwrtRqst.Write, WriteRqst))
			xlogctl->LogwrtRqst.Write = WriteRqst;
T
Tom Lane 已提交
862
		/* update local result copy while I have the chance */
863 864
		LogwrtResult = xlogctl->LogwrtResult;
		SpinLockRelease_NoHoldoff(&xlogctl->info_lck);
865 866
	}

867 868
	ProcLastRecEnd = RecPtr;

869
	END_CRIT_SECTION();
870

871
	return (RecPtr);
872
}
873

T
Tom Lane 已提交
874 875 876 877 878
/*
 * Advance the Insert state to the next buffer page, writing out the next
 * buffer if it still contains unwritten data.
 *
 * The global LogwrtRqst.Write pointer needs to be advanced to include the
879
 * just-filled page.  If we can do this for free (without an extra lock),
T
Tom Lane 已提交
880 881 882
 * 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.
 *
883
 * Must be called with WALInsertLock held.
T
Tom Lane 已提交
884 885 886
 */
static bool
AdvanceXLInsertBuffer(void)
887
{
T
Tom Lane 已提交
888 889 890 891 892 893
	XLogCtlInsert *Insert = &XLogCtl->Insert;
	XLogCtlWrite *Write = &XLogCtl->Write;
	uint16		nextidx = NextBufIdx(Insert->curridx);
	bool		update_needed = true;
	XLogRecPtr	OldPageRqstPtr;
	XLogwrtRqst WriteRqst;
894 895
	XLogRecPtr	NewPageEndPtr;
	XLogPageHeader NewPage;
896

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

T
Tom Lane 已提交
901
	/*
B
Bruce Momjian 已提交
902 903 904
	 * 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 已提交
905 906 907 908 909 910
	 */
	OldPageRqstPtr = XLogCtl->xlblocks[nextidx];
	if (!XLByteLE(OldPageRqstPtr, LogwrtResult.Write))
	{
		/* nope, got work to do... */
		XLogRecPtr	FinishedPageRqstPtr;
911

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

914
		/* Before waiting, get info_lck and update LogwrtResult */
915 916 917 918 919 920 921 922 923 924
		{
			/* use volatile pointer to prevent code rearrangement */
			volatile XLogCtlData *xlogctl = XLogCtl;

			SpinLockAcquire_NoHoldoff(&xlogctl->info_lck);
			if (XLByteLT(xlogctl->LogwrtRqst.Write, FinishedPageRqstPtr))
				xlogctl->LogwrtRqst.Write = FinishedPageRqstPtr;
			LogwrtResult = xlogctl->LogwrtResult;
			SpinLockRelease_NoHoldoff(&xlogctl->info_lck);
		}
925 926 927 928 929 930 931 932 933

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

		if (XLByteLE(OldPageRqstPtr, LogwrtResult.Write))
		{
			/* OK, someone wrote it already */
			Insert->LogwrtResult = LogwrtResult;
		}
		else
934
		{
935 936 937 938
			/* Must acquire write lock */
			LWLockAcquire(WALWriteLock, LW_EXCLUSIVE);
			LogwrtResult = Write->LogwrtResult;
			if (XLByteLE(OldPageRqstPtr, LogwrtResult.Write))
939
			{
940 941 942
				/* OK, someone wrote it already */
				LWLockRelease(WALWriteLock);
				Insert->LogwrtResult = LogwrtResult;
T
Tom Lane 已提交
943
			}
944
			else
T
Tom Lane 已提交
945 946
			{
				/*
B
Bruce Momjian 已提交
947 948
				 * Have to write buffers while holding insert lock. This
				 * is not good, so only write as much as we absolutely
T
Tom Lane 已提交
949 950 951 952 953 954
				 * must.
				 */
				WriteRqst.Write = OldPageRqstPtr;
				WriteRqst.Flush.xlogid = 0;
				WriteRqst.Flush.xrecoff = 0;
				XLogWrite(WriteRqst);
955
				LWLockRelease(WALWriteLock);
T
Tom Lane 已提交
956
				Insert->LogwrtResult = LogwrtResult;
957 958 959 960
			}
		}
	}

T
Tom Lane 已提交
961 962 963 964
	/*
	 * Now the next buffer slot is free and we can set it up to be the
	 * next output page.
	 */
965 966
	NewPageEndPtr = XLogCtl->xlblocks[Insert->curridx];
	if (NewPageEndPtr.xrecoff >= XLogFileSize)
967
	{
T
Tom Lane 已提交
968
		/* crossing a logid boundary */
969 970
		NewPageEndPtr.xlogid += 1;
		NewPageEndPtr.xrecoff = BLCKSZ;
971
	}
T
Tom Lane 已提交
972
	else
973 974 975
		NewPageEndPtr.xrecoff += BLCKSZ;
	XLogCtl->xlblocks[nextidx] = NewPageEndPtr;
	NewPage = (XLogPageHeader) (XLogCtl->pages + nextidx * BLCKSZ);
T
Tom Lane 已提交
976
	Insert->curridx = nextidx;
977 978
	Insert->currpage = NewPage;
	Insert->currpos = ((char *) NewPage) + SizeOfXLogPHD;
B
Bruce Momjian 已提交
979

T
Tom Lane 已提交
980
	/*
B
Bruce Momjian 已提交
981 982
	 * 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 已提交
983
	 */
984 985 986 987
	MemSet((char *) NewPage, 0, BLCKSZ);

	/* And fill the new page's header */
	NewPage->xlp_magic = XLOG_PAGE_MAGIC;
988
	/* NewPage->xlp_info = 0; */	/* done by memset */
989 990 991
	NewPage->xlp_sui = ThisStartUpID;
	NewPage->xlp_pageaddr.xlogid = NewPageEndPtr.xlogid;
	NewPage->xlp_pageaddr.xrecoff = NewPageEndPtr.xrecoff - BLCKSZ;
T
Tom Lane 已提交
992 993

	return update_needed;
994 995
}

T
Tom Lane 已提交
996 997 998
/*
 * Write and/or fsync the log at least as far as WriteRqst indicates.
 *
999
 * Must be called with WALWriteLock held.
T
Tom Lane 已提交
1000
 */
1001
static void
T
Tom Lane 已提交
1002
XLogWrite(XLogwrtRqst WriteRqst)
1003
{
1004 1005
	XLogCtlWrite *Write = &XLogCtl->Write;
	char	   *from;
T
Tom Lane 已提交
1006
	bool		ispartialpage;
1007
	bool		use_existent;
1008

B
Bruce Momjian 已提交
1009 1010 1011 1012
	/*
	 * Update local LogwrtResult (caller probably did this already,
	 * but...)
	 */
T
Tom Lane 已提交
1013 1014 1015
	LogwrtResult = Write->LogwrtResult;

	while (XLByteLT(LogwrtResult.Write, WriteRqst.Write))
1016
	{
1017 1018 1019 1020 1021 1022 1023
		/*
		 * 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.
		 */
		if (!XLByteLT(LogwrtResult.Write, XLogCtl->xlblocks[Write->curridx]))
1024
			elog(PANIC, "XLogWrite: write request %X/%X is past end of log %X/%X",
1025 1026 1027
				 LogwrtResult.Write.xlogid, LogwrtResult.Write.xrecoff,
				 XLogCtl->xlblocks[Write->curridx].xlogid,
				 XLogCtl->xlblocks[Write->curridx].xrecoff);
1028

T
Tom Lane 已提交
1029 1030 1031 1032 1033
		/* Advance LogwrtResult.Write to end of current buffer page */
		LogwrtResult.Write = XLogCtl->xlblocks[Write->curridx];
		ispartialpage = XLByteLT(WriteRqst.Write, LogwrtResult.Write);

		if (!XLByteInPrevSeg(LogwrtResult.Write, openLogId, openLogSeg))
1034
		{
T
Tom Lane 已提交
1035 1036 1037 1038
			/*
			 * Switch to new logfile segment.
			 */
			if (openLogFile >= 0)
1039
			{
T
Tom Lane 已提交
1040
				if (close(openLogFile) != 0)
1041
					elog(PANIC, "close of log file %u, segment %u failed: %m",
T
Tom Lane 已提交
1042 1043
						 openLogId, openLogSeg);
				openLogFile = -1;
1044
			}
T
Tom Lane 已提交
1045 1046
			XLByteToPrevSeg(LogwrtResult.Write, openLogId, openLogSeg);

1047 1048 1049 1050
			/* create/use new log file */
			use_existent = true;
			openLogFile = XLogFileInit(openLogId, openLogSeg,
									   &use_existent, true);
T
Tom Lane 已提交
1051
			openLogOff = 0;
1052 1053 1054

			if (!use_existent)	/* there was no precreated file */
				elog(LOG, "XLogWrite: new log file created - "
B
Bruce Momjian 已提交
1055
					 "consider increasing 'wal_files' in postgresql.conf.");
1056

T
Tom Lane 已提交
1057
			/* update pg_control, unless someone else already did */
1058
			LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
1059 1060 1061
			if (ControlFile->logId < openLogId ||
				(ControlFile->logId == openLogId &&
				 ControlFile->logSeg < openLogSeg + 1))
T
Tom Lane 已提交
1062 1063 1064 1065 1066
			{
				ControlFile->logId = openLogId;
				ControlFile->logSeg = openLogSeg + 1;
				ControlFile->time = time(NULL);
				UpdateControlFile();
B
Bruce Momjian 已提交
1067

1068
				/*
B
Bruce Momjian 已提交
1069 1070 1071 1072
				 * Signal postmaster 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.)
1073 1074 1075 1076 1077 1078 1079
				 */
				if (IsUnderPostmaster &&
					(openLogId != RedoRecPtr.xlogid ||
					 openLogSeg >= (RedoRecPtr.xrecoff / XLogSegSize) +
					 (uint32) CheckPointSegments))
				{
					if (XLOG_DEBUG)
1080
						elog(LOG, "XLogWrite: time for a checkpoint, signaling postmaster");
1081
					SendPostmasterSignal(PMSIGNAL_DO_CHECKPOINT);
1082
				}
T
Tom Lane 已提交
1083
			}
1084
			LWLockRelease(ControlFileLock);
1085 1086
		}

T
Tom Lane 已提交
1087
		if (openLogFile < 0)
1088
		{
T
Tom Lane 已提交
1089 1090 1091
			XLByteToPrevSeg(LogwrtResult.Write, openLogId, openLogSeg);
			openLogFile = XLogFileOpen(openLogId, openLogSeg, false);
			openLogOff = 0;
1092 1093
		}

T
Tom Lane 已提交
1094 1095
		/* Need to seek in the file? */
		if (openLogOff != (LogwrtResult.Write.xrecoff - BLCKSZ) % XLogSegSize)
1096
		{
T
Tom Lane 已提交
1097 1098
			openLogOff = (LogwrtResult.Write.xrecoff - BLCKSZ) % XLogSegSize;
			if (lseek(openLogFile, (off_t) openLogOff, SEEK_SET) < 0)
1099
				elog(PANIC, "lseek of log file %u, segment %u, offset %u failed: %m",
T
Tom Lane 已提交
1100
					 openLogId, openLogSeg, openLogOff);
1101 1102
		}

T
Tom Lane 已提交
1103 1104
		/* OK to write the page */
		from = XLogCtl->pages + Write->curridx * BLCKSZ;
1105
		errno = 0;
T
Tom Lane 已提交
1106
		if (write(openLogFile, from, BLCKSZ) != BLCKSZ)
1107 1108 1109 1110
		{
			/* if write didn't set errno, assume problem is no disk space */
			if (errno == 0)
				errno = ENOSPC;
1111
			elog(PANIC, "write of log file %u, segment %u, offset %u failed: %m",
T
Tom Lane 已提交
1112
				 openLogId, openLogSeg, openLogOff);
1113
		}
T
Tom Lane 已提交
1114
		openLogOff += BLCKSZ;
1115

T
Tom Lane 已提交
1116 1117 1118
		/*
		 * If we just wrote the whole last page of a logfile segment,
		 * fsync the segment immediately.  This avoids having to go back
B
Bruce Momjian 已提交
1119 1120 1121
		 * 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.
T
Tom Lane 已提交
1122 1123 1124
		 */
		if (openLogOff >= XLogSegSize && !ispartialpage)
		{
1125
			issue_xlog_fsync();
B
Bruce Momjian 已提交
1126
			LogwrtResult.Flush = LogwrtResult.Write;	/* end of current page */
T
Tom Lane 已提交
1127
		}
1128

T
Tom Lane 已提交
1129 1130 1131 1132 1133 1134 1135
		if (ispartialpage)
		{
			/* Only asked to write a partial page */
			LogwrtResult.Write = WriteRqst.Write;
			break;
		}
		Write->curridx = NextBufIdx(Write->curridx);
1136 1137
	}

T
Tom Lane 已提交
1138 1139 1140 1141 1142
	/*
	 * If asked to flush, do so
	 */
	if (XLByteLT(LogwrtResult.Flush, WriteRqst.Flush) &&
		XLByteLT(LogwrtResult.Flush, LogwrtResult.Write))
1143
	{
T
Tom Lane 已提交
1144
		/*
B
Bruce Momjian 已提交
1145 1146 1147
		 * 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 已提交
1148
		 */
1149
		if (sync_method != SYNC_METHOD_OPEN)
T
Tom Lane 已提交
1150
		{
1151
			if (openLogFile >= 0 &&
B
Bruce Momjian 已提交
1152
			 !XLByteInPrevSeg(LogwrtResult.Write, openLogId, openLogSeg))
1153 1154
			{
				if (close(openLogFile) != 0)
1155
					elog(PANIC, "close of log file %u, segment %u failed: %m",
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
						 openLogId, openLogSeg);
				openLogFile = -1;
			}
			if (openLogFile < 0)
			{
				XLByteToPrevSeg(LogwrtResult.Write, openLogId, openLogSeg);
				openLogFile = XLogFileOpen(openLogId, openLogSeg, false);
				openLogOff = 0;
			}
			issue_xlog_fsync();
T
Tom Lane 已提交
1166 1167
		}
		LogwrtResult.Flush = LogwrtResult.Write;
1168 1169
	}

T
Tom Lane 已提交
1170 1171 1172
	/*
	 * Update shared-memory status
	 *
B
Bruce Momjian 已提交
1173 1174
	 * We make sure that the shared 'request' values do not fall behind the
	 * 'result' values.  This is not absolutely essential, but it saves
T
Tom Lane 已提交
1175 1176
	 * some code in a couple of places.
	 */
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
	{
		/* use volatile pointer to prevent code rearrangement */
		volatile XLogCtlData *xlogctl = XLogCtl;

		SpinLockAcquire_NoHoldoff(&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_NoHoldoff(&xlogctl->info_lck);
	}
1189

T
Tom Lane 已提交
1190 1191 1192 1193 1194 1195
	Write->LogwrtResult = LogwrtResult;
}

/*
 * Ensure that all XLOG data through the given position is flushed to disk.
 *
1196
 * NOTE: this differs from XLogWrite mainly in that the WALWriteLock is not
T
Tom Lane 已提交
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
 * already held, and we try to avoid acquiring it if possible.
 */
void
XLogFlush(XLogRecPtr record)
{
	XLogRecPtr	WriteRqstPtr;
	XLogwrtRqst WriteRqst;

	if (XLOG_DEBUG)
	{
1207
		elog(LOG, "XLogFlush%s%s: request %X/%X; write %X/%X; flush %X/%X\n",
1208 1209 1210 1211 1212
			 (IsBootstrapProcessingMode()) ? "(bootstrap)" : "",
			 (InRedo) ? "(redo)" : "",
			 record.xlogid, record.xrecoff,
			 LogwrtResult.Write.xlogid, LogwrtResult.Write.xrecoff,
			 LogwrtResult.Flush.xlogid, LogwrtResult.Flush.xrecoff);
T
Tom Lane 已提交
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
		fflush(stderr);
	}

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

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

	START_CRIT_SECTION();

	/*
	 * Since fsync is usually a horribly expensive operation, we try to
	 * 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,
B
Bruce Momjian 已提交
1230 1231 1232
	 * 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 已提交
1233 1234 1235 1236 1237
	 */

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

1238
	/* read LogwrtResult and update local state */
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
	{
		/* use volatile pointer to prevent code rearrangement */
		volatile XLogCtlData *xlogctl = XLogCtl;

		SpinLockAcquire_NoHoldoff(&xlogctl->info_lck);
		if (XLByteLT(WriteRqstPtr, xlogctl->LogwrtRqst.Write))
			WriteRqstPtr = xlogctl->LogwrtRqst.Write;
		LogwrtResult = xlogctl->LogwrtResult;
		SpinLockRelease_NoHoldoff(&xlogctl->info_lck);
	}
1249 1250 1251

	/* done already? */
	if (!XLByteLE(record, LogwrtResult.Flush))
T
Tom Lane 已提交
1252 1253
	{
		/* if something was added to log cache then try to flush this too */
1254
		if (LWLockConditionalAcquire(WALInsertLock, LW_EXCLUSIVE))
T
Tom Lane 已提交
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
		{
			XLogCtlInsert *Insert = &XLogCtl->Insert;
			uint32		freespace = INSERT_FREESPACE(Insert);

			if (freespace < SizeOfXLogRecord)	/* buffer is full */
				WriteRqstPtr = XLogCtl->xlblocks[Insert->curridx];
			else
			{
				WriteRqstPtr = XLogCtl->xlblocks[Insert->curridx];
				WriteRqstPtr.xrecoff -= freespace;
			}
1266
			LWLockRelease(WALInsertLock);
T
Tom Lane 已提交
1267
		}
1268 1269 1270 1271
		/* now wait for the write lock */
		LWLockAcquire(WALWriteLock, LW_EXCLUSIVE);
		LogwrtResult = XLogCtl->Write.LogwrtResult;
		if (!XLByteLE(record, LogwrtResult.Flush))
T
Tom Lane 已提交
1272 1273 1274 1275 1276
		{
			WriteRqst.Write = WriteRqstPtr;
			WriteRqst.Flush = record;
			XLogWrite(WriteRqst);
		}
1277
		LWLockRelease(WALWriteLock);
T
Tom Lane 已提交
1278 1279 1280
	}

	END_CRIT_SECTION();
1281 1282 1283 1284 1285 1286

	/*
	 * If we still haven't flushed to the request point then we have a
	 * 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.
	 *
1287
	 * Formerly we treated this as a PANIC condition, but that hurts the
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
	 * 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.)
	 *
	 * The current approach is to ERROR under normal conditions, but only
B
Bruce Momjian 已提交
1298
	 * WARNING during recovery, so that the system can be brought up even if
1299
	 * there's a corrupt LSN.  Note that for calls from xact.c, the ERROR
1300
	 * will be promoted to PANIC since xact.c calls this routine inside a
1301 1302 1303 1304 1305
	 * critical 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.
	 */
	if (XLByteLT(LogwrtResult.Flush, record))
B
Bruce Momjian 已提交
1306
		elog(InRecovery ? WARNING : ERROR,
1307 1308 1309
			 "XLogFlush: request %X/%X is not satisfied --- flushed only to %X/%X",
			 record.xlogid, record.xrecoff,
			 LogwrtResult.Flush.xlogid, LogwrtResult.Flush.xrecoff);
1310 1311
}

T
Tom Lane 已提交
1312 1313 1314
/*
 * Create a new XLOG file segment, or open a pre-existing one.
 *
1315 1316 1317
 * log, seg: identify segment to be created/opened.
 *
 * *use_existent: if TRUE, OK to use a pre-existing file (else, any
B
Bruce Momjian 已提交
1318
 * pre-existing file will be deleted).	On return, TRUE if a pre-existing
1319 1320
 * file was used.
 *
1321
 * use_lock: if TRUE, acquire ControlFileLock while moving file into
1322
 * place.  This should be TRUE except during bootstrap log creation.  The
1323
 * caller must *not* hold the lock at call.
1324
 *
T
Tom Lane 已提交
1325 1326
 * Returns FD of opened file.
 */
1327
static int
1328 1329
XLogFileInit(uint32 log, uint32 seg,
			 bool *use_existent, bool use_lock)
1330
{
1331
	char		path[MAXPGPATH];
1332
	char		tmppath[MAXPGPATH];
1333
	char		zbuffer[BLCKSZ];
1334
	int			fd;
1335
	int			nbytes;
1336 1337

	XLogFileName(path, log, seg);
V
Vadim B. Mikheev 已提交
1338 1339

	/*
B
Bruce Momjian 已提交
1340 1341
	 * Try to use existent file (checkpoint maker may have created it
	 * already)
V
Vadim B. Mikheev 已提交
1342
	 */
1343
	if (*use_existent)
V
Vadim B. Mikheev 已提交
1344
	{
1345 1346
		fd = BasicOpenFile(path, O_RDWR | PG_BINARY | XLOG_SYNC_BIT,
						   S_IRUSR | S_IWUSR);
V
Vadim B. Mikheev 已提交
1347 1348 1349
		if (fd < 0)
		{
			if (errno != ENOENT)
1350
				elog(PANIC, "open of %s (log file %u, segment %u) failed: %m",
1351
					 path, log, seg);
V
Vadim B. Mikheev 已提交
1352 1353
		}
		else
B
Bruce Momjian 已提交
1354
			return (fd);
V
Vadim B. Mikheev 已提交
1355 1356
	}

1357
	/*
B
Bruce Momjian 已提交
1358 1359 1360
	 * 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
1361
	 * than holding the lock throughout this lengthy process.
1362
	 */
1363 1364
	snprintf(tmppath, MAXPGPATH, "%s/xlogtemp.%d",
			 XLogDir, (int) getpid());
1365 1366

	unlink(tmppath);
1367

1368
	/* do not use XLOG_SYNC_BIT here --- want to fsync only at end of fill */
1369
	fd = BasicOpenFile(tmppath, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
T
Tom Lane 已提交
1370
					   S_IRUSR | S_IWUSR);
1371
	if (fd < 0)
1372
		elog(PANIC, "creation of file %s failed: %m", tmppath);
1373

1374
	/*
B
Bruce Momjian 已提交
1375
	 * Zero-fill the file.	We have to do this the hard way to ensure that
1376 1377
	 * all the file space has really been allocated --- on platforms that
	 * allow "holes" in files, just seeking to the end doesn't allocate
B
Bruce Momjian 已提交
1378
	 * intermediate space.	This way, we know that we have all the space
1379
	 * and (after the fsync below) that all the indirect blocks are down
1380 1381
	 * on disk.  Therefore, fdatasync(2) or O_DSYNC will be sufficient to
	 * sync future writes to the log file.
1382 1383 1384 1385
	 */
	MemSet(zbuffer, 0, sizeof(zbuffer));
	for (nbytes = 0; nbytes < XLogSegSize; nbytes += sizeof(zbuffer))
	{
1386
		errno = 0;
1387
		if ((int) write(fd, zbuffer, sizeof(zbuffer)) != (int) sizeof(zbuffer))
T
Tom Lane 已提交
1388
		{
B
Bruce Momjian 已提交
1389
			int			save_errno = errno;
T
Tom Lane 已提交
1390

B
Bruce Momjian 已提交
1391 1392 1393 1394
			/*
			 * If we fail to make the file, delete it to release disk
			 * space
			 */
1395
			unlink(tmppath);
1396 1397
			/* if write didn't set errno, assume problem is no disk space */
			errno = save_errno ? save_errno : ENOSPC;
T
Tom Lane 已提交
1398

1399
			elog(PANIC, "ZeroFill failed to write %s: %m", tmppath);
T
Tom Lane 已提交
1400
		}
1401
	}
1402

1403
	if (pg_fsync(fd) != 0)
1404
		elog(PANIC, "fsync of file %s failed: %m", tmppath);
1405

V
Vadim B. Mikheev 已提交
1406
	close(fd);
T
Tom Lane 已提交
1407

1408
	/*
1409 1410
	 * Now move the segment into place with its final name.
	 *
1411 1412 1413 1414 1415
	 * If caller didn't want to use a pre-existing file, get rid of any
	 * 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.
	 */
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
	if (!InstallXLogFileSegment(log, seg, tmppath,
								*use_existent, XLOGfiles + XLOGfileslop,
								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)
1431
		elog(PANIC, "open of %s (log file %u, segment %u) failed: %m",
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
			 path, log, seg);

	return (fd);
}

/*
 * 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.
 *
 * log, seg: identify segment to install as (or first possible target).
 *
 * 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.
 *
 * max_advance: maximum number of log/seg slots to advance past the starting
 * point.  Fail if no free slot is found in this range.  (Irrelevant if
 * find_free is FALSE.)
 *
1455
 * use_lock: if TRUE, acquire ControlFileLock while moving file into
1456
 * place.  This should be TRUE except during bootstrap log creation.  The
1457
 * caller must *not* hold the lock at call.
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
 *
 * Returns TRUE if file installed, FALSE if not installed because of
 * exceeding max_advance limit.  (Any other kind of failure causes elog().)
 */
static bool
InstallXLogFileSegment(uint32 log, uint32 seg, char *tmppath,
					   bool find_free, int max_advance,
					   bool use_lock)
{
	char		path[MAXPGPATH];
	int			fd;

	XLogFileName(path, log, seg);

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

1478 1479 1480 1481 1482
	if (!find_free)
	{
		/* Force installation: get rid of any pre-existing segment file */
		unlink(path);
	}
1483 1484
	else
	{
1485 1486
		/* Find a free slot to put it in */
		while ((fd = BasicOpenFile(path, O_RDWR | PG_BINARY,
1487 1488 1489
								   S_IRUSR | S_IWUSR)) >= 0)
		{
			close(fd);
1490 1491 1492 1493
			if (--max_advance < 0)
			{
				/* Failed to find a free slot within specified range */
				if (use_lock)
1494
					LWLockRelease(ControlFileLock);
1495 1496 1497 1498
				return false;
			}
			NextLogSeg(log, seg);
			XLogFileName(path, log, seg);
1499 1500 1501 1502 1503 1504 1505
		}
	}

	/*
	 * 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.
1506
	 */
1507
#if !defined(__BEOS__) && !defined(N_PLAT_NLM) && !defined(__CYGWIN__)
1508
	if (link(tmppath, path) < 0)
1509
		elog(PANIC, "link from %s to %s (initialization of log file %u, segment %u) failed: %m",
1510
			 tmppath, path, log, seg);
1511
	unlink(tmppath);
1512
#else
1513
	if (rename(tmppath, path) < 0)
1514
		elog(PANIC, "rename from %s to %s (initialization of log file %u, segment %u) failed: %m",
1515
			 tmppath, path, log, seg);
1516
#endif
V
Vadim B. Mikheev 已提交
1517

1518
	if (use_lock)
1519
		LWLockRelease(ControlFileLock);
1520

1521
	return true;
1522 1523
}

T
Tom Lane 已提交
1524 1525 1526
/*
 * Open a pre-existing logfile segment.
 */
1527 1528 1529
static int
XLogFileOpen(uint32 log, uint32 seg, bool econt)
{
1530 1531
	char		path[MAXPGPATH];
	int			fd;
1532 1533 1534

	XLogFileName(path, log, seg);

1535 1536
	fd = BasicOpenFile(path, O_RDWR | PG_BINARY | XLOG_SYNC_BIT,
					   S_IRUSR | S_IWUSR);
1537 1538 1539 1540
	if (fd < 0)
	{
		if (econt && errno == ENOENT)
		{
1541 1542
			elog(LOG, "open of %s (log file %u, segment %u) failed: %m",
				 path, log, seg);
1543 1544
			return (fd);
		}
1545
		elog(PANIC, "open of %s (log file %u, segment %u) failed: %m",
1546
			 path, log, seg);
1547 1548
	}

1549
	return (fd);
1550 1551
}

V
Vadim B. Mikheev 已提交
1552
/*
T
Tom Lane 已提交
1553 1554 1555 1556 1557 1558 1559 1560 1561
 * Preallocate log files beyond the specified log endpoint, according to
 * the XLOGfile user parameter.
 */
static void
PreallocXlogFiles(XLogRecPtr endptr)
{
	uint32		_logId;
	uint32		_logSeg;
	int			lf;
1562
	bool		use_existent;
T
Tom Lane 已提交
1563 1564 1565 1566 1567 1568 1569 1570
	int			i;

	XLByteToPrevSeg(endptr, _logId, _logSeg);
	if (XLOGfiles > 0)
	{
		for (i = 1; i <= XLOGfiles; i++)
		{
			NextLogSeg(_logId, _logSeg);
1571 1572
			use_existent = true;
			lf = XLogFileInit(_logId, _logSeg, &use_existent, true);
T
Tom Lane 已提交
1573 1574 1575 1576 1577 1578 1579
			close(lf);
		}
	}
	else if ((endptr.xrecoff - 1) % XLogSegSize >=
			 (uint32) (0.75 * XLogSegSize))
	{
		NextLogSeg(_logId, _logSeg);
1580 1581
		use_existent = true;
		lf = XLogFileInit(_logId, _logSeg, &use_existent, true);
T
Tom Lane 已提交
1582 1583 1584 1585 1586 1587
		close(lf);
	}
}

/*
 * Remove or move offline all log files older or equal to passed log/seg#
1588 1589 1590
 *
 * 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 已提交
1591 1592
 */
static void
1593
MoveOfflineLogs(uint32 log, uint32 seg, XLogRecPtr endptr)
V
Vadim B. Mikheev 已提交
1594
{
1595 1596
	uint32		endlogId;
	uint32		endlogSeg;
B
Bruce Momjian 已提交
1597 1598 1599 1600
	DIR		   *xldir;
	struct dirent *xlde;
	char		lastoff[32];
	char		path[MAXPGPATH];
V
Vadim B. Mikheev 已提交
1601

1602
	XLByteToPrevSeg(endptr, endlogId, endlogSeg);
V
Vadim B. Mikheev 已提交
1603 1604 1605

	xldir = opendir(XLogDir);
	if (xldir == NULL)
1606
		elog(PANIC, "could not open transaction log directory (%s): %m",
1607
			 XLogDir);
V
Vadim B. Mikheev 已提交
1608

T
Tom Lane 已提交
1609
	sprintf(lastoff, "%08X%08X", log, seg);
V
Vadim B. Mikheev 已提交
1610 1611 1612 1613

	errno = 0;
	while ((xlde = readdir(xldir)) != NULL)
	{
T
Tom Lane 已提交
1614 1615 1616
		if (strlen(xlde->d_name) == 16 &&
			strspn(xlde->d_name, "0123456789ABCDEF") == 16 &&
			strcmp(xlde->d_name, lastoff) <= 0)
V
Vadim B. Mikheev 已提交
1617
		{
1618
			snprintf(path, MAXPGPATH, "%s/%s", XLogDir, xlde->d_name);
1619
			if (XLOG_archive_dir[0])
1620 1621 1622
			{
				elog(LOG, "archiving transaction log file %s",
					 xlde->d_name);
B
Bruce Momjian 已提交
1623
				elog(WARNING, "archiving log files is not implemented!");
1624
			}
1625
			else
1626 1627 1628
			{
				/*
				 * Before deleting the file, see if it can be recycled as
1629 1630
				 * a future log segment.  We allow recycling segments up
				 * to XLOGfiles + XLOGfileslop segments beyond the current
1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
				 * XLOG location.
				 */
				if (InstallXLogFileSegment(endlogId, endlogSeg, path,
										   true, XLOGfiles + XLOGfileslop,
										   true))
				{
					elog(LOG, "recycled transaction log file %s",
						 xlde->d_name);
				}
				else
				{
					/* No need for any more future segments... */
					elog(LOG, "removing transaction log file %s",
						 xlde->d_name);
					unlink(path);
				}
			}
V
Vadim B. Mikheev 已提交
1648 1649 1650 1651
		}
		errno = 0;
	}
	if (errno)
1652
		elog(PANIC, "could not read transaction log directory (%s): %m",
1653
			 XLogDir);
V
Vadim B. Mikheev 已提交
1654 1655 1656
	closedir(xldir);
}

T
Tom Lane 已提交
1657 1658 1659 1660 1661
/*
 * Restore the backup blocks present in an XLOG record, if any.
 *
 * We assume all of the record has been read into memory at *record.
 */
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
static void
RestoreBkpBlocks(XLogRecord *record, XLogRecPtr lsn)
{
	Relation	reln;
	Buffer		buffer;
	Page		page;
	BkpBlock	bkpb;
	char	   *blk;
	int			i;

B
Bruce Momjian 已提交
1672
	blk = (char *) XLogRecGetData(record) + record->xl_len;
T
Tom Lane 已提交
1673
	for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
1674
	{
T
Tom Lane 已提交
1675
		if (!(record->xl_info & XLR_SET_BKP_BLOCK(i)))
1676 1677
			continue;

B
Bruce Momjian 已提交
1678
		memcpy((char *) &bkpb, blk, sizeof(BkpBlock));
1679 1680 1681 1682 1683 1684 1685 1686 1687 1688
		blk += sizeof(BkpBlock);

		reln = XLogOpenRelation(true, record->xl_rmid, bkpb.node);

		if (reln)
		{
			buffer = XLogReadBuffer(true, reln, bkpb.block);
			if (BufferIsValid(buffer))
			{
				page = (Page) BufferGetPage(buffer);
B
Bruce Momjian 已提交
1689
				memcpy((char *) page, blk, BLCKSZ);
1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
				PageSetLSN(page, lsn);
				PageSetSUI(page, ThisStartUpID);
				UnlockAndWriteBuffer(buffer);
			}
		}

		blk += BLCKSZ;
	}
}

T
Tom Lane 已提交
1700 1701 1702 1703 1704 1705 1706
/*
 * 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.
 */
1707 1708 1709 1710 1711 1712 1713 1714 1715
static bool
RecordIsValid(XLogRecord *record, XLogRecPtr recptr, int emode)
{
	crc64		crc;
	crc64		cbuf;
	int			i;
	uint32		len = record->xl_len;
	char	   *blk;

T
Tom Lane 已提交
1716
	/* Check CRC of rmgr data and record header */
1717
	INIT_CRC64(crc);
T
Tom Lane 已提交
1718
	COMP_CRC64(crc, XLogRecGetData(record), len);
B
Bruce Momjian 已提交
1719
	COMP_CRC64(crc, (char *) record + sizeof(crc64),
T
Tom Lane 已提交
1720
			   SizeOfXLogRecord - sizeof(crc64));
1721 1722
	FIN_CRC64(crc);

T
Tom Lane 已提交
1723
	if (!EQ_CRC64(record->xl_crc, crc))
1724
	{
1725
		elog(emode, "ReadRecord: bad resource manager data checksum in record at %X/%X",
T
Tom Lane 已提交
1726
			 recptr.xlogid, recptr.xrecoff);
B
Bruce Momjian 已提交
1727
		return (false);
1728 1729
	}

T
Tom Lane 已提交
1730
	/* Check CRCs of backup blocks, if any */
B
Bruce Momjian 已提交
1731
	blk = (char *) XLogRecGetData(record) + len;
T
Tom Lane 已提交
1732
	for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
1733
	{
T
Tom Lane 已提交
1734
		if (!(record->xl_info & XLR_SET_BKP_BLOCK(i)))
1735 1736 1737
			continue;

		INIT_CRC64(crc);
T
Tom Lane 已提交
1738 1739 1740
		COMP_CRC64(crc, blk + sizeof(BkpBlock), BLCKSZ);
		COMP_CRC64(crc, blk + sizeof(crc64),
				   sizeof(BkpBlock) - sizeof(crc64));
1741
		FIN_CRC64(crc);
B
Bruce Momjian 已提交
1742 1743
		memcpy((char *) &cbuf, blk, sizeof(crc64));		/* don't assume
														 * alignment */
1744

T
Tom Lane 已提交
1745
		if (!EQ_CRC64(cbuf, crc))
1746
		{
1747
			elog(emode, "ReadRecord: bad checksum of backup block %d in record at %X/%X",
T
Tom Lane 已提交
1748
				 i + 1, recptr.xlogid, recptr.xrecoff);
B
Bruce Momjian 已提交
1749
			return (false);
1750
		}
T
Tom Lane 已提交
1751
		blk += sizeof(BkpBlock) + BLCKSZ;
1752 1753
	}

B
Bruce Momjian 已提交
1754
	return (true);
1755 1756
}

T
Tom Lane 已提交
1757 1758 1759 1760 1761 1762
/*
 * 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.
 *
1763 1764
 * If no valid record is available, returns NULL, or fails if emode is PANIC.
 * (emode must be either PANIC or LOG.)
T
Tom Lane 已提交
1765 1766 1767 1768 1769
 *
 * buffer is a workspace at least _INTL_MAXLOGRECSZ bytes long.  It is needed
 * to reassemble a record that crosses block boundaries.  Note that on
 * successful return, the returned record pointer always points at buffer.
 */
1770
static XLogRecord *
T
Tom Lane 已提交
1771
ReadRecord(XLogRecPtr *RecPtr, int emode, char *buffer)
1772
{
1773 1774
	XLogRecord *record;
	XLogRecPtr	tmpRecPtr = EndRecPtr;
T
Tom Lane 已提交
1775 1776 1777 1778
	uint32		len,
				total_len;
	uint32		targetPageOff;
	unsigned	i;
1779
	bool		nextmode = false;
T
Tom Lane 已提交
1780 1781 1782 1783 1784 1785

	if (readBuf == NULL)
	{
		/*
		 * First time through, permanently allocate readBuf.  We do it
		 * this way, rather than just making a static array, for two
B
Bruce Momjian 已提交
1786 1787 1788 1789
		 * 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 已提交
1790 1791 1792 1793
		 */
		readBuf = (char *) malloc(BLCKSZ);
		Assert(readBuf != NULL);
	}
1794

T
Tom Lane 已提交
1795
	if (RecPtr == NULL)
1796
	{
1797
		RecPtr = &tmpRecPtr;
1798
		nextmode = true;
T
Tom Lane 已提交
1799
		/* fast case if next record is on same page */
1800 1801 1802 1803 1804
		if (nextRecord != NULL)
		{
			record = nextRecord;
			goto got_record;
		}
T
Tom Lane 已提交
1805
		/* align old recptr to next page */
1806 1807 1808 1809 1810 1811 1812 1813
		if (tmpRecPtr.xrecoff % BLCKSZ != 0)
			tmpRecPtr.xrecoff += (BLCKSZ - tmpRecPtr.xrecoff % BLCKSZ);
		if (tmpRecPtr.xrecoff >= XLogFileSize)
		{
			(tmpRecPtr.xlogid)++;
			tmpRecPtr.xrecoff = 0;
		}
		tmpRecPtr.xrecoff += SizeOfXLogPHD;
1814
	}
1815
	else if (!XRecOffIsValid(RecPtr->xrecoff))
1816
		elog(PANIC, "ReadRecord: invalid record offset at %X/%X",
1817
			 RecPtr->xlogid, RecPtr->xrecoff);
1818

T
Tom Lane 已提交
1819
	if (readFile >= 0 && !XLByteInSeg(*RecPtr, readId, readSeg))
1820
	{
1821 1822
		close(readFile);
		readFile = -1;
1823
	}
T
Tom Lane 已提交
1824
	XLByteToSeg(*RecPtr, readId, readSeg);
1825
	if (readFile < 0)
1826
	{
T
Tom Lane 已提交
1827
		readFile = XLogFileOpen(readId, readSeg, (emode == LOG));
1828 1829
		if (readFile < 0)
			goto next_record_is_invalid;
1830
		readOff = (uint32) (-1);	/* force read to occur below */
1831 1832
	}

T
Tom Lane 已提交
1833 1834
	targetPageOff = ((RecPtr->xrecoff % XLogSegSize) / BLCKSZ) * BLCKSZ;
	if (readOff != targetPageOff)
1835
	{
T
Tom Lane 已提交
1836 1837 1838
		readOff = targetPageOff;
		if (lseek(readFile, (off_t) readOff, SEEK_SET) < 0)
		{
1839
			elog(emode, "ReadRecord: lseek of log file %u, segment %u, offset %u failed: %m",
1840
				 readId, readSeg, readOff);
T
Tom Lane 已提交
1841 1842
			goto next_record_is_invalid;
		}
1843
		if (read(readFile, readBuf, BLCKSZ) != BLCKSZ)
T
Tom Lane 已提交
1844
		{
1845
			elog(emode, "ReadRecord: read of log file %u, segment %u, offset %u failed: %m",
1846
				 readId, readSeg, readOff);
T
Tom Lane 已提交
1847 1848
			goto next_record_is_invalid;
		}
1849
		if (!ValidXLOGHeader((XLogPageHeader) readBuf, emode, nextmode))
1850 1851
			goto next_record_is_invalid;
	}
T
Tom Lane 已提交
1852
	if ((((XLogPageHeader) readBuf)->xlp_info & XLP_FIRST_IS_CONTRECORD) &&
1853 1854
		RecPtr->xrecoff % BLCKSZ == SizeOfXLogPHD)
	{
1855
		elog(emode, "ReadRecord: contrecord is requested by %X/%X",
1856
			 RecPtr->xlogid, RecPtr->xrecoff);
1857 1858
		goto next_record_is_invalid;
	}
1859
	record = (XLogRecord *) ((char *) readBuf + RecPtr->xrecoff % BLCKSZ);
1860 1861

got_record:;
B
Bruce Momjian 已提交
1862

T
Tom Lane 已提交
1863
	/*
B
Bruce Momjian 已提交
1864 1865
	 * Currently, xl_len == 0 must be bad data, but that might not be true
	 * forever.  See note in XLogInsert.
T
Tom Lane 已提交
1866
	 */
1867 1868
	if (record->xl_len == 0)
	{
1869
		elog(emode, "ReadRecord: record with zero length at %X/%X",
T
Tom Lane 已提交
1870
			 RecPtr->xlogid, RecPtr->xrecoff);
1871 1872
		goto next_record_is_invalid;
	}
B
Bruce Momjian 已提交
1873

T
Tom Lane 已提交
1874
	/*
B
Bruce Momjian 已提交
1875 1876
	 * Compute total length of record including any appended backup
	 * blocks.
T
Tom Lane 已提交
1877 1878 1879 1880 1881 1882 1883 1884
	 */
	total_len = SizeOfXLogRecord + record->xl_len;
	for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
	{
		if (!(record->xl_info & XLR_SET_BKP_BLOCK(i)))
			continue;
		total_len += sizeof(BkpBlock) + BLCKSZ;
	}
B
Bruce Momjian 已提交
1885

T
Tom Lane 已提交
1886 1887 1888 1889 1890 1891
	/*
	 * Make sure it will fit in buffer (currently, it is mechanically
	 * impossible for this test to fail, but it seems like a good idea
	 * anyway).
	 */
	if (total_len > _INTL_MAXLOGRECSZ)
1892
	{
1893
		elog(emode, "ReadRecord: record length %u at %X/%X too long",
T
Tom Lane 已提交
1894
			 total_len, RecPtr->xlogid, RecPtr->xrecoff);
1895 1896 1897 1898
		goto next_record_is_invalid;
	}
	if (record->xl_rmid > RM_MAX_ID)
	{
1899
		elog(emode, "ReadRecord: invalid resource manager id %u at %X/%X",
1900
			 record->xl_rmid, RecPtr->xlogid, RecPtr->xrecoff);
1901 1902 1903
		goto next_record_is_invalid;
	}
	nextRecord = NULL;
T
Tom Lane 已提交
1904 1905
	len = BLCKSZ - RecPtr->xrecoff % BLCKSZ;
	if (total_len > len)
1906
	{
T
Tom Lane 已提交
1907 1908
		/* Need to reassemble record */
		XLogContRecord *contrecord;
B
Bruce Momjian 已提交
1909
		uint32		gotlen = len;
1910

T
Tom Lane 已提交
1911
		memcpy(buffer, record, len);
1912
		record = (XLogRecord *) buffer;
T
Tom Lane 已提交
1913
		buffer += len;
1914
		for (;;)
1915
		{
T
Tom Lane 已提交
1916 1917
			readOff += BLCKSZ;
			if (readOff >= XLogSegSize)
1918 1919
			{
				close(readFile);
T
Tom Lane 已提交
1920 1921 1922
				readFile = -1;
				NextLogSeg(readId, readSeg);
				readFile = XLogFileOpen(readId, readSeg, (emode == LOG));
1923 1924
				if (readFile < 0)
					goto next_record_is_invalid;
T
Tom Lane 已提交
1925
				readOff = 0;
1926 1927
			}
			if (read(readFile, readBuf, BLCKSZ) != BLCKSZ)
T
Tom Lane 已提交
1928
			{
1929
				elog(emode, "ReadRecord: read of log file %u, segment %u, offset %u failed: %m",
1930
					 readId, readSeg, readOff);
T
Tom Lane 已提交
1931 1932
				goto next_record_is_invalid;
			}
1933
			if (!ValidXLOGHeader((XLogPageHeader) readBuf, emode, true))
1934
				goto next_record_is_invalid;
T
Tom Lane 已提交
1935
			if (!(((XLogPageHeader) readBuf)->xlp_info & XLP_FIRST_IS_CONTRECORD))
1936
			{
1937
				elog(emode, "ReadRecord: there is no ContRecord flag in log file %u, segment %u, offset %u",
1938
					 readId, readSeg, readOff);
1939 1940
				goto next_record_is_invalid;
			}
T
Tom Lane 已提交
1941
			contrecord = (XLogContRecord *) ((char *) readBuf + SizeOfXLogPHD);
B
Bruce Momjian 已提交
1942
			if (contrecord->xl_rem_len == 0 ||
T
Tom Lane 已提交
1943
				total_len != (contrecord->xl_rem_len + gotlen))
1944
			{
1945
				elog(emode, "ReadRecord: invalid ContRecord length %u in log file %u, segment %u, offset %u",
T
Tom Lane 已提交
1946
					 contrecord->xl_rem_len, readId, readSeg, readOff);
1947 1948
				goto next_record_is_invalid;
			}
T
Tom Lane 已提交
1949 1950
			len = BLCKSZ - SizeOfXLogPHD - SizeOfXLogContRecord;
			if (contrecord->xl_rem_len > len)
1951
			{
B
Bruce Momjian 已提交
1952
				memcpy(buffer, (char *) contrecord + SizeOfXLogContRecord, len);
T
Tom Lane 已提交
1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965
				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;
		if (BLCKSZ - SizeOfXLogRecord >= SizeOfXLogPHD +
			SizeOfXLogContRecord + MAXALIGN(contrecord->xl_rem_len))
		{
B
Bruce Momjian 已提交
1966
			nextRecord = (XLogRecord *) ((char *) contrecord +
T
Tom Lane 已提交
1967 1968 1969 1970
				SizeOfXLogContRecord + MAXALIGN(contrecord->xl_rem_len));
		}
		EndRecPtr.xlogid = readId;
		EndRecPtr.xrecoff = readSeg * XLogSegSize + readOff +
B
Bruce Momjian 已提交
1971
			SizeOfXLogPHD + SizeOfXLogContRecord +
T
Tom Lane 已提交
1972 1973 1974
			MAXALIGN(contrecord->xl_rem_len);
		ReadRecPtr = *RecPtr;
		return record;
1975 1976
	}

T
Tom Lane 已提交
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
	/* Record does not cross a page boundary */
	if (!RecordIsValid(record, *RecPtr, emode))
		goto next_record_is_invalid;
	if (BLCKSZ - SizeOfXLogRecord >= RecPtr->xrecoff % BLCKSZ +
		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);
	return (XLogRecord *) buffer;
1988

T
Tom Lane 已提交
1989 1990 1991 1992 1993
next_record_is_invalid:;
	close(readFile);
	readFile = -1;
	nextRecord = NULL;
	return NULL;
1994 1995
}

1996 1997 1998 1999
/*
 * 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 已提交
2000
 * ReadRecord.	It's not intended for use from anywhere else.
2001 2002 2003 2004
 */
static bool
ValidXLOGHeader(XLogPageHeader hdr, int emode, bool checkSUI)
{
2005 2006
	XLogRecPtr	recaddr;

2007 2008
	if (hdr->xlp_magic != XLOG_PAGE_MAGIC)
	{
2009
		elog(emode, "ReadRecord: invalid magic number %04X in log file %u, segment %u, offset %u",
2010 2011 2012 2013 2014
			 hdr->xlp_magic, readId, readSeg, readOff);
		return false;
	}
	if ((hdr->xlp_info & ~XLP_ALL_FLAGS) != 0)
	{
2015
		elog(emode, "ReadRecord: invalid info bits %04X in log file %u, segment %u, offset %u",
2016 2017 2018
			 hdr->xlp_info, readId, readSeg, readOff);
		return false;
	}
2019 2020 2021 2022
	recaddr.xlogid = readId;
	recaddr.xrecoff = readSeg * XLogSegSize + readOff;
	if (!XLByteEQ(hdr->xlp_pageaddr, recaddr))
	{
2023
		elog(emode, "ReadRecord: unexpected pageaddr %X/%X in log file %u, segment %u, offset %u",
2024 2025 2026 2027
			 hdr->xlp_pageaddr.xlogid, hdr->xlp_pageaddr.xrecoff,
			 readId, readSeg, readOff);
		return false;
	}
B
Bruce Momjian 已提交
2028

2029
	/*
B
Bruce Momjian 已提交
2030 2031 2032 2033
	 * We disbelieve a SUI less than the previous page's SUI, or more than
	 * a few counts greater.  In theory as many as 512 shutdown checkpoint
	 * records could appear on a 32K-sized xlog page, so that's the most
	 * differential there could legitimately be.
2034 2035
	 *
	 * Note this check can only be applied when we are reading the next page
B
Bruce Momjian 已提交
2036 2037
	 * in sequence, so ReadRecord passes a flag indicating whether to
	 * check.
2038 2039 2040 2041 2042 2043
	 */
	if (checkSUI)
	{
		if (hdr->xlp_sui < lastReadSUI ||
			hdr->xlp_sui > lastReadSUI + 512)
		{
2044 2045
			/* translator: SUI = startup id */
			elog(emode, "ReadRecord: out-of-sequence SUI %u (after %u) in log file %u, segment %u, offset %u",
2046 2047 2048 2049 2050 2051 2052 2053
				 hdr->xlp_sui, lastReadSUI, readId, readSeg, readOff);
			return false;
		}
	}
	lastReadSUI = hdr->xlp_sui;
	return true;
}

2054 2055 2056 2057
/*
 * I/O routines for pg_control
 *
 * *ControlFile is a buffer in shared memory that holds an image of the
B
Bruce Momjian 已提交
2058
 * contents of pg_control.	WriteControlFile() initializes pg_control
2059 2060 2061 2062 2063 2064 2065 2066 2067 2068
 * 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.
 */

2069 2070 2071
void
SetXLogDir(char *path)
{
2072 2073
	char *xsubdir = "/pg_xlog";

2074 2075
	if (path != NULL)
	{
2076
		XLogDir = malloc(strlen(path)+1);
2077 2078 2079 2080
		strcpy(XLogDir, path);
	}
	else
	{
2081 2082
		XLogDir = malloc(strlen(DataDir)+strlen(xsubdir)+1);
		snprintf(XLogDir, MAXPGPATH, "%s%s", DataDir, xsubdir);
2083 2084 2085
	}
}

2086 2087 2088 2089
void
XLOGPathInit(void)
{
	/* Init XLOG file paths */
2090
	if (XLogDir == NULL)
2091
		SetXLogDir(NULL);
2092
	snprintf(ControlFilePath, MAXPGPATH, "%s/global/pg_control", DataDir);
2093 2094 2095 2096 2097 2098
}

static void
WriteControlFile(void)
{
	int			fd;
B
Bruce Momjian 已提交
2099
	char		buffer[BLCKSZ]; /* need not be aligned */
2100 2101 2102
	char	   *localeptr;

	/*
T
Tom Lane 已提交
2103
	 * Initialize version and compatibility-check fields
2104
	 */
T
Tom Lane 已提交
2105 2106
	ControlFile->pg_control_version = PG_CONTROL_VERSION;
	ControlFile->catalog_version_no = CATALOG_VERSION_NO;
2107 2108
	ControlFile->blcksz = BLCKSZ;
	ControlFile->relseg_size = RELSEG_SIZE;
2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119

	ControlFile->nameDataLen = NAMEDATALEN;
	ControlFile->funcMaxArgs = FUNC_MAX_ARGS;

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

	ControlFile->localeBuflen = LOCALE_NAME_BUFLEN;
2120 2121
	localeptr = setlocale(LC_COLLATE, NULL);
	if (!localeptr)
2122
		elog(PANIC, "invalid LC_COLLATE setting");
2123 2124 2125
	StrNCpy(ControlFile->lc_collate, localeptr, LOCALE_NAME_BUFLEN);
	localeptr = setlocale(LC_CTYPE, NULL);
	if (!localeptr)
2126
		elog(PANIC, "invalid LC_CTYPE setting");
2127
	StrNCpy(ControlFile->lc_ctype, localeptr, LOCALE_NAME_BUFLEN);
B
Bruce Momjian 已提交
2128

T
Tom Lane 已提交
2129 2130
	/* Contents are protected with a CRC */
	INIT_CRC64(ControlFile->crc);
B
Bruce Momjian 已提交
2131 2132
	COMP_CRC64(ControlFile->crc,
			   (char *) ControlFile + sizeof(crc64),
T
Tom Lane 已提交
2133 2134 2135
			   sizeof(ControlFileData) - sizeof(crc64));
	FIN_CRC64(ControlFile->crc);

2136
	/*
B
Bruce Momjian 已提交
2137 2138 2139 2140 2141
	 * We write out BLCKSZ 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".
2142 2143
	 */
	if (sizeof(ControlFileData) > BLCKSZ)
2144
		elog(PANIC, "sizeof(ControlFileData) is larger than BLCKSZ; fix either one");
2145

2146 2147 2148
	memset(buffer, 0, BLCKSZ);
	memcpy(buffer, ControlFile, sizeof(ControlFileData));

2149 2150
	fd = BasicOpenFile(ControlFilePath, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
					   S_IRUSR | S_IWUSR);
2151
	if (fd < 0)
2152
		elog(PANIC, "WriteControlFile: could not create control file (%s): %m",
2153 2154
			 ControlFilePath);

2155
	errno = 0;
2156
	if (write(fd, buffer, BLCKSZ) != BLCKSZ)
2157 2158 2159 2160
	{
		/* if write didn't set errno, assume problem is no disk space */
		if (errno == 0)
			errno = ENOSPC;
2161
		elog(PANIC, "WriteControlFile: write to control file failed: %m");
2162
	}
2163

2164
	if (pg_fsync(fd) != 0)
2165
		elog(PANIC, "WriteControlFile: fsync of control file failed: %m");
2166 2167 2168 2169 2170 2171 2172

	close(fd);
}

static void
ReadControlFile(void)
{
2173
	crc64		crc;
2174 2175 2176 2177 2178 2179 2180
	int			fd;

	/*
	 * Read data...
	 */
	fd = BasicOpenFile(ControlFilePath, O_RDWR | PG_BINARY, S_IRUSR | S_IWUSR);
	if (fd < 0)
2181
		elog(PANIC, "could not open control file (%s): %m", ControlFilePath);
2182 2183

	if (read(fd, ControlFile, sizeof(ControlFileData)) != sizeof(ControlFileData))
2184
		elog(PANIC, "read from control file failed: %m");
2185 2186 2187

	close(fd);

T
Tom Lane 已提交
2188 2189 2190 2191 2192 2193 2194
	/*
	 * 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.
	 */
	if (ControlFile->pg_control_version != PG_CONTROL_VERSION)
2195
		elog(PANIC,
2196 2197 2198
			 "The database cluster was initialized with PG_CONTROL_VERSION %d,\n"
			 "\tbut the server was compiled with PG_CONTROL_VERSION %d.\n"
			 "\tIt looks like you need to initdb.",
T
Tom Lane 已提交
2199 2200 2201
			 ControlFile->pg_control_version, PG_CONTROL_VERSION);

	/* Now check the CRC. */
2202
	INIT_CRC64(crc);
B
Bruce Momjian 已提交
2203 2204
	COMP_CRC64(crc,
			   (char *) ControlFile + sizeof(crc64),
T
Tom Lane 已提交
2205
			   sizeof(ControlFileData) - sizeof(crc64));
2206 2207
	FIN_CRC64(crc);

T
Tom Lane 已提交
2208
	if (!EQ_CRC64(crc, ControlFile->crc))
2209
		elog(PANIC, "invalid checksum in control file");
2210

2211
	/*
B
Bruce Momjian 已提交
2212 2213
	 * Do compatibility checking immediately.  We do this here for 2
	 * reasons:
2214
	 *
B
Bruce Momjian 已提交
2215 2216
	 * (1) if the database isn't compatible with the backend executable, we
	 * want to abort before we can possibly do any damage;
2217 2218 2219
	 *
	 * (2) this code is executed in the postmaster, so the setlocale() will
	 * propagate to forked backends, which aren't going to read this file
B
Bruce Momjian 已提交
2220
	 * for themselves.	(These locale settings are considered critical
2221 2222
	 * compatibility items because they can affect sort order of indexes.)
	 */
T
Tom Lane 已提交
2223
	if (ControlFile->catalog_version_no != CATALOG_VERSION_NO)
2224
		elog(PANIC,
2225
			 "The database cluster was initialized with CATALOG_VERSION_NO %d,\n"
2226
			 "\tbut the backend was compiled with CATALOG_VERSION_NO %d.\n"
2227
			 "\tIt looks like you need to initdb.",
T
Tom Lane 已提交
2228
			 ControlFile->catalog_version_no, CATALOG_VERSION_NO);
2229
	if (ControlFile->blcksz != BLCKSZ)
2230
		elog(PANIC,
2231 2232 2233
			 "The database cluster was initialized with BLCKSZ %d,\n"
			 "\tbut the backend was compiled with BLCKSZ %d.\n"
			 "\tIt looks like you need to initdb.",
2234 2235
			 ControlFile->blcksz, BLCKSZ);
	if (ControlFile->relseg_size != RELSEG_SIZE)
2236
		elog(PANIC,
2237 2238
			 "The database cluster was initialized with RELSEG_SIZE %d,\n"
			 "\tbut the backend was compiled with RELSEG_SIZE %d.\n"
2239
			 "\tIt looks like you need to recompile or initdb.",
2240
			 ControlFile->relseg_size, RELSEG_SIZE);
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276

	if (ControlFile->nameDataLen != NAMEDATALEN)
		elog(PANIC,
			 "The database cluster was initialized with NAMEDATALEN %d,\n"
			 "\tbut the backend was compiled with NAMEDATALEN %d.\n"
			 "\tIt looks like you need to recompile or initdb.",
			 ControlFile->nameDataLen, NAMEDATALEN);

	if (ControlFile->funcMaxArgs != FUNC_MAX_ARGS)
		elog(PANIC,
			 "The database cluster was initialized with FUNC_MAX_ARGS %d,\n"
			 "\tbut the backend was compiled with FUNC_MAX_ARGS %d.\n"
			 "\tIt looks like you need to recompile or initdb.",
			 ControlFile->funcMaxArgs, FUNC_MAX_ARGS);

#ifdef HAVE_INT64_TIMESTAMP
	if (ControlFile->enableIntTimes != TRUE)
		elog(PANIC,
			 "The database cluster was initialized without HAVE_INT64_TIMESTAMP\n"
			 "\tbut the backend was compiled with HAVE_INT64_TIMESTAMP.\n"
			 "\tIt looks like you need to recompile or initdb.");
#else
	if (ControlFile->enableIntTimes != FALSE)
		elog(PANIC,
			 "The database cluster was initialized with HAVE_INT64_TIMESTAMP\n"
			 "\tbut the backend was compiled without HAVE_INT64_TIMESTAMP.\n"
			 "\tIt looks like you need to recompile or initdb.");
#endif

	if (ControlFile->localeBuflen != LOCALE_NAME_BUFLEN)
		elog(PANIC,
			 "The database cluster was initialized with LOCALE_NAME_BUFLEN %d,\n"
			 "\tbut the backend was compiled with LOCALE_NAME_BUFLEN %d.\n"
			 "\tIt looks like you need to initdb.",
			 ControlFile->localeBuflen, LOCALE_NAME_BUFLEN);

2277
	if (setlocale(LC_COLLATE, ControlFile->lc_collate) == NULL)
2278
		elog(PANIC,
2279
			 "The database cluster was initialized with LC_COLLATE '%s',\n"
2280 2281
			 "\twhich is not recognized by setlocale().\n"
			 "\tIt looks like you need to initdb.",
2282 2283
			 ControlFile->lc_collate);
	if (setlocale(LC_CTYPE, ControlFile->lc_ctype) == NULL)
2284
		elog(PANIC,
2285 2286 2287
			 "The database cluster was initialized with LC_CTYPE '%s',\n"
			 "\twhich is not recognized by setlocale().\n"
			 "\tIt looks like you need to initdb.",
2288 2289 2290
			 ControlFile->lc_ctype);
}

2291
void
2292
UpdateControlFile(void)
2293
{
2294
	int			fd;
2295

2296
	INIT_CRC64(ControlFile->crc);
B
Bruce Momjian 已提交
2297 2298
	COMP_CRC64(ControlFile->crc,
			   (char *) ControlFile + sizeof(crc64),
T
Tom Lane 已提交
2299
			   sizeof(ControlFileData) - sizeof(crc64));
2300 2301
	FIN_CRC64(ControlFile->crc);

2302
	fd = BasicOpenFile(ControlFilePath, O_RDWR | PG_BINARY, S_IRUSR | S_IWUSR);
2303
	if (fd < 0)
2304
		elog(PANIC, "could not open control file (%s): %m", ControlFilePath);
2305

2306
	errno = 0;
2307
	if (write(fd, ControlFile, sizeof(ControlFileData)) != sizeof(ControlFileData))
2308 2309 2310 2311
	{
		/* if write didn't set errno, assume problem is no disk space */
		if (errno == 0)
			errno = ENOSPC;
2312
		elog(PANIC, "write to control file failed: %m");
2313
	}
2314

2315
	if (pg_fsync(fd) != 0)
2316
		elog(PANIC, "fsync of control file failed: %m");
2317 2318 2319 2320

	close(fd);
}

2321
/*
T
Tom Lane 已提交
2322
 * Initialization of shared memory for XLOG
2323 2324
 */

2325
int
2326
XLOGShmemSize(void)
2327 2328 2329 2330
{
	if (XLOGbuffers < MinXLOGbuffers)
		XLOGbuffers = MinXLOGbuffers;

T
Tom Lane 已提交
2331 2332 2333
	return MAXALIGN(sizeof(XLogCtlData) + sizeof(XLogRecPtr) * XLOGbuffers)
		+ BLCKSZ * XLOGbuffers +
		MAXALIGN(sizeof(ControlFileData));
2334 2335 2336 2337 2338
}

void
XLOGShmemInit(void)
{
2339
	bool		found;
2340

2341
	/* this must agree with space requested by XLOGShmemSize() */
2342 2343 2344
	if (XLOGbuffers < MinXLOGbuffers)
		XLOGbuffers = MinXLOGbuffers;

2345
	XLogCtl = (XLogCtlData *)
T
Tom Lane 已提交
2346 2347 2348 2349 2350
		ShmemInitStruct("XLOG Ctl",
						MAXALIGN(sizeof(XLogCtlData) +
								 sizeof(XLogRecPtr) * XLOGbuffers)
						+ BLCKSZ * XLOGbuffers,
						&found);
2351
	Assert(!found);
2352 2353 2354 2355
	ControlFile = (ControlFileData *)
		ShmemInitStruct("Control File", sizeof(ControlFileData), &found);
	Assert(!found);

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

T
Tom Lane 已提交
2358 2359 2360 2361 2362 2363 2364 2365
	/*
	 * Since XLogCtlData contains XLogRecPtr fields, its sizeof should be
	 * a multiple of the alignment for same, so no extra alignment padding
	 * is needed here.
	 */
	XLogCtl->xlblocks = (XLogRecPtr *)
		(((char *) XLogCtl) + sizeof(XLogCtlData));
	memset(XLogCtl->xlblocks, 0, sizeof(XLogRecPtr) * XLOGbuffers);
B
Bruce Momjian 已提交
2366

T
Tom Lane 已提交
2367
	/*
B
Bruce Momjian 已提交
2368 2369
	 * Here, on the other hand, we must MAXALIGN to ensure the page
	 * buffers have worst-case alignment.
T
Tom Lane 已提交
2370 2371 2372 2373 2374 2375 2376
	 */
	XLogCtl->pages =
		((char *) XLogCtl) + MAXALIGN(sizeof(XLogCtlData) +
									  sizeof(XLogRecPtr) * XLOGbuffers);
	memset(XLogCtl->pages, 0, BLCKSZ * XLOGbuffers);

	/*
B
Bruce Momjian 已提交
2377 2378
	 * Do basic initialization of XLogCtl shared data. (StartupXLOG will
	 * fill in additional info.)
T
Tom Lane 已提交
2379 2380 2381 2382
	 */
	XLogCtl->XLogCacheByte = BLCKSZ * XLOGbuffers;
	XLogCtl->XLogCacheBlck = XLOGbuffers - 1;
	XLogCtl->Insert.currpage = (XLogPageHeader) (XLogCtl->pages);
2383
	SpinLockInit(&XLogCtl->info_lck);
T
Tom Lane 已提交
2384

2385 2386 2387 2388 2389 2390 2391
	/*
	 * 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).
	 */
	if (!IsBootstrapProcessingMode())
		ReadControlFile();
2392 2393 2394
}

/*
T
Tom Lane 已提交
2395 2396
 * This func must be called ONCE on system install.  It creates pg_control
 * and the initial XLOG segment.
2397 2398
 */
void
T
Tom Lane 已提交
2399
BootStrapXLOG(void)
2400
{
2401
	CheckPoint	checkPoint;
T
Tom Lane 已提交
2402 2403
	char	   *buffer;
	XLogPageHeader page;
2404
	XLogRecord *record;
B
Bruce Momjian 已提交
2405
	bool		use_existent;
2406
	crc64		crc;
2407

T
Tom Lane 已提交
2408 2409 2410 2411
	/* Use malloc() to ensure buffer is MAXALIGNED */
	buffer = (char *) malloc(BLCKSZ);
	page = (XLogPageHeader) buffer;

2412 2413 2414
	checkPoint.redo.xlogid = 0;
	checkPoint.redo.xrecoff = SizeOfXLogPHD;
	checkPoint.undo = checkPoint.redo;
T
Tom Lane 已提交
2415
	checkPoint.ThisStartUpID = 0;
2416
	checkPoint.nextXid = FirstNormalTransactionId;
2417
	checkPoint.nextOid = BootstrapObjectIdData;
T
Tom Lane 已提交
2418
	checkPoint.time = time(NULL);
2419

2420 2421 2422 2423
	ShmemVariableCache->nextXid = checkPoint.nextXid;
	ShmemVariableCache->nextOid = checkPoint.nextOid;
	ShmemVariableCache->oidCount = 0;

2424 2425 2426
	memset(buffer, 0, BLCKSZ);
	page->xlp_magic = XLOG_PAGE_MAGIC;
	page->xlp_info = 0;
2427
	page->xlp_sui = checkPoint.ThisStartUpID;
2428 2429
	page->xlp_pageaddr.xlogid = 0;
	page->xlp_pageaddr.xrecoff = 0;
2430 2431 2432
	record = (XLogRecord *) ((char *) page + SizeOfXLogPHD);
	record->xl_prev.xlogid = 0;
	record->xl_prev.xrecoff = 0;
2433 2434 2435
	record->xl_xact_prev = record->xl_prev;
	record->xl_xid = InvalidTransactionId;
	record->xl_len = sizeof(checkPoint);
T
Tom Lane 已提交
2436
	record->xl_info = XLOG_CHECKPOINT_SHUTDOWN;
2437
	record->xl_rmid = RM_XLOG_ID;
T
Tom Lane 已提交
2438
	memcpy(XLogRecGetData(record), &checkPoint, sizeof(checkPoint));
2439

2440
	INIT_CRC64(crc);
T
Tom Lane 已提交
2441
	COMP_CRC64(crc, &checkPoint, sizeof(checkPoint));
B
Bruce Momjian 已提交
2442
	COMP_CRC64(crc, (char *) record + sizeof(crc64),
T
Tom Lane 已提交
2443
			   SizeOfXLogRecord - sizeof(crc64));
2444 2445 2446
	FIN_CRC64(crc);
	record->xl_crc = crc;

2447 2448
	use_existent = false;
	openLogFile = XLogFileInit(0, 0, &use_existent, false);
2449

2450
	errno = 0;
T
Tom Lane 已提交
2451
	if (write(openLogFile, buffer, BLCKSZ) != BLCKSZ)
2452 2453 2454 2455
	{
		/* if write didn't set errno, assume problem is no disk space */
		if (errno == 0)
			errno = ENOSPC;
2456
		elog(PANIC, "BootStrapXLOG failed to write log file: %m");
2457
	}
2458

T
Tom Lane 已提交
2459
	if (pg_fsync(openLogFile) != 0)
2460
		elog(PANIC, "BootStrapXLOG failed to fsync log file: %m");
2461

T
Tom Lane 已提交
2462 2463
	close(openLogFile);
	openLogFile = -1;
2464

2465
	memset(ControlFile, 0, sizeof(ControlFileData));
T
Tom Lane 已提交
2466 2467 2468
	/* Initialize pg_control status fields */
	ControlFile->state = DB_SHUTDOWNED;
	ControlFile->time = checkPoint.time;
2469 2470 2471
	ControlFile->logId = 0;
	ControlFile->logSeg = 1;
	ControlFile->checkPoint = checkPoint.redo;
T
Tom Lane 已提交
2472
	ControlFile->checkPointCopy = checkPoint;
2473
	/* some additional ControlFile fields are set in WriteControlFile() */
2474

2475
	WriteControlFile();
2476 2477 2478

	/* Bootstrap the commit log, too */
	BootStrapCLOG();
2479 2480
}

2481
static char *
2482 2483
str_time(time_t tnow)
{
T
Tom Lane 已提交
2484
	static char buf[32];
2485

2486
	strftime(buf, sizeof(buf),
T
Tom Lane 已提交
2487
			 "%Y-%m-%d %H:%M:%S %Z",
2488
			 localtime(&tnow));
2489

2490
	return buf;
2491 2492 2493
}

/*
T
Tom Lane 已提交
2494
 * This must be called ONCE during postmaster or standalone-backend startup
2495 2496
 */
void
T
Tom Lane 已提交
2497
StartupXLOG(void)
2498
{
2499 2500
	XLogCtlInsert *Insert;
	CheckPoint	checkPoint;
T
Tom Lane 已提交
2501
	bool		wasShutdown;
2502
	XLogRecPtr	RecPtr,
T
Tom Lane 已提交
2503 2504 2505
				LastRec,
				checkPointLoc,
				EndOfLog;
2506
	XLogRecord *record;
T
Tom Lane 已提交
2507
	char	   *buffer;
2508

T
Tom Lane 已提交
2509 2510
	/* Use malloc() to ensure record buffer is MAXALIGNED */
	buffer = (char *) malloc(_INTL_MAXLOGRECSZ);
2511

T
Tom Lane 已提交
2512
	CritSectionCount++;
2513 2514

	/*
2515 2516
	 * Read control file and check XLOG status looks valid.
	 *
B
Bruce Momjian 已提交
2517 2518
	 * 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.
2519
	 */
2520
	ReadControlFile();
2521

2522 2523 2524
	if (ControlFile->logSeg == 0 ||
		ControlFile->state < DB_SHUTDOWNED ||
		ControlFile->state > DB_IN_PRODUCTION ||
2525
		!XRecOffIsValid(ControlFile->checkPoint.xrecoff))
2526
		elog(PANIC, "control file context is broken");
2527 2528

	if (ControlFile->state == DB_SHUTDOWNED)
2529
		elog(LOG, "database system was shut down at %s",
2530
			 str_time(ControlFile->time));
2531
	else if (ControlFile->state == DB_SHUTDOWNING)
2532
		elog(LOG, "database system shutdown was interrupted at %s",
2533
			 str_time(ControlFile->time));
2534
	else if (ControlFile->state == DB_IN_RECOVERY)
2535
		elog(LOG, "database system was interrupted being in recovery at %s\n"
T
Tom Lane 已提交
2536
			 "\tThis probably means that some data blocks are corrupted\n"
2537
			 "\tand you will have to use the last backup for recovery.",
2538
			 str_time(ControlFile->time));
2539
	else if (ControlFile->state == DB_IN_PRODUCTION)
2540
		elog(LOG, "database system was interrupted at %s",
2541
			 str_time(ControlFile->time));
2542

T
Tom Lane 已提交
2543 2544 2545 2546
	/*
	 * Get the last valid checkpoint record.  If the latest one according
	 * to pg_control is broken, try the next-to-last one.
	 */
2547
	record = ReadCheckpointRecord(ControlFile->checkPoint, 1, buffer);
T
Tom Lane 已提交
2548 2549 2550
	if (record != NULL)
	{
		checkPointLoc = ControlFile->checkPoint;
2551
		elog(LOG, "checkpoint record is at %X/%X",
T
Tom Lane 已提交
2552 2553 2554 2555
			 checkPointLoc.xlogid, checkPointLoc.xrecoff);
	}
	else
	{
2556
		record = ReadCheckpointRecord(ControlFile->prevCheckPoint, 2, buffer);
T
Tom Lane 已提交
2557 2558 2559
		if (record != NULL)
		{
			checkPointLoc = ControlFile->prevCheckPoint;
2560
			elog(LOG, "using previous checkpoint record at %X/%X",
T
Tom Lane 已提交
2561 2562 2563 2564
				 checkPointLoc.xlogid, checkPointLoc.xrecoff);
			InRecovery = true;	/* force recovery even if SHUTDOWNED */
		}
		else
2565
			elog(PANIC, "unable to locate a valid checkpoint record");
T
Tom Lane 已提交
2566 2567 2568 2569
	}
	LastRec = RecPtr = checkPointLoc;
	memcpy(&checkPoint, XLogRecGetData(record), sizeof(CheckPoint));
	wasShutdown = (record->xl_info == XLOG_CHECKPOINT_SHUTDOWN);
2570

2571
	elog(LOG, "redo record is at %X/%X; undo record is at %X/%X; shutdown %s",
2572
		 checkPoint.redo.xlogid, checkPoint.redo.xrecoff,
V
Vadim B. Mikheev 已提交
2573
		 checkPoint.undo.xlogid, checkPoint.undo.xrecoff,
T
Tom Lane 已提交
2574
		 wasShutdown ? "TRUE" : "FALSE");
2575
	elog(LOG, "next transaction id: %u; next oid: %u",
2576
		 checkPoint.nextXid, checkPoint.nextOid);
2577
	if (!TransactionIdIsNormal(checkPoint.nextXid))
2578
		elog(PANIC, "invalid next transaction id");
2579 2580 2581

	ShmemVariableCache->nextXid = checkPoint.nextXid;
	ShmemVariableCache->nextOid = checkPoint.nextOid;
2582
	ShmemVariableCache->oidCount = 0;
2583

V
WAL  
Vadim B. Mikheev 已提交
2584
	ThisStartUpID = checkPoint.ThisStartUpID;
B
Bruce Momjian 已提交
2585
	RedoRecPtr = XLogCtl->Insert.RedoRecPtr =
2586
		XLogCtl->SavedRedoRecPtr = checkPoint.redo;
V
WAL  
Vadim B. Mikheev 已提交
2587

2588
	if (XLByteLT(RecPtr, checkPoint.redo))
2589
		elog(PANIC, "invalid redo in checkpoint record");
2590 2591 2592
	if (checkPoint.undo.xrecoff == 0)
		checkPoint.undo = RecPtr;

B
Bruce Momjian 已提交
2593
	if (XLByteLT(checkPoint.undo, RecPtr) ||
V
Vadim B. Mikheev 已提交
2594
		XLByteLT(checkPoint.redo, RecPtr))
2595
	{
T
Tom Lane 已提交
2596
		if (wasShutdown)
2597
			elog(PANIC, "invalid redo/undo record in shutdown checkpoint");
V
WAL  
Vadim B. Mikheev 已提交
2598
		InRecovery = true;
2599 2600
	}
	else if (ControlFile->state != DB_SHUTDOWNED)
V
WAL  
Vadim B. Mikheev 已提交
2601
		InRecovery = true;
2602

V
WAL  
Vadim B. Mikheev 已提交
2603 2604
	/* REDO */
	if (InRecovery)
2605
	{
2606
		elog(LOG, "database system was not properly shut down; "
2607
			 "automatic recovery in progress");
2608 2609 2610 2611
		ControlFile->state = DB_IN_RECOVERY;
		ControlFile->time = time(NULL);
		UpdateControlFile();

V
WAL  
Vadim B. Mikheev 已提交
2612
		XLogInitRelationCache();
V
Vadim B. Mikheev 已提交
2613

2614 2615
		/* Is REDO required ? */
		if (XLByteLT(checkPoint.redo, RecPtr))
2616
			record = ReadRecord(&(checkPoint.redo), PANIC, buffer);
B
Bruce Momjian 已提交
2617
		else
2618 2619
		{
			/* read past CheckPoint record */
T
Tom Lane 已提交
2620
			record = ReadRecord(NULL, LOG, buffer);
2621
		}
2622

T
Tom Lane 已提交
2623
		if (record != NULL)
2624
		{
V
WAL  
Vadim B. Mikheev 已提交
2625
			InRedo = true;
2626
			elog(LOG, "redo starts at %X/%X",
2627
				 ReadRecPtr.xlogid, ReadRecPtr.xrecoff);
2628 2629
			do
			{
2630 2631
				/* nextXid must be beyond record's xid */
				if (TransactionIdFollowsOrEquals(record->xl_xid,
2632
											ShmemVariableCache->nextXid))
2633 2634 2635 2636
				{
					ShmemVariableCache->nextXid = record->xl_xid;
					TransactionIdAdvance(ShmemVariableCache->nextXid);
				}
V
WAL  
Vadim B. Mikheev 已提交
2637 2638
				if (XLOG_DEBUG)
				{
B
Bruce Momjian 已提交
2639
					char		buf[8192];
V
WAL  
Vadim B. Mikheev 已提交
2640

2641
					sprintf(buf, "REDO @ %X/%X; LSN %X/%X: ",
B
Bruce Momjian 已提交
2642 2643
							ReadRecPtr.xlogid, ReadRecPtr.xrecoff,
							EndRecPtr.xlogid, EndRecPtr.xrecoff);
V
WAL  
Vadim B. Mikheev 已提交
2644 2645
					xlog_outrec(buf, record);
					strcat(buf, " - ");
B
Bruce Momjian 已提交
2646 2647
					RmgrTable[record->xl_rmid].rm_desc(buf,
								record->xl_info, XLogRecGetData(record));
2648
					elog(LOG, "%s", buf);
V
WAL  
Vadim B. Mikheev 已提交
2649 2650
				}

T
Tom Lane 已提交
2651
				if (record->xl_info & XLR_BKP_BLOCK_MASK)
2652 2653
					RestoreBkpBlocks(record, EndRecPtr);

2654
				RmgrTable[record->xl_rmid].rm_redo(EndRecPtr, record);
T
Tom Lane 已提交
2655 2656
				record = ReadRecord(NULL, LOG, buffer);
			} while (record != NULL);
2657
			elog(LOG, "redo done at %X/%X",
2658
				 ReadRecPtr.xlogid, ReadRecPtr.xrecoff);
2659
			LastRec = ReadRecPtr;
V
WAL  
Vadim B. Mikheev 已提交
2660
			InRedo = false;
2661 2662
		}
		else
2663
			elog(LOG, "redo is not required");
V
WAL  
Vadim B. Mikheev 已提交
2664 2665
	}

T
Tom Lane 已提交
2666 2667 2668 2669
	/*
	 * Init xlog buffer cache using the block containing the last valid
	 * record from the previous incarnation.
	 */
2670
	record = ReadRecord(&LastRec, PANIC, buffer);
T
Tom Lane 已提交
2671 2672 2673 2674 2675 2676
	EndOfLog = EndRecPtr;
	XLByteToPrevSeg(EndOfLog, openLogId, openLogSeg);
	openLogFile = XLogFileOpen(openLogId, openLogSeg, false);
	openLogOff = 0;
	ControlFile->logId = openLogId;
	ControlFile->logSeg = openLogSeg + 1;
V
WAL  
Vadim B. Mikheev 已提交
2677
	Insert = &XLogCtl->Insert;
2678
	Insert->PrevRecord = LastRec;
B
Bruce Momjian 已提交
2679 2680

	/*
2681 2682
	 * If the next record will go to the new page then initialize for that
	 * one.
T
Tom Lane 已提交
2683
	 */
2684 2685 2686 2687
	if ((BLCKSZ - EndOfLog.xrecoff % BLCKSZ) < SizeOfXLogRecord)
		EndOfLog.xrecoff += (BLCKSZ - EndOfLog.xrecoff % BLCKSZ);
	if (EndOfLog.xrecoff % BLCKSZ == 0)
	{
2688 2689 2690 2691
		XLogRecPtr	NewPageEndPtr;

		NewPageEndPtr = EndOfLog;
		if (NewPageEndPtr.xrecoff >= XLogFileSize)
2692
		{
2693 2694 2695
			/* crossing a logid boundary */
			NewPageEndPtr.xlogid += 1;
			NewPageEndPtr.xrecoff = BLCKSZ;
2696 2697
		}
		else
2698 2699
			NewPageEndPtr.xrecoff += BLCKSZ;
		XLogCtl->xlblocks[0] = NewPageEndPtr;
2700 2701 2702 2703 2704
		Insert->currpage->xlp_magic = XLOG_PAGE_MAGIC;
		if (InRecovery)
			Insert->currpage->xlp_sui = ThisStartUpID;
		else
			Insert->currpage->xlp_sui = ThisStartUpID + 1;
2705 2706
		Insert->currpage->xlp_pageaddr.xlogid = NewPageEndPtr.xlogid;
		Insert->currpage->xlp_pageaddr.xrecoff = NewPageEndPtr.xrecoff - BLCKSZ;
2707
		/* rest of buffer was zeroed in XLOGShmemInit */
2708
		Insert->currpos = (char *) Insert->currpage + SizeOfXLogPHD;
2709 2710 2711 2712 2713 2714
	}
	else
	{
		XLogCtl->xlblocks[0].xlogid = openLogId;
		XLogCtl->xlblocks[0].xrecoff =
			((EndOfLog.xrecoff - 1) / BLCKSZ + 1) * BLCKSZ;
2715

2716 2717
		/*
		 * Tricky point here: readBuf contains the *last* block that the
2718
		 * LastRec record spans, not the one it starts in.	The last block
2719
		 * is indeed the one we want to use.
2720 2721 2722 2723 2724 2725 2726 2727
		 */
		Assert(readOff == (XLogCtl->xlblocks[0].xrecoff - BLCKSZ) % XLogSegSize);
		memcpy((char *) Insert->currpage, readBuf, BLCKSZ);
		Insert->currpos = (char *) Insert->currpage +
			(EndOfLog.xrecoff + BLCKSZ - XLogCtl->xlblocks[0].xrecoff);
		/* Make sure rest of page is zero */
		memset(Insert->currpos, 0, INSERT_FREESPACE(Insert));
	}
V
WAL  
Vadim B. Mikheev 已提交
2728

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

T
Tom Lane 已提交
2731 2732 2733
	XLogCtl->Write.LogwrtResult = LogwrtResult;
	Insert->LogwrtResult = LogwrtResult;
	XLogCtl->LogwrtResult = LogwrtResult;
V
WAL  
Vadim B. Mikheev 已提交
2734

T
Tom Lane 已提交
2735 2736
	XLogCtl->LogwrtRqst.Write = EndOfLog;
	XLogCtl->LogwrtRqst.Flush = EndOfLog;
2737

V
Vadim B. Mikheev 已提交
2738
#ifdef NOT_USED
V
WAL  
Vadim B. Mikheev 已提交
2739 2740 2741
	/* UNDO */
	if (InRecovery)
	{
2742 2743 2744
		RecPtr = ReadRecPtr;
		if (XLByteLT(checkPoint.undo, RecPtr))
		{
2745
			elog(LOG, "undo starts at %X/%X",
2746
				 RecPtr.xlogid, RecPtr.xrecoff);
2747 2748
			do
			{
2749
				record = ReadRecord(&RecPtr, PANIC, buffer);
2750
				if (TransactionIdIsValid(record->xl_xid) &&
2751
					!TransactionIdDidCommit(record->xl_xid))
V
misc  
Vadim B. Mikheev 已提交
2752
					RmgrTable[record->xl_rmid].rm_undo(EndRecPtr, record);
2753 2754
				RecPtr = record->xl_prev;
			} while (XLByteLE(checkPoint.undo, RecPtr));
2755
			elog(LOG, "undo done at %X/%X",
2756
				 ReadRecPtr.xlogid, ReadRecPtr.xrecoff);
2757 2758
		}
		else
2759
			elog(LOG, "undo is not required");
2760
	}
V
WAL  
Vadim B. Mikheev 已提交
2761
#endif
2762

V
WAL  
Vadim B. Mikheev 已提交
2763
	if (InRecovery)
2764
	{
T
Tom Lane 已提交
2765 2766 2767 2768 2769 2770 2771
		/*
		 * In case we had to use the secondary checkpoint, make sure that
		 * it will still be shown as the secondary checkpoint after this
		 * CreateCheckPoint operation; we don't want the broken primary
		 * checkpoint to become prevCheckPoint...
		 */
		ControlFile->checkPoint = checkPointLoc;
2772
		CreateCheckPoint(true);
V
WAL  
Vadim B. Mikheev 已提交
2773
		XLogCloseRelationCache();
2774
	}
2775

T
Tom Lane 已提交
2776 2777 2778 2779
	/*
	 * Preallocate additional log files, if wanted.
	 */
	PreallocXlogFiles(EndOfLog);
2780

V
WAL  
Vadim B. Mikheev 已提交
2781
	InRecovery = false;
2782 2783 2784 2785 2786

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

V
WAL  
Vadim B. Mikheev 已提交
2787 2788 2789
	ThisStartUpID++;
	XLogCtl->ThisStartUpID = ThisStartUpID;

2790 2791 2792
	/* Start up the commit log, too */
	StartupCLOG();

2793
	elog(LOG, "database system is ready");
2794
	CritSectionCount--;
2795

T
Tom Lane 已提交
2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810
	/* Shut down readFile facility, free space */
	if (readFile >= 0)
	{
		close(readFile);
		readFile = -1;
	}
	if (readBuf)
	{
		free(readBuf);
		readBuf = NULL;
	}

	free(buffer);
}

2811 2812 2813 2814
/*
 * Subroutine to try to fetch and validate a prior checkpoint record.
 * whichChkpt = 1 for "primary", 2 for "secondary", merely informative
 */
T
Tom Lane 已提交
2815 2816
static XLogRecord *
ReadCheckpointRecord(XLogRecPtr RecPtr,
2817
					 int whichChkpt,
T
Tom Lane 已提交
2818 2819 2820 2821 2822 2823
					 char *buffer)
{
	XLogRecord *record;

	if (!XRecOffIsValid(RecPtr.xrecoff))
	{
2824 2825 2826
		elog(LOG, (whichChkpt == 1 ?
				   "invalid primary checkpoint link in control file" :
				   "invalid secondary checkpoint link in control file"));
T
Tom Lane 已提交
2827 2828 2829 2830 2831 2832 2833
		return NULL;
	}

	record = ReadRecord(&RecPtr, LOG, buffer);

	if (record == NULL)
	{
2834 2835 2836
		elog(LOG, (whichChkpt == 1 ?
				   "invalid primary checkpoint record" :
				   "invalid secondary checkpoint record"));
T
Tom Lane 已提交
2837 2838 2839 2840
		return NULL;
	}
	if (record->xl_rmid != RM_XLOG_ID)
	{
2841
		elog(LOG, (whichChkpt == 1 ?
2842 2843
			 "invalid resource manager id in primary checkpoint record" :
		  "invalid resource manager id in secondary checkpoint record"));
T
Tom Lane 已提交
2844 2845 2846 2847 2848
		return NULL;
	}
	if (record->xl_info != XLOG_CHECKPOINT_SHUTDOWN &&
		record->xl_info != XLOG_CHECKPOINT_ONLINE)
	{
2849 2850 2851
		elog(LOG, (whichChkpt == 1 ?
				   "invalid xl_info in primary checkpoint record" :
				   "invalid xl_info in secondary checkpoint record"));
T
Tom Lane 已提交
2852 2853 2854 2855
		return NULL;
	}
	if (record->xl_len != sizeof(CheckPoint))
	{
2856 2857 2858
		elog(LOG, (whichChkpt == 1 ?
				   "invalid length of primary checkpoint record" :
				   "invalid length of secondary checkpoint record"));
T
Tom Lane 已提交
2859 2860 2861
		return NULL;
	}
	return record;
2862 2863
}

V
WAL  
Vadim B. Mikheev 已提交
2864
/*
T
Tom Lane 已提交
2865
 * Postmaster uses this to initialize ThisStartUpID & RedoRecPtr from
2866
 * XLogCtlData located in shmem after successful startup.
V
WAL  
Vadim B. Mikheev 已提交
2867 2868 2869 2870 2871
 */
void
SetThisStartUpID(void)
{
	ThisStartUpID = XLogCtl->ThisStartUpID;
2872
	RedoRecPtr = XLogCtl->SavedRedoRecPtr;
2873 2874 2875
}

/*
T
Tom Lane 已提交
2876
 * CheckPoint process called by postmaster saves copy of new RedoRecPtr
2877 2878 2879 2880 2881 2882
 * in shmem (using SetSavedRedoRecPtr).  When checkpointer completes,
 * postmaster calls GetSavedRedoRecPtr to update its own copy of RedoRecPtr,
 * so that subsequently-spawned backends will start out with a reasonably
 * up-to-date local RedoRecPtr.  Since these operations are not protected by
 * any lock and copying an XLogRecPtr isn't atomic, it's unsafe to use either
 * of these routines at other times!
2883 2884
 */
void
2885
SetSavedRedoRecPtr(void)
2886
{
2887
	XLogCtl->SavedRedoRecPtr = RedoRecPtr;
2888 2889 2890
}

void
2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901
GetSavedRedoRecPtr(void)
{
	RedoRecPtr = XLogCtl->SavedRedoRecPtr;
}

/*
 * 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
2902 2903
GetRedoRecPtr(void)
{
2904 2905 2906 2907 2908 2909 2910 2911 2912
	/* use volatile pointer to prevent code rearrangement */
	volatile XLogCtlData *xlogctl = XLogCtl;

	SpinLockAcquire_NoHoldoff(&xlogctl->info_lck);
	Assert(XLByteLE(RedoRecPtr, xlogctl->Insert.RedoRecPtr));
	RedoRecPtr = xlogctl->Insert.RedoRecPtr;
	SpinLockRelease_NoHoldoff(&xlogctl->info_lck);

	return RedoRecPtr;
V
WAL  
Vadim B. Mikheev 已提交
2913 2914
}

2915
/*
T
Tom Lane 已提交
2916
 * This must be called ONCE during postmaster or standalone-backend shutdown
2917 2918
 */
void
T
Tom Lane 已提交
2919
ShutdownXLOG(void)
2920
{
2921
	elog(LOG, "shutting down");
2922

T
Tom Lane 已提交
2923 2924
	/* suppress in-transaction check in CreateCheckPoint */
	MyLastRecPtr.xrecoff = 0;
2925
	MyXactMadeXLogEntry = false;
T
Tom Lane 已提交
2926

2927
	CritSectionCount++;
V
Vadim B. Mikheev 已提交
2928
	CreateDummyCaches();
2929
	CreateCheckPoint(true);
2930
	ShutdownCLOG();
2931
	CritSectionCount--;
2932

2933
	elog(LOG, "database system is shut down");
2934 2935
}

T
Tom Lane 已提交
2936 2937 2938
/*
 * Perform a checkpoint --- either during shutdown, or on-the-fly
 */
2939 2940 2941
void
CreateCheckPoint(bool shutdown)
{
2942 2943 2944
	CheckPoint	checkPoint;
	XLogRecPtr	recptr;
	XLogCtlInsert *Insert = &XLogCtl->Insert;
B
Bruce Momjian 已提交
2945
	XLogRecData rdata;
2946
	uint32		freespace;
V
Vadim B. Mikheev 已提交
2947 2948 2949
	uint32		_logId;
	uint32		_logSeg;

2950
	if (MyXactMadeXLogEntry)
V
Vadim B. Mikheev 已提交
2951
		elog(ERROR, "CreateCheckPoint: cannot be called inside transaction block");
B
Bruce Momjian 已提交
2952

2953 2954
	/*
	 * The CheckpointLock can be held for quite a while, which is not good
2955 2956 2957 2958 2959
	 * because we won't respond to a cancel/die request while waiting for
	 * an LWLock.  (But the alternative of using a regular lock won't work
	 * for background checkpoint processes, which are not regular
	 * backends.) So, rather than use a plain LWLockAcquire, use this
	 * kluge to allow an interrupt to be accepted while we are waiting:
2960 2961
	 */
	while (!LWLockConditionalAcquire(CheckpointLock, LW_EXCLUSIVE))
V
Vadim B. Mikheev 已提交
2962
	{
2963 2964
		CHECK_FOR_INTERRUPTS();
		sleep(1);
V
Vadim B. Mikheev 已提交
2965
	}
2966

2967 2968
	START_CRIT_SECTION();

2969 2970 2971 2972 2973 2974
	if (shutdown)
	{
		ControlFile->state = DB_SHUTDOWNING;
		ControlFile->time = time(NULL);
		UpdateControlFile();
	}
T
Tom Lane 已提交
2975 2976

	memset(&checkPoint, 0, sizeof(checkPoint));
V
WAL  
Vadim B. Mikheev 已提交
2977
	checkPoint.ThisStartUpID = ThisStartUpID;
T
Tom Lane 已提交
2978
	checkPoint.time = time(NULL);
2979

2980
	LWLockAcquire(WALInsertLock, LW_EXCLUSIVE);
T
Tom Lane 已提交
2981 2982 2983 2984

	/*
	 * If this isn't a shutdown, and we have not inserted any XLOG records
	 * since the start of the last checkpoint, skip the checkpoint.  The
B
Bruce Momjian 已提交
2985 2986 2987 2988 2989 2990
	 * 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 已提交
2991 2992
	 *
	 * We have to make two tests to determine that nothing has happened since
B
Bruce Momjian 已提交
2993 2994 2995
	 * 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 已提交
2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009
	 */
	if (!shutdown)
	{
		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)
		{
3010 3011
			LWLockRelease(WALInsertLock);
			LWLockRelease(CheckpointLock);
T
Tom Lane 已提交
3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022
			END_CRIT_SECTION();
			return;
		}
	}

	/*
	 * Compute new REDO record ptr = location of next XLOG record.
	 *
	 * 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
B
Bruce Momjian 已提交
3023
	 * after the checkpoint, even though physically before it.	Got that?
T
Tom Lane 已提交
3024 3025
	 */
	freespace = INSERT_FREESPACE(Insert);
3026 3027
	if (freespace < SizeOfXLogRecord)
	{
T
Tom Lane 已提交
3028 3029
		(void) AdvanceXLInsertBuffer();
		/* OK to ignore update return flag, since we will do flush anyway */
3030 3031
		freespace = BLCKSZ - SizeOfXLogPHD;
	}
T
Tom Lane 已提交
3032
	INSERT_RECPTR(checkPoint.redo, Insert, Insert->curridx);
B
Bruce Momjian 已提交
3033

T
Tom Lane 已提交
3034 3035
	/*
	 * Here we update the shared RedoRecPtr for future XLogInsert calls;
3036
	 * this must be done while holding the insert lock AND the info_lck.
T
Tom Lane 已提交
3037
	 */
3038 3039 3040 3041 3042 3043 3044 3045
	{
		/* use volatile pointer to prevent code rearrangement */
		volatile XLogCtlData *xlogctl = XLogCtl;

		SpinLockAcquire_NoHoldoff(&xlogctl->info_lck);
		RedoRecPtr = xlogctl->Insert.RedoRecPtr = checkPoint.redo;
		SpinLockRelease_NoHoldoff(&xlogctl->info_lck);
	}
B
Bruce Momjian 已提交
3046

T
Tom Lane 已提交
3047
	/*
J
Jan Wieck 已提交
3048
	 * Get UNDO record ptr - this is oldest of PGPROC->logRec values. We do
B
Bruce Momjian 已提交
3049 3050 3051
	 * this while holding insert lock to ensure that we won't miss any
	 * about-to-commit transactions (UNDO must include all xacts that have
	 * commits after REDO point).
3052 3053 3054 3055 3056 3057 3058 3059 3060
	 *
	 * XXX temporarily ifdef'd out to avoid three-way deadlock condition:
	 * GetUndoRecPtr needs to grab SInvalLock to ensure that it is looking
	 * at a stable set of proc records, but grabbing SInvalLock while holding
	 * WALInsertLock is no good.  GetNewTransactionId may cause a WAL record
	 * to be written while holding XidGenLock, and GetSnapshotData needs to
	 * get XidGenLock while holding SInvalLock, so there's a risk of deadlock.
	 * Need to find a better solution.  See pgsql-hackers discussion of
	 * 17-Dec-01.
T
Tom Lane 已提交
3061
	 */
3062
#ifdef NOT_USED
T
Tom Lane 已提交
3063 3064 3065
	checkPoint.undo = GetUndoRecPtr();

	if (shutdown && checkPoint.undo.xrecoff != 0)
3066
		elog(PANIC, "active transaction while database system is shutting down");
3067
#endif
T
Tom Lane 已提交
3068 3069 3070 3071 3072

	/*
	 * Now we can release insert lock, allowing other xacts to proceed
	 * even while we are flushing disk buffers.
	 */
3073
	LWLockRelease(WALInsertLock);
3074

3075
	LWLockAcquire(XidGenLock, LW_SHARED);
3076
	checkPoint.nextXid = ShmemVariableCache->nextXid;
3077
	LWLockRelease(XidGenLock);
T
Tom Lane 已提交
3078

3079
	LWLockAcquire(OidGenLock, LW_SHARED);
3080
	checkPoint.nextOid = ShmemVariableCache->nextOid;
3081 3082
	if (!shutdown)
		checkPoint.nextOid += ShmemVariableCache->oidCount;
3083
	LWLockRelease(OidGenLock);
3084

T
Tom Lane 已提交
3085
	/*
B
Bruce Momjian 已提交
3086 3087
	 * Having constructed the checkpoint record, ensure all shmem disk
	 * buffers are flushed to disk.
T
Tom Lane 已提交
3088
	 */
V
Vadim B. Mikheev 已提交
3089
	FlushBufferPool();
3090

3091 3092 3093
	/* And commit-log buffers, too */
	CheckPointCLOG();

T
Tom Lane 已提交
3094 3095 3096
	/*
	 * Now insert the checkpoint record into XLOG.
	 */
3097
	rdata.buffer = InvalidBuffer;
B
Bruce Momjian 已提交
3098
	rdata.data = (char *) (&checkPoint);
3099 3100 3101
	rdata.len = sizeof(checkPoint);
	rdata.next = NULL;

T
Tom Lane 已提交
3102 3103 3104 3105 3106 3107
	recptr = XLogInsert(RM_XLOG_ID,
						shutdown ? XLOG_CHECKPOINT_SHUTDOWN :
						XLOG_CHECKPOINT_ONLINE,
						&rdata);

	XLogFlush(recptr);
3108

T
Tom Lane 已提交
3109 3110 3111 3112 3113
	/*
	 * We now have ProcLastRecPtr = start of actual checkpoint record,
	 * recptr = end of actual checkpoint record.
	 */
	if (shutdown && !XLByteEQ(checkPoint.redo, ProcLastRecPtr))
3114
		elog(PANIC, "concurrent transaction log activity while database system is shutting down");
3115

T
Tom Lane 已提交
3116
	/*
3117 3118 3119 3120 3121 3122 3123
	 * Select point at which we can truncate the log, which we base on the
	 * prior checkpoint's earliest info.
	 *
	 * With UNDO support: oldest item is redo or undo, whichever is older;
	 * but watch out for case that undo = 0.
	 *
	 * Without UNDO support: just use the redo pointer.  This allows xlog
3124 3125
	 * space to be freed much faster when there are long-running
	 * transactions.
T
Tom Lane 已提交
3126
	 */
3127
#ifdef NOT_USED
B
Bruce Momjian 已提交
3128
	if (ControlFile->checkPointCopy.undo.xrecoff != 0 &&
T
Tom Lane 已提交
3129 3130 3131 3132
		XLByteLT(ControlFile->checkPointCopy.undo,
				 ControlFile->checkPointCopy.redo))
		XLByteToSeg(ControlFile->checkPointCopy.undo, _logId, _logSeg);
	else
3133
#endif
T
Tom Lane 已提交
3134
		XLByteToSeg(ControlFile->checkPointCopy.redo, _logId, _logSeg);
3135

T
Tom Lane 已提交
3136 3137 3138
	/*
	 * Update the control file.
	 */
3139
	LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
3140 3141
	if (shutdown)
		ControlFile->state = DB_SHUTDOWNED;
T
Tom Lane 已提交
3142 3143 3144
	ControlFile->prevCheckPoint = ControlFile->checkPoint;
	ControlFile->checkPoint = ProcLastRecPtr;
	ControlFile->checkPointCopy = checkPoint;
3145 3146
	ControlFile->time = time(NULL);
	UpdateControlFile();
3147
	LWLockRelease(ControlFileLock);
3148

V
Vadim B. Mikheev 已提交
3149
	/*
T
Tom Lane 已提交
3150 3151
	 * Delete offline log files (those no longer needed even for previous
	 * checkpoint).
V
Vadim B. Mikheev 已提交
3152 3153 3154
	 */
	if (_logId || _logSeg)
	{
T
Tom Lane 已提交
3155
		PrevLogSeg(_logId, _logSeg);
3156
		MoveOfflineLogs(_logId, _logSeg, recptr);
V
Vadim B. Mikheev 已提交
3157 3158
	}

T
Tom Lane 已提交
3159 3160 3161 3162 3163 3164 3165 3166
	/*
	 * Make more log segments if needed.  (Do this after deleting offline
	 * log segments, to avoid having peak disk space usage higher than
	 * necessary.)
	 */
	if (!shutdown)
		PreallocXlogFiles(recptr);

3167
	LWLockRelease(CheckpointLock);
V
Vadim B. Mikheev 已提交
3168

3169
	END_CRIT_SECTION();
3170
}
V
WAL  
Vadim B. Mikheev 已提交
3171

T
Tom Lane 已提交
3172 3173 3174
/*
 * Write a NEXTOID log record
 */
3175 3176 3177
void
XLogPutNextOid(Oid nextOid)
{
B
Bruce Momjian 已提交
3178
	XLogRecData rdata;
3179

3180
	rdata.buffer = InvalidBuffer;
B
Bruce Momjian 已提交
3181
	rdata.data = (char *) (&nextOid);
3182 3183 3184 3185
	rdata.len = sizeof(Oid);
	rdata.next = NULL;
	(void) XLogInsert(RM_XLOG_ID, XLOG_NEXTOID, &rdata);
}
V
WAL  
Vadim B. Mikheev 已提交
3186

T
Tom Lane 已提交
3187 3188 3189
/*
 * XLOG resource manager's routines
 */
V
WAL  
Vadim B. Mikheev 已提交
3190 3191 3192
void
xlog_redo(XLogRecPtr lsn, XLogRecord *record)
{
B
Bruce Momjian 已提交
3193
	uint8		info = record->xl_info & ~XLR_INFO_MASK;
3194

3195
	if (info == XLOG_NEXTOID)
3196
	{
B
Bruce Momjian 已提交
3197
		Oid			nextOid;
3198 3199 3200

		memcpy(&nextOid, XLogRecGetData(record), sizeof(Oid));
		if (ShmemVariableCache->nextOid < nextOid)
T
Tom Lane 已提交
3201
		{
3202
			ShmemVariableCache->nextOid = nextOid;
T
Tom Lane 已提交
3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220
			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;
	}
	else if (info == XLOG_CHECKPOINT_ONLINE)
	{
		CheckPoint	checkPoint;

		memcpy(&checkPoint, XLogRecGetData(record), sizeof(CheckPoint));
3221
		/* In an ONLINE checkpoint, treat the counters like NEXTOID */
3222 3223
		if (TransactionIdPrecedes(ShmemVariableCache->nextXid,
								  checkPoint.nextXid))
T
Tom Lane 已提交
3224 3225 3226 3227 3228 3229
			ShmemVariableCache->nextXid = checkPoint.nextXid;
		if (ShmemVariableCache->nextOid < checkPoint.nextOid)
		{
			ShmemVariableCache->nextOid = checkPoint.nextOid;
			ShmemVariableCache->oidCount = 0;
		}
3230
	}
V
WAL  
Vadim B. Mikheev 已提交
3231
}
B
Bruce Momjian 已提交
3232

V
WAL  
Vadim B. Mikheev 已提交
3233 3234 3235 3236
void
xlog_undo(XLogRecPtr lsn, XLogRecord *record)
{
}
B
Bruce Momjian 已提交
3237

V
WAL  
Vadim B. Mikheev 已提交
3238
void
B
Bruce Momjian 已提交
3239
xlog_desc(char *buf, uint8 xl_info, char *rec)
V
WAL  
Vadim B. Mikheev 已提交
3240
{
B
Bruce Momjian 已提交
3241
	uint8		info = xl_info & ~XLR_INFO_MASK;
V
WAL  
Vadim B. Mikheev 已提交
3242

T
Tom Lane 已提交
3243 3244
	if (info == XLOG_CHECKPOINT_SHUTDOWN ||
		info == XLOG_CHECKPOINT_ONLINE)
V
WAL  
Vadim B. Mikheev 已提交
3245
	{
B
Bruce Momjian 已提交
3246 3247
		CheckPoint *checkpoint = (CheckPoint *) rec;

3248
		sprintf(buf + strlen(buf), "checkpoint: redo %X/%X; undo %X/%X; "
B
Bruce Momjian 已提交
3249 3250 3251 3252 3253 3254
				"sui %u; xid %u; oid %u; %s",
				checkpoint->redo.xlogid, checkpoint->redo.xrecoff,
				checkpoint->undo.xlogid, checkpoint->undo.xrecoff,
				checkpoint->ThisStartUpID, checkpoint->nextXid,
				checkpoint->nextOid,
			 (info == XLOG_CHECKPOINT_SHUTDOWN) ? "shutdown" : "online");
T
Tom Lane 已提交
3255
	}
3256 3257
	else if (info == XLOG_NEXTOID)
	{
B
Bruce Momjian 已提交
3258
		Oid			nextOid;
3259 3260 3261 3262

		memcpy(&nextOid, rec, sizeof(Oid));
		sprintf(buf + strlen(buf), "nextOid: %u", nextOid);
	}
V
WAL  
Vadim B. Mikheev 已提交
3263 3264 3265 3266 3267 3268 3269
	else
		strcat(buf, "UNKNOWN");
}

static void
xlog_outrec(char *buf, XLogRecord *record)
{
B
Bruce Momjian 已提交
3270 3271
	int			bkpb;
	int			i;
3272

3273
	sprintf(buf + strlen(buf), "prev %X/%X; xprev %X/%X; xid %u",
B
Bruce Momjian 已提交
3274 3275 3276
			record->xl_prev.xlogid, record->xl_prev.xrecoff,
			record->xl_xact_prev.xlogid, record->xl_xact_prev.xrecoff,
			record->xl_xid);
3277

T
Tom Lane 已提交
3278
	for (i = 0, bkpb = 0; i < XLR_MAX_BKP_BLOCKS; i++)
3279 3280 3281 3282 3283 3284 3285 3286 3287 3288
	{
		if (!(record->xl_info & (XLR_SET_BKP_BLOCK(i))))
			continue;
		bkpb++;
	}

	if (bkpb)
		sprintf(buf + strlen(buf), "; bkpb %d", bkpb);

	sprintf(buf + strlen(buf), ": %s",
B
Bruce Momjian 已提交
3289
			RmgrTable[record->xl_rmid].rm_name);
V
WAL  
Vadim B. Mikheev 已提交
3290
}
3291 3292 3293


/*
3294
 * GUC support
3295
 */
3296 3297
const char *
assign_xlog_sync_method(const char *method, bool doit, bool interactive)
3298
{
B
Bruce Momjian 已提交
3299 3300
	int			new_sync_method;
	int			new_sync_bit;
3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329

	if (strcasecmp(method, "fsync") == 0)
	{
		new_sync_method = SYNC_METHOD_FSYNC;
		new_sync_bit = 0;
	}
#ifdef HAVE_FDATASYNC
	else if (strcasecmp(method, "fdatasync") == 0)
	{
		new_sync_method = SYNC_METHOD_FDATASYNC;
		new_sync_bit = 0;
	}
#endif
#ifdef OPEN_SYNC_FLAG
	else if (strcasecmp(method, "open_sync") == 0)
	{
		new_sync_method = SYNC_METHOD_OPEN;
		new_sync_bit = OPEN_SYNC_FLAG;
	}
#endif
#ifdef OPEN_DATASYNC_FLAG
	else if (strcasecmp(method, "open_datasync") == 0)
	{
		new_sync_method = SYNC_METHOD_OPEN;
		new_sync_bit = OPEN_DATASYNC_FLAG;
	}
#endif
	else
	{
3330
		return NULL;
3331 3332
	}

3333 3334 3335
	if (!doit)
		return method;

3336 3337 3338
	if (sync_method != new_sync_method || open_sync_bit != new_sync_bit)
	{
		/*
B
Bruce Momjian 已提交
3339 3340 3341 3342
		 * To ensure that no blocks escape unsynced, force an fsync on the
		 * currently open log segment (if any).  Also, if the open flag is
		 * changing, close the log file so it will be reopened (with new
		 * flag bit) at next use.
3343 3344 3345 3346
		 */
		if (openLogFile >= 0)
		{
			if (pg_fsync(openLogFile) != 0)
3347
				elog(PANIC, "fsync of log file %u, segment %u failed: %m",
3348 3349 3350 3351
					 openLogId, openLogSeg);
			if (open_sync_bit != new_sync_bit)
			{
				if (close(openLogFile) != 0)
3352
					elog(PANIC, "close of log file %u, segment %u failed: %m",
3353 3354 3355 3356 3357 3358 3359
						 openLogId, openLogSeg);
				openLogFile = -1;
			}
		}
		sync_method = new_sync_method;
		open_sync_bit = new_sync_bit;
	}
3360 3361

	return method;
3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372
}


/*
 * Issue appropriate kind of fsync (if any) on the current XLOG output file
 */
static void
issue_xlog_fsync(void)
{
	switch (sync_method)
	{
3373
		case SYNC_METHOD_FSYNC:
3374
			if (pg_fsync(openLogFile) != 0)
3375
				elog(PANIC, "fsync of log file %u, segment %u failed: %m",
3376 3377 3378 3379 3380
					 openLogId, openLogSeg);
			break;
#ifdef HAVE_FDATASYNC
		case SYNC_METHOD_FDATASYNC:
			if (pg_fdatasync(openLogFile) != 0)
3381
				elog(PANIC, "fdatasync of log file %u, segment %u failed: %m",
3382 3383 3384 3385 3386 3387 3388
					 openLogId, openLogSeg);
			break;
#endif
		case SYNC_METHOD_OPEN:
			/* write synced it already */
			break;
		default:
3389
			elog(PANIC, "bogus wal_sync_method %d", sync_method);
3390 3391 3392
			break;
	}
}