8000 Make DtmGetNextXid return invalid transaction id if failed to obtain … · postgrespro/postgres_cluster@66fc52b · GitHub
[go: up one dir, main page]

Skip to content

Commit 66fc52b

Browse files
committed
Make DtmGetNextXid return invalid transaction id if failed to obtain one.
1 parent 63ec9b9 commit 66fc52b

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

contrib/pg_dtm/pg_dtm.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ static void DtmUpdateRecentXmin(Snapshot snapshot)
295295
*/
296296
static TransactionId DtmGetNextXid()
297297
{
298-
TransactionId xid;
298+
TransactionId xid = InvalidTransactionId;
299299
LWLockAcquire(dtm->xidLock, LW_EXCLUSIVE);
300300
if (TransactionIdIsValid(DtmNextXid))
301301
{
@@ -321,7 +321,12 @@ static TransactionId DtmGetNextXid()
321321
if (dtm->nReservedXids == 0)
322322
{
323323
dtm->nReservedXids = DtmGlobalReserve(ShmemVariableCache->nextXid, DtmLocalXidReserve, &dtm->nextXid);
324-
Assert(dtm->nReservedXids > 0);
324+
if (dtm->nReservedXids < 1)
325+
{
326+
elog(WARNING, "failed to reserve a local range of xids on arbiter");
327+
goto end;
328+
}
329+
325330
Assert(TransactionIdFollowsOrEquals(dtm->nextXid, ShmemVariableCache->nextXid));
326331

327332
/* Advance ShmemVariableCache->nextXid formward until new Xid */
@@ -339,6 +344,7 @@ static TransactionId DtmGetNextXid()
339344
dtm->nReservedXids -= 1;
340345
XTM_INFO("Obtain new local XID %d\n", xid);
341346
}
347+
end:
342348
LWLockRelease(dtm->xidLock);
343349
return xid;
344350
}
@@ -387,6 +393,8 @@ DtmGetNewTransactionId(bool isSubXact)
387393

388394
LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
389395
xid = DtmGetNextXid();
396+
if (!TransactionIdIsValid(xid))
397+
elog(ERROR, "failed to get next xid from XTM");
390398

391399
/*----------
392400
* Check to see if it's safe to assign another XID. This protects against
@@ -620,7 +628,11 @@ static void DtmSetTransactionStatus(TransactionId xid, int nsubxids, Transaction
620628
if (status == TRANSACTION_STATUS_ABORTED)
621629
{
622630
PgTransactionIdSetTreeStatus(xid, nsubxids, subxids, status, lsn);
623-
DtmGlobalSetTransStatus(xid, status, false);
631+
if (DtmGlobalSetTransStatus(xid, status, false) == -1)
632+
{
633+
elog(WARNING, "failed to set 'aborted' transaction status on arbiter");
634+
return; // FIXME: return bool
635+
}
624636
XTM_INFO("Abort transaction %d\n", xid);
625637
return;
626638
}
@@ -631,7 +643,11 @@ static void DtmSetTransactionStatus(TransactionId xid, int nsubxids, Transaction
631643
LWLockAcquire(dtm->hashLock, LW_EXCLUSIVE);
632644
hash_search(xid_in_doubt, &DtmNextXid, HASH_ENTER, NULL);
633645
LWLockRelease(dtm->hashLock);
634-
DtmGlobalSetTransStatus(xid, status, true);
646+
if (DtmGlobalSetTransStatus(xid, status, true) == -1)
647+
{
648+
elog(WARNING, "failed to set 'committed' transaction status on arbiter");
649+
return; // FIXME: return bool
650+
}
635651
XTM_INFO("Commit transaction %d\n", xid);
636652
}
637653
}
@@ -648,6 +664,7 @@ static void DtmSetTransactionStatus(TransactionId xid, int nsubxids, Transaction
648664
status = gs;
649665
}
650666
PgTransactionIdSetTreeStatus(xid, nsubxids, subxids, status, lsn);
667+
// FIXME: return bool
651668
}
652669

653670
static uint32 dtm_xid_hash_fn(const void *key, Size keysize)

0 commit comments

Comments
 (0)
0