PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
subtrans.c File Reference
#include "postgres.h"
#include "access/slru.h"
#include "access/subtrans.h"
#include "access/transam.h"
#include "pg_trace.h"
#include "utils/snapmgr.h"
Include dependency graph for subtrans.c:

Go to the source code of this file.

Macros

#define SUBTRANS_XACTS_PER_PAGE   (BLCKSZ / sizeof(TransactionId))
 
#define TransactionIdToPage(xid)   ((xid) / (TransactionId) SUBTRANS_XACTS_PER_PAGE)
 
#define TransactionIdToEntry(xid)   ((xid) % (TransactionId) SUBTRANS_XACTS_PER_PAGE)
 
#define SubTransCtl   (&SubTransCtlData)
 

Functions

static int ZeroSUBTRANSPage (int pageno)
 
static bool SubTransPagePrecedes (int page1, int page2)
 
void SubTransSetParent (TransactionId xid, TransactionId parent, bool overwriteOK)
 
TransactionId SubTransGetParent (TransactionId xid)
 
TransactionId SubTransGetTopmostTransaction (TransactionId xid)
 
Size SUBTRANSShmemSize (void)
 
void SUBTRANSShmemInit (void)
 
void BootStrapSUBTRANS (void)
 
void StartupSUBTRANS (TransactionId oldestActiveXID)
 
void ShutdownSUBTRANS (void)
 
void CheckPointSUBTRANS (void)
 
void ExtendSUBTRANS (TransactionId newestXact)
 
void TruncateSUBTRANS (TransactionId oldestXact)
 

Variables

static SlruCtlData SubTransCtlData
 

Macro Definition Documentation

#define SUBTRANS_XACTS_PER_PAGE   (BLCKSZ / sizeof(TransactionId))

Definition at line 52 of file subtrans.c.

Referenced by SubTransPagePrecedes().

#define TransactionIdToEntry (   xid)    ((xid) % (TransactionId) SUBTRANS_XACTS_PER_PAGE)

Definition at line 55 of file subtrans.c.

Referenced by ExtendSUBTRANS(), SubTransGetParent(), and SubTransSetParent().

#define TransactionIdToPage (   xid)    ((xid) / (TransactionId) SUBTRANS_XACTS_PER_PAGE)

Function Documentation

void BootStrapSUBTRANS ( void  )

Definition at line 200 of file subtrans.c.

References Assert, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), SimpleLruWritePage(), SubTransCtl, and ZeroSUBTRANSPage().

Referenced by BootStrapXLOG().

201 {
202  int slotno;
203 
204  LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
205 
206  /* Create and zero the first page of the subtrans log */
207  slotno = ZeroSUBTRANSPage(0);
208 
209  /* Make sure it's written out */
211  Assert(!SubTransCtl->shared->page_dirty[slotno]);
212 
213  LWLockRelease(SubtransControlLock);
214 }
static int ZeroSUBTRANSPage(int pageno)
Definition: subtrans.c:225
#define SubTransCtl
Definition: subtrans.c:63
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
void SimpleLruWritePage(SlruCtl ctl, int slotno)
Definition: slru.c:573
#define Assert(condition)
Definition: c.h:670
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1110
void CheckPointSUBTRANS ( void  )

Definition at line 288 of file subtrans.c.

References SimpleLruFlush(), and SubTransCtl.

Referenced by CheckPointGuts().

289 {
290  /*
291  * Flush dirty SUBTRANS pages to disk
292  *
293  * This is not actually necessary from a correctness point of view. We do
294  * it merely to improve the odds that writing of dirty pages is done by
295  * the checkpoint process and not by backends.
296  */
297  TRACE_POSTGRESQL_SUBTRANS_CHECKPOINT_START(true);
299  TRACE_POSTGRESQL_SUBTRANS_CHECKPOINT_DONE(true);
300 }
void SimpleLruFlush(SlruCtl ctl, bool allow_redirtied)
Definition: slru.c:1090
#define SubTransCtl
Definition: subtrans.c:63
void ExtendSUBTRANS ( TransactionId  newestXact)

Definition at line 312 of file subtrans.c.

References FirstNormalTransactionId, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), TransactionIdEquals, TransactionIdToEntry, TransactionIdToPage, and ZeroSUBTRANSPage().

Referenced by GetNewTransactionId(), ProcArrayApplyRecoveryInfo(), and RecordKnownAssignedTransactionIds().

313 {
314  int pageno;
315 
316  /*
317  * No work except at first XID of a page. But beware: just after
318  * wraparound, the first XID of page zero is FirstNormalTransactionId.
319  */
320  if (TransactionIdToEntry(newestXact) != 0 &&
322  return;
323 
324  pageno = TransactionIdToPage(newestXact);
325 
326  LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
327 
328  /* Zero the page */
329  ZeroSUBTRANSPage(pageno);
330 
331  LWLockRelease(SubtransControlLock);
332 }
static int ZeroSUBTRANSPage(int pageno)
Definition: subtrans.c:225
#define TransactionIdToEntry(xid)
Definition: subtrans.c:55
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
#define FirstNormalTransactionId
Definition: transam.h:34
#define TransactionIdToPage(xid)
Definition: subtrans.c:54
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1110
void ShutdownSUBTRANS ( void  )

Definition at line 271 of file subtrans.c.

References SimpleLruFlush(), and SubTransCtl.

Referenced by ShutdownXLOG().

272 {
273  /*
274  * Flush dirty SUBTRANS pages to disk
275  *
276  * This is not actually necessary from a correctness point of view. We do
277  * it merely as a debugging aid.
278  */
279  TRACE_POSTGRESQL_SUBTRANS_CHECKPOINT_START(false);
280  SimpleLruFlush(SubTransCtl, false);
281  TRACE_POSTGRESQL_SUBTRANS_CHECKPOINT_DONE(false);
282 }
void SimpleLruFlush(SlruCtl ctl, bool allow_redirtied)
Definition: slru.c:1090
#define SubTransCtl
Definition: subtrans.c:63
void StartupSUBTRANS ( TransactionId  oldestActiveXID)

Definition at line 238 of file subtrans.c.

References LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MaxTransactionId, VariableCacheData::nextXid, ShmemVariableCache, TransactionIdToPage, and ZeroSUBTRANSPage().

Referenced by StartupXLOG().

239 {
240  int startPage;
241  int endPage;
242 
243  /*
244  * Since we don't expect pg_subtrans to be valid across crashes, we
245  * initialize the currently-active page(s) to zeroes during startup.
246  * Whenever we advance into a new page, ExtendSUBTRANS will likewise zero
247  * the new page without regard to whatever was previously on disk.
248  */
249  LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
250 
251  startPage = TransactionIdToPage(oldestActiveXID);
253 
254  while (startPage != endPage)
255  {
256  (void) ZeroSUBTRANSPage(startPage);
257  startPage++;
258  /* must account for wraparound */
259  if (startPage > TransactionIdToPage(MaxTransactionId))
260  startPage = 0;
261  }
262  (void) ZeroSUBTRANSPage(startPage);
263 
264  LWLockRelease(SubtransControlLock);
265 }
static int ZeroSUBTRANSPage(int pageno)
Definition: subtrans.c:225
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
TransactionId nextXid
Definition: transam.h:117
VariableCache ShmemVariableCache
Definition: varsup.c:34
#define MaxTransactionId
Definition: transam.h:35
#define TransactionIdToPage(xid)
Definition: subtrans.c:54
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1110
TransactionId SubTransGetParent ( TransactionId  xid)

Definition at line 106 of file subtrans.c.

References Assert, InvalidTransactionId, LWLockRelease(), SimpleLruReadPage_ReadOnly(), SubTransCtl, TransactionIdFollowsOrEquals(), TransactionIdIsNormal, TransactionIdToEntry, TransactionIdToPage, and TransactionXmin.

Referenced by ConditionalXactLockTableWait(), SubTransGetTopmostTransaction(), TransactionIdDidAbort(), TransactionIdDidCommit(), and XactLockTableWait().

107 {
108  int pageno = TransactionIdToPage(xid);
109  int entryno = TransactionIdToEntry(xid);
110  int slotno;
111  TransactionId *ptr;
112  TransactionId parent;
113 
114  /* Can't ask about stuff that might not be around anymore */
116 
117  /* Bootstrap and frozen XIDs have no parent */
118  if (!TransactionIdIsNormal(xid))
119  return InvalidTransactionId;
120 
121  /* lock is acquired by SimpleLruReadPage_ReadOnly */
122 
123  slotno = SimpleLruReadPage_ReadOnly(SubTransCtl, pageno, xid);
124  ptr = (TransactionId *) SubTransCtl->shared->page_buffer[slotno];
125  ptr += entryno;
126 
127  parent = *ptr;
128 
129  LWLockRelease(SubtransControlLock);
130 
131  return parent;
132 }
#define TransactionIdToEntry(xid)
Definition: subtrans.c:55
uint32 TransactionId
Definition: c.h:393
bool TransactionIdFollowsOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:349
TransactionId TransactionXmin
Definition: snapmgr.c:164
#define SubTransCtl
Definition: subtrans.c:63
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
#define InvalidTransactionId
Definition: transam.h:31
#define TransactionIdToPage(xid)
Definition: subtrans.c:54
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int pageno, TransactionId xid)
Definition: slru.c:462
#define Assert(condition)
Definition: c.h:670
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
TransactionId SubTransGetTopmostTransaction ( TransactionId  xid)

Definition at line 147 of file subtrans.c.

References Assert, SubTransGetParent(), TransactionIdFollowsOrEquals(), TransactionIdIsValid, TransactionIdPrecedes(), and TransactionXmin.

Referenced by CheckForSerializableConflictOut(), PredicateLockTuple(), TransactionIdIsInProgress(), and XidInMVCCSnapshot().

148 {
149  TransactionId parentXid = xid,
150  previousXid = xid;
151 
152  /* Can't ask about stuff that might not be around anymore */
154 
155  while (TransactionIdIsValid(parentXid))
156  {
157  previousXid = parentXid;
158  if (TransactionIdPrecedes(parentXid, TransactionXmin))
159  break;
160  parentXid = SubTransGetParent(parentXid);
161  }
162 
163  Assert(TransactionIdIsValid(previousXid));
164 
165  return previousXid;
166 }
uint32 TransactionId
Definition: c.h:393
TransactionId SubTransGetParent(TransactionId xid)
Definition: subtrans.c:106
bool TransactionIdFollowsOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:349
TransactionId TransactionXmin
Definition: snapmgr.c:164
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
#define Assert(condition)
Definition: c.h:670
#define TransactionIdIsValid(xid)
Definition: transam.h:41
static bool SubTransPagePrecedes ( int  page1,
int  page2 
)
static

Definition at line 371 of file subtrans.c.

References FirstNormalTransactionId, SUBTRANS_XACTS_PER_PAGE, and TransactionIdPrecedes().

Referenced by SUBTRANSShmemInit().

372 {
373  TransactionId xid1;
374  TransactionId xid2;
375 
376  xid1 = ((TransactionId) page1) * SUBTRANS_XACTS_PER_PAGE;
377  xid1 += FirstNormalTransactionId;
378  xid2 = ((TransactionId) page2) * SUBTRANS_XACTS_PER_PAGE;
379  xid2 += FirstNormalTransactionId;
380 
381  return TransactionIdPrecedes(xid1, xid2);
382 }
uint32 TransactionId
Definition: c.h:393
#define FirstNormalTransactionId
Definition: transam.h:34
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
#define SUBTRANS_XACTS_PER_PAGE
Definition: subtrans.c:52
void SubTransSetParent ( TransactionId  xid,
TransactionId  parent,
bool  overwriteOK 
)

Definition at line 76 of file subtrans.c.

References Assert, InvalidTransactionId, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), SimpleLruReadPage(), SubTransCtl, TransactionIdIsValid, TransactionIdToEntry, and TransactionIdToPage.

Referenced by AssignTransactionId(), ProcArrayApplyXidAssignment(), RecoverPreparedTransactions(), and StandbyRecoverPreparedTransactions().

77 {
78  int pageno = TransactionIdToPage(xid);
79  int entryno = TransactionIdToEntry(xid);
80  int slotno;
81  TransactionId *ptr;
82 
84 
85  LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
86 
87  slotno = SimpleLruReadPage(SubTransCtl, pageno, true, xid);
88  ptr = (TransactionId *) SubTransCtl->shared->page_buffer[slotno];
89  ptr += entryno;
90 
91  /* Current state should be 0 */
92  Assert(*ptr == InvalidTransactionId ||
93  (*ptr == parent && overwriteOK));
94 
95  *ptr = parent;
96 
97  SubTransCtl->shared->page_dirty[slotno] = true;
98 
99  LWLockRelease(SubtransControlLock);
100 }
#define TransactionIdToEntry(xid)
Definition: subtrans.c:55
uint32 TransactionId
Definition: c.h:393
#define SubTransCtl
Definition: subtrans.c:63
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok, TransactionId xid)
Definition: slru.c:370
#define InvalidTransactionId
Definition: transam.h:31
#define TransactionIdToPage(xid)
Definition: subtrans.c:54
#define Assert(condition)
Definition: c.h:670
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1110
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void SUBTRANSShmemInit ( void  )

Definition at line 179 of file subtrans.c.

References LWTRANCHE_SUBTRANS_BUFFERS, NUM_SUBTRANS_BUFFERS, SimpleLruInit(), SubTransCtl, and SubTransPagePrecedes().

Referenced by CreateSharedMemoryAndSemaphores().

180 {
181  SubTransCtl->PagePrecedes = SubTransPagePrecedes;
183  SubtransControlLock, "pg_subtrans",
185  /* Override default assumption that writes should be fsync'd */
186  SubTransCtl->do_fsync = false;
187 }
#define SubTransCtl
Definition: subtrans.c:63
static bool SubTransPagePrecedes(int page1, int page2)
Definition: subtrans.c:371
#define NUM_SUBTRANS_BUFFERS
Definition: subtrans.h:15
void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns, LWLock *ctllock, const char *subdir, int tranche_id)
Definition: slru.c:164
Size SUBTRANSShmemSize ( void  )

Definition at line 173 of file subtrans.c.

References NUM_SUBTRANS_BUFFERS, and SimpleLruShmemSize().

Referenced by CreateSharedMemoryAndSemaphores().

174 {
176 }
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:144
#define NUM_SUBTRANS_BUFFERS
Definition: subtrans.h:15
void TruncateSUBTRANS ( TransactionId  oldestXact)

Definition at line 342 of file subtrans.c.

References SimpleLruTruncate(), SubTransCtl, TransactionIdRetreat, and TransactionIdToPage.

Referenced by CreateCheckPoint(), and CreateRestartPoint().

343 {
344  int cutoffPage;
345 
346  /*
347  * The cutoff point is the start of the segment containing oldestXact. We
348  * pass the *page* containing oldestXact to SimpleLruTruncate. We step
349  * back one transaction to avoid passing a cutoff page that hasn't been
350  * created yet in the rare case that oldestXact would be the first item on
351  * a page and oldestXact == next XID. In that case, if we didn't subtract
352  * one, we'd trigger SimpleLruTruncate's wraparound detection.
353  */
354  TransactionIdRetreat(oldestXact);
355  cutoffPage = TransactionIdToPage(oldestXact);
356 
357  SimpleLruTruncate(SubTransCtl, cutoffPage);
358 }
void SimpleLruTruncate(SlruCtl ctl, int cutoffPage)
Definition: slru.c:1153
#define TransactionIdRetreat(dest)
Definition: transam.h:56
#define SubTransCtl
Definition: subtrans.c:63
#define TransactionIdToPage(xid)
Definition: subtrans.c:54
static int ZeroSUBTRANSPage ( int  pageno)
static

Definition at line 225 of file subtrans.c.

References SimpleLruZeroPage(), and SubTransCtl.

Referenced by BootStrapSUBTRANS(), ExtendSUBTRANS(), and StartupSUBTRANS().

226 {
227  return SimpleLruZeroPage(SubTransCtl, pageno);
228 }
#define SubTransCtl
Definition: subtrans.c:63
int SimpleLruZeroPage(SlruCtl ctl, int pageno)
Definition: slru.c:258

Variable Documentation

SlruCtlData SubTransCtlData
static

Definition at line 61 of file subtrans.c.