PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
subtrans.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * subtrans.c
4  * PostgreSQL subtransaction-log manager
5  *
6  * The pg_subtrans manager is a pg_xact-like manager that stores the parent
7  * transaction Id for each transaction. It is a fundamental part of the
8  * nested transactions implementation. A main transaction has a parent
9  * of InvalidTransactionId, and each subtransaction has its immediate parent.
10  * The tree can easily be walked from child to parent, but not in the
11  * opposite direction.
12  *
13  * are completely different from pg_xact, because we only need to remember
14  * pg_subtrans information for currently-open transactions. Thus, there is
15  * no need to preserve data over a crash and restart.
16  *
17  * There are no XLOG interactions since we do not care about preserving
18  * data across crashes. During database startup, we simply force the
19  * currently-active page of SUBTRANS to zeroes.
20  *
21  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
22  * Portions Copyright (c) 1994, Regents of the University of California
23  *
24  * src/backend/access/transam/subtrans.c
25  *
26  *-------------------------------------------------------------------------
27  */
28 #include "postgres.h"
29 
30 #include "access/slru.h"
31 #include "access/subtrans.h"
32 #include "access/transam.h"
33 #include "pg_trace.h"
34 #include "utils/snapmgr.h"
35 
36 
37 /*
38  * Defines for SubTrans page sizes. A page is the same BLCKSZ as is used
39  * everywhere else in Postgres.
40  *
41  * Note: because TransactionIds are 32 bits and wrap around at 0xFFFFFFFF,
42  * SubTrans page numbering also wraps around at
43  * 0xFFFFFFFF/SUBTRANS_XACTS_PER_PAGE, and segment numbering at
44  * 0xFFFFFFFF/SUBTRANS_XACTS_PER_PAGE/SLRU_PAGES_PER_SEGMENT. We need take no
45  * explicit notice of that fact in this module, except when comparing segment
46  * and page numbers in TruncateSUBTRANS (see SubTransPagePrecedes) and zeroing
47  * them in StartupSUBTRANS.
48  */
49 
50 /* We need four bytes per xact */
51 #define SUBTRANS_XACTS_PER_PAGE (BLCKSZ / sizeof(TransactionId))
52 
53 #define TransactionIdToPage(xid) ((xid) / (TransactionId) SUBTRANS_XACTS_PER_PAGE)
54 #define TransactionIdToEntry(xid) ((xid) % (TransactionId) SUBTRANS_XACTS_PER_PAGE)
55 
56 
57 /*
58  * Link to shared-memory data structures for SUBTRANS control
59  */
61 
62 #define SubTransCtl (&SubTransCtlData)
63 
64 
65 static int ZeroSUBTRANSPage(int pageno);
66 static bool SubTransPagePrecedes(int page1, int page2);
67 
68 
69 /*
70  * Record the parent of a subtransaction in the subtrans log.
71  *
72  * In some cases we may need to overwrite an existing value.
73  */
74 void
75 SubTransSetParent(TransactionId xid, TransactionId parent, bool overwriteOK)
76 {
77  int pageno = TransactionIdToPage(xid);
78  int entryno = TransactionIdToEntry(xid);
79  int slotno;
80  TransactionId *ptr;
81 
83 
84  LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
85 
86  slotno = SimpleLruReadPage(SubTransCtl, pageno, true, xid);
87  ptr = (TransactionId *) SubTransCtl->shared->page_buffer[slotno];
88  ptr += entryno;
89 
90  /* Current state should be 0 */
91  Assert(*ptr == InvalidTransactionId ||
92  (*ptr == parent && overwriteOK));
93 
94  *ptr = parent;
95 
96  SubTransCtl->shared->page_dirty[slotno] = true;
97 
98  LWLockRelease(SubtransControlLock);
99 }
100 
101 /*
102  * Interrogate the parent of a transaction in the subtrans log.
103  */
106 {
107  int pageno = TransactionIdToPage(xid);
108  int entryno = TransactionIdToEntry(xid);
109  int slotno;
110  TransactionId *ptr;
111  TransactionId parent;
112 
113  /* Can't ask about stuff that might not be around anymore */
115 
116  /* Bootstrap and frozen XIDs have no parent */
117  if (!TransactionIdIsNormal(xid))
118  return InvalidTransactionId;
119 
120  /* lock is acquired by SimpleLruReadPage_ReadOnly */
121 
122  slotno = SimpleLruReadPage_ReadOnly(SubTransCtl, pageno, xid);
123  ptr = (TransactionId *) SubTransCtl->shared->page_buffer[slotno];
124  ptr += entryno;
125 
126  parent = *ptr;
127 
128  LWLockRelease(SubtransControlLock);
129 
130  return parent;
131 }
132 
133 /*
134  * SubTransGetTopmostTransaction
135  *
136  * Returns the topmost transaction of the given transaction id.
137  *
138  * Because we cannot look back further than TransactionXmin, it is possible
139  * that this function will lie and return an intermediate subtransaction ID
140  * instead of the true topmost parent ID. This is OK, because in practice
141  * we only care about detecting whether the topmost parent is still running
142  * or is part of a current snapshot's list of still-running transactions.
143  * Therefore, any XID before TransactionXmin is as good as any other.
144  */
147 {
148  TransactionId parentXid = xid,
149  previousXid = xid;
150 
151  /* Can't ask about stuff that might not be around anymore */
153 
154  while (TransactionIdIsValid(parentXid))
155  {
156  previousXid = parentXid;
157  if (TransactionIdPrecedes(parentXid, TransactionXmin))
158  break;
159  parentXid = SubTransGetParent(parentXid);
160  }
161 
162  Assert(TransactionIdIsValid(previousXid));
163 
164  return previousXid;
165 }
166 
167 
168 /*
169  * Initialization of shared memory for SUBTRANS
170  */
171 Size
173 {
175 }
176 
177 void
179 {
180  SubTransCtl->PagePrecedes = SubTransPagePrecedes;
182  SubtransControlLock, "pg_subtrans",
184  /* Override default assumption that writes should be fsync'd */
185  SubTransCtl->do_fsync = false;
186 }
187 
188 /*
189  * This func must be called ONCE on system install. It creates
190  * the initial SUBTRANS segment. (The SUBTRANS directory is assumed to
191  * have been created by the initdb shell script, and SUBTRANSShmemInit
192  * must have been called already.)
193  *
194  * Note: it's not really necessary to create the initial segment now,
195  * since slru.c would create it on first write anyway. But we may as well
196  * do it to be sure the directory is set up correctly.
197  */
198 void
200 {
201  int slotno;
202 
203  LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
204 
205  /* Create and zero the first page of the subtrans log */
206  slotno = ZeroSUBTRANSPage(0);
207 
208  /* Make sure it's written out */
210  Assert(!SubTransCtl->shared->page_dirty[slotno]);
211 
212  LWLockRelease(SubtransControlLock);
213 }
214 
215 /*
216  * Initialize (or reinitialize) a page of SUBTRANS to zeroes.
217  *
218  * The page is not actually written, just set up in shared memory.
219  * The slot number of the new page is returned.
220  *
221  * Control lock must be held at entry, and will be held at exit.
222  */
223 static int
224 ZeroSUBTRANSPage(int pageno)
225 {
226  return SimpleLruZeroPage(SubTransCtl, pageno);
227 }
228 
229 /*
230  * This must be called ONCE during postmaster or standalone-backend startup,
231  * after StartupXLOG has initialized ShmemVariableCache->nextXid.
232  *
233  * oldestActiveXID is the oldest XID of any prepared transaction, or nextXid
234  * if there are none.
235  */
236 void
238 {
239  int startPage;
240  int endPage;
241 
242  /*
243  * Since we don't expect pg_subtrans to be valid across crashes, we
244  * initialize the currently-active page(s) to zeroes during startup.
245  * Whenever we advance into a new page, ExtendSUBTRANS will likewise zero
246  * the new page without regard to whatever was previously on disk.
247  */
248  LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
249 
250  startPage = TransactionIdToPage(oldestActiveXID);
252 
253  while (startPage != endPage)
254  {
255  (void) ZeroSUBTRANSPage(startPage);
256  startPage++;
257  /* must account for wraparound */
258  if (startPage > TransactionIdToPage(MaxTransactionId))
259  startPage = 0;
260  }
261  (void) ZeroSUBTRANSPage(startPage);
262 
263  LWLockRelease(SubtransControlLock);
264 }
265 
266 /*
267  * This must be called ONCE during postmaster or standalone-backend shutdown
268  */
269 void
271 {
272  /*
273  * Flush dirty SUBTRANS pages to disk
274  *
275  * This is not actually necessary from a correctness point of view. We do
276  * it merely as a debugging aid.
277  */
278  TRACE_POSTGRESQL_SUBTRANS_CHECKPOINT_START(false);
279  SimpleLruFlush(SubTransCtl, false);
280  TRACE_POSTGRESQL_SUBTRANS_CHECKPOINT_DONE(false);
281 }
282 
283 /*
284  * Perform a checkpoint --- either during shutdown, or on-the-fly
285  */
286 void
288 {
289  /*
290  * Flush dirty SUBTRANS pages to disk
291  *
292  * This is not actually necessary from a correctness point of view. We do
293  * it merely to improve the odds that writing of dirty pages is done by
294  * the checkpoint process and not by backends.
295  */
296  TRACE_POSTGRESQL_SUBTRANS_CHECKPOINT_START(true);
298  TRACE_POSTGRESQL_SUBTRANS_CHECKPOINT_DONE(true);
299 }
300 
301 
302 /*
303  * Make sure that SUBTRANS has room for a newly-allocated XID.
304  *
305  * NB: this is called while holding XidGenLock. We want it to be very fast
306  * most of the time; even when it's not so fast, no actual I/O need happen
307  * unless we're forced to write out a dirty subtrans page to make room
308  * in shared memory.
309  */
310 void
312 {
313  int pageno;
314 
315  /*
316  * No work except at first XID of a page. But beware: just after
317  * wraparound, the first XID of page zero is FirstNormalTransactionId.
318  */
319  if (TransactionIdToEntry(newestXact) != 0 &&
321  return;
322 
323  pageno = TransactionIdToPage(newestXact);
324 
325  LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
326 
327  /* Zero the page */
328  ZeroSUBTRANSPage(pageno);
329 
330  LWLockRelease(SubtransControlLock);
331 }
332 
333 
334 /*
335  * Remove all SUBTRANS segments before the one holding the passed transaction ID
336  *
337  * This is normally called during checkpoint, with oldestXact being the
338  * oldest TransactionXmin of any running transaction.
339  */
340 void
342 {
343  int cutoffPage;
344 
345  /*
346  * The cutoff point is the start of the segment containing oldestXact. We
347  * pass the *page* containing oldestXact to SimpleLruTruncate. We step
348  * back one transaction to avoid passing a cutoff page that hasn't been
349  * created yet in the rare case that oldestXact would be the first item on
350  * a page and oldestXact == next XID. In that case, if we didn't subtract
351  * one, we'd trigger SimpleLruTruncate's wraparound detection.
352  */
353  TransactionIdRetreat(oldestXact);
354  cutoffPage = TransactionIdToPage(oldestXact);
355 
356  SimpleLruTruncate(SubTransCtl, cutoffPage);
357 }
358 
359 
360 /*
361  * Decide which of two SUBTRANS page numbers is "older" for truncation purposes.
362  *
363  * We need to use comparison of TransactionIds here in order to do the right
364  * thing with wraparound XID arithmetic. However, if we are asked about
365  * page number zero, we don't want to hand InvalidTransactionId to
366  * TransactionIdPrecedes: it'll get weird about permanent xact IDs. So,
367  * offset both xids by FirstNormalTransactionId to avoid that.
368  */
369 static bool
370 SubTransPagePrecedes(int page1, int page2)
371 {
372  TransactionId xid1;
373  TransactionId xid2;
374 
375  xid1 = ((TransactionId) page1) * SUBTRANS_XACTS_PER_PAGE;
376  xid1 += FirstNormalTransactionId;
377  xid2 = ((TransactionId) page2) * SUBTRANS_XACTS_PER_PAGE;
378  xid2 += FirstNormalTransactionId;
379 
380  return TransactionIdPrecedes(xid1, xid2);
381 }
static int ZeroSUBTRANSPage(int pageno)
Definition: subtrans.c:224
#define TransactionIdToEntry(xid)
Definition: subtrans.c:54
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
void ShutdownSUBTRANS(void)
Definition: subtrans.c:270
uint32 TransactionId
Definition: c.h:397
void SUBTRANSShmemInit(void)
Definition: subtrans.c:178
TransactionId SubTransGetTopmostTransaction(TransactionId xid)
Definition: subtrans.c:146
TransactionId SubTransGetParent(TransactionId xid)
Definition: subtrans.c:105
void SubTransSetParent(TransactionId xid, TransactionId parent, bool overwriteOK)
Definition: subtrans.c:75
void SimpleLruTruncate(SlruCtl ctl, int cutoffPage)
Definition: slru.c:1165
bool TransactionIdFollowsOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:349
void TruncateSUBTRANS(TransactionId oldestXact)
Definition: subtrans.c:341
#define TransactionIdRetreat(dest)
Definition: transam.h:56
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:145
void SimpleLruFlush(SlruCtl ctl, bool allow_redirtied)
Definition: slru.c:1100
TransactionId TransactionXmin
Definition: snapmgr.c:164
#define SubTransCtl
Definition: subtrans.c:62
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
void BootStrapSUBTRANS(void)
Definition: subtrans.c:199
#define FirstNormalTransactionId
Definition: transam.h:34
void ExtendSUBTRANS(TransactionId newestXact)
Definition: subtrans.c:311
TransactionId nextXid
Definition: transam.h:117
int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok, TransactionId xid)
Definition: slru.c:371
VariableCache ShmemVariableCache
Definition: varsup.c:34
#define InvalidTransactionId
Definition: transam.h:31
void SimpleLruWritePage(SlruCtl ctl, int slotno)
Definition: slru.c:574
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
#define MaxTransactionId
Definition: transam.h:35
static bool SubTransPagePrecedes(int page1, int page2)
Definition: subtrans.c:370
#define TransactionIdToPage(xid)
Definition: subtrans.c:53
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int pageno, TransactionId xid)
Definition: slru.c:463
#define NUM_SUBTRANS_BUFFERS
Definition: subtrans.h:15
void StartupSUBTRANS(TransactionId oldestActiveXID)
Definition: subtrans.c:237
#define Assert(condition)
Definition: c.h:675
size_t Size
Definition: c.h:356
void CheckPointSUBTRANS(void)
Definition: subtrans.c:287
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
Size SUBTRANSShmemSize(void)
Definition: subtrans.c:172
#define SUBTRANS_XACTS_PER_PAGE
Definition: subtrans.c:51
#define TransactionIdIsValid(xid)
Definition: transam.h:41
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
int SimpleLruZeroPage(SlruCtl ctl, int pageno)
Definition: slru.c:259
void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns, LWLock *ctllock, const char *subdir, int tranche_id)
Definition: slru.c:165
static SlruCtlData SubTransCtlData
Definition: subtrans.c:60