2001-08-25 20:52:43 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2004-08-24 01:22:45 +02:00
|
|
|
* XLOG interactions: this module generates an XLOG record whenever a new
|
2014-05-06 18:12:18 +02:00
|
|
|
* CLOG page is initialized to zeroes. Other writes of CLOG come from
|
2004-08-24 01:22:45 +02:00
|
|
|
* 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
|
2014-05-06 18:12:18 +02:00
|
|
|
* on redo; so we need make no additional XLOG entry here. For synchronous
|
2007-08-02 00:45:09 +02:00
|
|
|
* 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
|
2014-05-06 18:12:18 +02:00
|
|
|
* XLOG that far and satisfy the WAL rule. We don't have to worry about this
|
2007-08-02 00:45:09 +02:00
|
|
|
* for aborts (whether sync or async), since the post-crash assumption would
|
|
|
|
* be that such transactions failed anyway.
|
2004-08-24 01:22:45 +02:00
|
|
|
*
|
2019-01-02 18:44:25 +01:00
|
|
|
* Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
|
2001-08-25 20:52:43 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/access/transam/clog.c
|
2001-08-25 20:52:43 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/clog.h"
|
2003-06-12 00:37:46 +02:00
|
|
|
#include "access/slru.h"
|
2006-07-13 18:49:20 +02:00
|
|
|
#include "access/transam.h"
|
2014-11-06 12:52:08 +01:00
|
|
|
#include "access/xlog.h"
|
|
|
|
#include "access/xloginsert.h"
|
|
|
|
#include "access/xlogutils.h"
|
2012-01-06 20:30:23 +01:00
|
|
|
#include "miscadmin.h"
|
2017-09-01 17:45:17 +02:00
|
|
|
#include "pgstat.h"
|
2008-08-01 15:16:09 +02:00
|
|
|
#include "pg_trace.h"
|
2017-09-01 17:45:17 +02:00
|
|
|
#include "storage/proc.h"
|
2001-08-25 20:52:43 +02:00
|
|
|
|
|
|
|
/*
|
2004-08-24 01:22:45 +02:00
|
|
|
* Defines for CLOG page sizes. A page is the same BLCKSZ as is used
|
|
|
|
* everywhere else in Postgres.
|
2001-08-25 20:52:43 +02:00
|
|
|
*
|
|
|
|
* Note: because TransactionIds are 32 bits and wrap around at 0xFFFFFFFF,
|
|
|
|
* CLOG page numbering also wraps around at 0xFFFFFFFF/CLOG_XACTS_PER_PAGE,
|
2012-01-30 01:23:56 +01:00
|
|
|
* and CLOG segment numbering at
|
|
|
|
* 0xFFFFFFFF/CLOG_XACTS_PER_PAGE/SLRU_PAGES_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).
|
2001-08-25 20:52:43 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* We need two bits per xact, so four xacts fit in a byte */
|
|
|
|
#define CLOG_BITS_PER_XACT 2
|
2001-10-25 07:50:21 +02:00
|
|
|
#define CLOG_XACTS_PER_BYTE 4
|
2003-06-12 00:37:46 +02:00
|
|
|
#define CLOG_XACTS_PER_PAGE (BLCKSZ * CLOG_XACTS_PER_BYTE)
|
2001-08-25 20:52:43 +02:00
|
|
|
#define CLOG_XACT_BITMASK ((1 << CLOG_BITS_PER_XACT) - 1)
|
|
|
|
|
|
|
|
#define TransactionIdToPage(xid) ((xid) / (TransactionId) CLOG_XACTS_PER_PAGE)
|
2001-10-25 07:50:21 +02:00
|
|
|
#define TransactionIdToPgIndex(xid) ((xid) % (TransactionId) CLOG_XACTS_PER_PAGE)
|
2001-08-25 20:52:43 +02:00
|
|
|
#define TransactionIdToByte(xid) (TransactionIdToPgIndex(xid) / CLOG_XACTS_PER_BYTE)
|
|
|
|
#define TransactionIdToBIndex(xid) ((xid) % (TransactionId) CLOG_XACTS_PER_BYTE)
|
|
|
|
|
2007-08-02 00:45:09 +02:00
|
|
|
/* We store the latest async LSN for each group of transactions */
|
2007-11-15 22:14:46 +01:00
|
|
|
#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)
|
2007-08-02 00:45:09 +02:00
|
|
|
|
|
|
|
#define GetLSNIndex(slotno, xid) ((slotno) * CLOG_LSNS_PER_PAGE + \
|
|
|
|
((xid) % (TransactionId) CLOG_XACTS_PER_PAGE) / CLOG_XACTS_PER_LSN_GROUP)
|
|
|
|
|
2017-09-01 17:45:17 +02:00
|
|
|
/*
|
|
|
|
* The number of subtransactions below which we consider to apply clog group
|
|
|
|
* update optimization. Testing reveals that the number higher than this can
|
|
|
|
* hurt performance.
|
|
|
|
*/
|
|
|
|
#define THRESHOLD_SUBTRANS_CLOG_OPT 5
|
2001-08-25 20:52:43 +02:00
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
/*
|
|
|
|
* Link to shared-memory data structures for CLOG control
|
2001-08-25 20:52:43 +02:00
|
|
|
*/
|
2003-06-12 00:37:46 +02:00
|
|
|
static SlruCtlData ClogCtlData;
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
#define ClogCtl (&ClogCtlData)
|
2003-08-04 02:43:34 +02:00
|
|
|
|
2001-08-25 20:52:43 +02:00
|
|
|
|
|
|
|
static int ZeroCLOGPage(int pageno, bool writeXlog);
|
|
|
|
static bool CLOGPagePrecedes(int page1, int page2);
|
|
|
|
static void WriteZeroPageXlogRec(int pageno);
|
2017-03-23 19:08:23 +01:00
|
|
|
static void WriteTruncateXlogRec(int pageno, TransactionId oldestXact,
|
2019-05-22 19:04:48 +02:00
|
|
|
Oid oldestXidDb);
|
2008-10-20 21:18:18 +02:00
|
|
|
static void TransactionIdSetPageStatus(TransactionId xid, int nsubxids,
|
2019-05-22 19:04:48 +02:00
|
|
|
TransactionId *subxids, XidStatus status,
|
|
|
|
XLogRecPtr lsn, int pageno,
|
|
|
|
bool all_xact_same_page);
|
2008-10-20 21:18:18 +02:00
|
|
|
static void TransactionIdSetStatusBit(TransactionId xid, XidStatus status,
|
2019-05-22 19:04:48 +02:00
|
|
|
XLogRecPtr lsn, int slotno);
|
2008-10-20 21:18:18 +02:00
|
|
|
static void set_status_by_pages(int nsubxids, TransactionId *subxids,
|
2019-05-22 19:04:48 +02:00
|
|
|
XidStatus status, XLogRecPtr lsn);
|
2017-09-01 17:45:17 +02:00
|
|
|
static bool TransactionGroupUpdateXidStatus(TransactionId xid,
|
2019-05-22 19:04:48 +02:00
|
|
|
XidStatus status, XLogRecPtr lsn, int pageno);
|
2017-09-01 17:45:17 +02:00
|
|
|
static void TransactionIdSetPageStatusInternal(TransactionId xid, int nsubxids,
|
2019-05-22 19:04:48 +02:00
|
|
|
TransactionId *subxids, XidStatus status,
|
|
|
|
XLogRecPtr lsn, int pageno);
|
2001-08-25 20:52:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2008-10-20 21:18:18 +02:00
|
|
|
* 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.
|
2001-08-25 20:52:43 +02:00
|
|
|
*
|
2007-08-02 00:45:09 +02:00
|
|
|
* lsn must be the WAL location of the commit record when recording an async
|
2014-05-06 18:12:18 +02:00
|
|
|
* commit. For a synchronous commit it can be InvalidXLogRecPtr, since the
|
2007-08-02 00:45:09 +02:00
|
|
|
* caller guarantees the commit record is already flushed in that case. It
|
|
|
|
* should be InvalidXLogRecPtr for abort cases, too.
|
|
|
|
*
|
2008-10-20 21:18:18 +02:00
|
|
|
* 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
|
2009-06-11 16:49:15 +02:00
|
|
|
* 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
|
2008-10-20 21:18:18 +02:00
|
|
|
* 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:
|
2009-06-11 16:49:15 +02:00
|
|
|
* set t1 as sub-committed,
|
2008-10-20 21:18:18 +02:00
|
|
|
* 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
|
2009-06-11 16:49:15 +02:00
|
|
|
*
|
2001-08-25 20:52:43 +02:00
|
|
|
* NB: this is a low-level routine and is NOT the preferred entry point
|
2008-10-20 21:18:18 +02:00
|
|
|
* for most uses; functions in transam.c are the intended callers.
|
|
|
|
*
|
2019-07-16 06:23:53 +02:00
|
|
|
* XXX Think about issuing POSIX_FADV_WILLNEED on pages that we will need,
|
2008-10-20 21:18:18 +02:00
|
|
|
* but aren't yet in cache, as well as hinting pages not to fall out of
|
|
|
|
* cache yet.
|
2001-08-25 20:52:43 +02:00
|
|
|
*/
|
|
|
|
void
|
2008-10-20 21:18:18 +02:00
|
|
|
TransactionIdSetTreeStatus(TransactionId xid, int nsubxids,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
TransactionId *subxids, XidStatus status, XLogRecPtr lsn)
|
2008-10-20 21:18:18 +02:00
|
|
|
{
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
|
|
|
int pageno = TransactionIdToPage(xid); /* get page of parent */
|
2009-06-11 16:49:15 +02:00
|
|
|
int i;
|
2008-10-20 21:18:18 +02:00
|
|
|
|
|
|
|
Assert(status == TRANSACTION_STATUS_COMMITTED ||
|
|
|
|
status == TRANSACTION_STATUS_ABORTED);
|
|
|
|
|
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* See how many subxids, if any, are on the same page as the parent, if
|
|
|
|
* any.
|
2008-10-20 21:18:18 +02:00
|
|
|
*/
|
|
|
|
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,
|
2017-09-01 17:45:17 +02:00
|
|
|
pageno, true);
|
2008-10-20 21:18:18 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
int nsubxids_on_first_page = i;
|
2008-10-20 21:18:18 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If this is a commit then we care about doing this correctly (i.e.
|
2009-06-11 16:49:15 +02:00
|
|
|
* 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.
|
2008-10-20 21:18:18 +02:00
|
|
|
*
|
|
|
|
* 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,
|
2017-09-01 17:45:17 +02:00
|
|
|
lsn, pageno, false);
|
2008-10-20 21:18:18 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
int pageno = TransactionIdToPage(subxids[0]);
|
|
|
|
int offset = 0;
|
|
|
|
int i = 0;
|
2008-10-20 21:18:18 +02:00
|
|
|
|
2017-10-06 18:20:12 +02:00
|
|
|
Assert(nsubxids > 0); /* else the pageno fetch above is unsafe */
|
|
|
|
|
2008-10-20 21:18:18 +02:00
|
|
|
while (i < nsubxids)
|
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
int num_on_page = 0;
|
2017-10-06 18:20:12 +02:00
|
|
|
int nextpageno;
|
2008-10-20 21:18:18 +02:00
|
|
|
|
2017-10-06 18:20:12 +02:00
|
|
|
do
|
2008-10-20 21:18:18 +02:00
|
|
|
{
|
2017-10-06 18:20:12 +02:00
|
|
|
nextpageno = TransactionIdToPage(subxids[i]);
|
|
|
|
if (nextpageno != pageno)
|
|
|
|
break;
|
2008-10-20 21:18:18 +02:00
|
|
|
num_on_page++;
|
|
|
|
i++;
|
2017-10-06 18:20:12 +02:00
|
|
|
} while (i < nsubxids);
|
2008-10-20 21:18:18 +02:00
|
|
|
|
|
|
|
TransactionIdSetPageStatus(InvalidTransactionId,
|
|
|
|
num_on_page, subxids + offset,
|
2017-09-01 17:45:17 +02:00
|
|
|
status, lsn, pageno, false);
|
2008-10-20 21:18:18 +02:00
|
|
|
offset = i;
|
2017-10-06 18:20:12 +02:00
|
|
|
pageno = nextpageno;
|
2008-10-20 21:18:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-09-01 17:45:17 +02:00
|
|
|
* Record the final state of transaction entries in the commit log for all
|
|
|
|
* entries on a single page. Atomic only on this page.
|
2008-10-20 21:18:18 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
TransactionIdSetPageStatus(TransactionId xid, int nsubxids,
|
|
|
|
TransactionId *subxids, XidStatus status,
|
2017-09-01 17:45:17 +02:00
|
|
|
XLogRecPtr lsn, int pageno,
|
|
|
|
bool all_xact_same_page)
|
|
|
|
{
|
|
|
|
/* Can't use group update when PGPROC overflows. */
|
|
|
|
StaticAssertStmt(THRESHOLD_SUBTRANS_CLOG_OPT <= PGPROC_MAX_CACHED_SUBXIDS,
|
|
|
|
"group clog threshold less than PGPROC cached subxids");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When there is contention on CLogControlLock, we try to group multiple
|
|
|
|
* updates; a single leader process will perform transaction status
|
|
|
|
* updates for multiple backends so that the number of times
|
|
|
|
* CLogControlLock needs to be acquired is reduced.
|
|
|
|
*
|
|
|
|
* For this optimization to be safe, the XID in MyPgXact and the subxids
|
|
|
|
* in MyProc must be the same as the ones for which we're setting the
|
|
|
|
* status. Check that this is the case.
|
|
|
|
*
|
|
|
|
* For this optimization to be efficient, we shouldn't have too many
|
|
|
|
* sub-XIDs and all of the XIDs for which we're adjusting clog should be
|
|
|
|
* on the same page. Check those conditions, too.
|
|
|
|
*/
|
|
|
|
if (all_xact_same_page && xid == MyPgXact->xid &&
|
|
|
|
nsubxids <= THRESHOLD_SUBTRANS_CLOG_OPT &&
|
|
|
|
nsubxids == MyPgXact->nxids &&
|
|
|
|
memcmp(subxids, MyProc->subxids.xids,
|
|
|
|
nsubxids * sizeof(TransactionId)) == 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We don't try to do group update optimization if a process has
|
|
|
|
* overflowed the subxids array in its PGPROC, since in that case we
|
|
|
|
* don't have a complete list of XIDs for it.
|
|
|
|
*/
|
|
|
|
Assert(THRESHOLD_SUBTRANS_CLOG_OPT <= PGPROC_MAX_CACHED_SUBXIDS);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we can immediately acquire CLogControlLock, we update the status
|
|
|
|
* of our own XID and release the lock. If not, try use group XID
|
|
|
|
* update. If that doesn't work out, fall back to waiting for the
|
|
|
|
* lock to perform an update for this transaction only.
|
|
|
|
*/
|
|
|
|
if (LWLockConditionalAcquire(CLogControlLock, LW_EXCLUSIVE))
|
|
|
|
{
|
|
|
|
/* Got the lock without waiting! Do the update. */
|
|
|
|
TransactionIdSetPageStatusInternal(xid, nsubxids, subxids, status,
|
|
|
|
lsn, pageno);
|
|
|
|
LWLockRelease(CLogControlLock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (TransactionGroupUpdateXidStatus(xid, status, lsn, pageno))
|
|
|
|
{
|
|
|
|
/* Group update mechanism has done the work. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fall through only if update isn't done yet. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Group update not applicable, or couldn't accept this page number. */
|
|
|
|
LWLockAcquire(CLogControlLock, LW_EXCLUSIVE);
|
|
|
|
TransactionIdSetPageStatusInternal(xid, nsubxids, subxids, status,
|
|
|
|
lsn, pageno);
|
|
|
|
LWLockRelease(CLogControlLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Record the final state of transaction entry in the commit log
|
|
|
|
*
|
|
|
|
* We don't do any locking here; caller must handle that.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
TransactionIdSetPageStatusInternal(TransactionId xid, int nsubxids,
|
|
|
|
TransactionId *subxids, XidStatus status,
|
|
|
|
XLogRecPtr lsn, int pageno)
|
2001-08-25 20:52:43 +02:00
|
|
|
{
|
2004-08-24 01:22:45 +02:00
|
|
|
int slotno;
|
2009-06-11 16:49:15 +02:00
|
|
|
int i;
|
2001-08-25 20:52:43 +02:00
|
|
|
|
|
|
|
Assert(status == TRANSACTION_STATUS_COMMITTED ||
|
2004-07-01 02:52:04 +02:00
|
|
|
status == TRANSACTION_STATUS_ABORTED ||
|
2008-10-20 21:18:18 +02:00
|
|
|
(status == TRANSACTION_STATUS_SUB_COMMITTED && !TransactionIdIsValid(xid)));
|
2017-09-01 17:45:17 +02:00
|
|
|
Assert(LWLockHeldByMeInMode(CLogControlLock, LW_EXCLUSIVE));
|
2001-08-25 20:52:43 +02:00
|
|
|
|
2007-08-02 00:45:09 +02:00
|
|
|
/*
|
|
|
|
* 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
|
2008-10-20 21:18:18 +02:00
|
|
|
* we think.
|
2007-08-02 00:45:09 +02:00
|
|
|
*/
|
|
|
|
slotno = SimpleLruReadPage(ClogCtl, pageno, XLogRecPtrIsInvalid(lsn), xid);
|
2008-10-20 21:18:18 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
2009-06-11 16:49:15 +02:00
|
|
|
* 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.
|
2008-10-20 21:18:18 +02:00
|
|
|
*/
|
|
|
|
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;
|
2017-09-01 17:45:17 +02:00
|
|
|
}
|
2008-10-20 21:18:18 +02:00
|
|
|
|
2017-09-01 17:45:17 +02:00
|
|
|
/*
|
|
|
|
* When we cannot immediately acquire CLogControlLock in exclusive mode at
|
|
|
|
* commit time, add ourselves to a list of processes that need their XIDs
|
|
|
|
* status update. The first process to add itself to the list will acquire
|
|
|
|
* CLogControlLock in exclusive mode and set transaction status as required
|
|
|
|
* on behalf of all group members. This avoids a great deal of contention
|
|
|
|
* around CLogControlLock when many processes are trying to commit at once,
|
|
|
|
* since the lock need not be repeatedly handed off from one committing
|
|
|
|
* process to the next.
|
|
|
|
*
|
|
|
|
* Returns true when transaction status has been updated in clog; returns
|
|
|
|
* false if we decided against applying the optimization because the page
|
|
|
|
* number we need to update differs from those processes already waiting.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
TransactionGroupUpdateXidStatus(TransactionId xid, XidStatus status,
|
|
|
|
XLogRecPtr lsn, int pageno)
|
|
|
|
{
|
|
|
|
volatile PROC_HDR *procglobal = ProcGlobal;
|
|
|
|
PGPROC *proc = MyProc;
|
|
|
|
uint32 nextidx;
|
|
|
|
uint32 wakeidx;
|
|
|
|
|
|
|
|
/* We should definitely have an XID whose status needs to be updated. */
|
|
|
|
Assert(TransactionIdIsValid(xid));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add ourselves to the list of processes needing a group XID status
|
|
|
|
* update.
|
|
|
|
*/
|
|
|
|
proc->clogGroupMember = true;
|
|
|
|
proc->clogGroupMemberXid = xid;
|
|
|
|
proc->clogGroupMemberXidStatus = status;
|
|
|
|
proc->clogGroupMemberPage = pageno;
|
|
|
|
proc->clogGroupMemberLsn = lsn;
|
|
|
|
|
|
|
|
nextidx = pg_atomic_read_u32(&procglobal->clogGroupFirst);
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Add the proc to list, if the clog page where we need to update the
|
|
|
|
* current transaction status is same as group leader's clog page.
|
|
|
|
*
|
|
|
|
* There is a race condition here, which is that after doing the below
|
|
|
|
* check and before adding this proc's clog update to a group, the
|
|
|
|
* group leader might have already finished the group update for this
|
|
|
|
* page and becomes group leader of another group. This will lead to a
|
|
|
|
* situation where a single group can have different clog page
|
|
|
|
* updates. This isn't likely and will still work, just maybe a bit
|
|
|
|
* less efficiently.
|
|
|
|
*/
|
|
|
|
if (nextidx != INVALID_PGPROCNO &&
|
|
|
|
ProcGlobal->allProcs[nextidx].clogGroupMemberPage != proc->clogGroupMemberPage)
|
|
|
|
{
|
|
|
|
proc->clogGroupMember = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pg_atomic_write_u32(&proc->clogGroupNext, nextidx);
|
|
|
|
|
|
|
|
if (pg_atomic_compare_exchange_u32(&procglobal->clogGroupFirst,
|
|
|
|
&nextidx,
|
|
|
|
(uint32) proc->pgprocno))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the list was not empty, the leader will update the status of our
|
|
|
|
* XID. It is impossible to have followers without a leader because the
|
|
|
|
* first process that has added itself to the list will always have
|
|
|
|
* nextidx as INVALID_PGPROCNO.
|
|
|
|
*/
|
|
|
|
if (nextidx != INVALID_PGPROCNO)
|
|
|
|
{
|
|
|
|
int extraWaits = 0;
|
|
|
|
|
|
|
|
/* Sleep until the leader updates our XID status. */
|
|
|
|
pgstat_report_wait_start(WAIT_EVENT_CLOG_GROUP_UPDATE);
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* acts as a read barrier */
|
|
|
|
PGSemaphoreLock(proc->sem);
|
|
|
|
if (!proc->clogGroupMember)
|
|
|
|
break;
|
|
|
|
extraWaits++;
|
|
|
|
}
|
|
|
|
pgstat_report_wait_end();
|
|
|
|
|
|
|
|
Assert(pg_atomic_read_u32(&proc->clogGroupNext) == INVALID_PGPROCNO);
|
|
|
|
|
|
|
|
/* Fix semaphore count for any absorbed wakeups */
|
|
|
|
while (extraWaits-- > 0)
|
|
|
|
PGSemaphoreUnlock(proc->sem);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are the leader. Acquire the lock on behalf of everyone. */
|
|
|
|
LWLockAcquire(CLogControlLock, LW_EXCLUSIVE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now that we've got the lock, clear the list of processes waiting for
|
|
|
|
* group XID status update, saving a pointer to the head of the list.
|
|
|
|
* Trying to pop elements one at a time could lead to an ABA problem.
|
|
|
|
*/
|
|
|
|
nextidx = pg_atomic_exchange_u32(&procglobal->clogGroupFirst,
|
|
|
|
INVALID_PGPROCNO);
|
|
|
|
|
|
|
|
/* Remember head of list so we can perform wakeups after dropping lock. */
|
|
|
|
wakeidx = nextidx;
|
|
|
|
|
|
|
|
/* Walk the list and update the status of all XIDs. */
|
|
|
|
while (nextidx != INVALID_PGPROCNO)
|
|
|
|
{
|
|
|
|
PGPROC *proc = &ProcGlobal->allProcs[nextidx];
|
|
|
|
PGXACT *pgxact = &ProcGlobal->allPgXact[nextidx];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Overflowed transactions should not use group XID status update
|
|
|
|
* mechanism.
|
|
|
|
*/
|
|
|
|
Assert(!pgxact->overflowed);
|
|
|
|
|
|
|
|
TransactionIdSetPageStatusInternal(proc->clogGroupMemberXid,
|
|
|
|
pgxact->nxids,
|
|
|
|
proc->subxids.xids,
|
|
|
|
proc->clogGroupMemberXidStatus,
|
|
|
|
proc->clogGroupMemberLsn,
|
|
|
|
proc->clogGroupMemberPage);
|
|
|
|
|
|
|
|
/* Move to next proc in list. */
|
|
|
|
nextidx = pg_atomic_read_u32(&proc->clogGroupNext);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We're done with the lock now. */
|
2008-10-20 21:18:18 +02:00
|
|
|
LWLockRelease(CLogControlLock);
|
2017-09-01 17:45:17 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now that we've released the lock, go back and wake everybody up. We
|
|
|
|
* don't do this under the lock so as to keep lock hold times to a
|
|
|
|
* minimum.
|
|
|
|
*/
|
|
|
|
while (wakeidx != INVALID_PGPROCNO)
|
|
|
|
{
|
|
|
|
PGPROC *proc = &ProcGlobal->allProcs[wakeidx];
|
|
|
|
|
|
|
|
wakeidx = pg_atomic_read_u32(&proc->clogGroupNext);
|
|
|
|
pg_atomic_write_u32(&proc->clogGroupNext, INVALID_PGPROCNO);
|
|
|
|
|
|
|
|
/* ensure all previous writes are visible before follower continues. */
|
|
|
|
pg_write_barrier();
|
|
|
|
|
|
|
|
proc->clogGroupMember = false;
|
|
|
|
|
|
|
|
if (proc != MyProc)
|
|
|
|
PGSemaphoreUnlock(proc->sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2008-10-20 21:18:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2008-11-03 20:24:03 +01:00
|
|
|
char curval;
|
2008-10-20 21:18:18 +02:00
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
byteptr = ClogCtl->shared->page_buffer[slotno] + byteno;
|
2008-11-03 20:26:07 +01:00
|
|
|
curval = (*byteptr >> bshift) & CLOG_XACT_BITMASK;
|
2001-08-25 20:52:43 +02:00
|
|
|
|
2008-11-03 20:24:03 +01:00
|
|
|
/*
|
|
|
|
* When replaying transactions during recovery we still need to perform
|
|
|
|
* the two phases of subcommit and then commit. However, some transactions
|
|
|
|
* are already correctly marked, so we just treat those as a no-op which
|
|
|
|
* allows us to keep the following Assert as restrictive as possible.
|
|
|
|
*/
|
|
|
|
if (InRecovery && status == TRANSACTION_STATUS_SUB_COMMITTED &&
|
|
|
|
curval == TRANSACTION_STATUS_COMMITTED)
|
|
|
|
return;
|
|
|
|
|
2009-06-11 16:49:15 +02:00
|
|
|
/*
|
2008-11-03 20:24:03 +01:00
|
|
|
* Current state change should be from 0 or subcommitted to target state
|
|
|
|
* or we should already be there when replaying changes during recovery.
|
|
|
|
*/
|
|
|
|
Assert(curval == 0 ||
|
|
|
|
(curval == TRANSACTION_STATUS_SUB_COMMITTED &&
|
|
|
|
status != TRANSACTION_STATUS_IN_PROGRESS) ||
|
|
|
|
curval == status);
|
2001-08-25 20:52:43 +02:00
|
|
|
|
2004-07-03 04:55:56 +02:00
|
|
|
/* note this assumes exclusive access to the clog page */
|
|
|
|
byteval = *byteptr;
|
|
|
|
byteval &= ~(((1 << CLOG_BITS_PER_XACT) - 1) << bshift);
|
|
|
|
byteval |= (status << bshift);
|
|
|
|
*byteptr = byteval;
|
2001-08-25 20:52:43 +02:00
|
|
|
|
2007-08-02 00:45:09 +02:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
|
2012-12-28 17:06:15 +01:00
|
|
|
if (ClogCtl->shared->group_lsn[lsnindex] < lsn)
|
2007-08-02 00:45:09 +02:00
|
|
|
ClogCtl->shared->group_lsn[lsnindex] = lsn;
|
|
|
|
}
|
2001-08-25 20:52:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Interrogate the state of a transaction in the commit log.
|
|
|
|
*
|
2007-08-02 00:45:09 +02:00
|
|
|
* 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
|
2007-11-15 22:14:46 +01:00
|
|
|
* record! For example, for long-past transactions (those whose clog pages
|
2007-08-02 00:45:09 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2001-08-25 20:52:43 +02:00
|
|
|
* NB: this is a low-level routine and is NOT the preferred entry point
|
2004-07-01 02:52:04 +02:00
|
|
|
* for most uses; TransactionLogFetch() in transam.c is the intended caller.
|
2001-08-25 20:52:43 +02:00
|
|
|
*/
|
|
|
|
XidStatus
|
2007-08-02 00:45:09 +02:00
|
|
|
TransactionIdGetStatus(TransactionId xid, XLogRecPtr *lsn)
|
2001-08-25 20:52:43 +02:00
|
|
|
{
|
|
|
|
int pageno = TransactionIdToPage(xid);
|
|
|
|
int byteno = TransactionIdToByte(xid);
|
|
|
|
int bshift = TransactionIdToBIndex(xid) * CLOG_BITS_PER_XACT;
|
2004-08-24 01:22:45 +02:00
|
|
|
int slotno;
|
2007-08-02 00:45:09 +02:00
|
|
|
int lsnindex;
|
2001-08-25 20:52:43 +02:00
|
|
|
char *byteptr;
|
|
|
|
XidStatus status;
|
|
|
|
|
2005-12-06 19:10:06 +01:00
|
|
|
/* lock is acquired by SimpleLruReadPage_ReadOnly */
|
2001-08-25 20:52:43 +02:00
|
|
|
|
2005-12-06 19:10:06 +01:00
|
|
|
slotno = SimpleLruReadPage_ReadOnly(ClogCtl, pageno, xid);
|
2004-08-24 01:22:45 +02:00
|
|
|
byteptr = ClogCtl->shared->page_buffer[slotno] + byteno;
|
2001-08-25 20:52:43 +02:00
|
|
|
|
|
|
|
status = (*byteptr >> bshift) & CLOG_XACT_BITMASK;
|
|
|
|
|
2007-08-02 00:45:09 +02:00
|
|
|
lsnindex = GetLSNIndex(slotno, xid);
|
|
|
|
*lsn = ClogCtl->shared->group_lsn[lsnindex];
|
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
LWLockRelease(CLogControlLock);
|
2001-08-25 20:52:43 +02:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2012-01-06 20:30:23 +01:00
|
|
|
/*
|
|
|
|
* Number of shared CLOG buffers.
|
|
|
|
*
|
2016-04-08 17:18:52 +02:00
|
|
|
* On larger multi-processor systems, it is possible to have many CLOG page
|
|
|
|
* requests in flight at one time which could lead to disk access for CLOG
|
|
|
|
* page if the required page is not found in memory. Testing revealed that we
|
|
|
|
* can get the best performance by having 128 CLOG buffers, more than that it
|
|
|
|
* doesn't improve performance.
|
2012-01-06 20:30:23 +01:00
|
|
|
*
|
2016-04-08 17:18:52 +02:00
|
|
|
* Unconditionally keeping the number of CLOG buffers to 128 did not seem like
|
|
|
|
* a good idea, because it would increase the minimum amount of shared memory
|
|
|
|
* required to start, which could be a problem for people running very small
|
|
|
|
* configurations. The following formula seems to represent a reasonable
|
2012-01-06 20:30:23 +01:00
|
|
|
* compromise: people with very low values for shared_buffers will get fewer
|
2016-04-08 17:18:52 +02:00
|
|
|
* CLOG buffers as well, and everyone else will get 128.
|
2012-01-06 20:30:23 +01:00
|
|
|
*/
|
|
|
|
Size
|
|
|
|
CLOGShmemBuffers(void)
|
|
|
|
{
|
2016-04-08 17:18:52 +02:00
|
|
|
return Min(128, Max(4, NBuffers / 512));
|
2012-01-06 20:30:23 +01:00
|
|
|
}
|
2001-08-25 20:52:43 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialization of shared memory for CLOG
|
|
|
|
*/
|
2005-08-21 01:26:37 +02:00
|
|
|
Size
|
2001-08-25 20:52:43 +02:00
|
|
|
CLOGShmemSize(void)
|
|
|
|
{
|
2012-01-06 20:30:23 +01:00
|
|
|
return SimpleLruShmemSize(CLOGShmemBuffers(), CLOG_LSNS_PER_PAGE);
|
2001-08-25 20:52:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CLOGShmemInit(void)
|
|
|
|
{
|
2003-06-12 00:37:46 +02:00
|
|
|
ClogCtl->PagePrecedes = CLOGPagePrecedes;
|
2015-11-12 20:59:09 +01:00
|
|
|
SimpleLruInit(ClogCtl, "clog", CLOGShmemBuffers(), CLOG_LSNS_PER_PAGE,
|
2017-03-17 14:46:58 +01:00
|
|
|
CLogControlLock, "pg_xact", LWTRANCHE_CLOG_BUFFERS);
|
2001-08-25 20:52:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This func must be called ONCE on system install. It creates
|
|
|
|
* the initial CLOG segment. (The CLOG directory is assumed to
|
2011-02-02 01:07:42 +01:00
|
|
|
* have been created by initdb, and CLOGShmemInit must have been
|
|
|
|
* called already.)
|
2001-08-25 20:52:43 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
BootStrapCLOG(void)
|
|
|
|
{
|
|
|
|
int slotno;
|
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
LWLockAcquire(CLogControlLock, LW_EXCLUSIVE);
|
2001-08-25 20:52:43 +02:00
|
|
|
|
|
|
|
/* Create and zero the first page of the commit log */
|
|
|
|
slotno = ZeroCLOGPage(0, false);
|
|
|
|
|
|
|
|
/* Make sure it's written out */
|
2010-12-30 16:09:17 +01:00
|
|
|
SimpleLruWritePage(ClogCtl, slotno);
|
2005-11-05 22:19:47 +01:00
|
|
|
Assert(!ClogCtl->shared->page_dirty[slotno]);
|
2001-08-25 20:52:43 +02:00
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
LWLockRelease(CLogControlLock);
|
2001-08-25 20:52:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize (or reinitialize) a page of CLOG to zeroes.
|
2017-08-16 06:22:32 +02:00
|
|
|
* If writeXlog is true, also emit an XLOG record saying we did this.
|
2001-08-25 20:52:43 +02:00
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
{
|
2004-08-24 01:22:45 +02:00
|
|
|
int slotno;
|
|
|
|
|
|
|
|
slotno = SimpleLruZeroPage(ClogCtl, pageno);
|
2001-08-25 20:52:43 +02:00
|
|
|
|
|
|
|
if (writeXlog)
|
|
|
|
WriteZeroPageXlogRec(pageno);
|
|
|
|
|
|
|
|
return slotno;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This must be called ONCE during postmaster or standalone-backend startup,
|
2019-03-27 22:34:43 +01:00
|
|
|
* after StartupXLOG has initialized ShmemVariableCache->nextFullXid.
|
2001-08-25 20:52:43 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
StartupCLOG(void)
|
|
|
|
{
|
2019-03-27 22:34:43 +01:00
|
|
|
TransactionId xid = XidFromFullTransactionId(ShmemVariableCache->nextFullXid);
|
2004-08-30 21:00:42 +02:00
|
|
|
int pageno = TransactionIdToPage(xid);
|
|
|
|
|
|
|
|
LWLockAcquire(CLogControlLock, LW_EXCLUSIVE);
|
|
|
|
|
2001-08-25 20:52:43 +02:00
|
|
|
/*
|
|
|
|
* Initialize our idea of the latest page number.
|
|
|
|
*/
|
2004-08-30 21:00:42 +02:00
|
|
|
ClogCtl->shared->latest_page_number = pageno;
|
|
|
|
|
2011-11-02 09:07:44 +01:00
|
|
|
LWLockRelease(CLogControlLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This must be called ONCE at the end of startup/recovery.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
TrimCLOG(void)
|
|
|
|
{
|
2019-03-27 22:34:43 +01:00
|
|
|
TransactionId xid = XidFromFullTransactionId(ShmemVariableCache->nextFullXid);
|
2011-11-02 09:07:44 +01:00
|
|
|
int pageno = TransactionIdToPage(xid);
|
|
|
|
|
|
|
|
LWLockAcquire(CLogControlLock, LW_EXCLUSIVE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Re-Initialize our idea of the latest page number.
|
|
|
|
*/
|
|
|
|
ClogCtl->shared->latest_page_number = pageno;
|
|
|
|
|
2004-08-30 21:00:42 +02:00
|
|
|
/*
|
|
|
|
* Zero out the remainder of the current clog page. Under normal
|
|
|
|
* circumstances it should be zeroes already, but it seems at least
|
2005-10-15 04:49:52 +02:00
|
|
|
* 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
|
2019-03-27 22:34:43 +01:00
|
|
|
* nextFullXid is exactly at a page boundary; and it's likely that the
|
2005-10-15 04:49:52 +02:00
|
|
|
* "current" page doesn't exist yet in that case.)
|
2004-08-30 21:00:42 +02:00
|
|
|
*/
|
2004-12-22 19:45:49 +01:00
|
|
|
if (TransactionIdToPgIndex(xid) != 0)
|
|
|
|
{
|
|
|
|
int byteno = TransactionIdToByte(xid);
|
|
|
|
int bshift = TransactionIdToBIndex(xid) * CLOG_BITS_PER_XACT;
|
|
|
|
int slotno;
|
|
|
|
char *byteptr;
|
|
|
|
|
2007-08-02 00:45:09 +02:00
|
|
|
slotno = SimpleLruReadPage(ClogCtl, pageno, false, xid);
|
2004-12-22 19:45:49 +01:00
|
|
|
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);
|
|
|
|
|
2005-11-05 22:19:47 +01:00
|
|
|
ClogCtl->shared->page_dirty[slotno] = true;
|
2004-12-22 19:45:49 +01:00
|
|
|
}
|
2004-08-30 21:00:42 +02:00
|
|
|
|
|
|
|
LWLockRelease(CLogControlLock);
|
2001-08-25 20:52:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This must be called ONCE during postmaster or standalone-backend shutdown
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ShutdownCLOG(void)
|
|
|
|
{
|
2004-08-24 01:22:45 +02:00
|
|
|
/* Flush dirty CLOG pages to disk */
|
2008-08-01 15:16:09 +02:00
|
|
|
TRACE_POSTGRESQL_CLOG_CHECKPOINT_START(false);
|
2003-06-12 00:37:46 +02:00
|
|
|
SimpleLruFlush(ClogCtl, false);
|
2017-03-27 18:33:01 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* fsync pg_xact to ensure that any files flushed previously are durably
|
|
|
|
* on disk.
|
|
|
|
*/
|
|
|
|
fsync_fname("pg_xact", true);
|
|
|
|
|
2008-08-01 15:16:09 +02:00
|
|
|
TRACE_POSTGRESQL_CLOG_CHECKPOINT_DONE(false);
|
2001-08-25 20:52:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform a checkpoint --- either during shutdown, or on-the-fly
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
CheckPointCLOG(void)
|
|
|
|
{
|
2004-08-24 01:22:45 +02:00
|
|
|
/* Flush dirty CLOG pages to disk */
|
2008-08-01 15:16:09 +02:00
|
|
|
TRACE_POSTGRESQL_CLOG_CHECKPOINT_START(true);
|
2003-06-12 00:37:46 +02:00
|
|
|
SimpleLruFlush(ClogCtl, true);
|
2017-03-27 18:33:01 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* fsync pg_xact to ensure that any files flushed previously are durably
|
|
|
|
* on disk.
|
|
|
|
*/
|
|
|
|
fsync_fname("pg_xact", true);
|
|
|
|
|
2008-08-01 15:16:09 +02:00
|
|
|
TRACE_POSTGRESQL_CLOG_CHECKPOINT_DONE(true);
|
2001-08-25 20:52:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2001-08-26 18:56:03 +02:00
|
|
|
/*
|
|
|
|
* 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))
|
2001-08-25 20:52:43 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
pageno = TransactionIdToPage(newestXact);
|
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
LWLockAcquire(CLogControlLock, LW_EXCLUSIVE);
|
2001-08-25 20:52:43 +02:00
|
|
|
|
|
|
|
/* Zero the page and make an XLOG entry about it */
|
2013-11-22 13:38:59 +01:00
|
|
|
ZeroCLOGPage(pageno, true);
|
2001-08-25 20:52:43 +02:00
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
LWLockRelease(CLogControlLock);
|
2001-08-25 20:52:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove all CLOG segments before the one holding the passed transaction ID
|
|
|
|
*
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
* 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.
|
2001-08-25 20:52:43 +02:00
|
|
|
*/
|
|
|
|
void
|
2017-03-23 19:08:23 +01:00
|
|
|
TruncateCLOG(TransactionId oldestXact, Oid oldestxid_datoid)
|
2001-08-25 20:52:43 +02:00
|
|
|
{
|
|
|
|
int cutoffPage;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* The cutoff point is the start of the segment containing oldestXact. We
|
|
|
|
* pass the *page* containing oldestXact to SimpleLruTruncate.
|
2001-08-25 20:52:43 +02:00
|
|
|
*/
|
|
|
|
cutoffPage = TransactionIdToPage(oldestXact);
|
2004-08-24 01:22:45 +02:00
|
|
|
|
|
|
|
/* Check to see if there's any files that could be removed */
|
2011-09-28 16:32:38 +02:00
|
|
|
if (!SlruScanDirectory(ClogCtl, SlruScanDirCbReportPresence, &cutoffPage))
|
2004-08-24 01:22:45 +02:00
|
|
|
return; /* nothing to remove */
|
|
|
|
|
2017-03-23 19:08:23 +01:00
|
|
|
/*
|
|
|
|
* Advance oldestClogXid before truncating clog, so concurrent xact status
|
|
|
|
* lookups can ensure they don't attempt to access truncated-away clog.
|
|
|
|
*
|
|
|
|
* It's only necessary to do this if we will actually truncate away clog
|
|
|
|
* pages.
|
|
|
|
*/
|
|
|
|
AdvanceOldestClogXid(oldestXact);
|
|
|
|
|
|
|
|
/*
|
2017-05-17 22:31:56 +02:00
|
|
|
* Write XLOG record and flush XLOG to disk. We record the oldest xid
|
|
|
|
* we're keeping information about here so we can ensure that it's always
|
|
|
|
* ahead of clog truncation in case we crash, and so a standby finds out
|
|
|
|
* the new valid xid before the next checkpoint.
|
2017-03-23 19:08:23 +01:00
|
|
|
*/
|
|
|
|
WriteTruncateXlogRec(cutoffPage, oldestXact, oldestxid_datoid);
|
2004-08-24 01:22:45 +02:00
|
|
|
|
|
|
|
/* Now we can remove the old CLOG segment(s) */
|
2003-06-12 00:37:46 +02:00
|
|
|
SimpleLruTruncate(ClogCtl, cutoffPage);
|
2001-08-25 20:52:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2001-10-25 22:37:30 +02:00
|
|
|
xid1 = ((TransactionId) page1) * CLOG_XACTS_PER_PAGE;
|
2001-08-25 20:52:43 +02:00
|
|
|
xid1 += FirstNormalTransactionId;
|
2001-10-25 22:37:30 +02:00
|
|
|
xid2 = ((TransactionId) page2) * CLOG_XACTS_PER_PAGE;
|
2001-08-25 20:52:43 +02:00
|
|
|
xid2 += FirstNormalTransactionId;
|
|
|
|
|
|
|
|
return TransactionIdPrecedes(xid1, xid2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a ZEROPAGE xlog record
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
WriteZeroPageXlogRec(int pageno)
|
|
|
|
{
|
Revamp the WAL record format.
Each WAL record now carries information about the modified relation and
block(s) in a standardized format. That makes it easier to write tools that
need that information, like pg_rewind, prefetching the blocks to speed up
recovery, etc.
There's a whole new API for building WAL records, replacing the XLogRecData
chains used previously. The new API consists of XLogRegister* functions,
which are called for each buffer and chunk of data that is added to the
record. The new API also gives more control over when a full-page image is
written, by passing flags to the XLogRegisterBuffer function.
This also simplifies the XLogReadBufferForRedo() calls. The function can dig
the relation and block number from the WAL record, so they no longer need to
be passed as arguments.
For the convenience of redo routines, XLogReader now disects each WAL record
after reading it, copying the main data part and the per-block data into
MAXALIGNed buffers. The data chunks are not aligned within the WAL record,
but the redo routines can assume that the pointers returned by XLogRecGet*
functions are. Redo routines are now passed the XLogReaderState, which
contains the record in the already-disected format, instead of the plain
XLogRecord.
The new record format also makes the fixed size XLogRecord header smaller,
by removing the xl_len field. The length of the "main data" portion is now
stored at the end of the WAL record, and there's a separate header after
XLogRecord for it. The alignment padding at the end of XLogRecord is also
removed. This compansates for the fact that the new format would otherwise
be more bulky than the old format.
Reviewed by Andres Freund, Amit Kapila, Michael Paquier, Alvaro Herrera,
Fujii Masao.
2014-11-20 16:56:26 +01:00
|
|
|
XLogBeginInsert();
|
|
|
|
XLogRegisterData((char *) (&pageno), sizeof(int));
|
|
|
|
(void) XLogInsert(RM_CLOG_ID, CLOG_ZEROPAGE);
|
2001-08-25 20:52:43 +02:00
|
|
|
}
|
|
|
|
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
/*
|
|
|
|
* Write a TRUNCATE xlog record
|
|
|
|
*
|
|
|
|
* We must flush the xlog record to disk before returning --- see notes
|
|
|
|
* in TruncateCLOG().
|
|
|
|
*/
|
|
|
|
static void
|
2017-03-23 19:08:23 +01:00
|
|
|
WriteTruncateXlogRec(int pageno, TransactionId oldestXact, Oid oldestXactDb)
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
{
|
|
|
|
XLogRecPtr recptr;
|
2017-03-23 19:08:23 +01:00
|
|
|
xl_clog_truncate xlrec;
|
|
|
|
|
|
|
|
xlrec.pageno = pageno;
|
|
|
|
xlrec.oldestXact = oldestXact;
|
|
|
|
xlrec.oldestXactDb = oldestXactDb;
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
|
Revamp the WAL record format.
Each WAL record now carries information about the modified relation and
block(s) in a standardized format. That makes it easier to write tools that
need that information, like pg_rewind, prefetching the blocks to speed up
recovery, etc.
There's a whole new API for building WAL records, replacing the XLogRecData
chains used previously. The new API consists of XLogRegister* functions,
which are called for each buffer and chunk of data that is added to the
record. The new API also gives more control over when a full-page image is
written, by passing flags to the XLogRegisterBuffer function.
This also simplifies the XLogReadBufferForRedo() calls. The function can dig
the relation and block number from the WAL record, so they no longer need to
be passed as arguments.
For the convenience of redo routines, XLogReader now disects each WAL record
after reading it, copying the main data part and the per-block data into
MAXALIGNed buffers. The data chunks are not aligned within the WAL record,
but the redo routines can assume that the pointers returned by XLogRecGet*
functions are. Redo routines are now passed the XLogReaderState, which
contains the record in the already-disected format, instead of the plain
XLogRecord.
The new record format also makes the fixed size XLogRecord header smaller,
by removing the xl_len field. The length of the "main data" portion is now
stored at the end of the WAL record, and there's a separate header after
XLogRecord for it. The alignment padding at the end of XLogRecord is also
removed. This compansates for the fact that the new format would otherwise
be more bulky than the old format.
Reviewed by Andres Freund, Amit Kapila, Michael Paquier, Alvaro Herrera,
Fujii Masao.
2014-11-20 16:56:26 +01:00
|
|
|
XLogBeginInsert();
|
2017-03-23 19:08:23 +01:00
|
|
|
XLogRegisterData((char *) (&xlrec), sizeof(xl_clog_truncate));
|
Revamp the WAL record format.
Each WAL record now carries information about the modified relation and
block(s) in a standardized format. That makes it easier to write tools that
need that information, like pg_rewind, prefetching the blocks to speed up
recovery, etc.
There's a whole new API for building WAL records, replacing the XLogRecData
chains used previously. The new API consists of XLogRegister* functions,
which are called for each buffer and chunk of data that is added to the
record. The new API also gives more control over when a full-page image is
written, by passing flags to the XLogRegisterBuffer function.
This also simplifies the XLogReadBufferForRedo() calls. The function can dig
the relation and block number from the WAL record, so they no longer need to
be passed as arguments.
For the convenience of redo routines, XLogReader now disects each WAL record
after reading it, copying the main data part and the per-block data into
MAXALIGNed buffers. The data chunks are not aligned within the WAL record,
but the redo routines can assume that the pointers returned by XLogRecGet*
functions are. Redo routines are now passed the XLogReaderState, which
contains the record in the already-disected format, instead of the plain
XLogRecord.
The new record format also makes the fixed size XLogRecord header smaller,
by removing the xl_len field. The length of the "main data" portion is now
stored at the end of the WAL record, and there's a separate header after
XLogRecord for it. The alignment padding at the end of XLogRecord is also
removed. This compansates for the fact that the new format would otherwise
be more bulky than the old format.
Reviewed by Andres Freund, Amit Kapila, Michael Paquier, Alvaro Herrera,
Fujii Masao.
2014-11-20 16:56:26 +01:00
|
|
|
recptr = XLogInsert(RM_CLOG_ID, CLOG_TRUNCATE);
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
XLogFlush(recptr);
|
|
|
|
}
|
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
/*
|
|
|
|
* CLOG resource manager's routines
|
|
|
|
*/
|
2001-08-25 20:52:43 +02:00
|
|
|
void
|
Revamp the WAL record format.
Each WAL record now carries information about the modified relation and
block(s) in a standardized format. That makes it easier to write tools that
need that information, like pg_rewind, prefetching the blocks to speed up
recovery, etc.
There's a whole new API for building WAL records, replacing the XLogRecData
chains used previously. The new API consists of XLogRegister* functions,
which are called for each buffer and chunk of data that is added to the
record. The new API also gives more control over when a full-page image is
written, by passing flags to the XLogRegisterBuffer function.
This also simplifies the XLogReadBufferForRedo() calls. The function can dig
the relation and block number from the WAL record, so they no longer need to
be passed as arguments.
For the convenience of redo routines, XLogReader now disects each WAL record
after reading it, copying the main data part and the per-block data into
MAXALIGNed buffers. The data chunks are not aligned within the WAL record,
but the redo routines can assume that the pointers returned by XLogRecGet*
functions are. Redo routines are now passed the XLogReaderState, which
contains the record in the already-disected format, instead of the plain
XLogRecord.
The new record format also makes the fixed size XLogRecord header smaller,
by removing the xl_len field. The length of the "main data" portion is now
stored at the end of the WAL record, and there's a separate header after
XLogRecord for it. The alignment padding at the end of XLogRecord is also
removed. This compansates for the fact that the new format would otherwise
be more bulky than the old format.
Reviewed by Andres Freund, Amit Kapila, Michael Paquier, Alvaro Herrera,
Fujii Masao.
2014-11-20 16:56:26 +01:00
|
|
|
clog_redo(XLogReaderState *record)
|
2001-08-25 20:52:43 +02:00
|
|
|
{
|
Revamp the WAL record format.
Each WAL record now carries information about the modified relation and
block(s) in a standardized format. That makes it easier to write tools that
need that information, like pg_rewind, prefetching the blocks to speed up
recovery, etc.
There's a whole new API for building WAL records, replacing the XLogRecData
chains used previously. The new API consists of XLogRegister* functions,
which are called for each buffer and chunk of data that is added to the
record. The new API also gives more control over when a full-page image is
written, by passing flags to the XLogRegisterBuffer function.
This also simplifies the XLogReadBufferForRedo() calls. The function can dig
the relation and block number from the WAL record, so they no longer need to
be passed as arguments.
For the convenience of redo routines, XLogReader now disects each WAL record
after reading it, copying the main data part and the per-block data into
MAXALIGNed buffers. The data chunks are not aligned within the WAL record,
but the redo routines can assume that the pointers returned by XLogRecGet*
functions are. Redo routines are now passed the XLogReaderState, which
contains the record in the already-disected format, instead of the plain
XLogRecord.
The new record format also makes the fixed size XLogRecord header smaller,
by removing the xl_len field. The length of the "main data" portion is now
stored at the end of the WAL record, and there's a separate header after
XLogRecord for it. The alignment padding at the end of XLogRecord is also
removed. This compansates for the fact that the new format would otherwise
be more bulky than the old format.
Reviewed by Andres Freund, Amit Kapila, Michael Paquier, Alvaro Herrera,
Fujii Masao.
2014-11-20 16:56:26 +01:00
|
|
|
uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
|
2001-08-25 20:52:43 +02:00
|
|
|
|
2009-01-20 19:59:37 +01:00
|
|
|
/* Backup blocks are not used in clog records */
|
Revamp the WAL record format.
Each WAL record now carries information about the modified relation and
block(s) in a standardized format. That makes it easier to write tools that
need that information, like pg_rewind, prefetching the blocks to speed up
recovery, etc.
There's a whole new API for building WAL records, replacing the XLogRecData
chains used previously. The new API consists of XLogRegister* functions,
which are called for each buffer and chunk of data that is added to the
record. The new API also gives more control over when a full-page image is
written, by passing flags to the XLogRegisterBuffer function.
This also simplifies the XLogReadBufferForRedo() calls. The function can dig
the relation and block number from the WAL record, so they no longer need to
be passed as arguments.
For the convenience of redo routines, XLogReader now disects each WAL record
after reading it, copying the main data part and the per-block data into
MAXALIGNed buffers. The data chunks are not aligned within the WAL record,
but the redo routines can assume that the pointers returned by XLogRecGet*
functions are. Redo routines are now passed the XLogReaderState, which
contains the record in the already-disected format, instead of the plain
XLogRecord.
The new record format also makes the fixed size XLogRecord header smaller,
by removing the xl_len field. The length of the "main data" portion is now
stored at the end of the WAL record, and there's a separate header after
XLogRecord for it. The alignment padding at the end of XLogRecord is also
removed. This compansates for the fact that the new format would otherwise
be more bulky than the old format.
Reviewed by Andres Freund, Amit Kapila, Michael Paquier, Alvaro Herrera,
Fujii Masao.
2014-11-20 16:56:26 +01:00
|
|
|
Assert(!XLogRecHasAnyBlockRefs(record));
|
2009-01-20 19:59:37 +01:00
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
if (info == CLOG_ZEROPAGE)
|
|
|
|
{
|
|
|
|
int pageno;
|
|
|
|
int slotno;
|
2001-08-25 20:52:43 +02:00
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
memcpy(&pageno, XLogRecGetData(record), sizeof(int));
|
|
|
|
|
|
|
|
LWLockAcquire(CLogControlLock, LW_EXCLUSIVE);
|
|
|
|
|
|
|
|
slotno = ZeroCLOGPage(pageno, false);
|
2010-12-30 16:09:17 +01:00
|
|
|
SimpleLruWritePage(ClogCtl, slotno);
|
2005-11-05 22:19:47 +01:00
|
|
|
Assert(!ClogCtl->shared->page_dirty[slotno]);
|
2004-08-24 01:22:45 +02:00
|
|
|
|
|
|
|
LWLockRelease(CLogControlLock);
|
|
|
|
}
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
else if (info == CLOG_TRUNCATE)
|
|
|
|
{
|
2017-03-23 19:08:23 +01:00
|
|
|
xl_clog_truncate xlrec;
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
|
2017-03-23 19:08:23 +01:00
|
|
|
memcpy(&xlrec, XLogRecGetData(record), sizeof(xl_clog_truncate));
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
|
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* During XLOG replay, latest_page_number isn't set up yet; insert a
|
|
|
|
* suitable value to bypass the sanity test in SimpleLruTruncate.
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
*/
|
2017-03-23 19:08:23 +01:00
|
|
|
ClogCtl->shared->latest_page_number = xlrec.pageno;
|
|
|
|
|
|
|
|
AdvanceOldestClogXid(xlrec.oldestXact);
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
|
2017-03-23 19:08:23 +01:00
|
|
|
SimpleLruTruncate(ClogCtl, xlrec.pageno);
|
Fix recently-understood problems with handling of XID freezing, particularly
in PITR scenarios. We now WAL-log the replacement of old XIDs with
FrozenTransactionId, so that such replacement is guaranteed to propagate to
PITR slave databases. Also, rather than relying on hint-bit updates to be
preserved, pg_clog is not truncated until all instances of an XID are known to
have been replaced by FrozenTransactionId. Add new GUC variables and
pg_autovacuum columns to allow management of the freezing policy, so that
users can trade off the size of pg_clog against the amount of freezing work
done. Revise the already-existing code that forces autovacuum of tables
approaching the wraparound point to make it more bulletproof; also, revise the
autovacuum logic so that anti-wraparound vacuuming is done per-table rather
than per-database. initdb forced because of changes in pg_class, pg_database,
and pg_autovacuum catalogs. Heikki Linnakangas, Simon Riggs, and Tom Lane.
2006-11-05 23:42:10 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
elog(PANIC, "clog_redo: unknown op code %u", info);
|
2004-08-24 01:22:45 +02:00
|
|
|
}
|