| 1 |
|
| 2 |
# Copyright (c) 2024-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Run the standard regression tests with streaming replication |
| 5 |
use strict; |
| 6 |
use warnings FATAL => 'all'; |
| 7 |
use PostgreSQL::Test::Cluster; |
| 8 |
use PostgreSQL::Test::Utils; |
| 9 |
use Test::More; |
| 10 |
use File::Basename; |
| 11 |
|
| 12 |
# Initialize primary node |
| 13 |
my $node_primary = PostgreSQL::Test::Cluster->new('primary'); |
| 14 |
$node_primary->init(allows_streaming => 1); |
| 15 |
|
| 16 |
# Increase some settings that Cluster->new makes too low by default. |
| 17 |
$node_primary->adjust_conf('postgresql.conf', 'max_connections', '25'); |
| 18 |
$node_primary->append_conf('postgresql.conf', |
| 19 |
'max_prepared_transactions = 10'); |
| 20 |
|
| 21 |
# Enable pg_stat_statements to force tests to do query jumbling. |
| 22 |
# pg_stat_statements.max should be large enough to hold all the entries |
| 23 |
# of the regression database. |
| 24 |
$node_primary->append_conf( |
| 25 |
'postgresql.conf', |
| 26 |
qq{shared_preload_libraries = 'pg_stat_statements' |
| 27 |
pg_stat_statements.max = 50000 |
| 28 |
compute_query_id = 'regress' |
| 29 |
}); |
| 30 |
|
| 31 |
# We'll stick with Cluster->new's small default shared_buffers, but since that |
| 32 |
# makes synchronized seqscans more probable, it risks changing the results of |
| 33 |
# some test queries. Disable synchronized seqscans to prevent that. |
| 34 |
$node_primary->append_conf('postgresql.conf', 'synchronize_seqscans = off'); |
| 35 |
|
| 36 |
# WAL consistency checking is resource intensive so require opt-in with the |
| 37 |
# PG_TEST_EXTRA environment variable. |
| 38 |
if ( $ENV{PG_TEST_EXTRA} |
| 39 |
&& $ENV{PG_TEST_EXTRA} =~ m/\bwal_consistency_checking\b/) |
| 40 |
{ |
| 41 |
$node_primary->append_conf('postgresql.conf', |
| 42 |
'wal_consistency_checking = all'); |
| 43 |
} |
| 44 |
|
| 45 |
$node_primary->start; |
| 46 |
is( $node_primary->psql( |
| 47 |
'postgres', |
| 48 |
qq[SELECT pg_create_physical_replication_slot('standby_1');]), |
| 49 |
0, |
| 50 |
'physical slot created on primary'); |
| 51 |
my $backup_name = 'my_backup'; |
| 52 |
|
| 53 |
# Take backup |
| 54 |
$node_primary->backup($backup_name); |
| 55 |
|
| 56 |
# Create streaming standby linking to primary |
| 57 |
my $node_standby_1 = PostgreSQL::Test::Cluster->new('standby_1'); |
| 58 |
$node_standby_1->init_from_backup($node_primary, $backup_name, |
| 59 |
has_streaming => 1); |
| 60 |
$node_standby_1->append_conf('postgresql.conf', |
| 61 |
"primary_slot_name = standby_1"); |
| 62 |
$node_standby_1->append_conf('postgresql.conf', |
| 63 |
'max_standby_streaming_delay = 600s'); |
| 64 |
$node_standby_1->start; |
| 65 |
|
| 66 |
my $dlpath = dirname($ENV{REGRESS_SHLIB}); |
| 67 |
my $outputdir = $PostgreSQL::Test::Utils::tmp_check; |
| 68 |
|
| 69 |
# Run the regression tests against the primary. |
| 70 |
my $extra_opts = $ENV{EXTRA_REGRESS_OPTS} || ""; |
| 71 |
my $rc = |
| 72 |
system($ENV{PG_REGRESS} |
| 73 |
. " $extra_opts " |
| 74 |
. "--dlpath=\"$dlpath\" " |
| 75 |
. "--bindir= " |
| 76 |
. "--host=" |
| 77 |
. $node_primary->host . " " |
| 78 |
. "--port=" |
| 79 |
. $node_primary->port . " " |
| 80 |
. "--schedule=../regress/parallel_schedule " |
| 81 |
. "--max-concurrent-tests=20 " |
| 82 |
. "--inputdir=../regress " |
| 83 |
. "--outputdir=\"$outputdir\""); |
| 84 |
if ($rc != 0) |
| 85 |
{ |
| 86 |
# Dump out the regression diffs file, if there is one |
| 87 |
my $diffs = "$outputdir/regression.diffs"; |
| 88 |
if (-e $diffs) |
| 89 |
{ |
| 90 |
print "=== dumping $diffs ===\n"; |
| 91 |
print slurp_file($diffs); |
| 92 |
print "=== EOF ===\n"; |
| 93 |
} |
| 94 |
} |
| 95 |
is($rc, 0, 'regression tests pass'); |
| 96 |
|
| 97 |
# Clobber all sequences with their next value, so that we don't have |
| 98 |
# differences between nodes due to caching. |
| 99 |
$node_primary->psql('regression', |
| 100 |
"select setval(seqrelid, nextval(seqrelid)) from pg_sequence"); |
| 101 |
|
| 102 |
# Wait for standby to catch up |
| 103 |
$node_primary->wait_for_replay_catchup($node_standby_1); |
| 104 |
|
| 105 |
# Perform a logical dump of primary and standby, and check that they match |
| 106 |
command_ok( |
| 107 |
[ |
| 108 |
'pg_dumpall', |
| 109 |
'--file' => $outputdir . '/primary.dump', |
| 110 |
'--no-sync', '--no-statistics', |
| 111 |
'--restrict-key=test', |
| 112 |
'--port' => $node_primary->port, |
| 113 |
'--no-unlogged-table-data', # if unlogged, standby has schema only |
| 114 |
], |
| 115 |
'dump primary server'); |
| 116 |
command_ok( |
| 117 |
[ |
| 118 |
'pg_dumpall', |
| 119 |
'--file' => $outputdir . '/standby.dump', |
| 120 |
'--no-sync', '--no-statistics', |
| 121 |
'--restrict-key=test', |
| 122 |
'--port' => $node_standby_1->port, |
| 123 |
], |
| 124 |
'dump standby server'); |
| 125 |
compare_files( |
| 126 |
$outputdir . '/primary.dump', |
| 127 |
$outputdir . '/standby.dump', |
| 128 |
'compare primary and standby dumps'); |
| 129 |
|
| 130 |
# Likewise for the catalogs of the regression database, after disabling |
| 131 |
# autovacuum to make fields like relpages stop changing. |
| 132 |
$node_primary->append_conf('postgresql.conf', 'autovacuum = off'); |
| 133 |
$node_primary->restart; |
| 134 |
$node_primary->wait_for_replay_catchup($node_standby_1); |
| 135 |
command_ok( |
| 136 |
[ |
| 137 |
'pg_dump', |
| 138 |
'--schema' => 'pg_catalog', |
| 139 |
'--file' => $outputdir . '/catalogs_primary.dump', |
| 140 |
'--no-sync', |
| 141 |
'--restrict-key=test', |
| 142 |
'--port', $node_primary->port, |
| 143 |
'--no-unlogged-table-data', |
| 144 |
'regression', |
| 145 |
], |
| 146 |
'dump catalogs of primary server'); |
| 147 |
command_ok( |
| 148 |
[ |
| 149 |
'pg_dump', |
| 150 |
'--schema' => 'pg_catalog', |
| 151 |
'--file' => $outputdir . '/catalogs_standby.dump', |
| 152 |
'--no-sync', |
| 153 |
'--restrict-key=test', |
| 154 |
'--port' => $node_standby_1->port, |
| 155 |
'regression', |
| 156 |
], |
| 157 |
'dump catalogs of standby server'); |
| 158 |
compare_files( |
| 159 |
$outputdir . '/catalogs_primary.dump', |
| 160 |
$outputdir . '/catalogs_standby.dump', |
| 161 |
'compare primary and standby catalog dumps'); |
| 162 |
|
| 163 |
# Check some data from pg_stat_statements. |
| 164 |
$node_primary->safe_psql('postgres', 'CREATE EXTENSION pg_stat_statements'); |
| 165 |
# This gathers data based on the first characters for some common query types, |
| 166 |
# checking that reports are generated for SELECT, DMLs, and DDL queries with |
| 167 |
# CREATE. |
| 168 |
my $result = $node_primary->safe_psql( |
| 169 |
'postgres', |
| 170 |
qq{WITH select_stats AS |
| 171 |
(SELECT upper(substr(query, 1, 6)) AS select_query |
| 172 |
FROM pg_stat_statements |
| 173 |
WHERE upper(substr(query, 1, 6)) IN ('SELECT', 'UPDATE', |
| 174 |
'INSERT', 'DELETE', |
| 175 |
'CREATE')) |
| 176 |
SELECT select_query, count(select_query) > 1 AS some_rows |
| 177 |
FROM select_stats |
| 178 |
GROUP BY select_query ORDER BY select_query;}); |
| 179 |
is( $result, qq(CREATE|t |
| 180 |
DELETE|t |
| 181 |
INSERT|t |
| 182 |
SELECT|t |
| 183 |
UPDATE|t), 'check contents of pg_stat_statements on regression database'); |
| 184 |
|
| 185 |
$node_standby_1->stop; |
| 186 |
$node_primary->stop; |
| 187 |
|
| 188 |
done_testing(); |