<entry>If true, the subscription is enabled and should be replicating.</entry>
</row>
+ <row>
+ <entry><structfield>subsynccommit</structfield></entry>
+ <entry><type>text</type></entry>
+ <entry></entry>
+ <entry>
+ Contains the value of the <varname>synchronous_commit</varname>
+ setting for the subscription workers.
+ </entry>
+ </row>
+
<row>
<entry><structfield>subconninfo</structfield></entry>
<entry><type>text</type></entry>
<phrase>where <replaceable class="PARAMETER">suboption</replaceable> can be:</phrase>
SLOT NAME = <replaceable class="PARAMETER">slot_name</replaceable>
+ | SYNCHRONOUS_COMMIT = <replaceable class="PARAMETER">synchronous_commit</replaceable>
ALTER SUBSCRIPTION <replaceable class="PARAMETER">name</replaceable> SET PUBLICATION <replaceable class="PARAMETER">publication_name</replaceable> [, ...] { REFRESH WITH ( <replaceable class="PARAMETER">puboption</replaceable> [, ... ] ) | NOREFRESH }
ALTER SUBSCRIPTION <replaceable class="PARAMETER">name</replaceable> REFRESH PUBLICATION WITH ( <replaceable class="PARAMETER">puboption</replaceable> [, ... ] )
<varlistentry>
<term><literal>CONNECTION '<replaceable class="parameter">conninfo</replaceable>'</literal></term>
<term><literal>SLOT NAME = <replaceable class="parameter">slot_name</replaceable></literal></term>
+ <term><literal>SYNCHRONOUS_COMMIT = <replaceable class="PARAMETER">synchronous_commit</replaceable></literal></term>
<listitem>
<para>
These clauses alter properties originally set by
| CREATE SLOT | NOCREATE SLOT
| SLOT NAME = <replaceable class="PARAMETER">slot_name</replaceable>
| COPY DATA | NOCOPY DATA
+ | SYNCHRONOUS_COMMIT = <replaceable class="PARAMETER">synchronous_commit</replaceable>
| NOCONNECT
</synopsis>
</refsynopsisdiv>
</listitem>
</varlistentry>
+ <varlistentry>
+ <term><literal>SYNCHRONOUS_COMMIT = <replaceable class="PARAMETER">synchronous_commit</replaceable></literal></term>
+ <listitem>
+ <para>
+ The value of this parameter overrides the
+ <xref linkend="guc-synchronous-commit"> setting. The default value is
+ <literal>off</literal>.
+ </para>
+
+ <para>
+ It is safe to use <literal>off</literal> for logical replication: If the
+ subscriber loses transactions because of missing synchronization, the
+ data will be resent from the publisher.
+ </para>
+
+ <para>
+ A different setting might be appropriate when doing synchronous logical
+ replication. The logical replication workers report the positions of
+ writes and flushes to the publisher, and when using synchronous
+ replication, the publisher will wait for the actual flush. This means
+ that setting <literal>SYNCHRONOUS_COMMIT</literal> for the subscriber
+ to <literal>off</literal> when the subscription is used for synchronous
+ replication might increase the latency for <command>COMMIT</command> on
+ the publisher. In this scenario, it can be advantageous to set
+ <literal>SYNCHRONOUS_COMMIT</literal> to <literal>local</literal> or
+ higher.
+ </para>
+ </listitem>
+ </varlistentry>
+
<varlistentry>
<term><literal>NOCONNECT</literal></term>
<listitem>
Assert(!isnull);
sub->slotname = pstrdup(NameStr(*DatumGetName(datum)));
+ /* Get synccommit */
+ datum = SysCacheGetAttr(SUBSCRIPTIONOID,
+ tup,
+ Anum_pg_subscription_subsynccommit,
+ &isnull);
+ Assert(!isnull);
+ sub->synccommit = TextDatumGetCString(datum);
+
/* Get publications */
datum = SysCacheGetAttr(SUBSCRIPTIONOID,
tup,
#include "storage/lmgr.h"
#include "utils/builtins.h"
+#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/syscache.h"
static void
parse_subscription_options(List *options, bool *connect, bool *enabled_given,
bool *enabled, bool *create_slot, char **slot_name,
- bool *copy_data)
+ bool *copy_data, char **synchronous_commit)
{
ListCell *lc;
bool connect_given = false;
*slot_name = NULL;
if (copy_data)
*copy_data = true;
+ if (synchronous_commit)
+ *synchronous_commit = NULL;
/* Parse options */
foreach (lc, options)
copy_data_given = true;
*copy_data = !defGetBoolean(defel);
}
+ else if (strcmp(defel->defname, "synchronous_commit") == 0 &&
+ synchronous_commit)
+ {
+ if (*synchronous_commit)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("conflicting or redundant options")));
+
+ *synchronous_commit = defGetString(defel);
+
+ /* Test if the given value is valid for synchronous_commit GUC. */
+ (void) set_config_option("synchronous_commit", *synchronous_commit,
+ PGC_BACKEND, PGC_S_TEST, GUC_ACTION_SET,
+ false, 0, false);
+ }
else
elog(ERROR, "unrecognized option: %s", defel->defname);
}
bool enabled_given;
bool enabled;
bool copy_data;
+ char *synchronous_commit;
char *conninfo;
char *slotname;
char originname[NAMEDATALEN];
* Connection and publication should not be specified here.
*/
parse_subscription_options(stmt->options, &connect, &enabled_given,
- &enabled, &create_slot, &slotname, ©_data);
+ &enabled, &create_slot, &slotname, ©_data,
+ &synchronous_commit);
/*
* Since creating a replication slot is not transactional, rolling back
if (slotname == NULL)
slotname = stmt->subname;
+ /* The default for synchronous_commit of subscriptions is off. */
+ if (synchronous_commit == NULL)
+ synchronous_commit = "off";
conninfo = stmt->conninfo;
publications = stmt->publication;
CStringGetTextDatum(conninfo);
values[Anum_pg_subscription_subslotname - 1] =
DirectFunctionCall1(namein, CStringGetDatum(slotname));
+ values[Anum_pg_subscription_subsynccommit - 1] =
+ CStringGetTextDatum(synchronous_commit);
values[Anum_pg_subscription_subpublications - 1] =
publicationListToArray(publications);
case ALTER_SUBSCRIPTION_OPTIONS:
{
char *slot_name;
+ char *synchronous_commit;
parse_subscription_options(stmt->options, NULL, NULL, NULL,
- NULL, &slot_name, NULL);
+ NULL, &slot_name, NULL,
+ &synchronous_commit);
- values[Anum_pg_subscription_subslotname - 1] =
- DirectFunctionCall1(namein, CStringGetDatum(slot_name));
- replaces[Anum_pg_subscription_subslotname - 1] = true;
+ if (slot_name)
+ {
+ values[Anum_pg_subscription_subslotname - 1] =
+ DirectFunctionCall1(namein, CStringGetDatum(slot_name));
+ replaces[Anum_pg_subscription_subslotname - 1] = true;
+ }
+ if (synchronous_commit)
+ {
+ values[Anum_pg_subscription_subsynccommit - 1] =
+ CStringGetTextDatum(synchronous_commit);
+ replaces[Anum_pg_subscription_subsynccommit - 1] = true;
+ }
update_tuple = true;
break;
parse_subscription_options(stmt->options, NULL,
&enabled_given, &enabled, NULL,
- NULL, NULL);
+ NULL, NULL, NULL);
Assert(enabled_given);
values[Anum_pg_subscription_subenabled - 1] =
Subscription *sub = GetSubscription(subid, false);
parse_subscription_options(stmt->options, NULL, NULL, NULL,
- NULL, NULL, ©_data);
+ NULL, NULL, ©_data, NULL);
values[Anum_pg_subscription_subpublications - 1] =
publicationListToArray(stmt->publication);
Subscription *sub = GetSubscription(subid, false);
parse_subscription_options(stmt->options, NULL, NULL, NULL,
- NULL, NULL, ©_data);
+ NULL, NULL, ©_data, NULL);
AlterSubscription_refresh(sub, copy_data);
*/
oldcxt = MemoryContextSwitchTo(resultcxt);
- sub = (Subscription *) palloc(sizeof(Subscription));
+ sub = (Subscription *) palloc0(sizeof(Subscription));
sub->oid = HeapTupleGetOid(tup);
sub->dbid = subform->subdbid;
sub->owner = subform->subowner;
sub->enabled = subform->subenabled;
sub->name = pstrdup(NameStr(subform->subname));
-
/* We don't fill fields we are not interested in. */
- sub->conninfo = NULL;
- sub->slotname = NULL;
- sub->publications = NIL;
res = lappend(res, sub);
MemoryContextSwitchTo(oldcxt);
MemoryContextSwitchTo(oldctx);
+ /* Change synchronous commit according to the user's wishes */
+ SetConfigOption("synchronous_commit", MySubscription->synccommit,
+ PGC_BACKEND, PGC_S_OVERRIDE);
+
if (started_tx)
CommitTransactionCommand();
MySubscriptionValid = true;
MemoryContextSwitchTo(oldctx);
+ /* Setup synchronous commit according to the user's wishes */
+ SetConfigOption("synchronous_commit", MySubscription->synccommit,
+ PGC_BACKEND, PGC_S_OVERRIDE);
+
if (!MySubscription->enabled)
{
ereport(LOG,
int i_rolname;
int i_subconninfo;
int i_subslotname;
+ int i_subsynccommit;
int i_subpublications;
int i,
ntups;
appendPQExpBuffer(query,
"SELECT s.tableoid, s.oid, s.subname,"
"(%s s.subowner) AS rolname, "
- " s.subconninfo, s.subslotname, s.subpublications "
+ " s.subconninfo, s.subslotname, s.subsynccommit, "
+ " s.subpublications "
"FROM pg_catalog.pg_subscription s "
"WHERE s.subdbid = (SELECT oid FROM pg_catalog.pg_database"
" WHERE datname = current_database())",
i_rolname = PQfnumber(res, "rolname");
i_subconninfo = PQfnumber(res, "subconninfo");
i_subslotname = PQfnumber(res, "subslotname");
+ i_subsynccommit = PQfnumber(res, "subsynccommit");
i_subpublications = PQfnumber(res, "subpublications");
subinfo = pg_malloc(ntups * sizeof(SubscriptionInfo));
subinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
subinfo[i].subconninfo = pg_strdup(PQgetvalue(res, i, i_subconninfo));
subinfo[i].subslotname = pg_strdup(PQgetvalue(res, i, i_subslotname));
+ subinfo[i].subsynccommit =
+ pg_strdup(PQgetvalue(res, i, i_subsynccommit));
subinfo[i].subpublications =
pg_strdup(PQgetvalue(res, i, i_subpublications));
appendPQExpBuffer(query, " PUBLICATION %s WITH (NOCONNECT, SLOT NAME = ", publications->data);
appendStringLiteralAH(query, subinfo->subslotname, fout);
+
+ if (strcmp(subinfo->subsynccommit, "off") != 0)
+ appendPQExpBuffer(query, ", SYNCHRONOUS_COMMIT = %s", fmtId(subinfo->subsynccommit));
+
appendPQExpBufferStr(query, ");\n");
appendPQExpBuffer(labelq, "SUBSCRIPTION %s", fmtId(subinfo->dobj.name));
char *rolname;
char *subconninfo;
char *subslotname;
+ char *subsynccommit;
char *subpublications;
} SubscriptionInfo;
PQExpBufferData buf;
PGresult *res;
printQueryOpt myopt = pset.popt;
- static const bool translate_columns[] = {false, false, false, false, false};
+ static const bool translate_columns[] = {false, false, false, false,
+ false, false};
if (pset.sversion < 100000)
{
if (verbose)
{
appendPQExpBuffer(&buf,
+ ", subsynccommit AS \"%s\"\n"
", subconninfo AS \"%s\"\n",
+ gettext_noop("Synchronous commit"),
gettext_noop("Conninfo"));
}
#ifdef CATALOG_VARLEN /* variable-length fields start here */
text subconninfo; /* Connection string to the publisher */
NameData subslotname; /* Slot name on publisher */
-
+ text subsynccommit; /* Synchronous commit setting for worker */
text subpublications[1]; /* List of publications subscribed to */
#endif
} FormData_pg_subscription;
* compiler constants for pg_subscription
* ----------------
*/
-#define Natts_pg_subscription 7
+#define Natts_pg_subscription 8
#define Anum_pg_subscription_subdbid 1
#define Anum_pg_subscription_subname 2
#define Anum_pg_subscription_subowner 3
#define Anum_pg_subscription_subenabled 4
#define Anum_pg_subscription_subconninfo 5
#define Anum_pg_subscription_subslotname 6
-#define Anum_pg_subscription_subpublications 7
+#define Anum_pg_subscription_subsynccommit 7
+#define Anum_pg_subscription_subpublications 8
typedef struct Subscription
bool enabled; /* Indicates if the subscription is enabled */
char *conninfo; /* Connection string to the publisher */
char *slotname; /* Name of the replication slot */
+ char *synccommit; /* Synchronous commit setting for worker */
List *publications; /* List of publication names to subscribe to */
} Subscription;
ERROR: must be superuser to create subscriptions
SET SESSION AUTHORIZATION 'regress_subscription_user';
\dRs+
- List of subscriptions
- Name | Owner | Enabled | Publication | Conninfo
----------+---------------------------+---------+-------------+---------------------
- testsub | regress_subscription_user | f | {testpub} | dbname=doesnotexist
+ List of subscriptions
+ Name | Owner | Enabled | Publication | Synchronous commit | Conninfo
+---------+---------------------------+---------+-------------+--------------------+---------------------
+ testsub | regress_subscription_user | f | {testpub} | off | dbname=doesnotexist
(1 row)
ALTER SUBSCRIPTION testsub SET PUBLICATION testpub2, testpub3 NOREFRESH;
ALTER SUBSCRIPTION doesnotexist CONNECTION 'dbname=doesnotexist2';
ERROR: subscription "doesnotexist" does not exist
\dRs+
- List of subscriptions
- Name | Owner | Enabled | Publication | Conninfo
----------+---------------------------+---------+---------------------+----------------------
- testsub | regress_subscription_user | f | {testpub2,testpub3} | dbname=doesnotexist2
+ List of subscriptions
+ Name | Owner | Enabled | Publication | Synchronous commit | Conninfo
+---------+---------------------------+---------+---------------------+--------------------+----------------------
+ testsub | regress_subscription_user | f | {testpub2,testpub3} | off | dbname=doesnotexist2
(1 row)
BEGIN;
ERROR: must be owner of subscription testsub
RESET ROLE;
ALTER SUBSCRIPTION testsub RENAME TO testsub_foo;
-\dRs
- List of subscriptions
- Name | Owner | Enabled | Publication
--------------+---------------------------+---------+---------------------
- testsub_foo | regress_subscription_user | f | {testpub2,testpub3}
+ALTER SUBSCRIPTION testsub_foo WITH (SYNCHRONOUS_COMMIT = local);
+ALTER SUBSCRIPTION testsub_foo WITH (SYNCHRONOUS_COMMIT = foobar);
+ERROR: invalid value for parameter "synchronous_commit": "foobar"
+HINT: Available values: local, remote_write, remote_apply, on, off.
+\dRs+
+ List of subscriptions
+ Name | Owner | Enabled | Publication | Synchronous commit | Conninfo
+-------------+---------------------------+---------+---------------------+--------------------+----------------------
+ testsub_foo | regress_subscription_user | f | {testpub2,testpub3} | local | dbname=doesnotexist2
(1 row)
-- rename back to keep the rest simple
RESET ROLE;
ALTER SUBSCRIPTION testsub RENAME TO testsub_foo;
+ALTER SUBSCRIPTION testsub_foo WITH (SYNCHRONOUS_COMMIT = local);
+ALTER SUBSCRIPTION testsub_foo WITH (SYNCHRONOUS_COMMIT = foobar);
-\dRs
+\dRs+
-- rename back to keep the rest simple
ALTER SUBSCRIPTION testsub_foo RENAME TO testsub;