Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 37fd181

Browse files
author
Amit Kapila
committed
Ignore heap rewrites for materialized views in logical replication.
If you have a publication that specifies FOR ALL TABLES clause, a REFRESH MATERIALIZED VIEW can break your setup with the following message ERROR: logical replication target relation "public.pg_temp_NNN" does not exist Commit 1a499c2 introduces a heuristic to skip table writes that look like they are from a heap rewrite for a FOR ALL TABLES publication. However, it forgot to exclude materialized views (heap rewrites occur when you execute REFRESH MATERIALIZED VIEW). Since materialized views are not supported in logical decoding, it is appropriate to filter them too. As explained in the commit 1a499c2, a more robust solution was included in version 11 (commit 325f2ec), hence only version 10 is affected. Reported-by: Euler Taveira Author: Euler Taveira Reviewed-by: Amit Kapila Discussion: https://postgr.es/m/[email protected]
1 parent c051180 commit 37fd181

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/backend/replication/pgoutput/pgoutput.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,9 @@ get_rel_sync_entry(PGOutputData *data, Oid relid)
540540
if (sscanf(relname, "pg_temp_%u%n", &u, &n) == 1 &&
541541
relname[n] == '\0')
542542
{
543-
if (get_rel_relkind(u) == RELKIND_RELATION)
543+
char relkind = get_rel_relkind(u);
544+
545+
if (relkind == RELKIND_RELATION || relkind == RELKIND_MATVIEW)
544546
break;
545547
}
546548
}

src/test/subscription/t/006_rewrite.pl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use warnings;
44
use PostgresNode;
55
use TestLib;
6-
use Test::More tests => 2;
6+
use Test::More tests => 3;
77

88
sub wait_for_caught_up
99
{
@@ -26,6 +26,13 @@ sub wait_for_caught_up
2626
$node_publisher->safe_psql('postgres', $ddl);
2727
$node_subscriber->safe_psql('postgres', $ddl);
2828

29+
$ddl = "CREATE TABLE test2 (a int, b text);";
30+
$node_publisher->safe_psql('postgres', $ddl);
31+
$node_subscriber->safe_psql('postgres', $ddl);
32+
33+
$node_publisher->safe_psql('postgres', q{INSERT INTO test2 (a, b) VALUES (10, 'ten'), (20, 'twenty');});
34+
$node_publisher->safe_psql('postgres', 'CREATE MATERIALIZED VIEW test3 AS SELECT a, b FROM test2;');
35+
2936
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
3037
my $appname = 'encoding_test';
3138

@@ -69,5 +76,16 @@ sub wait_for_caught_up
6976
3|three|33),
7077
'data replicated to subscriber');
7178

79+
# Another DDL that causes a heap rewrite
80+
$node_publisher->safe_psql('postgres', 'REFRESH MATERIALIZED VIEW test3;');
81+
82+
# an additional row to check if the REFRESH worked
83+
$node_publisher->safe_psql('postgres', q{INSERT INTO test2 (a, b) VALUES (30, 'thirty');});
84+
85+
wait_for_caught_up($node_publisher, $appname);
86+
87+
is($node_subscriber->safe_psql('postgres', q{SELECT COUNT(1) FROM test2}), 3,
88+
'data replicated to subscriber after refresh');
89+
7290
$node_subscriber->stop;
7391
$node_publisher->stop;

0 commit comments

Comments
 (0)