Skip to content

Commit 2e00d59

Browse files
committed
Fix negative bitmapset member not allowed error in logical replication
This happens when we add a replica identity column on a subscriber that does not yet exist on the publisher, according to the mapping maintained by the subscriber. Code that checks whether the target relation on the subscriber is updatable would check the replica identity attribute bitmap with a column number -1, which would result in an error. To fix, skip such columns in the bitmap lookup and consider the relation not updatable. The result is consistent with the rule that the replica identity columns on the subscriber must be a subset of those on the publisher, since if the column doesn't exist on the publisher, the column set on the subscriber can't be a subset. Reported-by: Tim Clarke <tim.clarke@minerva.info> Analyzed-by: Jehan-Guillaume de Rorthais <jgdr@dalibo.com> Discussion: https://www.postgresql.org/message-id/flat/a9139c29-7ddd-973b-aa7f-71fed9c38d75%40minerva.info
1 parent 4cc66a2 commit 2e00d59

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/backend/replication/logical/relation.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
339339

340340
attnum = AttrNumberGetAttrOffset(attnum);
341341

342-
if (!bms_is_member(entry->attrmap[attnum], remoterel->attkeys))
342+
if (entry->attrmap[attnum] < 0 ||
343+
!bms_is_member(entry->attrmap[attnum], remoterel->attkeys))
343344
{
344345
entry->updatable = false;
345346
break;

src/test/subscription/t/008_diff_schema.pl

Lines changed: 35 additions & 2 deletions
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 => 4;
6+
use Test::More tests => 5;
77

88
sub wait_for_caught_up
99
{
@@ -35,7 +35,7 @@ sub wait_for_caught_up
3535

3636
# Setup logical replication
3737
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
38-
$node_publisher->safe_psql('postgres', "CREATE PUBLICATION tap_pub FOR TABLE test_tab");
38+
$node_publisher->safe_psql('postgres', "CREATE PUBLICATION tap_pub FOR ALL TABLES");
3939

4040
my $appname = 'tap_sub';
4141
$node_subscriber->safe_psql('postgres',
@@ -86,5 +86,38 @@ sub wait_for_caught_up
8686
$node_subscriber->safe_psql('postgres', "SELECT count(*), count(c), count(d = 999), count(e) FROM test_tab");
8787
is($result, qq(3|3|3|3), 'check extra columns contain local defaults after apply');
8888

89+
90+
# Check a bug about adding a replica identity column on the subscriber
91+
# that was not yet mapped to a column on the publisher. This would
92+
# result in errors on the subscriber and replication thus not
93+
# progressing.
94+
# (https://www.postgresql.org/message-id/flat/a9139c29-7ddd-973b-aa7f-71fed9c38d75%40minerva.info)
95+
96+
$node_publisher->safe_psql('postgres',
97+
"CREATE TABLE test_tab2 (a int)");
98+
99+
$node_subscriber->safe_psql('postgres',
100+
"CREATE TABLE test_tab2 (a int)");
101+
102+
$node_subscriber->safe_psql('postgres',
103+
"ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION");
104+
105+
# Add replica identity column. (The serial is not necessary, but it's
106+
# a convenient way to get a default on the new column so that rows
107+
# from the publisher that don't have the column yet can be inserted.)
108+
$node_subscriber->safe_psql('postgres',
109+
"ALTER TABLE test_tab2 ADD COLUMN b serial PRIMARY KEY");
110+
111+
$node_publisher->safe_psql('postgres',
112+
"INSERT INTO test_tab2 VALUES (1)");
113+
114+
wait_for_caught_up($node_publisher, $appname);
115+
116+
is($node_subscriber->safe_psql('postgres',
117+
"SELECT count(*), min(a), max(a) FROM test_tab2"),
118+
qq(1|1|1),
119+
'check replicated inserts on subscriber');
120+
121+
89122
$node_subscriber->stop;
90123
$node_publisher->stop;

0 commit comments

Comments
 (0)