agora inbox for [email protected]
help / color / mirror / Atom feedFrom: Petr Jelinek <[email protected]>
Subject: [PATCH] Add option to modify sync commit per subscription
Date: Mon, 6 Mar 2017 13:07:45 +0100
This also changes default behaviour of subscription workers to
synchronous_commit = off
---
doc/src/sgml/catalogs.sgml | 11 +++++++
doc/src/sgml/ref/alter_subscription.sgml | 2 ++
doc/src/sgml/ref/create_subscription.sgml | 12 ++++++++
src/backend/catalog/pg_subscription.c | 1 +
src/backend/commands/subscriptioncmds.c | 49 ++++++++++++++++++++++++------
src/backend/replication/logical/launcher.c | 2 +-
src/backend/replication/logical/worker.c | 10 ++++++
src/bin/pg_dump/pg_dump.c | 12 +++++++-
src/bin/pg_dump/pg_dump.h | 1 +
src/bin/pg_dump/t/002_pg_dump.pl | 2 +-
src/bin/psql/describe.c | 5 ++-
src/include/catalog/pg_subscription.h | 11 ++++---
src/test/regress/expected/subscription.out | 27 ++++++++--------
src/test/regress/sql/subscription.sql | 3 +-
14 files changed, 116 insertions(+), 32 deletions(-)
diff --git a/doc/src/sgml/catalogs.sgml b/doc/src/sgml/catalogs.sgml
index c531c73..9e072ea 100644
--- a/doc/src/sgml/catalogs.sgml
+++ b/doc/src/sgml/catalogs.sgml
@@ -6414,6 +6414,17 @@
</row>
<row>
+ <entry><structfield>subsynccommit</structfield></entry>
+ <entry><type>bool</type></entry>
+ <entry></entry>
+ <entry>
+ If true, the apply for the subscription will run with
+ <literal>synchronous_commit</literal> set to <literal>local</literal>.
+ Otherwise it will have it set to <literal>false</literal>.
+ </entry>
+ </row>
+
+ <row>
<entry><structfield>subconninfo</structfield></entry>
<entry><type>text</type></entry>
<entry></entry>
diff --git a/doc/src/sgml/ref/alter_subscription.sgml b/doc/src/sgml/ref/alter_subscription.sgml
index 6f94247..572e370 100644
--- a/doc/src/sgml/ref/alter_subscription.sgml
+++ b/doc/src/sgml/ref/alter_subscription.sgml
@@ -26,6 +26,8 @@ ALTER SUBSCRIPTION <replaceable class="PARAMETER">name</replaceable> WITH ( <rep
<phrase>where <replaceable class="PARAMETER">suboption</replaceable> can be:</phrase>
SLOT NAME = <replaceable class="PARAMETER">slot_name</replaceable>
+ SLOT NAME = slot_name
+ | SYNCHRONOUS_COMMIT = boolean
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> [, ... ] )
diff --git a/doc/src/sgml/ref/create_subscription.sgml b/doc/src/sgml/ref/create_subscription.sgml
index 8f3c30b..479f4e4 100644
--- a/doc/src/sgml/ref/create_subscription.sgml
+++ b/doc/src/sgml/ref/create_subscription.sgml
@@ -32,6 +32,7 @@ CREATE SUBSCRIPTION <replaceable class="PARAMETER">subscription_name</replaceabl
| CREATE SLOT | NOCREATE SLOT
| SLOT NAME = <replaceable class="PARAMETER">slot_name</replaceable>
| COPY DATA | NOCOPY DATA
+ | SYNCHRONOUS_COMMIT = boolean
| NOCONNECT
</synopsis>
</refsynopsisdiv>
@@ -148,6 +149,17 @@ CREATE SUBSCRIPTION <replaceable class="PARAMETER">subscription_name</replaceabl
</varlistentry>
<varlistentry>
+ <term><literal>SYNCHRONOUS_COMMIT = <replaceable class="parameter">boolean</replaceable></literal></term>
+ <listitem>
+ <para>
+ The value of this parameter overrides the
+ <xref linkend="guc-synchronous-commit">synchronous_commit setting.
+ The default value is <literal>false<literal>.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
<term>NOCONNECT</term>
<listitem>
<para>
diff --git a/src/backend/catalog/pg_subscription.c b/src/backend/catalog/pg_subscription.c
index e420ec1..e7a1634 100644
--- a/src/backend/catalog/pg_subscription.c
+++ b/src/backend/catalog/pg_subscription.c
@@ -68,6 +68,7 @@ GetSubscription(Oid subid, bool missing_ok)
sub->name = pstrdup(NameStr(subform->subname));
sub->owner = subform->subowner;
sub->enabled = subform->subenabled;
+ sub->synccommit = subform->subsynccommit;
/* Get conninfo */
datum = SysCacheGetAttr(SUBSCRIPTIONOID,
diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c
index 0784ca7..8f201b2 100644
--- a/src/backend/commands/subscriptioncmds.c
+++ b/src/backend/commands/subscriptioncmds.c
@@ -60,12 +60,13 @@ static List *fetch_table_list(WalReceiverConn *wrconn, List *publications);
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, bool *synchronous_commit)
{
ListCell *lc;
bool connect_given = false;
bool create_slot_given = false;
bool copy_data_given = false;
+ bool synchronous_commit_given = false;
if (connect)
*connect = true;
@@ -80,6 +81,8 @@ parse_subscription_options(List *options, bool *connect, bool *enabled_given,
*slot_name = NULL;
if (copy_data)
*copy_data = true;
+ if (synchronous_commit)
+ *synchronous_commit = false;
/* Parse options */
foreach (lc, options)
@@ -165,6 +168,16 @@ parse_subscription_options(List *options, bool *connect, bool *enabled_given,
copy_data_given = true;
*copy_data = !defGetBoolean(defel);
}
+ else if (strcmp(defel->defname, "synchronous_commit") == 0 && synchronous_commit)
+ {
+ if (synchronous_commit_given)
+ ereport(ERROR,
+ (errcode(ERRCODE_SYNTAX_ERROR),
+ errmsg("conflicting or redundant options")));
+
+ synchronous_commit_given = true;
+ *synchronous_commit = defGetBoolean(defel);
+ }
else
elog(ERROR, "unrecognized option: %s", defel->defname);
}
@@ -269,6 +282,7 @@ CreateSubscription(CreateSubscriptionStmt *stmt, bool isTopLevel)
bool enabled_given;
bool enabled;
bool copy_data;
+ bool synchronous_commit;
char *conninfo;
char *slotname;
char originname[NAMEDATALEN];
@@ -280,7 +294,8 @@ CreateSubscription(CreateSubscriptionStmt *stmt, bool isTopLevel)
* 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
@@ -330,6 +345,8 @@ CreateSubscription(CreateSubscriptionStmt *stmt, bool isTopLevel)
DirectFunctionCall1(namein, CStringGetDatum(stmt->subname));
values[Anum_pg_subscription_subowner - 1] = ObjectIdGetDatum(owner);
values[Anum_pg_subscription_subenabled - 1] = BoolGetDatum(enabled);
+ values[Anum_pg_subscription_subsynccommit - 1] =
+ BoolGetDatum(synchronous_commit);
values[Anum_pg_subscription_subconninfo - 1] =
CStringGetTextDatum(conninfo);
values[Anum_pg_subscription_subslotname - 1] =
@@ -581,14 +598,26 @@ AlterSubscription(AlterSubscriptionStmt *stmt)
{
case ALTER_SUBSCRIPTION_OPTIONS:
{
- char *slot_name;
+ char *slot_name;
+ bool synchronous_commit;
+ Form_pg_subscription form;
+
+ form = (Form_pg_subscription) GETSTRUCT(tup);
+ synchronous_commit = form->subsynccommit;
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;
+ }
+ values[Anum_pg_subscription_subsynccommit - 1] =
+ BoolGetDatum(synchronous_commit);
+ replaces[Anum_pg_subscription_subsynccommit - 1] = true;
update_tuple = true;
break;
@@ -601,7 +630,7 @@ AlterSubscription(AlterSubscriptionStmt *stmt)
parse_subscription_options(stmt->options, NULL,
&enabled_given, &enabled, NULL,
- NULL, NULL);
+ NULL, NULL, NULL);
Assert(enabled_given);
values[Anum_pg_subscription_subenabled - 1] =
@@ -626,7 +655,7 @@ AlterSubscription(AlterSubscriptionStmt *stmt)
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);
@@ -652,7 +681,7 @@ AlterSubscription(AlterSubscriptionStmt *stmt)
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);
diff --git a/src/backend/replication/logical/launcher.c b/src/backend/replication/logical/launcher.c
index 255b225..0d3ec27 100644
--- a/src/backend/replication/logical/launcher.c
+++ b/src/backend/replication/logical/launcher.c
@@ -126,7 +126,7 @@ get_subscription_list(void)
*/
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;
diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c
index fc01cd3..ffab8a4 100644
--- a/src/backend/replication/logical/worker.c
+++ b/src/backend/replication/logical/worker.c
@@ -1410,6 +1410,11 @@ reread_subscription(void)
MemoryContextSwitchTo(oldctx);
+ /* Change synchronous commit according to the user's wishes */
+ SetConfigOption("synchronous_commit",
+ MySubscription->synccommit ? "local" : "off",
+ PGC_BACKEND, PGC_S_OVERRIDE);
+
if (started_tx)
CommitTransactionCommand();
@@ -1478,6 +1483,11 @@ ApplyWorkerMain(Datum main_arg)
MySubscriptionValid = true;
MemoryContextSwitchTo(oldctx);
+ /* Setup synchronous commit according to the user's wishes */
+ SetConfigOption("synchronous_commit",
+ MySubscription->synccommit ? "local" : "off",
+ PGC_BACKEND, PGC_S_OVERRIDE);
+
if (!MySubscription->enabled)
{
ereport(LOG,
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index b3d95d7..14cfa69 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -3653,6 +3653,7 @@ getSubscriptions(Archive *fout)
int i_subname;
int i_rolname;
int i_subenabled;
+ int i_subsynccommit;
int i_subconninfo;
int i_subslotname;
int i_subpublications;
@@ -3670,7 +3671,8 @@ getSubscriptions(Archive *fout)
appendPQExpBuffer(query,
"SELECT s.tableoid, s.oid, s.subname,"
"(%s s.subowner) AS rolname, s.subenabled, "
- " s.subconninfo, s.subslotname, s.subpublications "
+ " s.subsynccommit, s.subconninfo, s.subslotname, "
+ " s.subpublications "
"FROM pg_catalog.pg_subscription s "
"WHERE s.subdbid = (SELECT oid FROM pg_catalog.pg_database"
" WHERE datname = current_database())",
@@ -3684,6 +3686,7 @@ getSubscriptions(Archive *fout)
i_subname = PQfnumber(res, "subname");
i_rolname = PQfnumber(res, "rolname");
i_subenabled = PQfnumber(res, "subenabled");
+ i_subsynccommit = PQfnumber(res, "subsynccommit");
i_subconninfo = PQfnumber(res, "subconninfo");
i_subslotname = PQfnumber(res, "subslotname");
i_subpublications = PQfnumber(res, "subpublications");
@@ -3701,6 +3704,8 @@ getSubscriptions(Archive *fout)
subinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
subinfo[i].subenabled =
(strcmp(PQgetvalue(res, i, i_subenabled), "t") == 0);
+ subinfo[i].subsynccommit =
+ (strcmp(PQgetvalue(res, i, i_subsynccommit), "t") == 0);
subinfo[i].subconninfo = pg_strdup(PQgetvalue(res, i, i_subconninfo));
subinfo[i].subslotname = pg_strdup(PQgetvalue(res, i, i_subslotname));
subinfo[i].subpublications =
@@ -3770,6 +3775,11 @@ dumpSubscription(Archive *fout, SubscriptionInfo *subinfo)
else
appendPQExpBufferStr(query, "DISABLED");
+ if (subinfo->subsynccommit)
+ appendPQExpBufferStr(query, ", SYNCHRONOUS_COMMIT = true");
+ else
+ appendPQExpBufferStr(query, ", SYNCHRONOUS_COMMIT = false");
+
appendPQExpBufferStr(query, ", SLOT NAME = ");
appendStringLiteralAH(query, subinfo->subslotname, fout);
diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h
index a466527..5934eb0 100644
--- a/src/bin/pg_dump/pg_dump.h
+++ b/src/bin/pg_dump/pg_dump.h
@@ -604,6 +604,7 @@ typedef struct _SubscriptionInfo
DumpableObject dobj;
char *rolname;
bool subenabled;
+ bool subsynccommit;
char *subconninfo;
char *subslotname;
char *subpublications;
diff --git a/src/bin/pg_dump/t/002_pg_dump.pl b/src/bin/pg_dump/t/002_pg_dump.pl
index 3667374..4423982 100644
--- a/src/bin/pg_dump/t/002_pg_dump.pl
+++ b/src/bin/pg_dump/t/002_pg_dump.pl
@@ -4226,7 +4226,7 @@ qr/CREATE TRANSFORM FOR integer LANGUAGE sql \(FROM SQL WITH FUNCTION pg_catalog
CONNECTION \'dbname=doesnotexist\' PUBLICATION pub1
WITH (DISABLED, NOCONNECT);',
regexp => qr/^
- \QCREATE SUBSCRIPTION sub1 CONNECTION 'dbname=doesnotexist' PUBLICATION pub1 WITH (DISABLED, SLOT NAME = 'sub1');\E
+ \QCREATE SUBSCRIPTION sub1 CONNECTION 'dbname=doesnotexist' PUBLICATION pub1 WITH (DISABLED, SYNCHRONOUS_COMMIT = false, SLOT NAME = 'sub1');\E
/xm,
like => {
binary_upgrade => 1,
diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index 8c58312..d3c51f4 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -5120,7 +5120,8 @@ describeSubscriptions(const char *pattern, bool verbose)
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)
{
@@ -5146,7 +5147,9 @@ describeSubscriptions(const char *pattern, bool verbose)
if (verbose)
{
appendPQExpBuffer(&buf,
+ ", subsynccommit AS \"%s\"\n"
", subconninfo AS \"%s\"\n",
+ gettext_noop("Synchronous Commit"),
gettext_noop("Conninfo"));
}
diff --git a/src/include/catalog/pg_subscription.h b/src/include/catalog/pg_subscription.h
index 0811880..62845e9 100644
--- a/src/include/catalog/pg_subscription.h
+++ b/src/include/catalog/pg_subscription.h
@@ -39,6 +39,7 @@ CATALOG(pg_subscription,6100) BKI_SHARED_RELATION BKI_ROWTYPE_OID(6101) BKI_SCHE
bool subenabled; /* True if the subscription is enabled
* (the worker should be running) */
+ bool subsynccommit; /* Should apply use synchronous commit? */
#ifdef CATALOG_VARLEN /* variable-length fields start here */
text subconninfo; /* Connection string to the publisher */
@@ -54,14 +55,15 @@ typedef FormData_pg_subscription *Form_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 5
+#define Anum_pg_subscription_subconninfo 6
+#define Anum_pg_subscription_subslotname 7
+#define Anum_pg_subscription_subpublications 8
typedef struct Subscription
@@ -71,6 +73,7 @@ typedef struct Subscription
char *name; /* Name of the subscription */
Oid owner; /* Oid of the subscription owner */
bool enabled; /* Indicates if the subscription is enabled */
+ bool synccommit; /* Indicates if apply should use synchronous commit */
char *conninfo; /* Connection string to the publisher */
char *slotname; /* Name of the replication slot */
List *publications; /* List of publication names to subscribe to */
diff --git a/src/test/regress/expected/subscription.out b/src/test/regress/expected/subscription.out
index 0912bef..44229ec 100644
--- a/src/test/regress/expected/subscription.out
+++ b/src/test/regress/expected/subscription.out
@@ -25,19 +25,19 @@ ERROR: invalid connection string syntax: missing "=" after "testconn" in connec
CREATE SUBSCRIPTION testsub CONNECTION 'dbname=doesnotexist' PUBLICATION testpub WITH (NOCONNECT);
WARNING: tables were not subscribed, you will have to run ALTER SUBSCRIPTION ... REFRESH PUBLICATION to subscribe the tables
\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} | f | dbname=doesnotexist
(1 row)
ALTER SUBSCRIPTION testsub SET PUBLICATION testpub2, testpub3 NOREFRESH;
ALTER SUBSCRIPTION testsub CONNECTION 'dbname=doesnotexist2';
\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} | f | dbname=doesnotexist2
(1 row)
BEGIN;
@@ -64,11 +64,12 @@ ALTER SUBSCRIPTION testsub RENAME TO testsub_dummy;
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 = true);
+\dRs+
+ List of subscriptions
+ Name | Owner | Enabled | Publication | Synchronous Commit | Conninfo
+-------------+---------------------------+---------+---------------------+--------------------+----------------------
+ testsub_foo | regress_subscription_user | f | {testpub2,testpub3} | t | dbname=doesnotexist2
(1 row)
-- rename back to keep the rest simple
diff --git a/src/test/regress/sql/subscription.sql b/src/test/regress/sql/subscription.sql
index c1199ee..84f6f36 100644
--- a/src/test/regress/sql/subscription.sql
+++ b/src/test/regress/sql/subscription.sql
@@ -45,8 +45,9 @@ ALTER SUBSCRIPTION testsub RENAME TO testsub_dummy;
RESET ROLE;
ALTER SUBSCRIPTION testsub RENAME TO testsub_foo;
+ALTER SUBSCRIPTION testsub_foo WITH (SYNCHRONOUS_COMMIT = true);
-\dRs
+\dRs+
-- rename back to keep the rest simple
ALTER SUBSCRIPTION testsub_foo RENAME TO testsub;
--
2.7.4
--------------4DC3589FBD93B4ED6512EA49
Content-Type: text/plain
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
MIME-Version: 1.0
--
Sent via pgsql-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers
--------------4DC3589FBD93B4ED6512EA49--
view thread (65+ messages) latest in thread
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected]
Subject: Re: [PATCH] Add option to modify sync commit per subscription
In-Reply-To: <no-message-id-538494@localhost>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox