PostgreSQL Source Code git master
Loading...
Searching...
No Matches
subtrans.c File Reference
#include "postgres.h"
#include "access/slru.h"
#include "access/subtrans.h"
#include "access/transam.h"
#include "miscadmin.h"
#include "pg_trace.h"
#include "utils/guc_hooks.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 TransactionIdToEntry(xid)   ((xid) % (TransactionId) SUBTRANS_XACTS_PER_PAGE)
 
#define SubTransCtl   (&SubTransCtlData)
 

Functions

static int64 TransactionIdToPage (TransactionId xid)
 
static bool SubTransPagePrecedes (int64 page1, int64 page2)
 
static int subtrans_errdetail_for_io_error (const void *opaque_data)
 
void SubTransSetParent (TransactionId xid, TransactionId parent)
 
TransactionId SubTransGetParent (TransactionId xid)
 
TransactionId SubTransGetTopmostTransaction (TransactionId xid)
 
static int SUBTRANSShmemBuffers (void)
 
Size SUBTRANSShmemSize (void)
 
void SUBTRANSShmemInit (void)
 
bool check_subtrans_buffers (int *newval, void **extra, GucSource source)
 
void BootStrapSUBTRANS (void)
 
void StartupSUBTRANS (TransactionId oldestActiveXID)
 
void CheckPointSUBTRANS (void)
 
void ExtendSUBTRANS (TransactionId newestXact)
 
void TruncateSUBTRANS (TransactionId oldestXact)
 

Variables

static SlruCtlData SubTransCtlData
 

Macro Definition Documentation

◆ SUBTRANS_XACTS_PER_PAGE

#define SUBTRANS_XACTS_PER_PAGE   (BLCKSZ / sizeof(TransactionId))

Definition at line 54 of file subtrans.c.

◆ SubTransCtl

#define SubTransCtl   (&SubTransCtlData)

Definition at line 74 of file subtrans.c.

◆ TransactionIdToEntry

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

Definition at line 66 of file subtrans.c.

Function Documentation

◆ BootStrapSUBTRANS()

void BootStrapSUBTRANS ( void  )

Definition at line 271 of file subtrans.c.

272{
273 /* Zero the initial page and flush it to disk */
275}
void SimpleLruZeroAndWritePage(SlruCtl ctl, int64 pageno)
Definition slru.c:449
#define SubTransCtl
Definition subtrans.c:74

References SimpleLruZeroAndWritePage(), and SubTransCtl.

Referenced by BootStrapXLOG().

◆ check_subtrans_buffers()

bool check_subtrans_buffers ( int newval,
void **  extra,
GucSource  source 
)

Definition at line 255 of file subtrans.c.

256{
257 return check_slru_buffers("subtransaction_buffers", newval);
258}
#define newval
bool check_slru_buffers(const char *name, int *newval)
Definition slru.c:360

References check_slru_buffers(), and newval.

◆ CheckPointSUBTRANS()

void CheckPointSUBTRANS ( void  )

Definition at line 331 of file subtrans.c.

332{
333 /*
334 * Write dirty SUBTRANS pages to disk
335 *
336 * This is not actually necessary from a correctness point of view. We do
337 * it merely to improve the odds that writing of dirty pages is done by
338 * the checkpoint process and not by backends.
339 */
343}
static int fb(int x)
void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied)
Definition slru.c:1355

References fb(), SimpleLruWriteAll(), and SubTransCtl.

Referenced by CheckPointGuts().

◆ ExtendSUBTRANS()

void ExtendSUBTRANS ( TransactionId  newestXact)

Definition at line 355 of file subtrans.c.

356{
357 int64 pageno;
358 LWLock *lock;
359
360 /*
361 * No work except at first XID of a page. But beware: just after
362 * wraparound, the first XID of page zero is FirstNormalTransactionId.
363 */
366 return;
367
369
370 lock = SimpleLruGetBankLock(SubTransCtl, pageno);
372
373 /* Zero the page */
375
376 LWLockRelease(lock);
377}
int64_t int64
Definition c.h:615
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1153
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1770
@ LW_EXCLUSIVE
Definition lwlock.h:104
int SimpleLruZeroPage(SlruCtl ctl, int64 pageno)
Definition slru.c:380
static LWLock * SimpleLruGetBankLock(SlruCtl ctl, int64 pageno)
Definition slru.h:171
#define TransactionIdToEntry(xid)
Definition subtrans.c:66
static int64 TransactionIdToPage(TransactionId xid)
Definition subtrans.c:61
#define TransactionIdEquals(id1, id2)
Definition transam.h:43
#define FirstNormalTransactionId
Definition transam.h:34

References fb(), FirstNormalTransactionId, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), SimpleLruGetBankLock(), SimpleLruZeroPage(), SubTransCtl, TransactionIdEquals, TransactionIdToEntry, and TransactionIdToPage().

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

◆ StartupSUBTRANS()

void StartupSUBTRANS ( TransactionId  oldestActiveXID)

Definition at line 285 of file subtrans.c.

286{
287 FullTransactionId nextXid;
291 LWLock *lock;
292
293 /*
294 * Since we don't expect pg_subtrans to be valid across crashes, we
295 * initialize the currently-active page(s) to zeroes during startup.
296 * Whenever we advance into a new page, ExtendSUBTRANS will likewise zero
297 * the new page without regard to whatever was previously on disk.
298 */
300 nextXid = TransamVariables->nextXid;
302
303 for (;;)
304 {
306 if (prevlock != lock)
307 {
308 if (prevlock)
311 prevlock = lock;
312 }
313
315 if (startPage == endPage)
316 break;
317
318 startPage++;
319 /* must account for wraparound */
321 startPage = 0;
322 }
323
324 LWLockRelease(lock);
325}
FullTransactionId nextXid
Definition transam.h:220
#define XidFromFullTransactionId(x)
Definition transam.h:48
#define MaxTransactionId
Definition transam.h:35
TransamVariablesData * TransamVariables
Definition varsup.c:34

