Skip to content

Commit 1d37430

Browse files
committed
Fix walsender error cleanup code
In commit 850196b I (Álvaro) failed to handle the case of walsender shutting down on an error before setting up its 'xlogreader' pointer; the error handling code dereferences the pointer, causing a crash. Fix by testing the pointer before trying to dereference it. Kyotaro authored the code fix; I adopted Nathan's test case to be used by the TAP tests and added the necessary PostgresNode change. Reported-by: Nathan Bossart <bossartn@amazon.com> Author: Kyotaro Horiguchi <horikyota.ntt@gmail.com> Author: Álvaro Herrera <alvherre@alvh.no-ip.org> Discussion: https://postgr.es/m/C04FC24E-903D-4423-B312-6910E4D846E5@amazon.com
1 parent 14a9101 commit 1d37430

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/backend/replication/walsender.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ WalSndErrorCleanup(void)
315315
ConditionVariableCancelSleep();
316316
pgstat_report_wait_end();
317317

318-
if (xlogreader->seg.ws_file >= 0)
318+
if (xlogreader != NULL && xlogreader->seg.ws_file >= 0)
319319
wal_segment_close(xlogreader);
320320

321321
if (MyReplicationSlot != NULL)

src/test/perl/PostgresNode.pm

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,12 @@ the B<timed_out> parameter is also given.
13861386
If B<timeout> is set and this parameter is given, the scalar it references
13871387
is set to true if the psql call times out.
13881388
1389+
=item replication => B<value>
1390+
1391+
If set, add B<replication=value> to the conninfo string.
1392+
Passing the literal value C<database> results in a logical replication
1393+
connection.
1394+
13891395
=item extra_params => ['--single-transaction']
13901396
13911397
If given, it must be an array reference containing additional parameters to B<psql>.
@@ -1414,10 +1420,17 @@ sub psql
14141420

14151421
my $stdout = $params{stdout};
14161422
my $stderr = $params{stderr};
1423+
my $replication = $params{replication};
14171424
my $timeout = undef;
14181425
my $timeout_exception = 'psql timed out';
1419-
my @psql_params =
1420-
('psql', '-XAtq', '-d', $self->connstr($dbname), '-f', '-');
1426+
my @psql_params = (
1427+
'psql',
1428+
'-XAtq',
1429+
'-d',
1430+
$self->connstr($dbname)
1431+
. (defined $replication ? " replication=$replication" : ""),
1432+
'-f',
1433+
'-');
14211434

14221435
# If the caller wants an array and hasn't passed stdout/stderr
14231436
# references, allocate temporary ones to capture them so we

src/test/recovery/t/006_logical_decoding.pl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use warnings;
88
use PostgresNode;
99
use TestLib;
10-
use Test::More tests => 12;
10+
use Test::More tests => 13;
1111
use Config;
1212

1313
# Initialize master node
@@ -27,12 +27,21 @@
2727
qq[SELECT pg_create_logical_replication_slot('test_slot', 'test_decoding');]
2828
);
2929

30+
# Cover walsender error shutdown code
31+
my ($result, $stdout, $stderr) = $node_master->psql(
32+
'template1',
33+
qq[START_REPLICATION SLOT test_slot LOGICAL 0/0],
34+
replication => 'database');
35+
ok( $stderr =~
36+
m/replication slot "test_slot" was not created in this database/,
37+
"Logical decoding correctly fails to start");
38+
3039
$node_master->safe_psql('postgres',
3140
qq[INSERT INTO decoding_test(x,y) SELECT s, s::text FROM generate_series(1,10) s;]
3241
);
3342

3443
# Basic decoding works
35-
my ($result) = $node_master->safe_psql('postgres',
44+
$result = $node_master->safe_psql('postgres',
3645
qq[SELECT pg_logical_slot_get_changes('test_slot', NULL, NULL);]);
3746
is(scalar(my @foobar = split /^/m, $result),
3847
12, 'Decoding produced 12 rows inc BEGIN/COMMIT');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy