Skip to content

Commit 39d316b

Browse files
committed
Add a developer-GUC 'gloabl_snapshot_source' to allow informed users to
override the way snapshots are computed. The default value of the GUC is 'gtm' which means that snapshots are always generated on the GTM so that we get a full and correct view of the currently running transactions. But with this developer-GUC we now allow users to override that and work with a coordinator generated snapshots. This can be especially useful for read-only queries which now don't need to talk to the GTM. If the snapshots can also be taken locally on a coordinator, this will even further reduce the round-trips to the GTM. Of course, this can lead to consistency issues because a coordinator may not be aware of all the transactions currently running on the XL cluster, especially in a multi-coordinator setup where different coordinators could be running different transactions without knowing about each other's activity. But even in a single coordinator setup, some transactions may start on a datanode and coordinator may not know about them or may only know quite late. Its advised that this feature must be used with caution and after due consideration of the effects
1 parent b8a7355 commit 39d316b

File tree

3 files changed

+71
-20
lines changed

3 files changed

+71
-20
lines changed

src/backend/storage/ipc/procarray.c

+34-19
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
#include "pgxc/pgxc.h"
8080
#include "access/gtm.h"
8181
#include "storage/ipc.h"
82+
#include "utils/guc.h"
8283
/* PGXC_DATANODE */
8384
#include "postmaster/autovacuum.h"
8485
#endif
@@ -219,6 +220,10 @@ static TransactionId KnownAssignedXidsGetOldestXmin(void);
219220
static void KnownAssignedXidsDisplay(int trace_level);
220221
static void KnownAssignedXidsReset(void);
221222

223+
#ifdef XCP
224+
int GlobalSnapshotSource;
225+
#endif
226+
222227
/*
223228
* Report shared-memory space needed by CreateSharedProcArray.
224229
*/
@@ -1508,22 +1513,35 @@ GetSnapshotData(Snapshot snapshot)
15081513

15091514
#ifdef PGXC /* PGXC_DATANODE */
15101515
/*
1511-
* Obtain a global snapshot for a Postgres-XC session
1512-
* if possible.
1513-
*/
1514-
if (GetPGXCSnapshotData(snapshot))
1515-
return snapshot;
1516-
/*
1517-
* We only make one exception for using local snapshot and that's the
1518-
* initdb time. When IsPostmasterEnvironment is true, snapshots must either
1519-
* be pushed down from the coordinator or directly obtained from the
1520-
* GTM.
1521-
*
1522-
* !!TODO We don't seem to fully support Hot Standby. So why should we even
1523-
* exempt RecoveryInProgress()?
1524-
*/
1525-
if (IsPostmasterEnvironment && !useLocalXid)
1526-
elog(ERROR, "Was unable to obtain a snapshot from GTM.");
1516+
* If the user has chosen to work with a coordinator-local snapshot, just
1517+
* compute snapshot locally. This can have adverse effects on the global
1518+
* consistency, in a multi-coordinator environment, but also in a
1519+
* single-coordinator setup because our recent changes to transaction
1520+
* management now allows datanodes to start global snapshots or more
1521+
* precisely attach current transaction to a global transaction. But users
1522+
* may still want to use this model for performance of their XL cluster, at
1523+
* the cost of reduced global consistency
1524+
*/
1525+
if (GlobalSnapshotSource == GLOBAL_SNAPSHOT_SOURCE_GTM)
1526+
{
1527+
/*
1528+
* Obtain a global snapshot for a Postgres-XC session
1529+
* if possible.
1530+
*/
1531+
if (GetPGXCSnapshotData(snapshot))
1532+
return snapshot;
1533+
/*
1534+
* We only make one exception for using local snapshot and that's the
1535+
* initdb time. When IsPostmasterEnvironment is true, snapshots must
1536+
* either be pushed down from the coordinator or directly obtained from
1537+
* the GTM.
1538+
*
1539+
* !!TODO We don't seem to fully support Hot Standby. So why should we
1540+
* even exempt RecoveryInProgress()?
1541+
*/
1542+
if (IsPostmasterEnvironment && !useLocalXid)
1543+
elog(ERROR, "Was unable to obtain a snapshot from GTM.");
1544+
}
15271545
#endif
15281546

15291547
/*
@@ -3254,8 +3272,6 @@ GetSnapshotFromGlobalSnapshot(Snapshot snapshot)
32543272
globalSnapshot.snapshot_source == SNAPSHOT_DIRECT)
32553273
&& TransactionIdIsValid(globalSnapshot.gxmin))
32563274
{
3257-
int index;
3258-
ProcArrayStruct *arrayP = procArray;
32593275
TransactionId global_xmin;
32603276

32613277
snapshot->xmin = globalSnapshot.gxmin;
@@ -4360,7 +4376,6 @@ ProcArrayCheckXminConsistency(TransactionId global_xmin)
43604376
for (index = 0; index < arrayP->numProcs; index++)
43614377
{
43624378
int pgprocno = arrayP->pgprocnos[index];
4363-
volatile PGPROC *proc = &allProcs[pgprocno];
43644379
volatile PGXACT *pgxact = &allPgXact[pgprocno];
43654380
TransactionId xid;
43664381

src/backend/utils/misc/guc.c

+28-1
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,14 @@
7171
#include "pgxc/poolmgr.h"
7272
#include "pgxc/nodemgr.h"
7373
#include "pgxc/xc_maintenance_mode.h"
74+
#include "storage/procarray.h"
7475
#endif
7576
#ifdef XCP
7677
#include "commands/sequence.h"
78+
#include "parser/parse_utilcmd.h"
7779
#include "pgxc/nodemgr.h"
7880
#include "pgxc/squeue.h"
7981
#include "utils/snapmgr.h"
80-
#include "parser/parse_utilcmd.h"
8182
#endif
8283
#include "postmaster/autovacuum.h"
8384
#include "postmaster/bgworker.h"
@@ -445,6 +446,18 @@ static const struct config_enum_entry row_security_options[] = {
445446
{NULL, 0, false}
446447
};
447448

449+
#ifdef XCP
450+
/*
451+
* Set global-snapshot source. 'gtm' is default, but user can choose
452+
* 'coordinator' for performance improvement at the cost of reduced consistency
453+
*/
454+
static const struct config_enum_entry global_snapshot_source_options[] = {
455+
{"gtm", GLOBAL_SNAPSHOT_SOURCE_GTM, true},
456+
{"coordinator", GLOBAL_SNAPSHOT_SOURCE_COORDINATOR, true},
457+
{NULL, 0, false}
458+
};
459+
#endif
460+
448461
/*
449462
* Options for enum values stored in other modules
450463
*/
@@ -4051,6 +4064,20 @@ static struct config_enum ConfigureNamesEnum[] =
40514064
NULL, NULL, NULL
40524065
},
40534066

4067+
#ifdef XCP
4068+
{
4069+
{"global_snapshot_source", PGC_USERSET, DEVELOPER_OPTIONS,
4070+
gettext_noop("Set preferred source of a snapshot."),
4071+
gettext_noop("When set to 'coordinator', a snapshot is taken at "
4072+
"the coordinator at the risk of reduced consistency. "
4073+
"Default is 'gtm'")
4074+
},
4075+
&GlobalSnapshotSource,
4076+
GLOBAL_SNAPSHOT_SOURCE_GTM, global_snapshot_source_options,
4077+
NULL, NULL, NULL
4078+
},
4079+
#endif
4080+
40544081
/* End-of-list marker */
40554082
{
40564083
{NULL, 0, 0, NULL, NULL}, NULL, 0, NULL, NULL, NULL, NULL

src/include/storage/procarray.h

+9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@
2424
#include "utils/relcache.h"
2525
#include "utils/snapshot.h"
2626

27+
#ifdef XCP
28+
extern int GlobalSnapshotSource;
29+
30+
typedef enum GlobalSnapshotSourceType
31+
{
32+
GLOBAL_SNAPSHOT_SOURCE_GTM,
33+
GLOBAL_SNAPSHOT_SOURCE_COORDINATOR
34+
} GlobalSnapshotSourceType;
35+
#endif
2736

2837
extern Size ProcArrayShmemSize(void);
2938
extern void CreateSharedProcArray(void);

0 commit comments

Comments
 (0)