Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c161ab7

Browse files
committed
Add PostgreSQL::Test::Cluster::advance_wal
This is a function that makes a node jump by N WAL segments, which is something a couple of tests have been relying on for some cases related to streaming, replication slot limits and logical decoding on standbys. Hence, this centralizes the logic, while making it cheaper by relying on pg_logical_emit_message() to emit WAL records before switching to a new segment. Author: Bharath Rupireddy Reviewed-by: Kyotaro Horiguchi, Euler Taveira Discussion: https://postgr.es/m/CALj2ACU3R8QFCvDewHCMKjgb2w_-CMCyd6DAK=Jb-af14da5eg@mail.gmail.com
1 parent bf6260b commit c161ab7

File tree

4 files changed

+42
-45
lines changed

4 files changed

+42
-45
lines changed

src/test/perl/PostgreSQL/Test/Cluster.pm

+25
Original file line numberDiff line numberDiff line change
@@ -3197,6 +3197,31 @@ sub create_logical_slot_on_standby
31973197

31983198
=pod
31993199
3200+
=item $node->advance_wal(num)
3201+
3202+
Advance WAL of node by given number of segments.
3203+
3204+
=cut
3205+
3206+
sub advance_wal
3207+
{
3208+
my ($self, $num) = @_;
3209+
3210+
# Advance by $n segments (= (wal_segment_size * $num) bytes).
3211+
# pg_switch_wal() forces a WAL flush, making pg_logical_emit_message()
3212+
# safe to use in non-transactional mode.
3213+
for (my $i = 0; $i < $num; $i++)
3214+
{
3215+
$self->safe_psql(
3216+
'postgres', qq{
3217+
SELECT pg_logical_emit_message(false, '', 'foo');
3218+
SELECT pg_switch_wal();
3219+
});
3220+
}
3221+
}
3222+
3223+
=pod
3224+
32003225
=back
32013226
32023227
=cut

src/test/recovery/t/001_stream_rep.pl

