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

Commit 240067b

Browse files
committed
Merge synchronous_replication setting into synchronous_commit.
This means one less thing to configure when setting up synchronous replication, and also avoids some ambiguity around what the behavior should be when the settings of these variables conflict. Fujii Masao, with additional hacking by me.
1 parent fc3459d commit 240067b

File tree

10 files changed

+91
-104
lines changed

10 files changed

+91
-104
lines changed

doc/src/sgml/config.sgml

+24-44
Original file line numberDiff line numberDiff line change
@@ -1507,8 +1507,9 @@ SET ENABLE_SEQSCAN TO OFF;
15071507
<para>
15081508
Specifies whether transaction commit will wait for WAL records
15091509
to be written to disk before the command returns a <quote>success</>
1510-
indication to the client. The default, and safe, setting is
1511-
<literal>on</>. When <literal>off</>, there can be a delay between
1510+
indication to the client. Valid values are <literal>on</>,
1511+
<literal>local</>, and <literal>off</>. The default, and safe, value
1512+
is <literal>on</>. When <literal>off</>, there can be a delay between
15121513
when success is reported to the client and when the transaction is
15131514
really guaranteed to be safe against a server crash. (The maximum
15141515
delay is three times <xref linkend="guc-wal-writer-delay">.) Unlike
@@ -1522,6 +1523,19 @@ SET ENABLE_SEQSCAN TO OFF;
15221523
exact certainty about the durability of a transaction. For more
15231524
discussion see <xref linkend="wal-async-commit">.
15241525
</para>
1526+
<para>
1527+
If <xref linkend="guc-synchronous-standby-names"> is set, this
1528+
parameter also controls whether or not transaction commit will wait
1529+
for the transaction's WAL records to be flushed to disk and replicated
1530+
to the standby server. The commit wait will last until a reply from
1531+
the current synchronous standby indicates it has written the commit
1532+
record of the transaction to durable storage. If synchronous
1533+
replication is in use, it will normally be sensible either to wait
1534+
both for WAL records to reach both the local and remote disks, or
1535+
to allow the transaction to commit asynchronously. However, the
1536+
special value <literal>local</> is available for transactions that
1537+
wish to wait for local flush to disk, but not synchronous replication.
1538+
</para>
15251539
<para>
15261540
This parameter can be changed at any time; the behavior for any
15271541
one transaction is determined by the setting in effect when it
@@ -2057,39 +2071,6 @@ SET ENABLE_SEQSCAN TO OFF;
20572071
</para>
20582072

20592073
<variablelist>
2060-
<varlistentry id="guc-synchronous-replication" xreflabel="synchronous_replication">
2061-
<term><varname>synchronous_replication</varname> (<type>boolean</type>)</term>
2062-
<indexterm>
2063-
<primary><varname>synchronous_replication</> configuration parameter</primary>
2064-
</indexterm>
2065-
<listitem>
2066-
<para>
2067-
Specifies whether transaction commit will wait for WAL records
2068-
to be replicated before the command returns a <quote>success</>
2069-
indication to the client. The default setting is <literal>off</>.
2070-
When <literal>on</>, there will be a delay while the client waits
2071-
for confirmation of successful replication. That delay will
2072-
increase depending upon the physical distance and network activity
2073-
between primary and standby. The commit wait will last until a
2074-
reply from the current synchronous standby indicates it has written
2075-
the commit record of the transaction to durable storage. This
2076-
parameter has no effect if
2077-
<xref linkend="guc-synchronous-standby-names"> is empty or
2078-
<xref linkend="guc-max-wal-senders"> is zero.
2079-
</para>
2080-
<para>
2081-
This parameter can be changed at any time; the
2082-
behavior for any one transaction is determined by the setting in
2083-
effect when it commits. It is therefore possible, and useful, to have
2084-
some transactions replicate synchronously and others asynchronously.
2085-
For example, to make a single multistatement transaction commit
2086-
asynchronously when the default is synchronous replication, issue
2087-
<command>SET LOCAL synchronous_replication TO OFF</> within the
2088-
transaction.
2089-
</para>
2090-
</listitem>
2091-
</varlistentry>
2092-
20932074
<varlistentry id="guc-synchronous-standby-names" xreflabel="synchronous_standby_names">
20942075
<term><varname>synchronous_standby_names</varname> (<type>string</type>)</term>
20952076
<indexterm>
@@ -2098,7 +2079,7 @@ SET ENABLE_SEQSCAN TO OFF;
20982079
<listitem>
20992080
<para>
21002081
Specifies a priority ordered list of standby names that can offer
2101-
synchronous replication. At any one time there will be just one
2082+
synchronous replication. At any one time there will be at most one
21022083
synchronous standby that will wake sleeping users following commit.
21032084
The synchronous standby will be the first named standby that is
21042085
both currently connected and streaming in real-time to the standby
@@ -2114,18 +2095,17 @@ SET ENABLE_SEQSCAN TO OFF;
21142095
not enforced for uniqueness. In case of duplicates one of the standbys
21152096
will be chosen to be the synchronous standby, though exactly which
21162097
one is indeterminate.
2117-
</para>
2118-
<para>
2119-
No value is set by default.
21202098
The special entry <literal>*</> matches any application_name, including
21212099
the default application name of <literal>walreceiver</>.
21222100
</para>
21232101
<para>
2124-
If a standby is removed from the list of servers then it will stop
2125-
being the synchronous standby, allowing another to take its place.
2126-
If the list is empty, synchronous replication will not be
2127-
possible, whatever the setting of <varname>synchronous_replication</>.
2128-
Standbys may also be added to the list without restarting the server.
2102+
If no synchronous standby names are specified, then synchronous
2103+
replication is not enabled and transaction commit will never wait for
2104+
replication. This is the default configuration. Even when
2105+
synchronous replication is enabled, individual transactions can be
2106+
configured not to wait for replication by setting the
2107+
<xref linkend="guc-synchronous-commit"> parameter to
2108+
<literal>local</> or <literal>off</>.
21292109
</para>
21302110
</listitem>
21312111
</varlistentry>

doc/src/sgml/high-availability.sgml

+20-28
Original file line numberDiff line numberDiff line change
@@ -929,19 +929,18 @@ primary_conninfo = 'host=192.168.1.50 port=5432 user=foo password=foopass'
929929
<title>Basic Configuration</title>
930930

931931
<para>
932-
All parameters have useful default values, so we can enable
933-
synchronous replication easily just by setting this on the primary:
934-
935-
<programlisting>
936-
synchronous_replication = on
937-
</programlisting>
938-
939-
When <varname>synchronous_replication</> is set, a commit will wait
940-
for confirmation that the standby has received the commit record,
941-
even if that takes a very long time.
942-
<varname>synchronous_replication</> can be set by individual
932+
Once streaming replication has been configured, configuring synchronous
933+
replication requires only one additional configuration step:
934+
<xref linkend="guc-synchronous-standby-names"> must be set to
935+
a non-empty value. <varname>synchronous_commit</> must also be set to
936+
<literal>on</>, but since this is the default value, typically no change is
937+
required. This configuration will cause each commit to wait for
938+
confirmation that the standby has written the commit record to durable
939+
storage, even if that takes a very long time.
940+
<varname>synchronous_commit</> can be set by individual
943941
users, so can be configured in the configuration file, for particular
944-
users or databases, or dynamically by applications.
942+
users or databases, or dynamically by applications, in order to control
943+
the durability guarantee on a per-transaction basis.
945944
</para>
946945

947946
<para>
@@ -959,17 +958,10 @@ synchronous_replication = on
959958
</para>
960959

961960
<para>
962-
Users will stop waiting if a fast shutdown is requested, though the
963-
server does not fully shutdown until all outstanding WAL records are
964-
transferred to standby servers.
965-
</para>
966-
967-
<para>
968-
Note also that <varname>synchronous_commit</> is used when the user
969-
specifies <varname>synchronous_replication</>, overriding even an
970-
explicit setting of <varname>synchronous_commit</> to <literal>off</>.
971-
This is because we must write WAL to disk on primary before we replicate
972-
to ensure the standby never gets ahead of the primary.
961+
Users will stop waiting if a fast shutdown is requested. However, as
962+
when using asynchronous replication, the server will does not fully
963+
shutdown until all outstanding WAL records are transferred to the currently
964+
connected standby servers.
973965
</para>
974966

975967
</sect3>
@@ -1019,9 +1011,9 @@ synchronous_replication = on
10191011
<title>Planning for High Availability</title>
10201012

10211013
<para>
1022-
Commits made when synchronous_replication is set will wait until
1023-
the sync standby responds. The response may never occur if the last,
1024-
or only, standby should crash.
1014+
Commits made when <varname>synchronous_commit</> is set to <literal>on</>
1015+
will wait until the sync standby responds. The response may never occur
1016+
if the last, or only, standby should crash.
10251017
</para>
10261018

10271019
<para>
@@ -1073,8 +1065,8 @@ synchronous_replication = on
10731065
If you need to re-create a standby server while transactions are
10741066
waiting, make sure that the commands to run pg_start_backup() and
10751067
pg_stop_backup() are run in a session with
1076-
synchronous_replication = off, otherwise those requests will wait
1077-
forever for the standby to appear.
1068+
<varname>synchronous_commit</> = <literal>off</>, otherwise those
1069+
requests will wait forever for the standby to appear.
10781070
</para>
10791071

10801072
</sect3>

doc/src/sgml/release-9.1.sgml

+4-3
Original file line numberDiff line numberDiff line change
@@ -651,14 +651,15 @@
651651
</para>
652652

653653
<para>
654+
This allows the primary to wait for a standby to write the transaction
655+
information to disk before acknowledging the commit.
654656
One standby at a time can take the role of the synchronous standby,
655657
as controlled by the
656658
<link linkend="guc-synchronous-standby-names"><varname>synchronous_standby_names</varname></link>
657659
setting. Synchronous replication can be enabled or disabled on a
658660
per-transaction basis using the
659-
<link linkend="guc-synchronous-replication"><varname>synchronous_replication</></link>
660-
setting. This allows the primary to wait for a standby to write the
661-
transaction information to disk before acknowledging the commit.
661+
<link linkend="guc-synchronous-commit"><varname>synchronous_commit</></link>
662+
setting.
662663
</para>
663664
</listitem>
664665

src/backend/access/transam/xact.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ bool XactReadOnly;
6868
bool DefaultXactDeferrable = false;
6969
bool XactDeferrable;
7070

71-
bool XactSyncCommit = true;
71+
int synchronous_commit = SYNCHRONOUS_COMMIT_ON;
7272

7373
int CommitDelay = 0; /* precommit delay in microseconds */
7474
int CommitSiblings = 5; /* # concurrent xacts needed to sleep */
@@ -1056,7 +1056,8 @@ RecordTransactionCommit(void)
10561056
* if all to-be-deleted tables are temporary though, since they are lost
10571057
* anyway if we crash.)
10581058
*/
1059-
if ((wrote_xlog && XactSyncCommit) || forceSyncCommit || nrels > 0 || SyncRepRequested())
1059+
if ((wrote_xlog && synchronous_commit >= SYNCHRONOUS_COMMIT_LOCAL) ||
1060+
forceSyncCommit || nrels > 0)
10601061
{
10611062
/*
10621063
* Synchronous commit case:

src/backend/postmaster/autovacuum.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,8 @@ AutoVacWorkerMain(int argc, char *argv[])
15311531
* if we are waiting for standbys to connect. This is important to
15321532
* ensure we aren't blocked from performing anti-wraparound tasks.
15331533
*/
1534-
SetConfigOption("synchronous_replication", "off", PGC_SUSET, PGC_S_OVERRIDE);
1534+
if (synchronous_commit == SYNCHRONOUS_COMMIT_ON)
1535+
SetConfigOption("synchronous_commit", "local", PGC_SUSET, PGC_S_OVERRIDE);
15351536

15361537
/*
15371538
* Get the info about the database we're going to work on.

src/backend/replication/syncrep.c

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
#include "utils/ps_status.h"
6464

6565
/* User-settable parameters for sync rep */
66-
bool synchronous_replication = false; /* Only set in user backends */
6766
char *SyncRepStandbyNames;
6867

6968
#define SyncStandbysDefined() \

src/backend/utils/misc/guc.c

+27-16
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,23 @@ static const struct config_enum_entry constraint_exclusion_options[] = {
350350
{NULL, 0, false}
351351
};
352352

353+
/*
354+
* Although only "on", "off", and "local" are documented, we
355+
* accept all the likely variants of "on" and "off".
356+
*/
357+
static const struct config_enum_entry synchronous_commit_options[] = {
358+
{"local", SYNCHRONOUS_COMMIT_LOCAL, false},
359+
{"on", SYNCHRONOUS_COMMIT_ON, false},
360+
{"off", SYNCHRONOUS_COMMIT_OFF, false},
361+
{"true", SYNCHRONOUS_COMMIT_ON, true},
362+
{"false", SYNCHRONOUS_COMMIT_OFF, true},
363+
{"yes", SYNCHRONOUS_COMMIT_ON, true},
364+
{"no", SYNCHRONOUS_COMMIT_OFF, true},
365+
{"1", SYNCHRONOUS_COMMIT_ON, true},
366+
{"0", SYNCHRONOUS_COMMIT_OFF, true},
367+
{NULL, 0, false}
368+
};
369+
353370
/*
354371
* Options for enum values stored in other modules
355372
*/
@@ -746,22 +763,6 @@ static struct config_bool ConfigureNamesBool[] =
746763
&enableFsync,
747764
true, NULL, NULL
748765
},
749-
{
750-
{"synchronous_commit", PGC_USERSET, WAL_SETTINGS,
751-
gettext_noop("Sets immediate fsync at commit."),
752-
NULL
753-
},
754-
&XactSyncCommit,
755-
true, NULL, NULL
756-
},
757-
{
758-
{"synchronous_replication", PGC_USERSET, WAL_REPLICATION,
759-
gettext_noop("Requests synchronous replication."),
760-
NULL
761-
},
762-
&synchronous_replication,
763-
false, NULL, NULL
764-
},
765766
{
766767
{"zero_damaged_pages", PGC_SUSET, DEVELOPER_OPTIONS,
767768
gettext_noop("Continues processing past damaged page headers."),
@@ -2908,6 +2909,16 @@ static struct config_enum ConfigureNamesEnum[] =
29082909
assign_session_replication_role, NULL
29092910
},
29102911

2912+
{
2913+
{"synchronous_commit", PGC_USERSET, WAL_SETTINGS,
2914+
gettext_noop("Sets the current transaction's synchronization level."),
2915+
NULL
2916+
},
2917+
&synchronous_commit,
2918+
SYNCHRONOUS_COMMIT_ON, synchronous_commit_options,
2919+
NULL, NULL
2920+
},
2921+
29112922
{
29122923
{"trace_recovery_messages", PGC_SIGHUP, DEVELOPER_OPTIONS,
29132924
gettext_noop("Enables logging of recovery-related debugging information."),

src/backend/utils/misc/postgresql.conf.sample

+1-5
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
#wal_level = minimal # minimal, archive, or hot_standby
154154
# (change requires restart)
155155
#fsync = on # turns forced synchronization on or off
156-
#synchronous_commit = on # immediate fsync at commit
156+
#synchronous_commit = on # synchronization level; on, off, or local
157157
#wal_sync_method = fsync # the default is the first option
158158
# supported by the operating system:
159159
# open_datasync
@@ -184,10 +184,6 @@
184184
#archive_timeout = 0 # force a logfile segment switch after this
185185
# number of seconds; 0 disables
186186

187-
# - Replication - User Settings
188-
189-
#synchronous_replication = off # does commit wait for reply from standby
190-
191187
# - Streaming Replication - Server Settings
192188

193189
#synchronous_standby_names = '' # standby servers that provide sync rep

src/include/access/xact.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,15 @@ extern bool XactReadOnly;
5252
extern bool DefaultXactDeferrable;
5353
extern bool XactDeferrable;
5454

55-
/* Asynchronous commits */
56-
extern bool XactSyncCommit;
55+
typedef enum
56+
{
57+
SYNCHRONOUS_COMMIT_OFF, /* asynchronous commit */
58+
SYNCHRONOUS_COMMIT_LOCAL, /* wait for only local flush */
59+
SYNCHRONOUS_COMMIT_ON /* wait for local flush and sync rep */
60+
} SyncCommitLevel;
61+
62+
/* Synchronous commit level */
63+
extern int synchronous_commit;
5764

5865
/* Kluge for 2PC support */
5966
extern bool MyXactAccessedTempRel;

src/include/replication/syncrep.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@
2020
#include "utils/guc.h"
2121

2222
#define SyncRepRequested() \
23-
(synchronous_replication && max_wal_senders > 0)
23+
(max_wal_senders > 0 && synchronous_commit == SYNCHRONOUS_COMMIT_ON)
2424

2525
/* syncRepState */
2626
#define SYNC_REP_NOT_WAITING 0
2727
#define SYNC_REP_WAITING 1
2828
#define SYNC_REP_WAIT_COMPLETE 2
2929

3030
/* user-settable parameters for synchronous replication */
31-
extern bool synchronous_replication;
3231
extern char *SyncRepStandbyNames;
3332

3433
/* called by user backend */

0 commit comments

Comments
 (0)