Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit35af542

Browse files
committed
Make the server track an 'XID epoch', that is, maintain higher-order bits
of the transaction ID counter. Nothing is done with the epoch except tostore it in checkpoint records, but this provides a foundation with whichadd-on code can pretend that XIDs never wrap around. This is a severelytrimmed and rewritten version of the xxid patch submitted by Marko Kreen.Per discussion, the epoch counter seems the only part of xxid that reallyneeds to be in the core server.
1 parent1054c38 commit35af542

File tree

6 files changed

+177
-44
lines changed

6 files changed

+177
-44
lines changed

‎doc/src/sgml/ref/pg_resetxlog.sgml

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/pg_resetxlog.sgml,v 1.16 2006/06/18 15:38:36 petere Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/pg_resetxlog.sgml,v 1.17 2006/08/21 16:16:31 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -22,6 +22,7 @@ PostgreSQL documentation
2222
<arg>-n</arg>
2323
<arg>-o<replaceable class="parameter">oid</replaceable> </arg>
2424
<arg>-x <replaceable class="parameter">xid</replaceable> </arg>
25+
<arg>-e <replaceable class="parameter">xid_epoch</replaceable> </arg>
2526
<arg>-m <replaceable class="parameter">mxid</replaceable> </arg>
2627
<arg>-O <replaceable class="parameter">mxoff</replaceable> </arg>
2728
<arg>-l <replaceable class="parameter">timelineid</replaceable>,<replaceable class="parameter">fileid</replaceable>,<replaceable class="parameter">seg</replaceable> </arg>
@@ -61,9 +62,9 @@ PostgreSQL documentation
6162
by specifying the <literal>-f</> (force) switch. In this case plausible
6263
values will be substituted for the missing data. Most of the fields can be
6364
expected to match, but manual assistance may be needed for the next OID,
64-
next transaction ID, next multitransaction ID and offset,
65+
next transaction ID and epoch, next multitransaction ID and offset,
6566
WAL starting address, and database locale fields.
66-
The firstfive of these can be set using the switches discussed below.
67+
The firstsix of these can be set using the switches discussed below.
6768
<command>pg_resetxlog</command>'s own environment is the source for its
6869
guess at the locale fields; take care that <envar>LANG</> and so forth
6970
match the environment that <command>initdb</> was run in.
@@ -76,11 +77,12 @@ PostgreSQL documentation
7677
</para>
7778

7879
<para>
79-
The <literal>-o</>, <literal>-x</>, <literal>-m</>, <literal>-O</>,
80+
The <literal>-o</>, <literal>-x</>, <literal>-e</>,
81+
<literal>-m</>, <literal>-O</>,
8082
and <literal>-l</>
81-
switches allow the next OID, next transaction ID, nextmultitransaction
82-
ID, next multitransaction offset, and WAL starting address values to
83-
be set manually. These are only needed when
83+
switches allow the next OID, next transaction ID, nexttransaction ID's
84+
epoch, next multitransactionID, next multitransaction offset, and WAL
85+
starting address values tobe set manually. These are only needed when
8486
<command>pg_resetxlog</command> is unable to determine appropriate values
8587
by reading <filename>pg_control</>. Safe values may be determined as
8688
follows:
@@ -146,6 +148,18 @@ PostgreSQL documentation
146148
get the next-OID setting right.
147149
</para>
148150
</listitem>
151+
152+
<listitem>
153+
<para>
154+
The transaction ID epoch is not actually stored anywhere in the database
155+
except in the field that is set by <command>pg_resetxlog</command>,
156+
so any value will work so far as the database itself is concerned.
157+
You might need to adjust this value to ensure that replication
158+
systems such as <application>Slony-I</> work correctly &mdash;
159+
if so, an appropriate value should be obtainable from the state of
160+
the downstream replicated database.
161+
</para>
162+
</listitem>
149163
</itemizedlist>
150164
</para>
151165

‎src/backend/access/transam/xlog.c

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.248 2006/08/17 23:04:05 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.249 2006/08/21 16:16:31 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -312,10 +312,8 @@ static XLogRecPtr RedoRecPtr;
312312
* new log file.
313313
*
314314
* CheckpointLock: must be held to do a checkpoint (ensures only one
315-
* checkpointer at a time; even though the postmaster won't launch
316-
* parallel checkpoint processes, we need this because manual checkpoints
317-
* could be launched simultaneously). XXX now that all checkpoints are
318-
* done by the bgwriter, isn't this lock redundant?
315+
* checkpointer at a time; currently, with all checkpoints done by the
316+
* bgwriter, this is just pro forma).
319317
*
320318
*----------
321319
*/
@@ -363,9 +361,13 @@ typedef struct XLogCtlData
363361
{
364362
/* Protected by WALInsertLock: */
365363
XLogCtlInsertInsert;
364+
366365
/* Protected by info_lck: */
367366
XLogwrtRqstLogwrtRqst;
368367
XLogwrtResultLogwrtResult;
368+
uint32ckptXidEpoch;/* nextXID & epoch of latest checkpoint */
369+
TransactionIdckptXid;
370+
369371
/* Protected by WALWriteLock: */
370372
XLogCtlWriteWrite;
371373

@@ -380,7 +382,7 @@ typedef struct XLogCtlData
380382
intXLogCacheBlck;/* highest allocated xlog buffer index */
381383
TimeLineIDThisTimeLineID;
382384

383-
slock_tinfo_lck;/* locks sharedLogwrtRqst/LogwrtResult */
385+
slock_tinfo_lck;/* locks sharedvariables shown above */
384386
}XLogCtlData;
385387

386388
staticXLogCtlData*XLogCtl=NULL;
@@ -4086,6 +4088,7 @@ BootStrapXLOG(void)
40864088
checkPoint.redo.xrecoff=SizeOfXLogLongPHD;
40874089
checkPoint.undo=checkPoint.redo;
40884090
checkPoint.ThisTimeLineID=ThisTimeLineID;
4091+
checkPoint.nextXidEpoch=0;
40894092
checkPoint.nextXid=FirstNormalTransactionId;
40904093
checkPoint.nextOid=FirstBootstrapObjectId;
40914094
checkPoint.nextMulti=FirstMultiXactId;
@@ -4752,8 +4755,9 @@ StartupXLOG(void)
47524755
checkPoint.undo.xlogid,checkPoint.undo.xrecoff,
47534756
wasShutdown ?"TRUE" :"FALSE")));
47544757
ereport(LOG,
4755-
(errmsg("next transaction ID: %u; next OID: %u",
4756-
checkPoint.nextXid,checkPoint.nextOid)));
4758+
(errmsg("next transaction ID: %u/%u; next OID: %u",
4759+
checkPoint.nextXidEpoch,checkPoint.nextXid,
4760+
checkPoint.nextOid)));
47574761
ereport(LOG,
47584762
(errmsg("next MultiXactId: %u; next MultiXactOffset: %u",
47594763
checkPoint.nextMulti,checkPoint.nextMultiOffset)));
@@ -5135,6 +5139,10 @@ StartupXLOG(void)
51355139
/* start the archive_timeout timer running */
51365140
XLogCtl->Write.lastSegSwitchTime=ControlFile->time;
51375141

5142+
/* initialize shared-memory copy of latest checkpoint XID/epoch */
5143+
XLogCtl->ckptXidEpoch=ControlFile->checkPointCopy.nextXidEpoch;
5144+
XLogCtl->ckptXid=ControlFile->checkPointCopy.nextXid;
5145+
51385146
/* Start up the commit log and related stuff, too */
51395147
StartupCLOG();
51405148
StartupSUBTRANS(oldestActiveXID);
@@ -5364,6 +5372,46 @@ GetRecentNextXid(void)
53645372
returnControlFile->checkPointCopy.nextXid;
53655373
}
53665374

5375+
/*
5376+
* GetNextXidAndEpoch - get the current nextXid value and associated epoch
5377+
*
5378+
* This is exported for use by code that would like to have 64-bit XIDs.
5379+
* We don't really support such things, but all XIDs within the system
5380+
* can be presumed "close to" the result, and thus the epoch associated
5381+
* with them can be determined.
5382+
*/
5383+
void
5384+
GetNextXidAndEpoch(TransactionId*xid,uint32*epoch)
5385+
{
5386+
uint32ckptXidEpoch;
5387+
TransactionIdckptXid;
5388+
TransactionIdnextXid;
5389+
5390+
/* Must read checkpoint info first, else have race condition */
5391+
{
5392+
/* use volatile pointer to prevent code rearrangement */
5393+
volatileXLogCtlData*xlogctl=XLogCtl;
5394+
5395+
SpinLockAcquire(&xlogctl->info_lck);
5396+
ckptXidEpoch=xlogctl->ckptXidEpoch;
5397+
ckptXid=xlogctl->ckptXid;
5398+
SpinLockRelease(&xlogctl->info_lck);
5399+
}
5400+
5401+
/* Now fetch current nextXid */
5402+
nextXid=ReadNewTransactionId();
5403+
5404+
/*
5405+
* nextXid is certainly logically later than ckptXid. So if it's
5406+
* numerically less, it must have wrapped into the next epoch.
5407+
*/
5408+
if (nextXid<ckptXid)
5409+
ckptXidEpoch++;
5410+
5411+
*xid=nextXid;
5412+
*epoch=ckptXidEpoch;
5413+
}
5414+
53675415
/*
53685416
* This must be called ONCE during postmaster or standalone-backend shutdown
53695417
*/
@@ -5531,6 +5579,11 @@ CreateCheckPoint(bool shutdown, bool force)
55315579
checkPoint.nextXid=ShmemVariableCache->nextXid;
55325580
LWLockRelease(XidGenLock);
55335581

