Skip to content

Commit 428fadb

Browse files
committed
Move SQL tests of pg_stat_io for WAL data to recovery test 029_stats_restart
Three tests in the main regression test suite are proving to not be portable across multiple runs on a deployed cluster as stats of pg_stat_io are reset. Problems happen for tests on: - Writes of WAL in the init context, when creating a WAL segment. - Syncs of WAL in the init context, when creating a WAL segment. - Reads of WAL in the normal context, requiring a WAL record to be read. For a `make check`, this could rely on the checkpoint record read by the startup process when starting the cluster, something that is not going to work for a deployed node. Two of the three tests are moved to the recovery TAP test 029_stats_restart, where we already check the consistency of stats data. The test for syncs is dropped as TAP can run with fsync=off. The other two are checked with some data from a freshly-initialized cluster. Per discussion with Tom Lane, Bertrand Drouvot and Nazir Bilal Yavuz. Discussion: https://postgr.es/m/915687.1738780322@sss.pgh.pa.us
1 parent 401a695 commit 428fadb

File tree

3 files changed

+33
-42
lines changed

3 files changed

+33
-42
lines changed

src/test/recovery/t/029_stats_restart.pl

+33-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@
1818
my $connect_db = 'postgres';
1919
my $db_under_test = 'test';
2020

21+
my $sect = "startup";
22+
23+
# Check some WAL statistics after a fresh startup. The startup process
24+
# should have done WAL reads, and initialization some WAL writes.
25+
my $standalone_io_stats = io_stats('init', 'wal', 'standalone backend');
26+
my $startup_io_stats = io_stats('normal', 'wal', 'startup');
27+
cmp_ok(
28+
'0', '<',
29+
$standalone_io_stats->{writes},
30+
"$sect: increased standalone backend IO writes");
31+
cmp_ok(
32+
'0', '<',
33+
$startup_io_stats->{reads},
34+
"$sect: increased startup IO reads");
35+
2136
# create test objects
2237
$node->safe_psql($connect_db, "CREATE DATABASE $db_under_test");
2338
$node->safe_psql($db_under_test,
@@ -39,7 +54,7 @@
3954
trigger_funcrel_stat();
4055

4156
# verify stats objects exist
42-
my $sect = "initial";
57+
$sect = "initial";
4358
is(have_stats('database', $dboid, 0), 't', "$sect: db stats do exist");
4459
is(have_stats('function', $dboid, $funcoid),
4560
't', "$sect: function stats do exist");
@@ -342,3 +357,20 @@ sub wal_stats
342357

343358
return \%results;
344359
}
360+
361+
sub io_stats
362+
{
363+
my ($context, $object, $backend_type) = @_;
364+
my %results;
365+
366+
$results{writes} = $node->safe_psql(
367+
$connect_db, qq{SELECT writes FROM pg_stat_io
368+
WHERE context = '$context' AND object = '$object' AND
369+
backend_type = '$backend_type'});
370+
$results{reads} = $node->safe_psql(
371+
$connect_db, qq{SELECT reads FROM pg_stat_io
372+
WHERE context = '$context' AND object = '$object' AND
373+
backend_type = '$backend_type'});
374+
375+
return \%results;
376+
}

src/test/regress/expected/stats.out

-27
Original file line numberDiff line numberDiff line change
@@ -862,33 +862,6 @@ WHERE pg_stat_get_backend_pid(beid) = pg_backend_pid();
862862
t
863863
(1 row)
864864

865-
-- Test pg_stat_io for WAL in an init context, that should do writes
866-
-- and syncs.
867-
SELECT sum(writes) AS writes, sum(fsyncs) AS fsyncs
868-
FROM pg_stat_io
869-
WHERE context = 'init' AND object = 'wal' \gset io_sum_wal_init_
870-
SELECT :io_sum_wal_init_writes > 0;
871-
?column?
872-
----------
873-
t
874-
(1 row)
875-
876-
SELECT current_setting('fsync') = 'off'
877-
OR :io_sum_wal_init_fsyncs > 0;
878-
?column?
879-
----------
880-
t
881-
(1 row)
882-
883-
-- Test pg_stat_io for WAL in a normal context, that should do reads as well.
884-
SELECT SUM(reads) > 0
885-
FROM pg_stat_io
886-
WHERE context = 'normal' AND object = 'wal';
887-
?column?
888-
----------
889-
t
890-
(1 row)
891-
892865
-----
893866
-- Test that resetting stats works for reset timestamp
894867
-----

src/test/regress/sql/stats.sql

-14
Original file line numberDiff line numberDiff line change
@@ -442,20 +442,6 @@ SELECT (current_schemas(true))[1] = ('pg_temp_' || beid::text) AS match
442442
FROM pg_stat_get_backend_idset() beid
443443
WHERE pg_stat_get_backend_pid(beid) = pg_backend_pid();
444444

445-
-- Test pg_stat_io for WAL in an init context, that should do writes
446-
-- and syncs.
447-
SELECT sum(writes) AS writes, sum(fsyncs) AS fsyncs
448-
FROM pg_stat_io
449-
WHERE context = 'init' AND object = 'wal' \gset io_sum_wal_init_
450-
SELECT :io_sum_wal_init_writes > 0;
451-
SELECT current_setting('fsync') = 'off'
452-
OR :io_sum_wal_init_fsyncs > 0;
453-
454-
-- Test pg_stat_io for WAL in a normal context, that should do reads as well.
455-
SELECT SUM(reads) > 0
456-
FROM pg_stat_io
457-
WHERE context = 'normal' AND object = 'wal';
458-
459445
-----
460446
-- Test that resetting stats works for reset timestamp
461447
-----

0 commit comments

Comments
 (0)