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

Go to the source code of this file.

Functions

static XidStatus TransactionLogFetch (TransactionId transactionId)
 
bool TransactionIdDidCommit (TransactionId transactionId)
 
bool TransactionIdDidAbort (TransactionId transactionId)
 
void TransactionIdCommitTree (TransactionId xid, int nxids, TransactionId *xids)
 
void TransactionIdAsyncCommitTree (TransactionId xid, int nxids, TransactionId *xids, XLogRecPtr lsn)
 
void TransactionIdAbortTree (TransactionId xid, int nxids, TransactionId *xids)
 
TransactionId TransactionIdLatest (TransactionId mainxid, int nxids, const TransactionId *xids)
 
XLogRecPtr TransactionIdGetCommitLSN (TransactionId xid)
 

Variables

static TransactionId cachedFetchXid = InvalidTransactionId
 
static XidStatus cachedFetchXidStatus
 
static XLogRecPtr cachedCommitLSN
 

Function Documentation

◆ TransactionIdAbortTree()

void TransactionIdAbortTree ( TransactionId  xid,
int  nxids,
TransactionId xids 
)

Definition at line 270 of file transam.c.

271{
272 TransactionIdSetTreeStatus(xid, nxids, xids,
274}
void TransactionIdSetTreeStatus(TransactionId xid, int nsubxids, TransactionId *subxids, XidStatus status, XLogRecPtr lsn)
Definition: clog.c:181
#define TRANSACTION_STATUS_ABORTED
Definition: clog.h:29
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28

References InvalidXLogRecPtr, TRANSACTION_STATUS_ABORTED, and TransactionIdSetTreeStatus().

Referenced by RecordTransactionAbort(), RecordTransactionAbortPrepared(), and xact_redo_abort().

◆ TransactionIdAsyncCommitTree()

void TransactionIdAsyncCommitTree ( TransactionId  xid,
int  nxids,
TransactionId xids,
XLogRecPtr  lsn 
)

Definition at line 252 of file transam.c.

254{
255 TransactionIdSetTreeStatus(xid, nxids, xids,
257}
#define TRANSACTION_STATUS_COMMITTED
Definition: clog.h:28

References TRANSACTION_STATUS_COMMITTED, and TransactionIdSetTreeStatus().

Referenced by RecordTransactionCommit(), and xact_redo_commit().

◆ TransactionIdCommitTree()

void TransactionIdCommitTree ( TransactionId  xid,
int  nxids,
TransactionId xids 
)

◆ TransactionIdDidAbort()

bool TransactionIdDidAbort ( TransactionId  transactionId)

Definition at line 188 of file transam.c.

189{
190 XidStatus xidstatus;
191
192 xidstatus = TransactionLogFetch(transactionId);
193
194 /*
195 * If it's marked aborted, it's aborted.
196 */
197 if (xidstatus == TRANSACTION_STATUS_ABORTED)
198 return true;
199
200 /*
201 * If it's marked subcommitted, we have to check the parent recursively.
202 * However, if it's older than TransactionXmin, we can't look at
203 * pg_subtrans; instead assume that the parent crashed without cleaning up
204 * its children.
205 */
206 if (xidstatus == TRANSACTION_STATUS_SUB_COMMITTED)
207 {
208 TransactionId parentXid;
209
210 if (TransactionIdPrecedes(transactionId, TransactionXmin))
211 return true;
212 parentXid = SubTransGetParent(transactionId);
213 if (!TransactionIdIsValid(parentXid))
214 {
215 /* see notes in TransactionIdDidCommit */
216 elog(WARNING, "no pg_subtrans entry for subcommitted XID %u",
217 transactionId);
218 return true;
219 }
220 return TransactionIdDidAbort(parentXid);
221 }
222
223 /*
224 * It's not aborted.
225 */
226 return false;
227}
uint32 TransactionId
Definition: c.h:661
int XidStatus
Definition: clog.h:25
#define TRANSACTION_STATUS_SUB_COMMITTED
Definition: clog.h:30
#define WARNING
Definition: elog.h:36
#define elog(elevel,...)
Definition: elog.h:226
TransactionId TransactionXmin
Definition: snapmgr.c:158
TransactionId SubTransGetParent(TransactionId xid)
Definition: subtrans.c:121
static XidStatus TransactionLogFetch(TransactionId transactionId)
Definition: transam.c:52
bool TransactionIdDidAbort(TransactionId transactionId)
Definition: transam.c:188
#define TransactionIdIsValid(xid)
Definition: transam.h:41
static bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.h:263

References elog, SubTransGetParent(), TRANSACTION_STATUS_ABORTED, TRANSACTION_STATUS_SUB_COMMITTED, TransactionIdDidAbort(), TransactionIdIsValid, TransactionIdPrecedes(), TransactionLogFetch(), TransactionXmin, and WARNING.

Referenced by DoesMultiXactIdConflict(), heap_lock_updated_tuple_rec(), heap_update(), ProcArrayApplyRecoveryInfo(), ProcessTwoPhaseBuffer(), StandbyAcquireAccessExclusiveLock(), test_lockmode_for_conflict(), TransactionIdDidAbort(), and TransactionIdIsInProgress().

◆ TransactionIdDidCommit()

bool TransactionIdDidCommit ( TransactionId  transactionId)

Definition at line 126 of file transam.c.

127{
128 XidStatus xidstatus;
129
130 xidstatus = TransactionLogFetch(transactionId);
131
132 /*
133 * If it's marked committed, it's committed.
134 */
135 if (xidstatus == TRANSACTION_STATUS_COMMITTED)
136 return true;
137
138 /*
139 * If it's marked subcommitted, we have to check the parent recursively.
140 * However, if it's older than TransactionXmin, we can't look at
141 * pg_subtrans; instead assume that the parent crashed without cleaning up
142 * its children.
143 *
144 * Originally we Assert'ed that the result of SubTransGetParent was not
145 * zero. However with the introduction of prepared transactions, there can
146 * be a window just after database startup where we do not have complete
147 * knowledge in pg_subtrans of the transactions after TransactionXmin.
148 * StartupSUBTRANS() has ensured that any missing information will be
149 * zeroed. Since this case should not happen under normal conditions, it
150 * seems reasonable to emit a WARNING for it.
151 */
152 if (xidstatus == TRANSACTION_STATUS_SUB_COMMITTED)
153 {
154 TransactionId parentXid;
155
156 if (TransactionIdPrecedes(transactionId, TransactionXmin))
157 return false;
158 parentXid = SubTransGetParent(transactionId);
159 if (!TransactionIdIsValid(parentXid))
160 {
161 elog(WARNING, "no pg_subtrans entry for subcommitted XID %u",
162 transactionId);
163 return false;
164 }
165 return TransactionIdDidCommit(parentXid);
166 }
167
168 /*
169 * It's not committed.
170 */
171 return false;
172}
bool TransactionIdDidCommit(TransactionId transactionId)
Definition: transam.c:126

References elog, SubTransGetParent(), TRANSACTION_STATUS_COMMITTED, TRANSACTION_STATUS_SUB_COMMITTED, TransactionIdDidCommit(), TransactionIdIsValid, TransactionIdPrecedes(), TransactionLogFetch(), TransactionXmin, and WARNING.

Referenced by AsyncNotifyFreezeXids(), asyncQueueProcessPageEntries(), check_safe_enum_use(), compute_new_xmax_infomask(), FreezeMultiXactId(), get_xid_status(), HandleConcurrentAbort(), heap_pre_freeze_checks(), HeapTupleHeaderAdvanceConflictHorizon(), HeapTupleHeaderIsOnlyLocked(), HeapTupleSatisfiesDirty(), HeapTupleSatisfiesHistoricMVCC(), HeapTupleSatisfiesMVCC(), HeapTupleSatisfiesSelf(), HeapTupleSatisfiesToast(), HeapTupleSatisfiesUpdate(), HeapTupleSatisfiesVacuumHorizon(), MultiXactIdExpand(), pg_xact_status(), ProcArrayApplyRecoveryInfo(), ProcessTwoPhaseBuffer(), RecordTransactionAbort(), RecordTransactionAbortPrepared(), ReorderBufferCheckAndTruncateAbortedTXN(), SetupCheckXidLive(), StandbyAcquireAccessExclusiveLock(), test_lockmode_for_conflict(), TransactionIdDidCommit(), UpdateLogicalMappings(), and UpdateXmaxHintBits().

◆ TransactionIdGetCommitLSN()

XLogRecPtr TransactionIdGetCommitLSN ( TransactionId  xid)

Definition at line 318 of file transam.c.

319{
320 XLogRecPtr result;
321
322 /*
323 * Currently, all uses of this function are for xids that were just
324 * reported to be committed by TransactionLogFetch, so we expect that
325 * checking TransactionLogFetch's cache will usually succeed and avoid an
326 * extra trip to shared memory.
327 */
329 return cachedCommitLSN;
330
331 /* Special XIDs are always known committed */
332 if (!TransactionIdIsNormal(xid))
333 return InvalidXLogRecPtr;
334
335 /*
336 * Get the transaction status.
337 */
338 (void) TransactionIdGetStatus(xid, &result);
339
340 return result;
341}
XidStatus TransactionIdGetStatus(TransactionId xid, XLogRecPtr *lsn)
Definition: clog.c:734
static TransactionId cachedFetchXid
Definition: transam.c:33
static XLogRecPtr cachedCommitLSN
Definition: transam.c:35
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
uint64 XLogRecPtr
Definition: xlogdefs.h:21

References cachedCommitLSN, cachedFetchXid, InvalidXLogRecPtr, TransactionIdEquals, TransactionIdGetStatus(), and TransactionIdIsNormal.

Referenced by SetHintBits().

◆ TransactionIdLatest()

TransactionId TransactionIdLatest ( TransactionId  mainxid,
int  nxids,
const TransactionId xids 
)

Definition at line 281 of file transam.c.

283{
284 TransactionId result;
285
286 /*
287 * In practice it is highly likely that the xids[] array is sorted, and so
288 * we could save some cycles by just taking the last child XID, but this
289 * probably isn't so performance-critical that it's worth depending on
290 * that assumption. But just to show we're not totally stupid, scan the
291 * array back-to-front to avoid useless assignments.
292 */
293 result = mainxid;
294 while (--nxids >= 0)
295 {
296 if (TransactionIdPrecedes(result, xids[nxids]))
297 result = xids[nxids];
298 }
299 return result;
300}

References TransactionIdPrecedes().

Referenced by FinishPreparedTransaction(), ProcArrayApplyXidAssignment(), RecordTransactionAbort(), RecordTransactionCommit(), xact_redo_abort(), and xact_redo_commit().

◆ TransactionLogFetch()

static XidStatus TransactionLogFetch ( TransactionId  transactionId)
static

Definition at line 52 of file transam.c.

53{
54 XidStatus xidstatus;
55 XLogRecPtr xidlsn;
56
57 /*
58 * Before going to the commit log manager, check our single item cache to
59 * see if we didn't just check the transaction status a moment ago.
60 */
61 if (TransactionIdEquals(transactionId, cachedFetchXid))
63
64 /*
65 * Also, check to see if the transaction ID is a permanent one.
66 */
67 if (!TransactionIdIsNormal(transactionId))
68 {
71 if (TransactionIdEquals(transactionId, FrozenTransactionId))
74 }
75
76 /*
77 * Get the transaction status.
78 */
79 xidstatus = TransactionIdGetStatus(transactionId, &xidlsn);
80
81 /*
82 * Cache it, but DO NOT cache status for unfinished or sub-committed
83 * transactions! We only cache status that is guaranteed not to change.
84 */
85 if (xidstatus != TRANSACTION_STATUS_IN_PROGRESS &&
87 {
88 cachedFetchXid = transactionId;
89 cachedFetchXidStatus = xidstatus;
90 cachedCommitLSN = xidlsn;
91 }
92
93 return xidstatus;
94}
#define TRANSACTION_STATUS_IN_PROGRESS
Definition: clog.h:27
static XidStatus cachedFetchXidStatus
Definition: transam.c:34
#define FrozenTransactionId
Definition: transam.h:33
#define BootstrapTransactionId
Definition: transam.h:32

References BootstrapTransactionId, cachedCommitLSN, cachedFetchXid, cachedFetchXidStatus, FrozenTransactionId, TRANSACTION_STATUS_ABORTED, TRANSACTION_STATUS_COMMITTED, TRANSACTION_STATUS_IN_PROGRESS, TRANSACTION_STATUS_SUB_COMMITTED, TransactionIdEquals, TransactionIdGetStatus(), and TransactionIdIsNormal.

Referenced by TransactionIdDidAbort(), and TransactionIdDidCommit().

Variable Documentation

◆ cachedCommitLSN

XLogRecPtr cachedCommitLSN
static

Definition at line 35 of file transam.c.

Referenced by TransactionIdGetCommitLSN(), and TransactionLogFetch().

◆ cachedFetchXid

TransactionId cachedFetchXid = InvalidTransactionId
static

Definition at line 33 of file transam.c.

Referenced by TransactionIdGetCommitLSN(), and TransactionLogFetch().

◆ cachedFetchXidStatus

XidStatus cachedFetchXidStatus
static

Definition at line 34 of file transam.c.

Referenced by TransactionLogFetch().