Skip to content

Commit 6f1b9aa

Browse files
committed
Fix logical replication between different encodings
When sending a tuple attribute, the previous coding erroneously sent the length byte before encoding conversion, which would lead to protocol failures on the receiving side if the length did not match the following string. To fix that, use pq_sendcountedtext() for sending tuple attributes, which takes care of all of that internally. To match the API of pq_sendcountedtext(), send even text values without a trailing zero byte and have the receiving end put it in place instead. This matches how the standard FE/BE protocol behaves. Reported-by: Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp>
1 parent 5f21f52 commit 6f1b9aa

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

doc/src/sgml/protocol.sgml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6107,11 +6107,14 @@ TupleData
61076107
</varlistentry>
61086108
<varlistentry>
61096109
<term>
6110-
String
6110+
Byte<replaceable>n</replaceable>
61116111
</term>
61126112
<listitem>
61136113
<para>
6114-
The text value.
6114+
The value of the column, in text format. (A future release
6115+
might support additional formats.)
6116+
<replaceable>n</replaceable> is the above length.
6117+
61156118
</para>
61166119
</listitem>
61176120
</varlistentry>

src/backend/replication/logical/proto.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,6 @@ logicalrep_write_tuple(StringInfo out, Relation rel, HeapTuple tuple)
417417
Form_pg_type typclass;
418418
Form_pg_attribute att = desc->attrs[i];
419419
char *outputstr;
420-
int len;
421420

422421
/* skip dropped columns */
423422
if (att->attisdropped)
@@ -442,10 +441,7 @@ logicalrep_write_tuple(StringInfo out, Relation rel, HeapTuple tuple)
442441
pq_sendbyte(out, 't'); /* 'text' data follows */
443442

444443
outputstr = OidOutputFunctionCall(typclass->typoutput, values[i]);
445-
len = strlen(outputstr) + 1; /* null terminated */
446-
pq_sendint(out, len, 4); /* length */
447-
pq_sendstring(out, outputstr); /* data */
448-
444+
pq_sendcountedtext(out, outputstr, strlen(outputstr), false);
449445
pfree(outputstr);
450446

451447
ReleaseSysCache(typtup);
@@ -492,7 +488,9 @@ logicalrep_read_tuple(StringInfo in, LogicalRepTupleData *tuple)
492488
len = pq_getmsgint(in, 4); /* read length */
493489

494490
/* and data */
495-
tuple->values[i] = (char *) pq_getmsgbytes(in, len);
491+
tuple->values[i] = palloc(len + 1);
492+
pq_copymsgbytes(in, tuple->values[i], len);
493+
tuple->values[i][len] = '\0';
496494
}
497495
break;
498496
default:
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Test replication between databases with different encodings
2+
use strict;
3+
use warnings;
4+
use PostgresNode;
5+
use TestLib;
6+
use Test::More tests => 1;
7+
8+
sub wait_for_caught_up
9+
{
10+
my ($node, $appname) = @_;
11+
12+
$node->poll_query_until('postgres',
13+
"SELECT pg_current_wal_location() <= replay_location FROM pg_stat_replication WHERE application_name = '$appname';")
14+
or die "Timed out while waiting for subscriber to catch up";
15+
}
16+
17+
my $node_publisher = get_new_node('publisher');
18+
$node_publisher->init(allows_streaming => 'logical', extra => ['--locale=C', '--encoding=UTF8']);
19+
$node_publisher->start;
20+
21+
my $node_subscriber = get_new_node('subscriber');
22+
$node_subscriber->init(allows_streaming => 'logical', extra => ['--locale=C', '--encoding=LATIN1']);
23+
$node_subscriber->start;
24+
25+
my $ddl = "CREATE TABLE test1 (a int, b text);";
26+
$node_publisher->safe_psql('postgres', $ddl);
27+
$node_subscriber->safe_psql('postgres', $ddl);
28+
29+
my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
30+
my $appname = 'encoding_test';
31+
32+
$node_publisher->safe_psql('postgres', "CREATE PUBLICATION mypub FOR ALL TABLES;");
33+
$node_subscriber->safe_psql('postgres', "CREATE SUBSCRIPTION mysub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION mypub;");
34+
35+
wait_for_caught_up($node_publisher, $appname);
36+
37+
$node_publisher->safe_psql('postgres', q{INSERT INTO test1 VALUES (1, E'Mot\xc3\xb6rhead')}); # hand-rolled UTF-8
38+
39+
wait_for_caught_up($node_publisher, $appname);
40+
41+
is($node_subscriber->safe_psql('postgres', q{SELECT a FROM test1 WHERE b = E'Mot\xf6rhead'}), # LATIN1
42+
qq(1),
43+
'data replicated to subscriber');
44+
45+
$node_subscriber->stop;
46+
$node_publisher->stop;

0 commit comments

Comments
 (0)