mirror of
https://github.com/postgres/postgres.git
synced 2025-09-02 04:21:28 +03:00
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
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
#include "catalog/partition.h"
|
||||
#include "catalog/pg_publication.h"
|
||||
#include "catalog/pg_publication_rel.h"
|
||||
#include "catalog/pg_subscription.h"
|
||||
#include "commands/defrem.h"
|
||||
#include "executor/executor.h"
|
||||
#include "fmgr.h"
|
||||
@@ -79,6 +80,7 @@ static void pgoutput_stream_prepare_txn(LogicalDecodingContext *ctx,
|
||||
|
||||
static bool publications_valid;
|
||||
static bool in_streaming;
|
||||
static bool publish_no_origin;
|
||||
|
||||
static List *LoadPublications(List *pubnames);
|
||||
static void publication_invalidation_cb(Datum arg, int cacheid,
|
||||
@@ -285,6 +287,7 @@ parse_output_parameters(List *options, PGOutputData *data)
|
||||
bool messages_option_given = false;
|
||||
bool streaming_given = false;
|
||||
bool two_phase_option_given = false;
|
||||
bool origin_option_given = false;
|
||||
|
||||
data->binary = false;
|
||||
data->streaming = false;
|
||||
@@ -378,6 +381,24 @@ parse_output_parameters(List *options, PGOutputData *data)
|
||||
|
||||
data->two_phase = defGetBoolean(defel);
|
||||
}
|
||||
else if (strcmp(defel->defname, "origin") == 0)
|
||||
{
|
||||
if (origin_option_given)
|
||||
ereport(ERROR,
|
||||
errcode(ERRCODE_SYNTAX_ERROR),
|
||||
errmsg("conflicting or redundant options"));
|
||||
origin_option_given = true;
|
||||
|
||||
data->origin = defGetString(defel);
|
||||
if (pg_strcasecmp(data->origin, LOGICALREP_ORIGIN_NONE) == 0)
|
||||
publish_no_origin = true;
|
||||
else if (pg_strcasecmp(data->origin, LOGICALREP_ORIGIN_ANY) == 0)
|
||||
publish_no_origin = false;
|
||||
else
|
||||
ereport(ERROR,
|
||||
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
||||
errmsg("unrecognized origin value: \"%s\"", data->origin));
|
||||
}
|
||||
else
|
||||
elog(ERROR, "unrecognized pgoutput option: %s", defel->defname);
|
||||
}
|
||||
@@ -1696,12 +1717,16 @@ pgoutput_message(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
|
||||
}
|
||||
|
||||
/*
|
||||
* Currently we always forward.
|
||||
* Return true if the data is associated with an origin and the user has
|
||||
* requested the changes that don't have an origin, false otherwise.
|
||||
*/
|
||||
static bool
|
||||
pgoutput_origin_filter(LogicalDecodingContext *ctx,
|
||||
RepOriginId origin_id)
|
||||
{
|
||||
if (publish_no_origin && origin_id != InvalidRepOriginId)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user