References fb(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MaxTransactionId, TransamVariablesData::nextXid, SimpleLruGetBankLock(), SimpleLruZeroPage(), SubTransCtl, TransactionIdToPage(), TransamVariables, and XidFromFullTransactionId.

Referenced by StartupXLOG().

◆ subtrans_errdetail_for_io_error()

static int subtrans_errdetail_for_io_error ( const void opaque_data)
static

Definition at line 426 of file subtrans.c.

427{
428 TransactionId xid = *(const TransactionId *) opaque_data;
429
430 return errdetail("Could not access subtransaction status of transaction %u.", xid);
431}
uint32 TransactionId
Definition c.h:738
int errdetail(const char *fmt,...) pg_attribute_printf(1

References errdetail(), and fb().

Referenced by SUBTRANSShmemInit().

◆ SubTransGetParent()

TransactionId SubTransGetParent ( TransactionId  xid)

Definition at line 122 of file subtrans.c.

123{
124 int64 pageno = TransactionIdToPage(xid);
125 int entryno = TransactionIdToEntry(xid);
126 int slotno;
127 TransactionId *ptr;
128 TransactionId parent;
129
130 /* Can't ask about stuff that might not be around anymore */
132
133 /* Bootstrap and frozen XIDs have no parent */
134 if (!TransactionIdIsNormal(xid))
136
137 /* lock is acquired by SimpleLruReadPage_ReadOnly */
138
140 ptr = (TransactionId *) SubTransCtl->shared->page_buffer[slotno];
141 ptr += entryno;
142
143 parent = *ptr;
144
146
147 return parent;
148}
#define Assert(condition)
Definition c.h:945
int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int64 pageno, const void *opaque_data)
Definition slru.c:637
TransactionId TransactionXmin
Definition snapmgr.c:159
#define InvalidTransactionId
Definition transam.h:31
static bool TransactionIdFollowsOrEquals(TransactionId id1, TransactionId id2)
Definition transam.h:312
#define TransactionIdIsNormal(xid)
Definition transam.h:42

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

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

◆ SubTransGetTopmostTransaction()

TransactionId SubTransGetTopmostTransaction ( TransactionId  xid)

Definition at line 163 of file subtrans.c.

164{
166 previousXid = xid;
167
168 /* Can't ask about stuff that might not be around anymore */
170
172 {
175 break;
177
178 /*
179 * By convention the parent xid gets allocated first, so should always
180 * precede the child xid. Anything else points to a corrupted data
181 * structure that could lead to an infinite loop, so exit.
182 */
184 elog(ERROR, "pg_subtrans contains invalid entry: xid %u points to parent xid %u",
186 }
187
189
190 return previousXid;
191}
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
TransactionId SubTransGetParent(TransactionId xid)
Definition subtrans.c:122
#define TransactionIdIsValid(xid)
Definition transam.h:41
static bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition transam.h:263

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

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

◆ SubTransPagePrecedes()

static bool SubTransPagePrecedes ( int64  page1,
int64  page2 
)
static

◆ SubTransSetParent()

void SubTransSetParent ( TransactionId  xid,
TransactionId  parent 
)

Definition at line 85 of file subtrans.c.

86{
87 int64 pageno = TransactionIdToPage(xid);
89 int slotno;
90 LWLock *lock;
91 TransactionId *ptr;
92
94 Assert(TransactionIdFollows(xid, parent));
95
96 lock = SimpleLruGetBankLock(SubTransCtl, pageno);
98
99 slotno = SimpleLruReadPage(SubTransCtl, pageno, true, &xid);
100 ptr = (TransactionId *) SubTransCtl->shared->page_buffer[slotno];
101 ptr += entryno;
102
103 /*
104 * It's possible we'll try to set the parent xid multiple times but we
105 * shouldn't ever be changing the xid from one valid xid to another valid
106 * xid, which would corrupt the data structure.
107 */
108 if (*ptr != parent)
109 {
111 *ptr = parent;
112 SubTransCtl->shared->page_dirty[slotno] = true;
113 }
114
115 LWLockRelease(lock);
116}
int SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok, const void *opaque_data)
Definition slru.c:533
static bool TransactionIdFollows(TransactionId id1, TransactionId id2)
Definition transam.h:297

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

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

◆ SUBTRANSShmemBuffers()

static int SUBTRANSShmemBuffers ( void  )
static

Definition at line 201 of file subtrans.c.

202{
203 /* auto-tune based on shared buffers */
204 if (subtransaction_buffers == 0)
205 return SimpleLruAutotuneBuffers(512, 1024);
206
208}
#define Min(x, y)
Definition c.h:1093
#define Max(x, y)
Definition c.h:1087
int subtransaction_buffers
Definition globals.c:166
int SimpleLruAutotuneBuffers(int divisor, int max)
Definition slru.c:233
#define SLRU_MAX_ALLOWED_BUFFERS
Definition slru.h:25

References Max, Min, SimpleLruAutotuneBuffers(), SLRU_MAX_ALLOWED_BUFFERS, and subtransaction_buffers.

Referenced by SUBTRANSShmemInit(), and SUBTRANSShmemSize().

◆ SUBTRANSShmemInit()

void SUBTRANSShmemInit ( void  )

Definition at line 220 of file subtrans.c.

221{
222 /* If auto-tuning is requested, now is the time to do it */
223 if (subtransaction_buffers == 0)
224 {
225 char buf[32];
226
227 snprintf(buf, sizeof(buf), "%d", SUBTRANSShmemBuffers());
228 SetConfigOption("subtransaction_buffers", buf, PGC_POSTMASTER,
230
231 /*
232 * We prefer to report this value's source as PGC_S_DYNAMIC_DEFAULT.
233 * However, if the DBA explicitly set subtransaction_buffers = 0 in
234 * the config file, then PGC_S_DYNAMIC_DEFAULT will fail to override
235 * that and we must force the matter with PGC_S_OVERRIDE.
236 */
237 if (subtransaction_buffers == 0) /* failed to apply it? */
238 SetConfigOption("subtransaction_buffers", buf, PGC_POSTMASTER,
240 }
242
243 SubTransCtl->PagePrecedes = SubTransPagePrecedes;
244 SubTransCtl->errdetail_for_io_error = subtrans_errdetail_for_io_error;
245 SimpleLruInit(SubTransCtl, "subtransaction", SUBTRANSShmemBuffers(), 0,
246 "pg_subtrans", LWTRANCHE_SUBTRANS_BUFFER,
249}
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition guc.c:4228
@ PGC_S_DYNAMIC_DEFAULT
Definition guc.h:114
@ PGC_S_OVERRIDE
Definition guc.h:123
@ PGC_POSTMASTER
Definition guc.h:74
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define snprintf
Definition port.h:260
void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns, const char *subdir, int buffer_tranche_id, int bank_tranche_id, SyncRequestHandler sync_handler, bool long_segment_names)
Definition slru.c:254
#define SlruPagePrecedesUnitTests(ctl, per_page)
Definition slru.h:196
static int subtrans_errdetail_for_io_error(const void *opaque_data)
Definition subtrans.c:426
static int SUBTRANSShmemBuffers(void)
Definition subtrans.c:201
static bool SubTransPagePrecedes(int64 page1, int64 page2)
Definition subtrans.c:411
@ SYNC_HANDLER_NONE
Definition sync.h:42

References Assert, buf, fb(), PGC_POSTMASTER, PGC_S_DYNAMIC_DEFAULT, PGC_S_OVERRIDE, SetConfigOption(), SimpleLruInit(), SlruPagePrecedesUnitTests, snprintf, subtrans_errdetail_for_io_error(), SUBTRANS_XACTS_PER_PAGE, subtransaction_buffers, SubTransCtl, SubTransPagePrecedes(), SUBTRANSShmemBuffers(), and SYNC_HANDLER_NONE.

Referenced by CreateOrAttachShmemStructs().

◆ SUBTRANSShmemSize()

Size SUBTRANSShmemSize ( void  )

Definition at line 214 of file subtrans.c.

215{
217}
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition slru.c:200

References SimpleLruShmemSize(), and SUBTRANSShmemBuffers().

Referenced by CalculateShmemSize().

◆ TransactionIdToPage()

static int64 TransactionIdToPage ( TransactionId  xid)
inlinestatic

◆ TruncateSUBTRANS()

void TruncateSUBTRANS ( TransactionId  oldestXact)

Definition at line 387 of file subtrans.c.

388{
390
391 /*
392 * The cutoff point is the start of the segment containing oldestXact. We
393 * pass the *page* containing oldestXact to SimpleLruTruncate. We step
394 * back one transaction to avoid passing a cutoff page that hasn't been
395 * created yet in the rare case that oldestXact would be the first item on
396 * a page and oldestXact == next XID. In that case, if we didn't subtract
397 * one, we'd trigger SimpleLruTruncate's wraparound detection.
398 */
399 TransactionIdRetreat(oldestXact);
400 cutoffPage = TransactionIdToPage(oldestXact);
401
403}
void SimpleLruTruncate(SlruCtl ctl, int64 cutoffPage)
Definition slru.c:1441
#define TransactionIdRetreat(dest)
Definition transam.h:141

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

Referenced by CreateCheckPoint(), and CreateRestartPoint().

Variable Documentation

◆ SubTransCtlData

SlruCtlData SubTransCtlData
static

Definition at line 72 of file subtrans.c.