Skip to content

Commit 29cc931

Browse files
committed
Support unique sequences through XTM
1 parent 02f6b20 commit 29cc931

File tree

6 files changed

+46
-8
lines changed

6 files changed

+46
-8
lines changed

contrib/mmts/multimaster.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ static char const* MtmGetName(void);
134134
static size_t MtmGetTransactionStateSize(void);
135135
static void MtmSerializeTransactionState(void* ctx);
136136
static void MtmDeserializeTransactionState(void* ctx);
137+
static void MtmInitializeSequence(int64* start, int64* step);
137138

138139
static void MtmCheckClusterLock(void);
139140
static void MtmCheckSlots(void);
@@ -171,7 +172,8 @@ static TransactionManager MtmTM = {
171172
MtmGetName,
172173
MtmGetTransactionStateSize,
173174
MtmSerializeTransactionState,
174-
MtmDeserializeTransactionState
175+
MtmDeserializeTransactionState,
176+
MtmInitializeSequence
175177
};
176178

177179
char const* const MtmNodeStatusMnem[] =
@@ -349,6 +351,13 @@ MtmDeserializeTransactionState(void* ctx)
349351
}
350352

351353

354+
static void
355+
MtmInitializeSequence(int64* start, int64* step)
356+
{
357+
*start = MtmNodeId;
358+
*step = MtmMaxNodes;
359+
}
360+
352361

353362
/*
354363
* -------------------------------------------

contrib/pg_dtm/pg_dtm.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ static TransactionManager DtmTM = {
122122
DtmGetName,
123123
PgGetTransactionStateSize,
124124
PgSerializeTransactionState,
125-
PgDeserializeTransactionState
125+
PgDeserializeTransactionState,
126+
PgInitializeSequence
126127
};
127128

128129
static char *Arbiters;
@@ -146,6 +147,13 @@ static char const* DtmGetName(void)
146147
return "pg_dtm";
147148
}
148149

150+
static void
151+
DtmInitializeSequence(int64* init, int64* step)
152+
{
153+
*init = MtmNodeId;
154+
*step = MtmMaxNodes;
155+
}
156+
149157

150158
static void DumpSnapshot(Snapshot s, char *name)
151159
{

contrib/pg_tsdtm/pg_tsdtm.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ static TransactionManager DtmTM = {
120120
DtmGetName,
121121
DtmGetTransactionStateSize,
122122
DtmSerializeTransactionState,
123-
DtmDeserializeTransactionState
123+
DtmDeserializeTransactionState,
124+
PgInitializeSequence
124125
};
125126

126127
void _PG_init(void);

src/backend/access/transam/xtm.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ PgDeserializeTransactionState(void* ctx)
5252
{
5353
}
5454

55+
void PgInitializeSequence(int64* init, int64* step)
56+
{
57+
*init = 1;
58+
*step = 1;
59+
}
60+
5561

5662
TransactionManager PgTM = {
5763
PgTransactionIdGetStatus,
@@ -66,7 +72,8 @@ TransactionManager PgTM = {
6672
PgGetTransactionManagerName,
6773
PgGetTransactionStateSize,
6874
PgSerializeTransactionState,
69-
PgDeserializeTransactionState
75+
PgDeserializeTransactionState,
76+
PgInitializeSequence
7077
};
7178

7279
TransactionManager *TM = &PgTM;

src/backend/commands/sequence.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "access/multixact.h"
1919
#include "access/transam.h"
2020
#include "access/xact.h"
21+
#include "access/xtm.h"
2122
#include "access/xlog.h"
2223
#include "access/xloginsert.h"
2324
#include "access/xlogutils.h"
@@ -1329,10 +1330,15 @@ init_params(List *options, bool isInit,
13291330
new->start_value = defGetInt64(start_value);
13301331
else if (isInit)
13311332
{
1332-
if (new->increment_by > 0)
1333-
new->start_value = new->min_value; /* ascending seq */
1334-
else
1335-
new->start_value = new->max_value; /* descending seq */
1333+
if (increment_by == NULL) {
1334+
/* if neither start, neither increment are not specified explcitly, assign this values using XTM API */
1335+
TM->InitializeSequence(&new->start_value, &new->increment_by);
1336+
} else {
1337+
if (new->increment_by > 0)
1338+
new->start_value = new->min_value; /* ascending seq */
1339+
else
1340+
new->start_value = new->max_value; /* descending seq */
1341+
}
13361342
}
13371343

13381344
/* crosscheck START */

src/include/access/xtm.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ typedef struct
8888
*/
8989
void (*DeserializeTransactionState)(void* ctx);
9090

91+
/*
92+
* Assign initial value and step to new sequence if them are not specified explicitly.
93+
* This function allows to generate unique (but not monotonic) values in distributed cluster
94+
*/
95+
void (*InitializeSequence)(int64* start, int64* step);
96+
9197
} TransactionManager;
9298

9399
/* Get pointer to transaction manager: actually returns content of TM variable */
@@ -121,6 +127,7 @@ extern char const *PgGetTransactionManagerName(void);
121127
extern size_t PgGetTransactionStateSize(void);
122128
extern void PgSerializeTransactionState(void* ctx);
123129
extern void PgDeserializeTransactionState(void* ctx);
130+
extern void PgInitializeSequence(int64* start, int64* step);
124131

125132

126133
#endif

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy