Skip to content

Commit d7c5088

Browse files
committed
Fix asignement of next Xid
1 parent 9b44cc6 commit d7c5088

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

contrib/pg_xtm/pg_dtm.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
typedef struct
3838
{
3939
LWLockId hashLock;
40-
LWLockId xidLock;
40+
LWLockId xidLock;
4141
TransactionId nextXid;
4242
size_t nReservedXids;
4343
} DtmState;
@@ -173,6 +173,9 @@ static void DtmMergeSnapshots(Snapshot dst, Snapshot src)
173173
}
174174
}
175175
dst->xcnt = j;
176+
if (RecentXmin > dst->xmin) {
177+
RecentXmin = dst->xmin;
178+
}
176179
DumpSnapshot(dst, "merged");
177180
}
178181

@@ -187,40 +190,40 @@ static void DtmUpdateRecentXmin(void)
187190
if (!TransactionIdIsNormal(xmin)) {
188191
xmin = FirstNormalTransactionId;
189192
}
190-
if (RecentGlobalDataXmin > xmin) {
193+
if (TransactionIdFollows(RecentGlobalDataXmin, xmin)) {
191194
RecentGlobalDataXmin = xmin;
192195
}
193-
if (RecentGlobalXmin > xmin) {
196+
if (TransactionIdFollows(RecentGlobalXmin, xmin)) {
194197
RecentGlobalXmin = xmin;
195198
}
196-
if (RecentXmin > xmin) {
197-
RecentXmin = xmin;
198-
}
199199
}
200200
}
201201

202202
static TransactionId DtmGetNextXid()
203203
{
204204
TransactionId xid;
205+
LWLockAcquire(dtm->xidLock, LW_EXCLUSIVE);
205206
if (TransactionIdIsValid(DtmNextXid)) {
206207
XTM_INFO("Use global XID %d\n", DtmNextXid);
207208
xid = DtmNextXid;
208-
dtm->nReservedXids = 0;
209-
ShmemVariableCache->nextXid = xid;
209+
if (ShmemVariableCache->nextXid <= xid) {
210+
dtm->nReservedXids = 0;
211+
ShmemVariableCache->nextXid = xid;
212+
}
210213
} else {
211-
LWLockAcquire(dtm->xidLock, LW_EXCLUSIVE);
212214
if (dtm->nReservedXids == 0) {
213-
dtm->nReservedXids = DtmGlobalReserve(ShmemVariableCache->nextXid, DtmLocalXidReserve, &xid);
214-
ShmemVariableCache->nextXid = dtm->nextXid = xid;
215+
dtm->nReservedXids = DtmGlobalReserve(ShmemVariableCache->nextXid, DtmLocalXidReserve, &dtm->nextXid);
216+
Assert(dtm->nReservedXids > 0);
217+
Assert(TransactionIdFollowsOrEquals(dtm->nextXid, ShmemVariableCache->nextXid));
218+
ShmemVariableCache->nextXid = dtm->nextXid;
215219
} else {
216-
Assert(dtm->nextXid == ShmemVariableCache->nextXid);
217-
xid = ShmemVariableCache->nextXid;
220+
Assert(ShmemVariableCache->nextXid == dtm->nextXid);
218221
}
219-
XTM_INFO("Obtain new local XID %d\n", xid);
220-
dtm->nextXid += 1;
222+
xid = dtm->nextXid++;
221223
dtm->nReservedXids -= 1;
222-
LWLockRelease(dtm->xidLock);
224+
XTM_INFO("Obtain new local XID %d\n", xid);
223225
}
226+
LWLockRelease(dtm->xidLock);
224227
return xid;
225228
}
226229

@@ -232,13 +235,13 @@ static Snapshot DtmGetSnapshot(Snapshot snapshot)
232235
DtmGlobalGetSnapshot(DtmNextXid, &DtmSnapshot);
233236
}
234237
DtmMergeSnapshots(snapshot, &DtmSnapshot);
235-
DtmUpdateRecentXmin();
236238
if (!IsolationUsesXactSnapshot()) {
237239
DtmHasGlobalSnapshot = false;
238240
}
239241
} else {
240242
snapshot = GetLocalSnapshotData(snapshot);
241243
}
244+
DtmUpdateRecentXmin();
242245
CurrentTransactionSnapshot = snapshot;
243246
return snapshot;
244247
}

contrib/pg_xtm/tests/transfers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
const (
11-
TRANSFER_CONNECTIONS = 2
11+
TRANSFER_CONNECTIONS = 8
1212
INIT_AMOUNT = 10000
1313
N_ITERATIONS = 10000
1414
N_ACCOUNTS = TRANSFER_CONNECTIONS//100000

src/backend/access/transam/varsup.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ GetNewTransactionId(bool isSubXact)
161161

162162
/* Re-acquire lock and start over */
163163
LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
164-
xid = ShmemVariableCache->nextXid;
164+
xid = TM->GetNextXid();
165165
}
166166

167167
/*
@@ -183,7 +183,11 @@ GetNewTransactionId(bool isSubXact)
183183
* want the next incoming transaction to try it again. We cannot assign
184184
* more XIDs until there is CLOG space for them.
185185
*/
186-
TransactionIdAdvance(ShmemVariableCache->nextXid);
186+
if (xid == ShmemVariableCache->nextXid) {
187+
TransactionIdAdvance(ShmemVariableCache->nextXid);
188+
} else {
189+
Assert(TransactionIdPrecedes(xid, ShmemVariableCache->nextXid));
190+
}
187191

188192
/*
189193
* We must store the new XID into the shared ProcArray before releasing

0 commit comments

Comments
 (0)