Skip to content

Commit c25adce

Browse files
committed
Merge branch 'xtm' of gitlab.postgrespro.ru:pgpro-dev/postgrespro into xtm
2 parents 7348623 + 536d556 commit c25adce

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
@@ -161,6 +161,30 @@ bool DtmGlobalStartTransaction(DTMConn dtm, GlobalTransactionId *gtid) {
161161
return ok;
162162
}
163163

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

192-
if (s->xip) free(s->xip);
193-
s->xip = malloc(s->xcnt * sizeof(TransactionId));
216+
DtmInitSnapshot(s);
194217
for (i = 0; i < s->xcnt; i++) {
195218
if (!dtm_read_hex16(dtm, &number)) return false;
196219
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)