Skip to content

Commit b79402a

Browse files
committed
WIP revork DtmAdjustOldestXid; it is okay to ignore slot_xmin, but we should wait for long-lived tx
1 parent e14e2f9 commit b79402a

File tree

1 file changed

+57
-31
lines changed

1 file changed

+57
-31
lines changed

src/backend/access/transam/global_snapshot.c

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ static HTAB *xid2status;
8989
static HTAB *gtid2xid;
9090
static DtmNodeState *local;
9191
static uint64 totalSleepInterrupts;
92-
static int DtmVacuumDelay = 10;
92+
static int DtmVacuumDelay = 2; /* sec */
9393
static bool DtmRecordCommits = 0;
9494

9595
DtmCurrentTrans dtm_tx; // XXXX: make static
9696

9797
static Snapshot DtmGetSnapshot(Snapshot snapshot);
9898
static TransactionId DtmGetOldestXmin(Relation rel, int flags);
9999
static bool DtmXidInMVCCSnapshot(TransactionId xid, Snapshot snapshot);
100-
static TransactionId DtmAdjustOldestXid(TransactionId xid);
100+
static void DtmAdjustOldestXid(void);
101101
static bool DtmDetectGlobalDeadLock(PGPROC *proc);
102102
static void DtmAddSubtransactions(DtmTransStatus * ts, TransactionId *subxids, int nSubxids);
103103
static char const *DtmGetName(void);
@@ -325,47 +325,58 @@ DtmTransactionListInsertAfter(DtmTransStatus * after, DtmTransStatus * ts)
325325
* is older than it more than DtmVacuumDelay.
326326
* If no such XID can be located, then return previously observed oldest XID
327327
*/
328-
static TransactionId
329-
DtmAdjustOldestXid(TransactionId xid)
328+
static void
329+
DtmAdjustOldestXid()
330330
{
331-
if (TransactionIdIsValid(xid))
332-
{
333-
DtmTransStatus *ts,
334-
*prev = NULL;
335-
timestamp_t now = dtm_get_current_time();
336-
timestamp_t cutoff_time = now - DtmVacuumDelay * USEC;
331+
DtmTransStatus *ts,
332+
*prev = NULL;
337333

338-
SpinLockAcquire(&local->lock);
339-
ts = (DtmTransStatus *) hash_search(xid2status, &xid, HASH_FIND, NULL);
340-
if (ts != NULL)
341-
{
342-
cutoff_time = ts->cid - DtmVacuumDelay * USEC;
334+
timestamp_t cutoff_time = dtm_get_current_time() - DtmVacuumDelay * USEC;
335+
int total = 0, deleted = 0;
343336

344-
for (ts = local->trans_list_head; ts != NULL && ts->cid < cutoff_time; prev = ts, ts = ts->next)
345-
{
346-
if (prev != NULL)
347-
hash_search(xid2status, &prev->xid, HASH_REMOVE, NULL);
348-
}
349-
}
337+
SpinLockAcquire(&local->lock);
338+
339+
for (ts = local->trans_list_head; ts != NULL; ts = ts->next)
340+
total++;
341+
342+
for (ts = local->trans_list_head; ts != NULL && ts->cid < cutoff_time; prev = ts, ts = ts->next)
343+
{
350344
if (prev != NULL)
351345
{
352-
local->trans_list_head = prev;
353-
local->oldest_xid = xid = prev->xid;
346+
hash_search(xid2status, &prev->xid, HASH_REMOVE, NULL);
347+
deleted++;
354348
}
355-
else
356-
{
357-
xid = local->oldest_xid;
358-
}
359-
SpinLockRelease(&local->lock);
360349
}
361-
return xid;
350+
351+
if (prev != NULL)
352+
local->trans_list_head = prev;
353+
354+
if (ts != NULL)
355+
local->oldest_xid = ts->xid;
356+
else
357+
local->oldest_xid = InvalidTransactionId;
358+
359+
SpinLockRelease(&local->lock);
360+
361+
// elog(LOG, "DtmAdjustOldestXid total=%d, deleted=%d, xid=%d, prev=%p, ts=%p", total, deleted, local->oldest_xid, prev, ts);
362362
}
363363

364364
Snapshot
365365
DtmGetSnapshot(Snapshot snapshot)
366366
{
367367
snapshot = PgGetSnapshotData(snapshot);
368-
RecentGlobalDataXmin = RecentGlobalXmin = DtmAdjustOldestXid(RecentGlobalDataXmin);
368+
// RecentGlobalDataXmin = RecentGlobalXmin = DtmAdjustOldestXid(RecentGlobalDataXmin);
369+
SpinLockAcquire(&local->lock);
370+
371+
if (TransactionIdIsValid(local->oldest_xid) &&
372+
TransactionIdPrecedes(local->oldest_xid, RecentGlobalXmin))
373+
RecentGlobalXmin = local->oldest_xid;
374+
375+
if (TransactionIdIsValid(local->oldest_xid) &&
376+
TransactionIdPrecedes(local->oldest_xid, RecentGlobalDataXmin))
377+
RecentGlobalDataXmin = local->oldest_xid;
378+
379+
SpinLockRelease(&local->lock);
369380
return snapshot;
370381
}
371382

@@ -374,7 +385,16 @@ DtmGetOldestXmin(Relation rel, int flags)
374385
{
375386
TransactionId xmin = PgGetOldestXmin(rel, flags);
376387

377-
xmin = DtmAdjustOldestXid(xmin);
388+
// xmin = DtmAdjustOldestXid(xmin);
389+
390+
SpinLockAcquire(&local->lock);
391+
392+
if (TransactionIdIsValid(local->oldest_xid) &&
393+
TransactionIdPrecedes(local->oldest_xid, xmin))
394+
xmin = local->oldest_xid;
395+
396+
SpinLockRelease(&local->lock);
397+
378398
return xmin;
379399
}
380400

@@ -670,6 +690,9 @@ DtmLocalCommitPrepared(DtmCurrentTrans * x)
670690
DTM_TRACE((stderr, "Global transaction %u(%s) is precommitted\n", x->xid, gtid));
671691
}
672692
SpinLockRelease(&local->lock);
693+
694+
DtmAdjustOldestXid();
695+
// elog(LOG, "DtmLocalCommitPrepared %d", x->xid);
673696
}
674697

675698
/*
@@ -717,6 +740,9 @@ DtmLocalCommit(DtmCurrentTrans * x)
717740
DTM_TRACE((stderr, "Local transaction %u is committed at %lu\n", x->xid, x->cid));
718741
}
719742
SpinLockRelease(&local->lock);
743+
744+
DtmAdjustOldestXid();
745+
// elog(LOG, "DtmLocalCommit %d", x->xid);
720746
}
721747

722748
/*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy