@@ -51,9 +51,8 @@ typedef struct
51
51
void _PG_init (void );
52
52
void _PG_fini (void );
53
53
54
- static Snapshot DtmGetSnapshot (void );
54
+ static Snapshot DtmGetSnapshot (Snapshot snapshot );
55
55
static void DtmMergeSnapshots (Snapshot dst , Snapshot src );
56
- static Snapshot DtmCopySnapshot (Snapshot snapshot );
57
56
static XidStatus DtmGetTransactionStatus (TransactionId xid , XLogRecPtr * lsn );
58
57
static void DtmSetTransactionStatus (TransactionId xid , int nsubxids , TransactionId * subxids , XidStatus status , XLogRecPtr lsn );
59
58
static void DtmUpdateRecentXmin (void );
@@ -73,11 +72,10 @@ static Snapshot CurrentTransactionSnapshot;
73
72
74
73
static TransactionId DtmNextXid ;
75
74
static SnapshotData DtmSnapshot = { HeapTupleSatisfiesMVCC };
76
- static SnapshotData DtmLocalSnapshot = { HeapTupleSatisfiesMVCC };
77
75
static bool DtmHasGlobalSnapshot ;
78
76
static bool DtmIsGlobalTransaction ;
79
77
static int DtmLocalXidReserve ;
80
- static TransactionManager DtmTM = { DtmGetTransactionStatus , DtmSetTransactionStatus , DtmGetSnapshot , DtmCopySnapshot , DtmGetNextXid };
78
+ static TransactionManager DtmTM = { DtmGetTransactionStatus , DtmSetTransactionStatus , DtmGetSnapshot , DtmGetNextXid };
81
79
82
80
83
81
#define XTM_TRACE (fmt , ...)
@@ -136,43 +134,42 @@ static void DtmMergeSnapshots(Snapshot dst, Snapshot src)
136
134
{
137
135
int i , j , n ;
138
136
TransactionId xid ;
139
- Snapshot local ;
140
137
141
138
Assert (TransactionIdIsValid (src -> xmin ) && TransactionIdIsValid (src -> xmax ));
142
139
143
140
GetLocalSnapshot :
144
- local = GetSnapshotData ( & DtmLocalSnapshot );
145
- for (i = 0 ; i < local -> xcnt ; i ++ ) {
146
- if (TransactionIdIsInDoubt (local -> xip [i ])) {
141
+ dst = GetLocalSnapshotData ( dst );
142
+ for (i = 0 ; i < dst -> xcnt ; i ++ ) {
143
+ if (TransactionIdIsInDoubt (dst -> xip [i ])) {
147
144
goto GetLocalSnapshot ;
148
145
}
149
146
}
150
- for (xid = local -> xmax ; xid < src -> xmax ; xid ++ ) {
147
+ for (xid = dst -> xmax ; xid < src -> xmax ; xid ++ ) {
151
148
if (TransactionIdIsInDoubt (xid )) {
152
149
goto GetLocalSnapshot ;
153
150
}
154
151
}
155
- DumpSnapshot (local , "local" );
152
+ DumpSnapshot (dst , "local" );
156
153
DumpSnapshot (src , "DTM" );
157
154
158
155
/* Merge two snapshots: produce most restrictive snapshots whihc includes running transactions from both of them */
159
- dst -> xmin = local -> xmin < src -> xmin ? local -> xmin : src -> xmin ;
160
- dst -> xmax = local -> xmax < src -> xmax ? local -> xmax : src -> xmax ;
156
+ if ( src -> xmin < dst -> xmin ) dst -> xmin = src -> xmin ;
157
+ if ( src -> xmax < dst -> xmax ) dst -> xmax = src -> xmax ;
161
158
162
- n = local -> xcnt ;
163
- for (xid = local -> xmax ; xid <= src -> xmin ; xid ++ ) {
164
- local -> xip [n ++ ] = xid ;
159
+ n = dst -> xcnt ;
160
+ for (xid = dst -> xmax ; xid <= src -> xmin ; xid ++ ) {
161
+ dst -> xip [n ++ ] = xid ;
165
162
}
166
- memcpy (local -> xip + n , src -> xip , src -> xcnt * sizeof (TransactionId ));
163
+ memcpy (dst -> xip + n , src -> xip , src -> xcnt * sizeof (TransactionId ));
167
164
n += src -> xcnt ;
168
165
Assert (n <= GetMaxSnapshotXidCount ());
169
166
170
- qsort (local -> xip , n , sizeof (TransactionId ), xidComparator );
167
+ qsort (dst -> xip , n , sizeof (TransactionId ), xidComparator );
171
168
xid = InvalidTransactionId ;
172
169
173
- for (i = 0 , j = 0 ; i < n && local -> xip [i ] < dst -> xmax ; i ++ ) {
174
- if (local -> xip [i ] != xid ) {
175
- dst -> xip [j ++ ] = xid = local -> xip [i ];
170
+ for (i = 0 , j = 0 ; i < n && dst -> xip [i ] < dst -> xmax ; i ++ ) {
171
+ if (dst -> xip [i ] != xid ) {
172
+ dst -> xip [j ++ ] = xid = dst -> xip [i ];
176
173
}
177
174
}
178
175
dst -> xcnt = j ;
@@ -202,54 +199,23 @@ static void DtmUpdateRecentXmin(void)
202
199
}
203
200
}
204
201
205
- static Snapshot DtmCopySnapshot (Snapshot snapshot )
206
- {
207
- Snapshot newsnap ;
208
- Size size = sizeof (SnapshotData ) + GetMaxSnapshotXidCount () * sizeof (TransactionId );
209
- Size subxipoff = size ;
210
- if (snapshot -> subxcnt > 0 ) {
211
- size += snapshot -> subxcnt * sizeof (TransactionId );
212
- }
213
- newsnap = (Snapshot ) MemoryContextAlloc (TopTransactionContext , size );
214
- memcpy (newsnap , snapshot , sizeof (SnapshotData ));
215
-
216
- newsnap -> regd_count = 0 ;
217
- newsnap -> active_count = 0 ;
218
- newsnap -> copied = true;
219
-
220
- newsnap -> xip = (TransactionId * ) (newsnap + 1 );
221
- if (snapshot -> xcnt > 0 )
222
- {
223
- memcpy (newsnap -> xip , snapshot -> xip , snapshot -> xcnt * sizeof (TransactionId ));
224
- }
225
- if (snapshot -> subxcnt > 0 &&
226
- (!snapshot -> suboverflowed || snapshot -> takenDuringRecovery ))
227
- {
228
- newsnap -> subxip = (TransactionId * ) ((char * ) newsnap + subxipoff );
229
- memcpy (newsnap -> subxip , snapshot -> subxip ,
230
- snapshot -> subxcnt * sizeof (TransactionId ));
231
- }
232
- else
233
- newsnap -> subxip = NULL ;
234
-
235
- return newsnap ;
236
- }
237
-
238
202
static TransactionId DtmGetNextXid ()
239
203
{
240
204
TransactionId xid ;
241
205
if (TransactionIdIsValid (DtmNextXid )) {
242
206
XTM_INFO ("Use global XID %d\n" , DtmNextXid );
243
207
xid = DtmNextXid ;
208
+ dtm -> nReservedXids = 0 ;
209
+ ShmemVariableCache -> nextXid = xid ;
244
210
} else {
245
211
LWLockAcquire (dtm -> xidLock , LW_EXCLUSIVE );
246
212
if (dtm -> nReservedXids == 0 ) {
247
213
dtm -> nReservedXids = DtmGlobalReserve (ShmemVariableCache -> nextXid , DtmLocalXidReserve , & xid );
248
- ShmemVariableCache -> nextXid = xid ;
249
- dtm -> nextXid = xid ;
250
- }
251
- Assert ( dtm -> nextXid == ShmemVariableCache -> nextXid ) ;
252
- xid = ShmemVariableCache -> nextXid ;
214
+ ShmemVariableCache -> nextXid = dtm -> nextXid = xid ;
215
+ } else {
216
+ Assert ( dtm -> nextXid == ShmemVariableCache -> nextXid );
217
+ xid = ShmemVariableCache -> nextXid ;
218
+ }
253
219
XTM_INFO ("Obtain new local XID %d\n" , xid );
254
220
dtm -> nextXid += 1 ;
255
221
dtm -> nReservedXids -= 1 ;
@@ -258,9 +224,9 @@ static TransactionId DtmGetNextXid()
258
224
return xid ;
259
225
}
260
226
261
- static Snapshot DtmGetSnapshot ()
227
+ static Snapshot DtmGetSnapshot (Snapshot snapshot )
262
228
{
263
- Snapshot snapshot = GetLocalTransactionSnapshot ();
229
+
264
230
if (TransactionIdIsValid (DtmNextXid )) {
265
231
if (!DtmHasGlobalSnapshot ) {
266
232
DtmGlobalGetSnapshot (DtmNextXid , & DtmSnapshot );
@@ -270,7 +236,9 @@ static Snapshot DtmGetSnapshot()
270
236
if (!IsolationUsesXactSnapshot ()) {
271
237
DtmHasGlobalSnapshot = false;
272
238
}
273
- }
239
+ } else {
240
+ snapshot = GetLocalSnapshotData (snapshot );
241
+ }
274
242
CurrentTransactionSnapshot = snapshot ;
275
243
return snapshot ;
276
244
}
@@ -354,7 +322,6 @@ static void DtmInitialize()
354
322
);
355
323
356
324
RegisterXactCallback (DtmXactCallback , NULL );
357
- DtmInitSnapshot (& DtmLocalSnapshot );
358
325
359
326
TM = & DtmTM ;
360
327
}
0 commit comments