Skip to content

Commit 6f6898b

Browse files
danolivoreshke
authored andcommitted
Add Commit Sequence Number (CSN) machinery into MVCC implementation for a timestamp-based resolving of visibility conflicts.
It allows to achieve proper snapshot isolation semantics in the case of distributed transactions involving more than one Postgres instance. Authors: K.Knizhnik, S.Kelvich, A.Sher, A.Lepikhov, M.Usama. Discussion: (2020/05/21 -) https://www.postgresql.org/message-id/flat/CA%2Bfd4k6HE8xLGEvqWzABEg8kkju5MxU%2Bif7bf-md0_2pjzXp9Q%40mail.gmail.com#ed1359340871688bed2e643921f73365 (2018/05/01 - 2019/04/21) https://www.postgresql.org/message-id/flat/21BC916B-80A1-43BF-8650-3363CCDAE09C%40postgrespro.ru
1 parent 5cf4f31 commit 6f6898b

File tree

52 files changed

+2651
-11
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2651
-11
lines changed

doc/src/sgml/config.sgml

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10295,8 +10295,56 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir'
1029510295
</varlistentry>
1029610296

1029710297
</variablelist>
10298-
</sect1>
1029910298

10299+
<sect2 id="runtime-config-CSN-based-snapshot">
10300+
<title>CSN Based Snapshot</title>
10301+
<para>
10302+
By default, snapshots in <productname>PostgreSQL</productname> contains a
10303+
XID (TransactionID) that allows to identify the status of a transaction
10304+
and make arbitrary visibility calculations.
10305+
</para>
10306+
10307+
<para>
10308+
<productname>PostgreSQL</productname> also provides a CSN (Commit
10309+
Sequence Number) based machinery as an additional tool for visibility
10310+
calculations. It may be used within distributed transactions when a xid of
10311+
a local transaction can't correctly identify order of the distributed one.
10312+
</para>
10313+
10314+
<variablelist>
10315+
<varlistentry id="guc-enable-csn-snapshot" xreflabel="enable_csn_snapshot">
10316+
<term><varname>enable_csn_snapshot</varname> (<type>boolean</type>)
10317+
<indexterm>
10318+
<primary><varname>enable_csn_snapshot</varname> configuration parameter</primary>
10319+
</indexterm>
10320+
</term>
10321+
<listitem>
10322+
10323+
<para>
10324+
Enable/disable the CSN tracking for the snapshot.
10325+
</para>
10326+
10327+
<para>
10328+
<productname>PostgreSQL</productname> uses a physical clock timestamp as
10329+
a CSN, so enabling the CSN based snapshots can be useful for implementing
10330+
cross-instance snapshots and visibility of distributed transaction.
10331+
</para>
10332+
10333+
<para>
10334+
when enabled <productname>PostgreSQL</productname> creates
10335+
<filename>pg_csn</filename> directory under <envar>PGDATA</envar> to keep
10336+
the track of CSN and XID mappings.
10337+
</para>
10338+
10339+
<para>
10340+
The default value is on.
10341+
</para>
10342+
</listitem>
10343+
</varlistentry>
10344+
10345+
</variablelist>
10346+
</sect2>
10347+
</sect1>
1030010348
<sect1 id="runtime-config-compatible">
1030110349
<title>Version and Platform Compatibility</title>
1030210350

src/backend/access/rmgrdesc/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ include $(top_builddir)/src/Makefile.global
1111
OBJS = \
1212
brindesc.o \
1313
clogdesc.o \
14+
csnlogdesc.o \
1415
committsdesc.o \
1516
dbasedesc.o \
1617
genericdesc.o \
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* clogdesc.c
4+
* rmgr descriptor routines for access/transam/csn_log.c
5+
*
6+
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7+
* Portions Copyright (c) 1994, Regents of the University of California
8+
*
9+
*
10+
* IDENTIFICATION
11+
* src/backend/access/rmgrdesc/csnlogdesc.c
12+
*
13+
*-------------------------------------------------------------------------
14+
*/
15+
#include "postgres.h"
16+
17+
#include "access/csn_log.h"
18+
19+
20+
void
21+
csnlog_desc(StringInfo buf, XLogReaderState *record)
22+
{
23+
char *rec = XLogRecGetData(record);
24+
uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
25+
26+
if (info == XLOG_CSN_ZEROPAGE)
27+
{
28+
int pageno;
29+
30+
memcpy(&pageno, XLogRecGetData(record), sizeof(int));
31+
appendStringInfo(buf, "pageno %d", pageno);
32+
}
33+
else if (info == XLOG_CSN_TRUNCATE)
34+
{
35+
int pageno;
36+
37+
memcpy(&pageno, XLogRecGetData(record), sizeof(int));
38+
appendStringInfo(buf, "pageno %d", pageno);
39+
}
40+
else if (info == XLOG_CSN_ASSIGNMENT)
41+
{
42+
CSN csn;
43+
44+
memcpy(&csn, XLogRecGetData(record), sizeof(CSN));
45+
appendStringInfo(buf, "assign "INT64_FORMAT"", csn);
46+
}
47+
else if (info == XLOG_CSN_SETCSN)
48+
{
49+
xl_csn_set *xlrec = (xl_csn_set *) rec;
50+
int nsubxids;
51+
52+
appendStringInfo(buf, "set "INT64_FORMAT" for: %u",
53+
xlrec->csn,
54+
xlrec->xtop);
55+
nsubxids = ((XLogRecGetDataLen(record) - MinSizeOfCSNSet) /
56+
sizeof(TransactionId));
57+
if (nsubxids > 0)
58+
{
59+
int i;
60+
TransactionId *subxids;
61+
62+
subxids = palloc(sizeof(TransactionId) * nsubxids);
63+
memcpy(subxids,
64+
XLogRecGetData(record) + MinSizeOfCSNSet,
65+
sizeof(TransactionId) * nsubxids);
66+
for (i = 0; i < nsubxids; i++)
67+
appendStringInfo(buf, ", %u", subxids[i]);
68+
pfree(subxids);
69+
}
70+
}
71+
}
72+
73+
const char *
74+
csnlog_identify(uint8 info)
75+
{
76+
const char *id = NULL;
77+
78+
switch (info & ~XLR_INFO_MASK)
79+
{
80+
case XLOG_CSN_ASSIGNMENT:
81+
id = "ASSIGNMENT";
82+
break;
83+
case XLOG_CSN_SETCSN:
84+
id = "SETCSN";
85+
break;
86+
case XLOG_CSN_ZEROPAGE:
87+
id = "ZEROPAGE";
88+
break;
89+
case XLOG_CSN_TRUNCATE:
90+
id = "TRUNCATE";
91+
break;
92+
}
93+
94+
return id;
95+
}

src/backend/access/rmgrdesc/xlogdesc.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,17 @@ xlog_desc(StringInfo buf, XLogReaderState *record)
113113
appendStringInfo(buf, "max_connections=%d max_worker_processes=%d "
114114
"max_wal_senders=%d max_prepared_xacts=%d "
115115
"max_locks_per_xact=%d wal_level=%s "
116-
"wal_log_hints=%s track_commit_timestamp=%s",
116+
"wal_log_hints=%s track_commit_timestamp=%s "
117+
"enable_csn_snapshot=%s",
117118
xlrec.MaxConnections,
118119
xlrec.max_worker_processes,
119120
xlrec.max_wal_senders,
120121
xlrec.max_prepared_xacts,
121122
xlrec.max_locks_per_xact,
122123
wal_level_str,
123124
xlrec.wal_log_hints ? "on" : "off",
124-
xlrec.track_commit_timestamp ? "on" : "off");
125+
xlrec.track_commit_timestamp ? "on" : "off",
126+
xlrec.enable_csn_snapshot ? "on" : "off");
125127
}
126128
else if (info == XLOG_FPW_CHANGE)
127129
{

src/backend/access/transam/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ include $(top_builddir)/src/Makefile.global
1515
OBJS = \
1616
clog.o \
1717
commit_ts.o \
18+
csn_log.o \
19+
csn_snapshot.o \
1820
generic_xlog.o \
1921
multixact.o \
2022
parallel.o \

0 commit comments

Comments
 (0)