Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3901fd7

Browse files
committed
Support quorum-based synchronous replication.
This feature is also known as "quorum commit" especially in discussion on pgsql-hackers. This commit adds the following new syntaxes into synchronous_standby_names GUC. By using FIRST and ANY keywords, users can specify the method to choose synchronous standbys from the listed servers. FIRST num_sync (standby_name [, ...]) ANY num_sync (standby_name [, ...]) The keyword FIRST specifies a priority-based synchronous replication which was available also in 9.6 or before. This method makes transaction commits wait until their WAL records are replicated to num_sync synchronous standbys chosen based on their priorities. The keyword ANY specifies a quorum-based synchronous replication and makes transaction commits wait until their WAL records are replicated to *at least* num_sync listed standbys. In this method, the values of sync_state.pg_stat_replication for the listed standbys are reported as "quorum". The priority is still assigned to each standby, but not used in this method. The existing syntaxes having neither FIRST nor ANY keyword are still supported. They are the same as new syntax with FIRST keyword, i.e., a priorirty-based synchronous replication. Author: Masahiko Sawada Reviewed-By: Michael Paquier, Amit Kapila and me Discussion: <CAD21AoAACi9NeC_ecm+Vahm+MMA6nYh=Kqs3KB3np+MBOS_gZg@mail.gmail.com> Many thanks to the various individuals who were involved in discussing and developing this feature.
1 parent 10238fa commit 3901fd7

File tree

11 files changed

+397
-87
lines changed

11 files changed

+397
-87
lines changed

doc/src/sgml/config.sgml

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3054,41 +3054,71 @@ include_dir 'conf.d'
30543054
transactions waiting for commit will be allowed to proceed after
30553055
these standby servers confirm receipt of their data.
30563056
The synchronous standbys will be those whose names appear
3057-
earlier in this list, and
3057+
in this list, and
30583058
that are both currently connected and streaming data in real-time
30593059
(as shown by a state of <literal>streaming</literal> in the
30603060
<link linkend="monitoring-stats-views-table">
30613061
<literal>pg_stat_replication</></link> view).
3062-
Other standby servers appearing later in this list represent potential
3063-
synchronous standbys. If any of the current synchronous
3064-
standbys disconnects for whatever reason,
3065-
it will be replaced immediately with the next-highest-priority standby.
3066-
Specifying more than one standby name can allow very high availability.
3062+
Specifying more than one standby names can allow very high availability.
30673063
</para>
30683064
<para>
30693065
This parameter specifies a list of standby servers using
30703066
either of the following syntaxes:
30713067
<synopsis>
3072-
<replaceable class="parameter">num_sync</replaceable> ( <replaceable class="parameter">standby_name</replaceable> [, ...] )
3068+
[FIRST] <replaceable class="parameter">num_sync</replaceable> ( <replaceable class="parameter">standby_name</replaceable> [, ...] )
3069+
ANY <replaceable class="parameter">num_sync</replaceable> ( <replaceable class="parameter">standby_name</replaceable> [, ...] )
30733070
<replaceable class="parameter">standby_name</replaceable> [, ...]
30743071
</synopsis>
30753072
where <replaceable class="parameter">num_sync</replaceable> is
30763073
the number of synchronous standbys that transactions need to
30773074
wait for replies from,
30783075
and <replaceable class="parameter">standby_name</replaceable>
3079-
is the name of a standby server. For example, a setting of
3080-
<literal>3 (s1, s2, s3, s4)</> makes transaction commits wait
3081-
until their WAL records are received by three higher-priority standbys
3082-
chosen from standby servers <literal>s1</>, <literal>s2</>,
3083-
<literal>s3</> and <literal>s4</>.
3084-
</para>
3085-
<para>
3086-
The second syntax was used before <productname>PostgreSQL</>
3076+
is the name of a standby server.
3077+
<literal>FIRST</> and <literal>ANY</> specify the method to choose
3078+
synchronous standbys from the listed servers.
3079+
</para>
3080+
<para>
3081+
The keyword <literal>FIRST</>, coupled with
3082+
<replaceable class="parameter">num_sync</replaceable>, specifies a
3083+
priority-based synchronous replication and makes transaction commits
3084+
wait until their WAL records are replicated to
3085+
<replaceable class="parameter">num_sync</replaceable> synchronous
3086+
standbys chosen based on their priorities. For example, a setting of
3087+
<literal>FIRST 3 (s1, s2, s3, s4)</> will cause each commit to wait for
3088+
replies from three higher-priority standbys chosen from standby servers
3089+
<literal>s1</>, <literal>s2</>, <literal>s3</> and <literal>s4</>.
3090+
The standbys whose names appear earlier in the list are given higher
3091+
priority and will be considered as synchronous. Other standby servers
3092+
appearing later in this list represent potential synchronous standbys.
3093+
If any of the current synchronous standbys disconnects for whatever
3094+
reason, it will be replaced immediately with the next-highest-priority
3095+
standby. The keyword <literal>FIRST</> is optional.
3096+
</para>
3097+
<para>
3098+
The keyword <literal>ANY</>, coupled with
3099+
<replaceable class="parameter">num_sync</replaceable>, specifies a
3100+
quorum-based synchronous replication and makes transaction commits
3101+
wait until their WAL records are replicated to <emphasis>at least</>
3102+
<replaceable class="parameter">num_sync</replaceable> listed standbys.
3103+
For example, a setting of <literal>ANY 3 (s1, s2, s3, s4)</> will cause
3104+
each commit to proceed as soon as at least any three standbys of
3105+
<literal>s1</>, <literal>s2</>, <literal>s3</> and <literal>s4</>
3106+
reply.
3107+
</para>
3108+
<para>
3109+
<literal>FIRST</> and <literal>ANY</> are case-insensitive. If these
3110+
keywords are used as the name of a standby server,
3111+
its <replaceable class="parameter">standby_name</replaceable> must
3112+
be double-quoted.
3113+
</para>
3114+
<para>
3115+
The third syntax was used before <productname>PostgreSQL</>
30873116
version 9.6 and is still supported. It's the same as the first syntax
3088-
with <replaceable class="parameter">num_sync</replaceable> equal to 1.
3089-
For example, <literal>1 (s1, s2)</> and
3090-
<literal>s1, s2</> have the same meaning: either <literal>s1</>
3091-
or <literal>s2</> is chosen as a synchronous standby.
3117+
with <literal>FIRST</> and
3118+
<replaceable class="parameter">num_sync</replaceable> equal to 1.
3119+
For example, <literal>FIRST 1 (s1, s2)</> and <literal>s1, s2</> have
3120+
the same meaning: either <literal>s1</> or <literal>s2</> is chosen
3121+
as a synchronous standby.
30923122
</para>
30933123
<para>
30943124
The name of a standby server for this purpose is the

doc/src/sgml/high-availability.sgml

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,19 +1138,25 @@ primary_slot_name = 'node_a_slot'
11381138
as synchronous confirm receipt of their data. The number of synchronous
11391139
standbys that transactions must wait for replies from is specified in
11401140
<varname>synchronous_standby_names</>. This parameter also specifies
1141-
a list of standby names, which determines the priority of each standby
1142-
for being chosen as a synchronous standby. The standbys whose names
1143-
appear earlier in the list are given higher priority and will be considered
1144-
as synchronous. Other standby servers appearing later in this list
1145-
represent potential synchronous standbys. If any of the current
1146-
synchronous standbys disconnects for whatever reason, it will be replaced
1147-
immediately with the next-highest-priority standby.
1141+
a list of standby names and the method (<literal>FIRST</> and
1142+
<literal>ANY</>) to choose synchronous standbys from the listed ones.
11481143
</para>
11491144
<para>
1150-
An example of <varname>synchronous_standby_names</> for multiple
1151-
synchronous standbys is:
1145+
The method <literal>FIRST</> specifies a priority-based synchronous
1146+
replication and makes transaction commits wait until their WAL records are
1147+
replicated to the requested number of synchronous standbys chosen based on
1148+
their priorities. The standbys whose names appear earlier in the list are
1149+
given higher priority and will be considered as synchronous. Other standby
1150+
servers appearing later in this list represent potential synchronous
1151+
standbys. If any of the current synchronous standbys disconnects for
1152+
whatever reason, it will be replaced immediately with the
1153+
next-highest-priority standby.
1154+
</para>
1155+
<para>
1156+
An example of <varname>synchronous_standby_names</> for
1157+
a priority-based multiple synchronous standbys is:
11521158
<programlisting>
1153-
synchronous_standby_names = '2 (s1, s2, s3)'
1159+
synchronous_standby_names = 'FIRST 2 (s1, s2, s3)'
11541160
</programlisting>
11551161
In this example, if four standby servers <literal>s1</>, <literal>s2</>,
11561162
<literal>s3</> and <literal>s4</> are running, the two standbys
@@ -1161,6 +1167,24 @@ synchronous_standby_names = '2 (s1, s2, s3)'
11611167
<literal>s2</> fails. <literal>s4</> is an asynchronous standby since
11621168
its name is not in the list.
11631169
</para>
1170+
<para>
1171+
The method <literal>ANY</> specifies a quorum-based synchronous
1172+
replication and makes transaction commits wait until their WAL records
1173+
are replicated to <emphasis>at least</> the requested number of
1174+
synchronous standbys in the list.
1175+
</para>
1176+
<para>
1177+
An example of <varname>synchronous_standby_names</> for
1178+
a quorum-based multiple synchronous standbys is:
1179+
<programlisting>
1180+
synchronous_standby_names = 'ANY 2 (s1, s2, s3)'
1181+
</programlisting>
1182+
In this example, if four standby servers <literal>s1</>, <literal>s2</>,
1183+
<literal>s3</> and <literal>s4</> are running, transaction commits will
1184+
wait for replies from at least any two standbys of <literal>s1</>,
1185+
<literal>s2</> and <literal>s3</>. <literal>s4</> is an asynchronous
1186+
standby since its name is not in the list.
1187+
</para>
11641188
<para>
11651189
The synchronous states of standby servers can be viewed using
11661190
the <structname>pg_stat_replication</structname> view.

doc/src/sgml/monitoring.sgml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,8 @@ SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event i
14041404
<entry><structfield>sync_priority</></entry>
14051405
<entry><type>integer</></entry>
14061406
<entry>Priority of this standby server for being chosen as the
1407-
synchronous standby</entry>
1407+
synchronous standby in a priority-based synchronous replication.
1408+
This has no effect in a quorum-based synchronous replication.</entry>
14081409
</row>
14091410
<row>
14101411
<entry><structfield>sync_state</></entry>
@@ -1429,6 +1430,12 @@ SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event i
14291430
<literal>sync</>: This standby server is synchronous.
14301431
</para>
14311432
</listitem>
1433+
<listitem>
1434+
<para>
1435+
<literal>quorum</>: This standby server is considered as a candidate
1436+
for quorum standbys.
1437+
</para>
1438+
</listitem>
14321439
</itemizedlist>
14331440
</entry>
14341441
</row>

src/backend/replication/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ repl_gram.o: repl_scanner.c
2626

2727
# syncrep_scanner is complied as part of syncrep_gram
2828
syncrep_gram.o: syncrep_scanner.c
29-
syncrep_scanner.c: FLEXFLAGS = -CF -p
29+
syncrep_scanner.c: FLEXFLAGS = -CF -p -i
3030
syncrep_scanner.c: FLEX_NO_BACKUP=yes
3131

3232
# repl_gram.c, repl_scanner.c, syncrep_gram.c and syncrep_scanner.c

0 commit comments

Comments
 (0)