Skip to content

Commit 19422a9

Browse files
committed
get rid of gtid2xid hash
1 parent c69da66 commit 19422a9

File tree

5 files changed

+48
-62
lines changed

5 files changed

+48
-62
lines changed

contrib/postgres_fdw/connection.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ pgfdw_xact_callback(XactEvent event, void *arg)
832832
}
833833
else /* Otherwise, just finalize dtm and proceed below to COMMIT */
834834
{
835-
finalize_dtm();
835+
// finalize_dtm();
836836
}
837837
}
838838

src/backend/access/transam/global_snapshot.c

Lines changed: 20 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ typedef struct
8585

8686
// static shmem_startup_hook_type prev_shmem_startup_hook;
8787
static HTAB *xid2status;
88-
static HTAB *gtid2xid;
8988
static DtmNodeState *local;
9089
static uint64 totalSleepInterrupts;
9190
static int DtmVacuumDelay = 15; /* sec */
@@ -432,13 +431,6 @@ DtmInitialize()
432431
&info,
433432
HASH_ELEM | HASH_BLOBS);
434433

435-
info.keysize = MAX_GTID_SIZE;
436-
info.entrysize = sizeof(DtmTransId);
437-
gtid2xid = ShmemInitHash("gtid2xid",
438-
DTM_HASH_INIT_SIZE, DTM_HASH_INIT_SIZE,
439-
&info,
440-
HASH_ELEM);
441-
442434
TM = &DtmTM;
443435

444436
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
@@ -478,16 +470,7 @@ DtmLocalExtend(GlobalTransactionId gtid)
478470

479471
if (gtid != NULL)
480472
{
481-
SpinLockAcquire(&local->lock);
482-
{
483-
DtmTransId *id = (DtmTransId *) hash_search(gtid2xid, gtid, HASH_ENTER, NULL);
484-
485-
id->xid = GetCurrentTransactionId();
486-
id->nSubxids = 0;
487-
id->subxids = 0;
488-
}
489473
strncpy(x->gtid, gtid, MAX_GTID_SIZE);
490-
SpinLockRelease(&local->lock);
491474
}
492475
DtmInitGlobalXmin(TransactionXmin);
493476
return x->snapshot;
@@ -506,14 +489,6 @@ DtmLocalAccess(DtmCurrentTrans * x, GlobalTransactionId gtid, cid_t global_cid)
506489

