diff options
| author | Amit Kapila <akapila@postgresql.org> | 2022-07-21 08:47:38 +0530 |
|---|---|---|
| committer | Amit Kapila <akapila@postgresql.org> | 2022-07-21 08:47:38 +0530 |
| commit | 366283961ac0ed6d89014444c6090f3fd02fce0a (patch) | |
| tree | 368e2dbce52b4002a09c801810837d69e0638bc7 /src/bin | |
| parent | f2d0c7f18b0632a93d99f373edc3d8109faffbe2 (diff) | |
Allow users to skip logical replication of data having origin.
This patch adds a new SUBSCRIPTION parameter "origin". It specifies
whether the subscription will request the publisher to only send changes
that don't have an origin or send changes regardless of origin. Setting it
to "none" means that the subscription will request the publisher to only
send changes that have no origin associated. Setting it to "any" means
that the publisher sends changes regardless of their origin. The default
is "any".
Usage:
CREATE SUBSCRIPTION sub1 CONNECTION 'dbname=postgres port=9999'
PUBLICATION pub1 WITH (origin = none);
This can be used to avoid loops (infinite replication of the same data)
among replication nodes.
This feature allows filtering only the replication data originating from
WAL but for initial sync (initial copy of table data) we don't have such a
facility as we can only distinguish the data based on origin from WAL. As
a follow-up patch, we are planning to forbid the initial sync if the
origin is specified as none and we notice that the publication tables were
also replicated from other publishers to avoid duplicate data or loops.
We forbid to allow creating origin with names 'none' and 'any' to avoid
confusion with the same name options.
Author: Vignesh C, Amit Kapila
Reviewed-By: Peter Smith, Amit Kapila, Dilip Kumar, Shi yu, Ashutosh Bapat, Hayato Kuroda
Discussion: https://postgr.es/m/CALDaNm0gwjY_4HFxvvty01BOT01q_fJLKQ3pWP9=9orqubhjcQ@mail.gmail.com
Diffstat (limited to 'src/bin')
| -rw-r--r-- | src/bin/pg_dump/pg_dump.c | 15 | ||||
| -rw-r--r-- | src/bin/pg_dump/pg_dump.h | 1 | ||||
| -rw-r--r-- | src/bin/pg_dump/t/002_pg_dump.pl | 22 | ||||
| -rw-r--r-- | src/bin/psql/describe.c | 7 | ||||
| -rw-r--r-- | src/bin/psql/tab-complete.c | 7 |
5 files changed, 46 insertions, 6 deletions
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index e4fdb6b75b0..f9c51d1e679 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -4412,6 +4412,7 @@ getSubscriptions(Archive *fout) int i_substream; int i_subtwophasestate; int i_subdisableonerr; + int i_suborigin; int i_subconninfo; int i_subslotname; int i_subsynccommit; @@ -4461,13 +4462,18 @@ getSubscriptions(Archive *fout) if (fout->remoteVersion >= 150000) appendPQExpBufferStr(query, " s.subtwophasestate,\n" - " s.subdisableonerr\n"); + " s.subdisableonerr,\n"); else appendPQExpBuffer(query, " '%c' AS subtwophasestate,\n" - " false AS subdisableonerr\n", + " false AS subdisableonerr,\n", LOGICALREP_TWOPHASE_STATE_DISABLED); + if (fout->remoteVersion >= 160000) + appendPQExpBufferStr(query, " s.suborigin\n"); + else + appendPQExpBuffer(query, " '%s' AS suborigin\n", LOGICALREP_ORIGIN_ANY); + appendPQExpBufferStr(query, "FROM pg_subscription s\n" "WHERE s.subdbid = (SELECT oid FROM pg_database\n" @@ -4493,6 +4499,7 @@ getSubscriptions(Archive *fout) i_substream = PQfnumber(res, "substream"); i_subtwophasestate = PQfnumber(res, "subtwophasestate"); i_subdisableonerr = PQfnumber(res, "subdisableonerr"); + i_suborigin = PQfnumber(res, "suborigin"); subinfo = pg_malloc(ntups * sizeof(SubscriptionInfo)); @@ -4522,6 +4529,7 @@ getSubscriptions(Archive *fout) pg_strdup(PQgetvalue(res, i, i_subtwophasestate)); subinfo[i].subdisableonerr = pg_strdup(PQgetvalue(res, i, i_subdisableonerr)); + subinfo[i].suborigin = pg_strdup(PQgetvalue(res, i, i_suborigin)); /* Decide whether we want to dump it */ selectDumpableObject(&(subinfo[i].dobj), fout); @@ -4595,6 +4603,9 @@ dumpSubscription(Archive *fout, const SubscriptionInfo *subinfo) if (strcmp(subinfo->subdisableonerr, "t") == 0) appendPQExpBufferStr(query, ", disable_on_error = true"); + if (pg_strcasecmp(subinfo->suborigin, LOGICALREP_ORIGIN_ANY) != 0) + appendPQExpBuffer(query, ", origin = %s", subinfo->suborigin); + if (strcmp(subinfo->subsynccommit, "off") != 0) appendPQExpBuffer(query, ", synchronous_commit = %s", fmtId(subinfo->subsynccommit)); diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h index 1d21c2906f1..69ee939d449 100644 --- a/src/bin/pg_dump/pg_dump.h +++ b/src/bin/pg_dump/pg_dump.h @@ -659,6 +659,7 @@ typedef struct _SubscriptionInfo char *substream; char *subtwophasestate; char *subdisableonerr; + char *suborigin; char *subsynccommit; char *subpublications; } SubscriptionInfo; diff --git a/src/bin/pg_dump/t/002_pg_dump.pl b/src/bin/pg_dump/t/002_pg_dump.pl index 1f08716f690..b10e1c4c0d4 100644 --- a/src/bin/pg_dump/t/002_pg_dump.pl +++ b/src/bin/pg_dump/t/002_pg_dump.pl @@ -2465,6 +2465,28 @@ my %tests = ( like => { %full_runs, section_post_data => 1, }, }, + 'CREATE SUBSCRIPTION sub2' => { + create_order => 50, + create_sql => 'CREATE SUBSCRIPTION sub2 + CONNECTION \'dbname=doesnotexist\' PUBLICATION pub1 + WITH (connect = false, origin = none);', + regexp => qr/^ + \QCREATE SUBSCRIPTION sub2 CONNECTION 'dbname=doesnotexist' PUBLICATION pub1 WITH (connect = false, slot_name = 'sub2', origin = none);\E + /xm, + like => { %full_runs, section_post_data => 1, }, + }, + + 'CREATE SUBSCRIPTION sub3' => { + create_order => 50, + create_sql => 'CREATE SUBSCRIPTION sub3 + CONNECTION \'dbname=doesnotexist\' PUBLICATION pub1 + WITH (connect = false, origin = any);', + regexp => qr/^ + \QCREATE SUBSCRIPTION sub3 CONNECTION 'dbname=doesnotexist' PUBLICATION pub1 WITH (connect = false, slot_name = 'sub3');\E + /xm, + like => { %full_runs, section_post_data => 1, }, + }, + 'ALTER PUBLICATION pub1 ADD TABLE test_table' => { create_order => 51, create_sql => diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c index 1938e1d6ec8..327a69487bb 100644 --- a/src/bin/psql/describe.c +++ b/src/bin/psql/describe.c @@ -6469,7 +6469,7 @@ describeSubscriptions(const char *pattern, bool verbose) PGresult *res; printQueryOpt myopt = pset.popt; static const bool translate_columns[] = {false, false, false, false, - false, false, false, false, false, false, false}; + false, false, false, false, false, false, false, false}; if (pset.sversion < 100000) { @@ -6511,6 +6511,11 @@ describeSubscriptions(const char *pattern, bool verbose) gettext_noop("Two-phase commit"), gettext_noop("Disable on error")); + if (pset.sversion >= 160000) + appendPQExpBuffer(&buf, + ", suborigin AS \"%s\"\n", + gettext_noop("Origin")); + appendPQExpBuffer(&buf, ", subsynccommit AS \"%s\"\n" ", subconninfo AS \"%s\"\n", diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index e572f585ef8..92207d2e160 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -1873,7 +1873,8 @@ psql_completion(const char *text, int start, int end) COMPLETE_WITH("(", "PUBLICATION"); /* ALTER SUBSCRIPTION <name> SET ( */ else if (HeadMatches("ALTER", "SUBSCRIPTION", MatchAny) && TailMatches("SET", "(")) - COMPLETE_WITH("binary", "disable_on_error", "slot_name", "streaming", "synchronous_commit"); + COMPLETE_WITH("binary", "disable_on_error", "origin", "slot_name", + "streaming", "synchronous_commit"); /* ALTER SUBSCRIPTION <name> SKIP ( */ else if (HeadMatches("ALTER", "SUBSCRIPTION", MatchAny) && TailMatches("SKIP", "(")) COMPLETE_WITH("lsn"); @@ -3152,8 +3153,8 @@ psql_completion(const char *text, int start, int end) /* Complete "CREATE SUBSCRIPTION <name> ... WITH ( <opt>" */ else if (HeadMatches("CREATE", "SUBSCRIPTION") && TailMatches("WITH", "(")) COMPLETE_WITH("binary", "connect", "copy_data", "create_slot", - "disable_on_error", "enabled", "slot_name", "streaming", - "synchronous_commit", "two_phase"); + "disable_on_error", "enabled", "origin", "slot_name", + "streaming", "synchronous_commit", "two_phase"); /* CREATE TRIGGER --- is allowed inside CREATE SCHEMA, so use TailMatches */ |
