PostgreSQL Source Code  git master
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 CheckPointSUBTRANS (void)
 
void ExtendSUBTRANS (TransactionId newestXact)
 
void TruncateSUBTRANS (TransactionId oldestXact)
 

Macro Definition Documentation

◆ NUM_SUBTRANS_BUFFERS

#define NUM_SUBTRANS_BUFFERS   32

Definition at line 15 of file subtrans.h.

Referenced by SUBTRANSShmemInit(), and SUBTRANSShmemSize().

Function Documentation

◆ BootStrapSUBTRANS()

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(SubtransSLRULock, 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(SubtransSLRULock);
225 }
static int ZeroSUBTRANSPage(int pageno)
Definition: subtrans.c:236
#define SubTransCtl
Definition: subtrans.c:63
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
void SimpleLruWritePage(SlruCtl ctl, int slotno)
Definition: slru.c:613
#define Assert(condition)
Definition: c.h:804
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203

◆ CheckPointSUBTRANS()

void CheckPointSUBTRANS ( void  )

Definition at line 284 of file subtrans.c.

References SimpleLruWriteAll(), and SubTransCtl.

Referenced by CheckPointGuts().

285 {
286  /*
287  * Write dirty SUBTRANS pages to disk
288  *
289  * This is not actually necessary from a correctness point of view. We do
290  * it merely to improve the odds that writing of dirty pages is done by
291  * the checkpoint process and not by backends.
292  */
293  TRACE_POSTGRESQL_SUBTRANS_CHECKPOINT_START(true);
295  TRACE_POSTGRESQL_SUBTRANS_CHECKPOINT_DONE(true);
296 }
#define SubTransCtl
Definition: subtrans.c:63
void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
Definition: slru.c:1155

◆ ExtendSUBTRANS()

void ExtendSUBTRANS ( TransactionId  newestXact)

Definition at line 308 of file subtrans.c.

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

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

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

◆ StartupSUBTRANS()

void StartupSUBTRANS ( TransactionId  oldestActiveXID)

Definition at line 249 of file subtrans.c.

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

Referenced by StartupXLOG().

250 {
251  FullTransactionId nextXid;
252  int startPage;
253  int endPage;
254 
255  /*
256  * Since we don't expect pg_subtrans to be valid across crashes, we
257  * initialize the currently-active page(s) to zeroes during startup.
258  * Whenever we advance into a new page, ExtendSUBTRANS will likewise zero
259  * the new page without regard to whatever was previously on disk.
260  */
261  LWLockAcquire(SubtransSLRULock, LW_EXCLUSIVE);
262 
263  startPage = TransactionIdToPage(oldestActiveXID);
264  nextXid = ShmemVariableCache->nextXid;
265  endPage = TransactionIdToPage(XidFromFullTransactionId(nextXid));
266 
267  while (startPage != endPage)
268  {
269  (void) ZeroSUBTRANSPage(startPage);
270  startPage++;
271  /* must account for wraparound */
272  if (startPage > TransactionIdToPage(MaxTransactionId))
273  startPage = 0;
274  }
275  (void) ZeroSUBTRANSPage(startPage);
276 
277  LWLockRelease(SubtransSLRULock);
278 }
static int ZeroSUBTRANSPage(int pageno)
Definition: subtrans.c:236
FullTransactionId nextXid
Definition: transam.h:213
#define XidFromFullTransactionId(x)
Definition: transam.h:48
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
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:1203

◆ SubTransGetParent()

TransactionId SubTransGetParent ( TransactionId  xid)

Definition at line 109 of file subtrans.c.

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

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

110 {
111  int pageno = TransactionIdToPage(xid);
112  int entryno = TransactionIdToEntry(xid);
113  int slotno;
114  TransactionId *ptr;
115  TransactionId parent;
116 
117  /* Can't ask about stuff that might not be around anymore */
119 
120  /* Bootstrap and frozen XIDs have no parent */
121  if (!TransactionIdIsNormal(xid))
122  return InvalidTransactionId;
123 
124  /* lock is acquired by SimpleLruReadPage_ReadOnly */
125 
126  slotno = SimpleLruReadPage_ReadOnly(SubTransCtl, pageno, xid);
127  ptr = (TransactionId *) SubTransCtl->shared->page_buffer[slotno];
128  ptr += entryno;
129 
130  parent = *ptr;
131 
132  LWLockRelease(SubtransSLRULock);
133 
134  return parent;
135 }
#define TransactionIdToEntry(xid)
Definition: subtrans.c:55
uint32 TransactionId
Definition: c.h:587
bool TransactionIdFollowsOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:349
TransactionId TransactionXmin
Definition: snapmgr.c:112
#define SubTransCtl
Definition: subtrans.c:63
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
#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:494
#define Assert(condition)
Definition: c.h:804
#define TransactionIdIsNormal(xid)
Definition: transam.h:42

◆ SubTransGetTopmostTransaction()

TransactionId SubTransGetTopmostTransaction ( TransactionId  xid)

Definition at line 150 of file subtrans.c.

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

Referenced by ConditionalXactLockTableWait(), HeapCheckForSerializableConflictOut(), TransactionIdIsInProgress(), XactLockTableWait(), and XidInMVCCSnapshot().

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

◆ SubTransSetParent()

void SubTransSetParent ( TransactionId  xid,
TransactionId  parent 
)

Definition at line 74 of file subtrans.c.

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

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

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

◆ SUBTRANSShmemInit()

void SUBTRANSShmemInit ( void  )

Definition at line 191 of file subtrans.c.

References LWTRANCHE_SUBTRANS_BUFFER, NUM_SUBTRANS_BUFFERS, SimpleLruInit(), SlruPagePrecedesUnitTests, SUBTRANS_XACTS_PER_PAGE, SubTransCtl, SubTransPagePrecedes(), and SYNC_HANDLER_NONE.

Referenced by CreateSharedMemoryAndSemaphores().

192 {
193  SubTransCtl->PagePrecedes = SubTransPagePrecedes;
195  SubtransSLRULock, "pg_subtrans",
198 }
void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns, LWLock *ctllock, const char *subdir, int tranche_id, SyncRequestHandler sync_handler)
Definition: slru.c:186
#define SubTransCtl
Definition: subtrans.c:63
static bool SubTransPagePrecedes(int page1, int page2)
Definition: subtrans.c:362
#define NUM_SUBTRANS_BUFFERS
Definition: subtrans.h:15
#define SUBTRANS_XACTS_PER_PAGE
Definition: subtrans.c:52
#define SlruPagePrecedesUnitTests(ctl, per_page)
Definition: slru.h:156

◆ SUBTRANSShmemSize()

Size SUBTRANSShmemSize ( void  )

Definition at line 185 of file subtrans.c.

References NUM_SUBTRANS_BUFFERS, and SimpleLruShmemSize().

Referenced by CreateSharedMemoryAndSemaphores().

186 {
188 }
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:155
#define NUM_SUBTRANS_BUFFERS
Definition: subtrans.h:15

◆ TruncateSUBTRANS()

void TruncateSUBTRANS ( TransactionId  oldestXact)

Definition at line 338 of file subtrans.c.

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

Referenced by CreateCheckPoint(), and CreateRestartPoint().

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