PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
subtrans.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define NUM_SUBTRANS_BUFFERS   32
 

Functions

void SubTransSetParent (TransactionId xid, TransactionId parent)
 
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)
 

Macro Definition Documentation

#define NUM_SUBTRANS_BUFFERS   32

Definition at line 15 of file subtrans.h.

Referenced by SUBTRANSShmemInit(), and SUBTRANSShmemSize().

Function Documentation

void BootStrapSUBTRANS ( void  )

Definition at line 211 of file subtrans.c.

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

Referenced by BootStrapXLOG().

212 {
213  int slotno;
214 
215  LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
216 
217  /* Create and zero the first page of the subtrans log */
218  slotno = ZeroSUBTRANSPage(0);
219 
220  /* Make sure it's written out */
222  Assert(!SubTransCtl->shared->page_dirty[slotno]);
223 
224  LWLockRelease(SubtransControlLock);
225 }
static int ZeroSUBTRANSPage(int pageno)
Definition: subtrans.c:236
#define SubTransCtl
Definition: subtrans.c:62
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
void SimpleLruWritePage(SlruCtl ctl, int slotno)
Definition: slru.c:574
#define Assert(condition)
Definition: c.h:675
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
void CheckPointSUBTRANS ( void  )

Definition at line 299 of file subtrans.c.

References SimpleLruFlush(), and SubTransCtl.

Referenced by CheckPointGuts().

300 {
301  /*
302  * Flush dirty SUBTRANS pages to disk
303  *
304  * This is not actually necessary from a correctness point of view. We do
305  * it merely to improve the odds that writing of dirty pages is done by
306  * the checkpoint process and not by backends.
307  */
308  TRACE_POSTGRESQL_SUBTRANS_CHECKPOINT_START(true);
310  TRACE_POSTGRESQL_SUBTRANS_CHECKPOINT_DONE(true);
311 }
void SimpleLruFlush(SlruCtl ctl, bool allow_redirtied)
Definition: slru.c:1100
#define SubTransCtl
Definition: subtrans.c:62
void ExtendSUBTRANS ( TransactionId  newestXact)

Definition at line 323 of file subtrans.c.

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

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

324 {
325  int pageno;
326 
327  /*
328  * No work except at first XID of a page. But beware: just after
329  * wraparound, the first XID of page zero is FirstNormalTransactionId.
330  */
331  if (TransactionIdToEntry(newestXact) != 0 &&
333  return;
334 
335  pageno = TransactionIdToPage(newestXact);
336 
337  LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
338 
339  /* Zero the page */
340  ZeroSUBTRANSPage(pageno);
341 
342  LWLockRelease(SubtransControlLock);
343 }
static int ZeroSUBTRANSPage(int pageno)
Definition: subtrans.c:236
#define TransactionIdToEntry(xid)
Definition: subtrans.c:54
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define FirstNormalTransactionId
Definition: transam.h:34
#define TransactionIdToPage(xid)
Definition: subtrans.c:53
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
void ShutdownSUBTRANS ( void  )

Definition at line 282 of file subtrans.c.

References SimpleLruFlush(), and SubTransCtl.

Referenced by ShutdownXLOG().

283 {
284  /*
285  * Flush dirty SUBTRANS pages to disk
286  *
287  * This is not actually necessary from a correctness point of view. We do
288  * it merely as a debugging aid.
289  */
290  TRACE_POSTGRESQL_SUBTRANS_CHECKPOINT_START(false);
291  SimpleLruFlush(SubTransCtl, false);
292  TRACE_POSTGRESQL_SUBTRANS_CHECKPOINT_DONE(false);
293 }
void SimpleLruFlush(SlruCtl ctl, bool allow_redirtied)
Definition: slru.c:1100
#define SubTransCtl
Definition: subtrans.c:62
void StartupSUBTRANS ( TransactionId  oldestActiveXID)

Definition at line 249 of file subtrans.c.

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

Referenced by StartupXLOG().

250 {
251  int startPage;
252  int endPage;
253 
254  /*
255  * Since we don't expect pg_subtrans to be valid across crashes, we
256  * initialize the currently-active page(s) to zeroes during startup.
257  * Whenever we advance into a new page, ExtendSUBTRANS will likewise zero
258  * the new page without regard to whatever was previously on disk.
259  */
260  LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
261 
262  startPage = TransactionIdToPage(oldestActiveXID);
264 
265  while (startPage != endPage)
266  {
267  (void) ZeroSUBTRANSPage(startPage);
268  startPage++;
269  /* must account for wraparound */
270  if (startPage > TransactionIdToPage(MaxTransactionId))
271  startPage = 0;
272  }
273  (void) ZeroSUBTRANSPage(startPage);
274 
275  LWLockRelease(SubtransControlLock);
276 }
static int ZeroSUBTRANSPage(int pageno)
Definition: subtrans.c:236
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
TransactionId nextXid
Definition: transam.h:117
VariableCache ShmemVariableCache
Definition: varsup.c:34
#define MaxTransactionId
Definition: transam.h:35
#define TransactionIdToPage(xid)
Definition: subtrans.c:53
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
TransactionId SubTransGetParent ( TransactionId  xid)

Definition at line 108 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().

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

Definition at line 149 of file subtrans.c.

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

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

150 {
151  TransactionId parentXid = xid,
152  previousXid = xid;
153 
154  /* Can't ask about stuff that might not be around anymore */
156 
157  while (TransactionIdIsValid(parentXid))
158  {
159  previousXid = parentXid;
160  if (TransactionIdPrecedes(parentXid, TransactionXmin))
161  break;
162  parentXid = SubTransGetParent(parentXid);
163 
164  /*
165  * By convention the parent xid gets allocated first, so should always
166  * precede the child xid. Anything else points to a corrupted data
167  * structure that could lead to an infinite loop, so exit.
168  */
169  if (!TransactionIdPrecedes(parentXid, previousXid))
170  elog(ERROR, "pg_subtrans contains invalid entry: xid %u points to parent xid %u",
171  previousXid, parentXid);
172  }
173 
174  Assert(TransactionIdIsValid(previousXid));
175 
176  return previousXid;
177 }
uint32 TransactionId
Definition: c.h:397
TransactionId SubTransGetParent(TransactionId xid)
Definition: subtrans.c:108
bool TransactionIdFollowsOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:349
TransactionId TransactionXmin
Definition: snapmgr.c:164
#define ERROR
Definition: elog.h:43
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
#define Assert(condition)
Definition: c.h:675
#define elog
Definition: elog.h:219
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void SubTransSetParent ( TransactionId  xid,
TransactionId  parent 
)

Definition at line 73 of file subtrans.c.

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

Referenced by AssignTransactionId(), ProcArrayApplyXidAssignment(), and ProcessTwoPhaseBuffer().

74 {
75  int pageno = TransactionIdToPage(xid);
76  int entryno = TransactionIdToEntry(xid);
77  int slotno;
78  TransactionId *ptr;
79 
81  Assert(TransactionIdFollows(xid, parent));
82 
83  LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
84 
85  slotno = SimpleLruReadPage(SubTransCtl, pageno, true, xid);
86  ptr = (TransactionId *) SubTransCtl->shared->page_buffer[slotno];
87  ptr += entryno;
88 
89  /*
90  * It's possible we'll try to set the parent xid multiple times but we
91  * shouldn't ever be changing the xid from one valid xid to another valid
92  * xid, which would corrupt the data structure.
93  */
94  if (*ptr != parent)
95  {
97  *ptr = parent;
98  SubTransCtl->shared->page_dirty[slotno] = true;
99  }
100 
101  LWLockRelease(SubtransControlLock);
102 }
#define TransactionIdToEntry(xid)
Definition: subtrans.c:54
bool TransactionIdFollows(TransactionId id1, TransactionId id2)
Definition: transam.c:334
uint32 TransactionId
Definition: c.h:397
#define SubTransCtl
Definition: subtrans.c:62
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok, TransactionId xid)
Definition: slru.c:371
#define InvalidTransactionId
Definition: transam.h:31
#define TransactionIdToPage(xid)
Definition: subtrans.c:53
#define Assert(condition)
Definition: c.h:675
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void SUBTRANSShmemInit ( void  )

Definition at line 190 of file subtrans.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

191 {
192  SubTransCtl->PagePrecedes = SubTransPagePrecedes;
194  SubtransControlLock, "pg_subtrans",
196  /* Override default assumption that writes should be fsync'd */
197  SubTransCtl->do_fsync = false;
198 }
#define SubTransCtl
Definition: subtrans.c:62
static bool SubTransPagePrecedes(int page1, int page2)
Definition: subtrans.c:382
#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:165
Size SUBTRANSShmemSize ( void  )

Definition at line 184 of file subtrans.c.

References NUM_SUBTRANS_BUFFERS, and SimpleLruShmemSize().

Referenced by CreateSharedMemoryAndSemaphores().

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

Definition at line 353 of file subtrans.c.

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

Referenced by CreateCheckPoint(), and CreateRestartPoint().

354 {
355  int cutoffPage;
356 
357  /*
358  * The cutoff point is the start of the segment containing oldestXact. We
359  * pass the *page* containing oldestXact to SimpleLruTruncate. We step
360  * back one transaction to avoid passing a cutoff page that hasn't been
361  * created yet in the rare case that oldestXact would be the first item on
362  * a page and oldestXact == next XID. In that case, if we didn't subtract
363  * one, we'd trigger SimpleLruTruncate's wraparound detection.
364  */
365  TransactionIdRetreat(oldestXact);
366  cutoffPage = TransactionIdToPage(oldestXact);
367 
368  SimpleLruTruncate(SubTransCtl, cutoffPage);
369 }
void SimpleLruTruncate(SlruCtl ctl, int cutoffPage)
Definition: slru.c:1165
#define TransactionIdRetreat(dest)
Definition: transam.h:56
#define SubTransCtl
Definition: subtrans.c:62
#define TransactionIdToPage(xid)
Definition: subtrans.c:53