507490
SpinLockAcquire(&local->lock);
508491
{
509-
if (gtid != NULL)
510-
{
511-
DtmTransId *id = (DtmTransId *) hash_search(gtid2xid, gtid, HASH_ENTER, NULL);
512-
513-
id->xid = GetCurrentTransactionId();
514-
id->nSubxids = 0;
515-
id->subxids = 0;
516-
}
517492
local_cid = dtm_sync(global_cid);
518493
x->snapshot = global_cid;
519494
}
@@ -536,18 +511,20 @@ DtmLocalAccess(DtmCurrentTrans * x, GlobalTransactionId gtid, cid_t global_cid)
536511
void
537512
DtmLocalBeginPrepare(GlobalTransactionId gtid)
538513
{
539-
// TransactionId xid = TwoPhaseGetTransactionId(gtid);
514+
TransactionId xid = TwoPhaseGetTransactionId(gtid);
515+
516+
if (!TransactionIdIsValid(xid))
517+
{
518+
// XXX: check that it is global tx with the same xid, XactTopTransactionId?
519+
xid = GetCurrentTransactionId();
520+
}
540521

541522
SpinLockAcquire(&local->lock);
542523
{
543524
DtmTransStatus *ts;
544-
DtmTransId *id;
545525
bool found;
546526

547-
id = (DtmTransId *) hash_search(gtid2xid, gtid, HASH_FIND, NULL);
548-
Assert(id != NULL);
549-
Assert(TransactionIdIsValid(id->xid));
550-
ts = (DtmTransStatus *) hash_search(xid2status, &id->xid, HASH_ENTER, &found);
527+
ts = (DtmTransStatus *) hash_search(xid2status, &xid, HASH_ENTER, &found);
551528
ts->status = TRANSACTION_STATUS_UNKNOWN;
552529
ts->cid = dtm_get_cid();
553530
if (!found)
@@ -582,15 +559,23 @@ DtmLocalPrepare(GlobalTransactionId gtid, cid_t global_cid)
582559
void
583560
DtmLocalEndPrepare(GlobalTransactionId gtid, cid_t cid)
584561
{
562+
TransactionId xid = TwoPhaseGetTransactionId(gtid);
563+
564+
if (!TransactionIdIsValid(xid))
565+
{
566+
// XXX: check that it is global tx with the same xid, XactTopTransactionId?
567+
xid = GetCurrentTransactionId();
568+
}
569+
570+
dtm_tx.xid = xid;
571+
585572
SpinLockAcquire(&local->lock);
586573
{
587574
DtmTransStatus *ts;
588575
DtmTransId *id;
589576
int i;
590577

591-
id = (DtmTransId *) hash_search(gtid2xid, gtid, HASH_FIND, NULL);
592-
593-
ts = (DtmTransStatus *) hash_search(xid2status, &id->xid, HASH_FIND, NULL);
578+
ts = (DtmTransStatus *) hash_search(xid2status, &xid, HASH_FIND, NULL);
594579
Assert(ts != NULL);
595580
ts->cid = cid;
596581
DtmAdjustSubtransactions(ts);
@@ -613,19 +598,7 @@ DtmLocalFinish(bool is_commit)
613598

614599
if (x->gtid[0] && finishing_prepared)
615600
{
616-
// Assert(!TransactionIdIsValid(xid));
617-
618-
SpinLockAcquire(&local->lock);
619-
{
620-
DtmTransId *id = (DtmTransId *) hash_search(gtid2xid, x->gtid, HASH_REMOVE, NULL);
621-
622-
Assert(id != NULL);
623-
Assert(TransactionIdIsValid(id->xid));
624-
625-
xid = id->xid;
626-
free(id->subxids);
627-
}
628-
SpinLockRelease(&local->lock);
601+
xid = x->xid;
629602
}
630603
else if (!TransactionIdIsValid(xid))
631604
{
@@ -716,20 +689,6 @@ DtmLocalSavePreparedState(DtmCurrentTrans * x)
716689
int nSubxids = xactGetCommittedChildren(&subxids);
717690

718691
SpinLockAcquire(&local->lock);
719-
{
720-
DtmTransId *id = (DtmTransId *) hash_search(gtid2xid, x->gtid, HASH_FIND, NULL);
721-
722-
if (id != NULL)
723-
{
724-
id->xid = GetCurrentTransactionId();
725-
726-
}
727-
}
728-
// SpinLockRelease(&local->lock);
729-
730-
731-
732-
// SpinLockAcquire(&local->lock);
733692
{
734693
DtmTransStatus *ts;
735694

src/backend/access/transam/twophase.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,31 @@ TwoPhaseGetDummyProc(TransactionId xid)
872872
return &ProcGlobal->allProcs[gxact->pgprocno];
873873
}
874874

875+
TransactionId
876+
TwoPhaseGetTransactionId(const char *gid)
877+
{
878+
int i;
879+
880+
LWLockAcquire(TwoPhaseStateLock, LW_SHARED);
881+
for (i = 0; i < TwoPhaseState->numPrepXacts; i++)
882+
{
883+
GlobalTransaction gxact = TwoPhaseState->prepXacts[i];
884+
885+
/* Ignore not-yet-valid GIDs */
886+
if (!gxact->valid)
887+
continue;
888+
889+
if (strcmp(gxact->gid, gid) == 0)
890+
{
891+
LWLockRelease(TwoPhaseStateLock);
892+
return gxact->xid;
893+
}
894+
}
895+
LWLockRelease(TwoPhaseStateLock);
896+
897+
return InvalidTransactionId;
898+
}
899+
875900
/************************************************************************/
876901
/* State file support */
877902
/************************************************************************/

src/include/access/global_snapshot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ typedef struct
1010
{
1111
cid_t snapshot;
1212
char gtid[MAX_GTID_SIZE];
13+
TransactionId xid;
1314
} DtmCurrentTrans;
1415

1516
typedef char const *GlobalTransactionId;

src/include/access/twophase.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ extern void PostPrepare_Twophase(void);
3535

3636
extern PGPROC *TwoPhaseGetDummyProc(TransactionId xid);
3737
extern BackendId TwoPhaseGetDummyBackendId(TransactionId xid);
38+
extern TransactionId TwoPhaseGetTransactionId(const char *gid);
3839

3940
extern GlobalTransaction MarkAsPreparing(TransactionId xid, const char *gid,
4041
TimestampTz prepared_at,

0 commit comments

Comments
 (0)