Commit bc1adc65 authored by Peter Eisentraut's avatar Peter Eisentraut

Fix filtering of unsupported relations in logical replication

In the pgoutput plugin, skip changes for relations that are not
publishable, per is_publishable_class().  This concerns in particular
materialized views and information_schema tables.  While those relations
cannot be part of a publication, per existing checks, they will be
considered by a FOR ALL TABLES publication.  A subscription would not
actually apply changes for those relations, again per existing checks,
but trying to match incoming changes to local tables on the subscriber
would lead to errors if no matching local table exists.  Skipping those
changes on the publisher avoids sending useless changes and eliminates
the error.

Bug: #15044
Reported-by: default avatarChad Trabant <chad@iris.washington.edu>
Reviewed-by: default avatarPetr Jelinek <petr.jelinek@2ndquadrant.com>
parent eec1a8cb
...@@ -105,6 +105,15 @@ is_publishable_class(Oid relid, Form_pg_class reltuple) ...@@ -105,6 +105,15 @@ is_publishable_class(Oid relid, Form_pg_class reltuple)
relid >= FirstNormalObjectId; relid >= FirstNormalObjectId;
} }
/*
* Another variant of this, taking a Relation.
*/
bool
is_publishable_relation(Relation rel)
{
return is_publishable_class(RelationGetRelid(rel), rel->rd_rel);
}
/* /*
* SQL-callable variant of the above * SQL-callable variant of the above
......
...@@ -262,6 +262,9 @@ pgoutput_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, ...@@ -262,6 +262,9 @@ pgoutput_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
MemoryContext old; MemoryContext old;
RelationSyncEntry *relentry; RelationSyncEntry *relentry;
if (!is_publishable_relation(relation))
return;
relentry = get_rel_sync_entry(data, RelationGetRelid(relation)); relentry = get_rel_sync_entry(data, RelationGetRelid(relation));
/* First check the table filter */ /* First check the table filter */
......
...@@ -93,6 +93,7 @@ extern List *GetPublicationRelations(Oid pubid); ...@@ -93,6 +93,7 @@ extern List *GetPublicationRelations(Oid pubid);
extern List *GetAllTablesPublications(void); extern List *GetAllTablesPublications(void);
extern List *GetAllTablesPublicationRelations(void); extern List *GetAllTablesPublicationRelations(void);
extern bool is_publishable_relation(Relation rel);
extern ObjectAddress publication_add_relation(Oid pubid, Relation targetrel, extern ObjectAddress publication_add_relation(Oid pubid, Relation targetrel,
bool if_not_exists); bool if_not_exists);
......
# Test materialized views behavior
use strict;
use warnings;
use PostgresNode;
use TestLib;
use Test::More tests => 1;
my $node_publisher = get_new_node('publisher');
$node_publisher->init(allows_streaming => 'logical');
$node_publisher->start;
my $node_subscriber = get_new_node('subscriber');
$node_subscriber->init(allows_streaming => 'logical');
$node_subscriber->start;
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
my $appname = 'replication_test';
$node_publisher->safe_psql('postgres',
"CREATE PUBLICATION mypub FOR ALL TABLES;");
$node_subscriber->safe_psql('postgres',
"CREATE SUBSCRIPTION mysub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION mypub;"
);
$node_publisher->safe_psql('postgres', q{CREATE TABLE test1 (a int PRIMARY KEY, b text)});
$node_publisher->safe_psql('postgres', q{INSERT INTO test1 (a, b) VALUES (1, 'one'), (2, 'two');});
$node_subscriber->safe_psql('postgres', q{CREATE TABLE test1 (a int PRIMARY KEY, b text);});
$node_publisher->wait_for_catchup($appname);
# Materialized views are not supported by logical replication, but
# logical decoding does produce change information for them, so we
# need to make sure they are properly ignored. (bug #15044)
# create a MV with some data
$node_publisher->safe_psql('postgres', q{CREATE MATERIALIZED VIEW testmv1 AS SELECT * FROM test1;});
$node_publisher->wait_for_catchup($appname);
# There is no equivalent relation on the subscriber, but MV data is
# not replicated, so this does not hang.
pass "materialized view data not replicated";
$node_subscriber->stop;
$node_publisher->stop;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment