Skip to content

Commit 536d556

Browse files
committed
Fix obtaning snashot though DTMD
1 parent aad5eae commit 536d556

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

contrib/pg_xtm/libdtm.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,30 @@ bool DtmGlobalStartTransaction(DTMConn dtm, GlobalTransactionId *gtid) {
163163
return ok;
164164
}
165165

166+
void DtmInitSnapshot(Snapshot snapshot)
167+
{
168+
if (snapshot->xip == NULL)
169+
{
170+
/*
171+
* First call for this snapshot. Snapshot is same size whether or not
172+
* we are in recovery, see later comments.
173+
*/
174+
snapshot->xip = (TransactionId *)
175+
malloc(GetMaxSnapshotXidCount() * sizeof(TransactionId));
176+
if (snapshot->xip == NULL)
177+
ereport(ERROR,
178+
(errcode(ERRCODE_OUT_OF_MEMORY),
179+
errmsg("out of memory")));
180+
Assert(snapshot->subxip == NULL);
181+
snapshot->subxip = (TransactionId *)
182+
malloc(GetMaxSnapshotSubxidCount() * sizeof(TransactionId));
183+
if (snapshot->subxip == NULL)
184+
ereport(ERROR,
185+
(errcode(ERRCODE_OUT_OF_MEMORY),
186+
errmsg("out of memory")));
187+
}
188+
}
189+
166190
// Asks DTM for a fresh snapshot. Returns 'true' on success, or 'false'
167191
// otherwise.
168192
bool DtmGlobalGetSnapshot(DTMConn dtm, NodeId nodeid, TransactionId xid, Snapshot s) {
@@ -191,8 +215,7 @@ bool DtmGlobalGetSnapshot(DTMConn dtm, NodeId nodeid, TransactionId xid, Snapsho
191215
s->xcnt = number;
192216
Assert(s->xcnt == number); // the number should definitely fit into xcnt field size
193217

194-
if (s->xip) free(s->xip);
195-
s->xip = malloc(s->xcnt * sizeof(TransactionId));
218+
DtmInitSnapshot(s);
196219
for (i = 0; i < s->xcnt; i++) {
197220
if (!dtm_read_hex16(dtm, &number)) return false;
198221
s->xip[i] = number;

contrib/pg_xtm/libdtm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "postgres.h"
55
#include "utils/snapmgr.h"
6+
#include "storage/procarray.h"
67
#include "access/clog.h"
78

89
#define INVALID_XID 0
@@ -19,6 +20,8 @@ DTMConn DtmConnect(char *host, int port);
1920
// bad things will happen.
2021
void DtmDisconnect(DTMConn dtm);
2122

23+
void DtmInitSnapshot(Snapshot snapshot);
24+
2225
typedef struct {
2326
TransactionId* xids;
2427
NodeId* nodes;

contrib/pg_xtm/pg_dtm.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void _PG_fini(void);
3737

3838
static void DtmEnsureConnection(void);
3939
static Snapshot DtmGetSnapshot(Snapshot snapshot);
40+
static void DtmCopySnapshot(Snapshot dst, Snapshot src);
4041
static XidStatus DtmGetTransactionStatus(TransactionId xid, XLogRecPtr *lsn);
4142
static void DtmSetTransactionStatus(TransactionId xid, int nsubxids, TransactionId *subxids, XidStatus status, XLogRecPtr lsn);
4243
static bool DtmTransactionIsRunning(TransactionId xid);
@@ -58,12 +59,21 @@ static void DtmEnsureConnection(void)
5859
}
5960
}
6061

61-
extern SnapshotData CatalogSnapshotData;
62+
static void DtmCopySnapshot(Snapshot dst, Snapshot src)
63+
{
64+
DtmInitSnapshot(dst);
65+
memcpy(dst->xip, src->xip, src->xcnt*sizeof(TransactionId));
66+
dst->xmax = src->xmax;
67+
dst->xmin = src->xmin;
68+
dst->xcnt = src->xcnt;
69+
dst->curcid = src->curcid;
70+
}
6271

6372
static Snapshot DtmGetSnapshot(Snapshot snapshot)
6473
{
65-
if (DtmHasSnapshot && snapshot->satisfies == HeapTupleSatisfiesMVCC) {
66-
return &DtmSnapshot;
74+
if (DtmHasSnapshot) {
75+
CopyDtmSnapshot(snapshot, &DtmSnapshot);
76+
return snapshot;
6777
}
6878
return GetLocalSnapshotData(snapshot);
6979
}

0 commit comments

Comments
 (0)