+1-5
Original file line numberDiff line numberDiff line change
@@ -522,11 +522,7 @@ sub replay_check
522522
my $segment_removed = $node_primary->safe_psql('postgres',
523523
'SELECT pg_walfile_name(pg_current_wal_lsn())');
524524
chomp($segment_removed);
525-
$node_primary->psql(
526-
'postgres', "
527-
CREATE TABLE tab_phys_slot (a int);
528-
INSERT INTO tab_phys_slot VALUES (generate_series(1,10));
529-
SELECT pg_switch_wal();");
525+
$node_primary->advance_wal(1);
530526
my $current_lsn =
531527
$node_primary->safe_psql('postgres', "SELECT pg_current_wal_lsn();");
532528
chomp($current_lsn);

src/test/recovery/t/019_replslot_limit.pl

+14-35
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
is($result, "reserved|t", 'check the catching-up state');
6060

6161
# Advance WAL by five segments (= 5MB) on primary
62-
advance_wal($node_primary, 1);
62+
$node_primary->advance_wal(1);
6363
$node_primary->safe_psql('postgres', "CHECKPOINT;");
6464

6565
# The slot is always "safe" when fitting max_wal_size
@@ -69,7 +69,7 @@
6969
is($result, "reserved|t",
7070
'check that it is safe if WAL fits in max_wal_size');
7171

72-
advance_wal($node_primary, 4);
72+
$node_primary->advance_wal(4);
7373
$node_primary->safe_psql('postgres', "CHECKPOINT;");
7474

7575
# The slot is always "safe" when max_slot_wal_keep_size is not set
@@ -100,7 +100,7 @@
100100
is($result, "reserved", 'check that max_slot_wal_keep_size is working');
101101

102102
# Advance WAL again then checkpoint, reducing remain by 2 MB.
103-
advance_wal($node_primary, 2);
103+
$node_primary->advance_wal(2);
104104
$node_primary->safe_psql('postgres', "CHECKPOINT;");
105105

106106
# The slot is still working
@@ -118,7 +118,7 @@
118118
$result = $node_primary->safe_psql('postgres',
119119
"ALTER SYSTEM SET wal_keep_size to '8MB'; SELECT pg_reload_conf();");
120120
# Advance WAL again then checkpoint, reducing remain by 6 MB.
121-
advance_wal($node_primary, 6);
121+
$node_primary->advance_wal(6);
122122
$result = $node_primary->safe_psql('postgres',
123123
"SELECT wal_status as remain FROM pg_replication_slots WHERE slot_name = 'rep1'"
124124
);
@@ -134,7 +134,7 @@
134134
$node_standby->stop;
135135

136136
# Advance WAL again without checkpoint, reducing remain by 6 MB.
137-
advance_wal($node_primary, 6);
137+
$node_primary->advance_wal(6);
138138

139139
# Slot gets into 'reserved' state
140140
$result = $node_primary->safe_psql('postgres',
@@ -145,7 +145,7 @@
145145
$node_primary->safe_psql('postgres', "CHECKPOINT;");
146146

147147
# Advance WAL again without checkpoint; remain goes to 0.
148-
advance_wal($node_primary, 1);
148+
$node_primary->advance_wal(1);
149149

150150
# Slot gets into 'unreserved' state and safe_wal_size is negative
151151
$result = $node_primary->safe_psql('postgres',
@@ -174,7 +174,7 @@
174174

175175
# Advance WAL again. The slot loses the oldest segment by the next checkpoint
176176
my $logstart = -s $node_primary->logfile;
177-
advance_wal($node_primary, 7);
177+
$node_primary->advance_wal(7);
178178

179179
# Now create another checkpoint and wait until the WARNING is issued
180180
$node_primary->safe_psql('postgres',
@@ -275,18 +275,12 @@
275275
has_streaming => 1);
276276
$node_standby->append_conf('postgresql.conf', "primary_slot_name = 'rep1'");
277277
$node_standby->start;
278-
my @result =
279-
split(
280-
'\n',
281-
$node_primary2->safe_psql(
282-
'postgres',
283-
"CREATE TABLE tt();
284-
DROP TABLE tt;
285-
SELECT pg_switch_wal();
286-
CHECKPOINT;
287-
SELECT 'finished';",
288-
timeout => $PostgreSQL::Test::Utils::timeout_default));
289-
is($result[1], 'finished', 'check if checkpoint command is not blocked');
278+
$node_primary2->advance_wal(1);
279+
$result = $node_primary2->safe_psql(
280+
'postgres',
281+
"CHECKPOINT; SELECT 'finished';",
282+
timeout => $PostgreSQL::Test::Utils::timeout_default);
283+
is($result, 'finished', 'check if checkpoint command is not blocked');
290284

291285
$node_primary2->stop;
292286
$node_standby->stop;
@@ -372,7 +366,7 @@
372366
# freeze walsender and walreceiver. Slot will still be active, but walreceiver
373367
# won't get anything anymore.
374368
kill 'STOP', $senderpid, $receiverpid;
375-
advance_wal($node_primary3, 2);
369+
$node_primary3->advance_wal(2);
376370

377371
my $msg_logged = 0;
378372
my $max_attempts = $PostgreSQL::Test::Utils::timeout_default;
@@ -418,19 +412,4 @@
418412
$node_primary3->stop;
419413
$node_standby3->stop;
420414

421-
#####################################
422-
# Advance WAL of $node by $n segments
423-
sub advance_wal
424-
{
425-
my ($node, $n) = @_;
426-
427-
# Advance by $n segments (= (wal_segment_size * $n) bytes) on primary.
428-
for (my $i = 0; $i < $n; $i++)
429-
{
430-
$node->safe_psql('postgres',
431-
"CREATE TABLE t (); DROP TABLE t; SELECT pg_switch_wal();");
432-
}
433-
return;
434-
}
435-
436415
done_testing();

src/test/recovery/t/035_standby_logical_decoding.pl

+2-5
Original file line numberDiff line numberDiff line change
@@ -524,11 +524,8 @@ sub check_for_invalidation
524524
chomp($walfile_name);
525525

526526
# Generate some activity and switch WAL file on the primary
527-
$node_primary->safe_psql(
528-
'postgres', "create table retain_test(a int);
529-
select pg_switch_wal();
530-
insert into retain_test values(1);
531-
checkpoint;");
527+
$node_primary->advance_wal(1);
528+
$node_primary->safe_psql('postgres', "checkpoint;");
532529

533530
# Wait for the standby to catch up
534531
$node_primary->wait_for_replay_catchup($node_standby);

0 commit comments

Comments
 (0)