5582+
/* Increase XID epoch if we've wrapped around since last checkpoint */
5583+
checkPoint.nextXidEpoch=ControlFile->checkPointCopy.nextXidEpoch;
5584+
if (checkPoint.nextXid<ControlFile->checkPointCopy.nextXid)
5585+
checkPoint.nextXidEpoch++;
5586+
55345587
LWLockAcquire(OidGenLock,LW_SHARED);
55355588
checkPoint.nextOid=ShmemVariableCache->nextOid;
55365589
if (!shutdown)
@@ -5600,6 +5653,17 @@ CreateCheckPoint(bool shutdown, bool force)
56005653
UpdateControlFile();
56015654
LWLockRelease(ControlFileLock);
56025655

5656+
/* Update shared-memory copy of checkpoint XID/epoch */
5657+
{
5658+
/* use volatile pointer to prevent code rearrangement */
5659+
volatileXLogCtlData*xlogctl=XLogCtl;
5660+
5661+
SpinLockAcquire(&xlogctl->info_lck);
5662+
xlogctl->ckptXidEpoch=checkPoint.nextXidEpoch;
5663+
xlogctl->ckptXid=checkPoint.nextXid;
5664+
SpinLockRelease(&xlogctl->info_lck);
5665+
}
5666+
56035667
/*
56045668
* We are now done with critical updates; no need for system panic if we
56055669
* have trouble while fooling with offline log segments.
@@ -5803,6 +5867,10 @@ xlog_redo(XLogRecPtr lsn, XLogRecord *record)
58035867
MultiXactSetNextMXact(checkPoint.nextMulti,
58045868
checkPoint.nextMultiOffset);
58055869

5870+
/* ControlFile->checkPointCopy always tracks the latest ckpt XID */
5871+
ControlFile->checkPointCopy.nextXidEpoch=checkPoint.nextXidEpoch;
5872+
ControlFile->checkPointCopy.nextXid=checkPoint.nextXid;
5873+
58065874
/*
58075875
* TLI may change in a shutdown checkpoint, but it shouldn't decrease
58085876
*/
@@ -5836,6 +5904,11 @@ xlog_redo(XLogRecPtr lsn, XLogRecord *record)
58365904
}
58375905
MultiXactAdvanceNextMXact(checkPoint.nextMulti,
58385906
checkPoint.nextMultiOffset);
5907+
5908+
/* ControlFile->checkPointCopy always tracks the latest ckpt XID */
5909+
ControlFile->checkPointCopy.nextXidEpoch=checkPoint.nextXidEpoch;
5910+
ControlFile->checkPointCopy.nextXid=checkPoint.nextXid;
5911+
58395912
/* TLI should not change in an on-line checkpoint */
58405913
if (checkPoint.ThisTimeLineID!=ThisTimeLineID)
58415914
ereport(PANIC,
@@ -5861,10 +5934,11 @@ xlog_desc(StringInfo buf, uint8 xl_info, char *rec)
58615934
CheckPoint*checkpoint= (CheckPoint*)rec;
58625935

58635936
appendStringInfo(buf,"checkpoint: redo %X/%X; undo %X/%X; "
5864-
"tli %u; xid %u; oid %u; multi %u; offset %u; %s",
5937+
"tli %u; xid %u/%u; oid %u; multi %u; offset %u; %s",
58655938
checkpoint->redo.xlogid,checkpoint->redo.xrecoff,
58665939
checkpoint->undo.xlogid,checkpoint->undo.xrecoff,
5867-
checkpoint->ThisTimeLineID,checkpoint->nextXid,
5940+
checkpoint->ThisTimeLineID,
5941+
checkpoint->nextXidEpoch,checkpoint->nextXid,
58685942
checkpoint->nextOid,
58695943
checkpoint->nextMulti,
58705944
checkpoint->nextMultiOffset,

‎src/bin/pg_controldata/pg_controldata.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* copyright (c) Oliver Elphick <olly@lfix.co.uk>, 2001;
77
* licence: BSD
88
*
9-
* $PostgreSQL: pgsql/src/bin/pg_controldata/pg_controldata.c,v 1.30 2006/08/07 16:57:56 tgl Exp $
9+
* $PostgreSQL: pgsql/src/bin/pg_controldata/pg_controldata.c,v 1.31 2006/08/21 16:16:31 tgl Exp $
1010
*/
1111
#include"postgres.h"
1212

@@ -177,7 +177,8 @@ main(int argc, char *argv[])
177177
ControlFile.checkPointCopy.undo.xrecoff);
178178
printf(_("Latest checkpoint's TimeLineID: %u\n"),
179179
ControlFile.checkPointCopy.ThisTimeLineID);
180-
printf(_("Latest checkpoint's NextXID: %u\n"),
180+
printf(_("Latest checkpoint's NextXID: %u/%u\n"),
181+
ControlFile.checkPointCopy.nextXidEpoch,
181182
ControlFile.checkPointCopy.nextXid);
182183
printf(_("Latest checkpoint's NextOID: %u\n"),
183184
ControlFile.checkPointCopy.nextOid);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp