clog.c 22.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*-------------------------------------------------------------------------
 *
 * clog.c
 *		PostgreSQL transaction-commit-log manager
 *
 * This module replaces the old "pg_log" access code, which treated pg_log
 * essentially like a relation, in that it went through the regular buffer
 * manager.  The problem with that was that there wasn't any good way to
 * recycle storage space for transactions so old that they'll never be
 * looked up again.  Now we use specialized access code so that the commit
 * log can be broken into relatively small, independent segments.
 *
13 14 15 16
 * XLOG interactions: this module generates an XLOG record whenever a new
 * CLOG page is initialized to zeroes.	Other writes of CLOG come from
 * recording of transaction commit or abort in xact.c, which generates its
 * own XLOG records for these events and will re-perform the status update
Bruce Momjian's avatar
Bruce Momjian committed
17
 * on redo; so we need make no additional XLOG entry here.	For synchronous
18 19 20 21
 * transaction commits, the XLOG is guaranteed flushed through the XLOG commit
 * record before we are called to log a commit, so the WAL rule "write xlog
 * before data" is satisfied automatically.  However, for async commits we
 * must track the latest LSN affecting each CLOG page, so that we can flush
Bruce Momjian's avatar
Bruce Momjian committed
22
 * XLOG that far and satisfy the WAL rule.	We don't have to worry about this
23 24
 * for aborts (whether sync or async), since the post-crash assumption would
 * be that such transactions failed anyway.
25
 *
26
 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
27 28
 * Portions Copyright (c) 1994, Regents of the University of California
 *
29
 * $PostgreSQL: pgsql/src/backend/access/transam/clog.c,v 1.48 2008/10/20 19:18:18 alvherre Exp $
30 31 32 33 34 35
 *
 *-------------------------------------------------------------------------
 */
#include "postgres.h"

#include "access/clog.h"
36
#include "access/slru.h"
37
#include "access/transam.h"
38
#include "pg_trace.h"
39
#include "postmaster/bgwriter.h"
40 41

/*
42 43
 * Defines for CLOG page sizes.  A page is the same BLCKSZ as is used
 * everywhere else in Postgres.
44 45 46 47 48 49 50 51 52 53
 *
 * Note: because TransactionIds are 32 bits and wrap around at 0xFFFFFFFF,
 * CLOG page numbering also wraps around at 0xFFFFFFFF/CLOG_XACTS_PER_PAGE,
 * and CLOG segment numbering at 0xFFFFFFFF/CLOG_XACTS_PER_SEGMENT.  We need
 * take no explicit notice of that fact in this module, except when comparing
 * segment and page numbers in TruncateCLOG (see CLOGPagePrecedes).
 */

/* We need two bits per xact, so four xacts fit in a byte */
#define CLOG_BITS_PER_XACT	2
54
#define CLOG_XACTS_PER_BYTE 4
55
#define CLOG_XACTS_PER_PAGE (BLCKSZ * CLOG_XACTS_PER_BYTE)
56 57 58
#define CLOG_XACT_BITMASK	((1 << CLOG_BITS_PER_XACT) - 1)

#define TransactionIdToPage(xid)	((xid) / (TransactionId) CLOG_XACTS_PER_PAGE)
59
#define TransactionIdToPgIndex(xid) ((xid) % (TransactionId) CLOG_XACTS_PER_PAGE)
60 61 62
#define TransactionIdToByte(xid)	(TransactionIdToPgIndex(xid) / CLOG_XACTS_PER_BYTE)
#define TransactionIdToBIndex(xid)	((xid) % (TransactionId) CLOG_XACTS_PER_BYTE)

63
/* We store the latest async LSN for each group of transactions */
Bruce Momjian's avatar
Bruce Momjian committed
64 65
#define CLOG_XACTS_PER_LSN_GROUP	32	/* keep this a power of 2 */
#define CLOG_LSNS_PER_PAGE	(CLOG_XACTS_PER_PAGE / CLOG_XACTS_PER_LSN_GROUP)
66 67 68 69

#define GetLSNIndex(slotno, xid)	((slotno) * CLOG_LSNS_PER_PAGE + \
	((xid) % (TransactionId) CLOG_XACTS_PER_PAGE) / CLOG_XACTS_PER_LSN_GROUP)

