37
37
typedef struct
38
38
{
39
39
LWLockId hashLock ;
40
- LWLockId xidLock ;
40
+ LWLockId xidLock ;
41
41
TransactionId nextXid ;
42
42
size_t nReservedXids ;
43
43
} DtmState ;
@@ -173,6 +173,9 @@ static void DtmMergeSnapshots(Snapshot dst, Snapshot src)
173
173
}
174
174
}
175
175
dst -> xcnt = j ;
176
+ if (RecentXmin > dst -> xmin ) {
177
+ RecentXmin = dst -> xmin ;
178
+ }
176
179
DumpSnapshot (dst , "merged" );
177
180
}
178
181
@@ -187,40 +190,40 @@ static void DtmUpdateRecentXmin(void)
187
190
if (!TransactionIdIsNormal (xmin )) {
188
191
xmin = FirstNormalTransactionId ;
189
192
}
190
- if (RecentGlobalDataXmin > xmin ) {
193
+ if (TransactionIdFollows ( RecentGlobalDataXmin , xmin ) ) {
191
194
RecentGlobalDataXmin = xmin ;
192
195
}
193
- if (RecentGlobalXmin > xmin ) {
196
+ if (TransactionIdFollows ( RecentGlobalXmin , xmin ) ) {
194
197
RecentGlobalXmin = xmin ;
195
198
}
196
- if (RecentXmin > xmin ) {
197
- RecentXmin = xmin ;
198
- }
199
199
}
200
200
}
201
201
202
202
static TransactionId DtmGetNextXid ()
203
203
{
204
204
TransactionId xid ;
205
+ LWLockAcquire (dtm -> xidLock , LW_EXCLUSIVE );
205
206
if (TransactionIdIsValid (DtmNextXid )) {
206
207
XTM_INFO ("Use global XID %d\n" , DtmNextXid );
207
208
xid = DtmNextXid ;
208
- dtm -> nReservedXids = 0 ;
209
- ShmemVariableCache -> nextXid = xid ;
209
+ if (ShmemVariableCache -> nextXid <= xid ) {
210
+ dtm -> nReservedXids = 0 ;
211
+ ShmemVariableCache -> nextXid = xid ;
212
+ }
210
213
} else {
211
- LWLockAcquire (dtm -> xidLock , LW_EXCLUSIVE );
212
214
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 ;
215
219
} else {
216
- Assert (dtm -> nextXid == ShmemVariableCache -> nextXid );
217
- xid = ShmemVariableCache -> nextXid ;
220
+ Assert (ShmemVariableCache -> nextXid == dtm -> nextXid );
218
221
}
219
- XTM_INFO ("Obtain new local XID %d\n" , xid );
220
- dtm -> nextXid += 1 ;
222
+ xid = dtm -> nextXid ++ ;
221
223
dtm -> nReservedXids -= 1 ;
222
- LWLockRelease ( dtm -> xidLock );
224
+ XTM_INFO ( "Obtain new local XID %d\n" , xid );
223
225
}
226
+ LWLockRelease (dtm -> xidLock );
224
227
return xid ;
225
228
}
226
229
@@ -232,13 +235,13 @@ static Snapshot DtmGetSnapshot(Snapshot snapshot)
232
235
DtmGlobalGetSnapshot (DtmNextXid , & DtmSnapshot );
233
236
}
234
237
DtmMergeSnapshots (snapshot , & DtmSnapshot );
235
- DtmUpdateRecentXmin ();
236
238
if (!IsolationUsesXactSnapshot ()) {
237
239
DtmHasGlobalSnapshot = false;
238
240
}
239
241
} else {
240
242
snapshot = GetLocalSnapshotData (snapshot );
241
243
}
244
+ DtmUpdateRecentXmin ();
242
245
CurrentTransactionSnapshot = snapshot ;
243
246
return snapshot ;
244
247
}
0 commit comments