Skip to content

Commit bc1adc6

Browse files
committed
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: Chad Trabant <chad@iris.washington.edu> Reviewed-by: Petr Jelinek <petr.jelinek@2ndquadrant.com>
1 parent eec1a8c commit bc1adc6

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

src/backend/catalog/pg_publication.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ is_publishable_class(Oid relid, Form_pg_class reltuple)
105105
relid >= FirstNormalObjectId;
106106
}
107107

108+
/*
109+
* Another variant of this, taking a Relation.
110+
*/
111+
bool
112+
is_publishable_relation(Relation rel)
113+
{
114+
return is_publishable_class(RelationGetRelid(rel), rel->rd_rel);
115+
}
116+
108117

109118
/*
110119
* SQL-callable variant of the above

src/backend/replication/pgoutput/pgoutput.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ pgoutput_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
262262
MemoryContext old;
263263
RelationSyncEntry *relentry;
264264

265+
if (!is_publishable_relation(relation))
266+
return;
267+
265268
relentry = get_rel_sync_entry(data, RelationGetRelid(relation));
266269

267270
/* First check the table filter */

src/include/catalog/pg_publication.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ extern List *GetPublicationRelations(Oid pubid);
9393
extern List *GetAllTablesPublications(void);
9494
extern List *GetAllTablesPublicationRelations(void);
9595

96+
extern bool is_publishable_relation(Relation rel);
9697
extern ObjectAddress publication_add_relation(Oid pubid, Relation targetrel,
9798
bool if_not_exists);
9899

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Test materialized views behavior
2+
use strict;
3+
use warnings;
4+
use PostgresNode;
5+
use TestLib;
6+
use Test::More tests => 1;
7+
8+
my $node_publisher = get_new_node('publisher');
9+
$node_publisher->init(allows_streaming => 'logical');
10+
$node_publisher->start;
11+
12+
my $node_subscriber = get_new_node('subscriber');
13+
$node_subscriber->init(allows_streaming => 'logical');
14+
$node_subscriber->start;
15+
16+
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
17+
my $appname = 'replication_test';
18+
19+
$node_publisher->safe_psql('postgres',
20+
"CREATE PUBLICATION mypub FOR ALL TABLES;");
21+
$node_subscriber->safe_psql('postgres',
22+
"CREATE SUBSCRIPTION mysub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION mypub;"
23+
);
24+
25+
$node_publisher->safe_psql('postgres', q{CREATE TABLE test1 (a int PRIMARY KEY, b text)});
26+
$node_publisher->safe_psql('postgres', q{INSERT INTO test1 (a, b) VALUES (1, 'one'), (2, 'two');});
27+
28+
$node_subscriber->safe_psql('postgres', q{CREATE TABLE test1 (a int PRIMARY KEY, b text);});
29+
30+
$node_publisher->wait_for_catchup($appname);
31+
32+
# Materialized views are not supported by logical replication, but
33+
# logical decoding does produce change information for them, so we
34+
# need to make sure they are properly ignored. (bug #15044)
35+
36+
# create a MV with some data
37+
$node_publisher->safe_psql('postgres', q{CREATE MATERIALIZED VIEW testmv1 AS SELECT * FROM test1;});
38+
$node_publisher->wait_for_catchup($appname);
39+
# There is no equivalent relation on the subscriber, but MV data is
40+
# not replicated, so this does not hang.
41+
42+
pass "materialized view data not replicated";
43+
44+
$node_subscriber->stop;
45+
$node_publisher->stop;

0 commit comments

Comments
 (0)