70

71 72
/*
 * Link to shared-memory data structures for CLOG control
73
 */
74
static SlruCtlData ClogCtlData;
Bruce Momjian's avatar
Bruce Momjian committed
75

76
#define ClogCtl (&ClogCtlData)
Bruce Momjian's avatar
Bruce Momjian committed
77

78 79 80 81

static int	ZeroCLOGPage(int pageno, bool writeXlog);
static bool CLOGPagePrecedes(int page1, int page2);
static void WriteZeroPageXlogRec(int pageno);
82
static void WriteTruncateXlogRec(int pageno);
83 84 85 86 87 88 89
static void TransactionIdSetPageStatus(TransactionId xid, int nsubxids,
					   	   TransactionId *subxids, XidStatus status,
						   XLogRecPtr lsn, int pageno);
static void TransactionIdSetStatusBit(TransactionId xid, XidStatus status,
						  XLogRecPtr lsn, int slotno);
static void set_status_by_pages(int nsubxids, TransactionId *subxids,
					XidStatus status, XLogRecPtr lsn);
90 91 92


/*
93 94 95 96 97 98 99 100 101 102 103 104
 * TransactionIdSetTreeStatus
 *
 * Record the final state of transaction entries in the commit log for
 * a transaction and its subtransaction tree. Take care to ensure this is
 * efficient, and as atomic as possible.
 *
 * xid is a single xid to set status for. This will typically be
 * the top level transactionid for a top level commit or abort. It can
 * also be a subtransaction when we record transaction aborts.
 *
 * subxids is an array of xids of length nsubxids, representing subtransactions
 * in the tree of xid. In various cases nsubxids may be zero.
105
 *
106
 * lsn must be the WAL location of the commit record when recording an async
Bruce Momjian's avatar
Bruce Momjian committed
107
 * commit.	For a synchronous commit it can be InvalidXLogRecPtr, since the
108 109 110
 * caller guarantees the commit record is already flushed in that case.  It
 * should be InvalidXLogRecPtr for abort cases, too.
 *
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
 * In the commit case, atomicity is limited by whether all the subxids are in
 * the same CLOG page as xid.  If they all are, then the lock will be grabbed
 * only once, and the status will be set to committed directly.  Otherwise
 * we must
 *   1. set sub-committed all subxids that are not on the same page as the
 *      main xid
 *   2. atomically set committed the main xid and the subxids on the same page
 *   3. go over the first bunch again and set them committed
 * Note that as far as concurrent checkers are concerned, main transaction
 * commit as a whole is still atomic.
 *
 * Example:
 *		TransactionId t commits and has subxids t1, t2, t3, t4
 *		t is on page p1, t1 is also on p1, t2 and t3 are on p2, t4 is on p3
 *		1. update pages2-3:
 *					page2: set t2,t3 as sub-committed
 *					page3: set t4 as sub-committed
 *		2. update page1:
 *					set t1 as sub-committed, 
 *					then set t as committed,
					then set t1 as committed
 *		3. update pages2-3:
 *					page2: set t2,t3 as committed
 *					page3: set t4 as committed
 * 
136
 * NB: this is a low-level routine and is NOT the preferred entry point
137 138 139 140 141
 * for most uses; functions in transam.c are the intended callers.
 *
 * XXX Think about issuing FADVISE_WILLNEED on pages that we will need,
 * but aren't yet in cache, as well as hinting pages not to fall out of
 * cache yet.
142 143
 */
void
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
TransactionIdSetTreeStatus(TransactionId xid, int nsubxids,
				TransactionId *subxids, XidStatus status, XLogRecPtr lsn)
{
	int		pageno = TransactionIdToPage(xid); /* get page of parent */
	int 	i;

	Assert(status == TRANSACTION_STATUS_COMMITTED ||
		   status == TRANSACTION_STATUS_ABORTED);

	/*
	 * See how many subxids, if any, are on the same page as the parent, if any.
	 */
	for (i = 0; i < nsubxids; i++)
	{
		if (TransactionIdToPage(subxids[i]) != pageno)
			break;
	}

	/*
	 * Do all items fit on a single page?
	 */
	if (i == nsubxids)
	{
		/*
		 * Set the parent and all subtransactions in a single call
		 */
		TransactionIdSetPageStatus(xid, nsubxids, subxids, status, lsn,
								   pageno);
	}
	else
	{
		int		nsubxids_on_first_page = i;

		/*
		 * If this is a commit then we care about doing this correctly (i.e.
		 * using the subcommitted intermediate status).  By here, we know we're
		 * updating more than one page of clog, so we must mark entries that
		 * are *not* on the first page so that they show as subcommitted before
		 * we then return to update the status to fully committed.
		 *
		 * To avoid touching the first page twice, skip marking subcommitted
		 * for the subxids on that first page.
		 */
		if (status == TRANSACTION_STATUS_COMMITTED)
			set_status_by_pages(nsubxids - nsubxids_on_first_page,
								subxids + nsubxids_on_first_page,
								TRANSACTION_STATUS_SUB_COMMITTED, lsn);

		/*
		 * Now set the parent and subtransactions on same page as the parent,
		 * if any
		 */
		pageno = TransactionIdToPage(xid);
		TransactionIdSetPageStatus(xid, nsubxids_on_first_page, subxids, status,
								   lsn, pageno);

		/*
		 * Now work through the rest of the subxids one clog page at a time,
		 * starting from the second page onwards, like we did above.
		 */
		set_status_by_pages(nsubxids - nsubxids_on_first_page,
							subxids + nsubxids_on_first_page,
							status, lsn);
	}
}

/*
 * Helper for TransactionIdSetTreeStatus: set the status for a bunch of
 * transactions, chunking in the separate CLOG pages involved. We never
 * pass the whole transaction tree to this function, only subtransactions
 * that are on different pages to the top level transaction id.
 */
static void
set_status_by_pages(int nsubxids, TransactionId *subxids,
					XidStatus status, XLogRecPtr lsn)
{
	int		pageno = TransactionIdToPage(subxids[0]);
	int		offset = 0;
	int		i = 0;

	while (i < nsubxids)
	{
		int		num_on_page = 0;

		while (TransactionIdToPage(subxids[i]) == pageno && i < nsubxids)
		{
			num_on_page++;
			i++;
		}

		TransactionIdSetPageStatus(InvalidTransactionId,
								   num_on_page, subxids + offset,
								   status, lsn, pageno);
		offset = i;
		pageno = TransactionIdToPage(subxids[offset]);
	}
}

/*
 * Record the final state of transaction entries in the commit log for
 * all entries on a single page.  Atomic only on this page.
 *
 * Otherwise API is same as TransactionIdSetTreeStatus()
 */
static void
TransactionIdSetPageStatus(TransactionId xid, int nsubxids,
						   TransactionId *subxids, XidStatus status,
						   XLogRecPtr lsn, int pageno)
252
{
253
	int			slotno;
254
	int 		i;
255 256

	Assert(status == TRANSACTION_STATUS_COMMITTED ||
257
		   status == TRANSACTION_STATUS_ABORTED ||
258
		   (status == TRANSACTION_STATUS_SUB_COMMITTED && !TransactionIdIsValid(xid)));
259

260
	LWLockAcquire(CLogControlLock, LW_EXCLUSIVE);
261

262 263 264 265 266 267 268
	/*
	 * If we're doing an async commit (ie, lsn is valid), then we must wait
	 * for any active write on the page slot to complete.  Otherwise our
	 * update could reach disk in that write, which will not do since we
	 * mustn't let it reach disk until we've done the appropriate WAL flush.
	 * But when lsn is invalid, it's OK to scribble on a page while it is
	 * write-busy, since we don't care if the update reaches disk sooner than
269
	 * we think.
270 271
	 */
	slotno = SimpleLruReadPage(ClogCtl, pageno, XLogRecPtrIsInvalid(lsn), xid);
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

	/*
	 * Set the main transaction id, if any.
	 *
	 * If we update more than one xid on this page while it is being written
	 * out, we might find that some of the bits go to disk and others don't.
	 * If we are updating commits on the page with the top-level xid that could
	 * break atomicity, so we subcommit the subxids first before we mark the
	 * top-level commit.
	 */
	if (TransactionIdIsValid(xid))
	{
		/* Subtransactions first, if needed ... */
		if (status == TRANSACTION_STATUS_COMMITTED)
		{
			for (i = 0; i < nsubxids; i++)
			{
				Assert(ClogCtl->shared->page_number[slotno] == TransactionIdToPage(subxids[i]));
				TransactionIdSetStatusBit(subxids[i],
										  TRANSACTION_STATUS_SUB_COMMITTED,
										  lsn, slotno);
			}
		}

		/* ... then the main transaction */
		TransactionIdSetStatusBit(xid, status, lsn, slotno);
	}

	/* Set the subtransactions */
	for (i = 0; i < nsubxids; i++)
	{
		Assert(ClogCtl->shared->page_number[slotno] == TransactionIdToPage(subxids[i]));
		TransactionIdSetStatusBit(subxids[i], status, lsn, slotno);
	}

	ClogCtl->shared->page_dirty[slotno] = true;

	LWLockRelease(CLogControlLock);
}

/*
 * Sets the commit status of a single transaction.
 *
 * Must be called with CLogControlLock held
 */
static void
TransactionIdSetStatusBit(TransactionId xid, XidStatus status, XLogRecPtr lsn, int slotno)
{
	int			byteno = TransactionIdToByte(xid);
	int			bshift = TransactionIdToBIndex(xid) * CLOG_BITS_PER_XACT;
	char	   *byteptr;
	char		byteval;

325
	byteptr = ClogCtl->shared->page_buffer[slotno] + byteno;
326

327
	/* Current state should be 0, subcommitted or target state */
328
	Assert(((*byteptr >> bshift) & CLOG_XACT_BITMASK) == 0 ||
329
		   ((*byteptr >> bshift) & CLOG_XACT_BITMASK) == TRANSACTION_STATUS_SUB_COMMITTED ||
330 331
		   ((*byteptr >> bshift) & CLOG_XACT_BITMASK) == status);

332 333 334 335 336
	/* note this assumes exclusive access to the clog page */
	byteval = *byteptr;
	byteval &= ~(((1 << CLOG_BITS_PER_XACT) - 1) << bshift);
	byteval |= (status << bshift);
	*byteptr = byteval;
337

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
	/*
	 * Update the group LSN if the transaction completion LSN is higher.
	 *
	 * Note: lsn will be invalid when supplied during InRecovery processing,
	 * so we don't need to do anything special to avoid LSN updates during
	 * recovery. After recovery completes the next clog change will set the
	 * LSN correctly.
	 */
	if (!XLogRecPtrIsInvalid(lsn))
	{
		int			lsnindex = GetLSNIndex(slotno, xid);

		if (XLByteLT(ClogCtl->shared->group_lsn[lsnindex], lsn))
			ClogCtl->shared->group_lsn[lsnindex] = lsn;
	}
353 354 355 356 357
}

/*
 * Interrogate the state of a transaction in the commit log.
 *
358 359 360 361
 * Aside from the actual commit status, this function returns (into *lsn)
 * an LSN that is late enough to be able to guarantee that if we flush up to
 * that LSN then we will have flushed the transaction's commit record to disk.
 * The result is not necessarily the exact LSN of the transaction's commit
Bruce Momjian's avatar
Bruce Momjian committed
362
 * record!	For example, for long-past transactions (those whose clog pages
363 364 365 366
 * already migrated to disk), we'll return InvalidXLogRecPtr.  Also, because
 * we group transactions on the same clog page to conserve storage, we might
 * return the LSN of a later transaction that falls into the same group.
 *
367
 * NB: this is a low-level routine and is NOT the preferred entry point
368
 * for most uses; TransactionLogFetch() in transam.c is the intended caller.
369 370
 */
XidStatus
371
TransactionIdGetStatus(TransactionId xid, XLogRecPtr *lsn)
372 373 374 375
{
	int			pageno = TransactionIdToPage(xid);
	int			byteno = TransactionIdToByte(xid);
	int			bshift = TransactionIdToBIndex(xid) * CLOG_BITS_PER_XACT;
376
	int			slotno;
377
	int			lsnindex;
378 379 380
	char	   *byteptr;
	XidStatus	status;

381
	/* lock is acquired by SimpleLruReadPage_ReadOnly */
382

383
	slotno = SimpleLruReadPage_ReadOnly(ClogCtl, pageno, xid);
384
	byteptr = ClogCtl->shared->page_buffer[slotno] + byteno;
385 386 387

	status = (*byteptr >> bshift) & CLOG_XACT_BITMASK;

388 389 390
	lsnindex = GetLSNIndex(slotno, xid);
	*lsn = ClogCtl->shared->group_lsn[lsnindex];

391
	LWLockRelease(CLogControlLock);
392 393 394 395 396 397 398 399

	return status;
}


/*
 * Initialization of shared memory for CLOG
 */
400
Size
401 402
CLOGShmemSize(void)
{
403
	return SimpleLruShmemSize(NUM_CLOG_BUFFERS, CLOG_LSNS_PER_PAGE);
404 405 406 407 408
}

void
CLOGShmemInit(void)
{
409
	ClogCtl->PagePrecedes = CLOGPagePrecedes;
410
	SimpleLruInit(ClogCtl, "CLOG Ctl", NUM_CLOG_BUFFERS, CLOG_LSNS_PER_PAGE,
411
				  CLogControlLock, "pg_clog");
412 413 414 415 416 417 418 419 420 421 422 423 424
}

/*
 * This func must be called ONCE on system install.  It creates
 * the initial CLOG segment.  (The CLOG directory is assumed to
 * have been created by the initdb shell script, and CLOGShmemInit
 * must have been called already.)
 */
void
BootStrapCLOG(void)
{
	int			slotno;

425
	LWLockAcquire(CLogControlLock, LW_EXCLUSIVE);
426 427 428 429 430

	/* Create and zero the first page of the commit log */
	slotno = ZeroCLOGPage(0, false);

	/* Make sure it's written out */
431
	SimpleLruWritePage(ClogCtl, slotno, NULL);
432
	Assert(!ClogCtl->shared->page_dirty[slotno]);
433

434
	LWLockRelease(CLogControlLock);
435 436 437 438 439 440 441 442 443 444 445 446 447 448
}

/*
 * Initialize (or reinitialize) a page of CLOG to zeroes.
 * If writeXlog is TRUE, also emit an XLOG record saying we did this.
 *
 * The page is not actually written, just set up in shared memory.
 * The slot number of the new page is returned.
 *
 * Control lock must be held at entry, and will be held at exit.
 */
static int
ZeroCLOGPage(int pageno, bool writeXlog)
{
449 450 451
	int			slotno;

	slotno = SimpleLruZeroPage(ClogCtl, pageno);
452 453 454 455 456 457 458 459 460 461 462 463 464 465

	if (writeXlog)
		WriteZeroPageXlogRec(pageno);

	return slotno;
}

/*
 * This must be called ONCE during postmaster or standalone-backend startup,
 * after StartupXLOG has initialized ShmemVariableCache->nextXid.
 */
void
StartupCLOG(void)
{
466 467 468 469 470
	TransactionId xid = ShmemVariableCache->nextXid;
	int			pageno = TransactionIdToPage(xid);

	LWLockAcquire(CLogControlLock, LW_EXCLUSIVE);

471 472 473
	/*
	 * Initialize our idea of the latest page number.
	 */
474 475 476 477 478
	ClogCtl->shared->latest_page_number = pageno;

	/*
	 * Zero out the remainder of the current clog page.  Under normal
	 * circumstances it should be zeroes already, but it seems at least
479 480 481 482 483 484 485 486
	 * theoretically possible that XLOG replay will have settled on a nextXID
	 * value that is less than the last XID actually used and marked by the
	 * previous database lifecycle (since subtransaction commit writes clog
	 * but makes no WAL entry).  Let's just be safe. (We need not worry about
	 * pages beyond the current one, since those will be zeroed when first
	 * used.  For the same reason, there is no need to do anything when
	 * nextXid is exactly at a page boundary; and it's likely that the
	 * "current" page doesn't exist yet in that case.)
487
	 */
488 489 490 491 492 493 494
	if (TransactionIdToPgIndex(xid) != 0)
	{
		int			byteno = TransactionIdToByte(xid);
		int			bshift = TransactionIdToBIndex(xid) * CLOG_BITS_PER_XACT;
		int			slotno;
		char	   *byteptr;

495
		slotno = SimpleLruReadPage(ClogCtl, pageno, false, xid);
496 497 498 499 500 501 502
		byteptr = ClogCtl->shared->page_buffer[slotno] + byteno;

		/* Zero so-far-unused positions in the current byte */
		*byteptr &= (1 << bshift) - 1;
		/* Zero the rest of the page */
		MemSet(byteptr + 1, 0, BLCKSZ - byteno - 1);

503
		ClogCtl->shared->page_dirty[slotno] = true;
504
	}
505 506

	LWLockRelease(CLogControlLock);
507 508 509 510 511 512 513 514
}

/*
 * This must be called ONCE during postmaster or standalone-backend shutdown
 */
void
ShutdownCLOG(void)
{
515
	/* Flush dirty CLOG pages to disk */
516
	TRACE_POSTGRESQL_CLOG_CHECKPOINT_START(false);
517
	SimpleLruFlush(ClogCtl, false);
518
	TRACE_POSTGRESQL_CLOG_CHECKPOINT_DONE(false);
519 520 521 522 523 524 525 526
}

/*
 * Perform a checkpoint --- either during shutdown, or on-the-fly
 */
void
CheckPointCLOG(void)
{
527
	/* Flush dirty CLOG pages to disk */
528
	TRACE_POSTGRESQL_CLOG_CHECKPOINT_START(true);
529
	SimpleLruFlush(ClogCtl, true);
530
	TRACE_POSTGRESQL_CLOG_CHECKPOINT_DONE(true);
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
}


/*
 * Make sure that CLOG has room for a newly-allocated XID.
 *
 * NB: this is called while holding XidGenLock.  We want it to be very fast
 * most of the time; even when it's not so fast, no actual I/O need happen
 * unless we're forced to write out a dirty clog or xlog page to make room
 * in shared memory.
 */
void
ExtendCLOG(TransactionId newestXact)
{
	int			pageno;

547 548 549 550 551 552
	/*
	 * No work except at first XID of a page.  But beware: just after
	 * wraparound, the first XID of page zero is FirstNormalTransactionId.
	 */
	if (TransactionIdToPgIndex(newestXact) != 0 &&
		!TransactionIdEquals(newestXact, FirstNormalTransactionId))
553 554 555 556
		return;

	pageno = TransactionIdToPage(newestXact);

557
	LWLockAcquire(CLogControlLock, LW_EXCLUSIVE);
558 559 560 561

	/* Zero the page and make an XLOG entry about it */
	ZeroCLOGPage(pageno, true);

562
	LWLockRelease(CLogControlLock);
563 564 565 566 567 568
}


/*
 * Remove all CLOG segments before the one holding the passed transaction ID
 *
569 570 571 572 573 574 575 576 577 578 579
 * Before removing any CLOG data, we must flush XLOG to disk, to ensure
 * that any recently-emitted HEAP_FREEZE records have reached disk; otherwise
 * a crash and restart might leave us with some unfrozen tuples referencing
 * removed CLOG data.  We choose to emit a special TRUNCATE XLOG record too.
 * Replaying the deletion from XLOG is not critical, since the files could
 * just as well be removed later, but doing so prevents a long-running hot
 * standby server from acquiring an unreasonably bloated CLOG directory.
 *
 * Since CLOG segments hold a large number of transactions, the opportunity to
 * actually remove a segment is fairly rare, and so it seems best not to do
 * the XLOG flush unless we have confirmed that there is a removable segment.
580 581 582 583 584 585 586
 */
void
TruncateCLOG(TransactionId oldestXact)
{
	int			cutoffPage;

	/*
587 588
	 * The cutoff point is the start of the segment containing oldestXact. We
	 * pass the *page* containing oldestXact to SimpleLruTruncate.
589 590
	 */
	cutoffPage = TransactionIdToPage(oldestXact);
591 592 593 594 595

	/* Check to see if there's any files that could be removed */
	if (!SlruScanDirectory(ClogCtl, cutoffPage, false))
		return;					/* nothing to remove */

596 597
	/* Write XLOG record and flush XLOG to disk */
	WriteTruncateXlogRec(cutoffPage);
598 599

	/* Now we can remove the old CLOG segment(s) */
600
	SimpleLruTruncate(ClogCtl, cutoffPage);
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
}


/*
 * Decide which of two CLOG page numbers is "older" for truncation purposes.
 *
 * We need to use comparison of TransactionIds here in order to do the right
 * thing with wraparound XID arithmetic.  However, if we are asked about
 * page number zero, we don't want to hand InvalidTransactionId to
 * TransactionIdPrecedes: it'll get weird about permanent xact IDs.  So,
 * offset both xids by FirstNormalTransactionId to avoid that.
 */
static bool
CLOGPagePrecedes(int page1, int page2)
{
	TransactionId xid1;
	TransactionId xid2;

619
	xid1 = ((TransactionId) page1) * CLOG_XACTS_PER_PAGE;
620
	xid1 += FirstNormalTransactionId;
621
	xid2 = ((TransactionId) page2) * CLOG_XACTS_PER_PAGE;
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
	xid2 += FirstNormalTransactionId;

	return TransactionIdPrecedes(xid1, xid2);
}


/*
 * Write a ZEROPAGE xlog record
 */
static void
WriteZeroPageXlogRec(int pageno)
{
	XLogRecData rdata;

	rdata.data = (char *) (&pageno);
	rdata.len = sizeof(int);
638
	rdata.buffer = InvalidBuffer;
639
	rdata.next = NULL;
640
	(void) XLogInsert(RM_CLOG_ID, CLOG_ZEROPAGE, &rdata);
641 642
}

643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
/*
 * Write a TRUNCATE xlog record
 *
 * We must flush the xlog record to disk before returning --- see notes
 * in TruncateCLOG().
 */
static void
WriteTruncateXlogRec(int pageno)
{
	XLogRecData rdata;
	XLogRecPtr	recptr;

	rdata.data = (char *) (&pageno);
	rdata.len = sizeof(int);
	rdata.buffer = InvalidBuffer;
	rdata.next = NULL;
659
	recptr = XLogInsert(RM_CLOG_ID, CLOG_TRUNCATE, &rdata);
660 661 662
	XLogFlush(recptr);
}

663 664 665
/*
 * CLOG resource manager's routines
 */
666
void
667
clog_redo(XLogRecPtr lsn, XLogRecord *record)
668
{
669
	uint8		info = record->xl_info & ~XLR_INFO_MASK;
670

671 672 673 674
	if (info == CLOG_ZEROPAGE)
	{
		int			pageno;
		int			slotno;
675

676 677 678 679 680 681
		memcpy(&pageno, XLogRecGetData(record), sizeof(int));

		LWLockAcquire(CLogControlLock, LW_EXCLUSIVE);

		slotno = ZeroCLOGPage(pageno, false);
		SimpleLruWritePage(ClogCtl, slotno, NULL);
682
		Assert(!ClogCtl->shared->page_dirty[slotno]);
683 684 685

		LWLockRelease(CLogControlLock);
	}
686 687 688 689 690 691 692
	else if (info == CLOG_TRUNCATE)
	{
		int			pageno;

		memcpy(&pageno, XLogRecGetData(record), sizeof(int));

		/*
Bruce Momjian's avatar
Bruce Momjian committed
693 694
		 * During XLOG replay, latest_page_number isn't set up yet; insert a
		 * suitable value to bypass the sanity test in SimpleLruTruncate.
695 696 697 698 699 700 701
		 */
		ClogCtl->shared->latest_page_number = pageno;

		SimpleLruTruncate(ClogCtl, pageno);
	}
	else
		elog(PANIC, "clog_redo: unknown op code %u", info);
702 703 704
}

void
705
clog_desc(StringInfo buf, uint8 xl_info, char *rec)
706
{
Bruce Momjian's avatar
Bruce Momjian committed
707
	uint8		info = xl_info & ~XLR_INFO_MASK;
708 709 710 711

	if (info == CLOG_ZEROPAGE)
	{
		int			pageno;
712

713
		memcpy(&pageno, rec, sizeof(int));
714
		appendStringInfo(buf, "zeropage: %d", pageno);
715
	}
716 717 718 719 720 721 722
	else if (info == CLOG_TRUNCATE)
	{
		int			pageno;

		memcpy(&pageno, rec, sizeof(int));
		appendStringInfo(buf, "truncate before: %d", pageno);
	}
723
	else
724
		appendStringInfo(buf, "UNKNOWN");
725
}