| 1 |
|
| 2 |
# Copyright (c) 2024-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
use strict; |
| 5 |
use warnings FATAL => 'all'; |
| 6 |
use PostgreSQL::Test::Cluster; |
| 7 |
use PostgreSQL::Test::Utils; |
| 8 |
use Test::More; |
| 9 |
use File::Basename; |
| 10 |
|
| 11 |
|
| 12 |
my $node_primary = PostgreSQL::Test::Cluster->new('primary'); |
| 13 |
$node_primary->init(allows_streaming => 1); |
| 14 |
$node_primary->append_conf( |
| 15 |
'postgresql.conf', q[ |
| 16 |
allow_in_place_tablespaces = true |
| 17 |
log_connections=receipt |
| 18 |
# to avoid "repairing" corruption |
| 19 |
full_page_writes=off |
| 20 |
log_min_messages=debug2 |
| 21 |
shared_buffers=1MB |
| 22 |
]); |
| 23 |
$node_primary->start; |
| 24 |
|
| 25 |
|
| 26 |
# Create streaming standby linking to primary |
| 27 |
my $backup_name = 'my_backup'; |
| 28 |
$node_primary->backup($backup_name); |
| 29 |
my $node_standby = PostgreSQL::Test::Cluster->new('standby'); |
| 30 |
$node_standby->init_from_backup($node_primary, $backup_name, |
| 31 |
has_streaming => 1); |
| 32 |
$node_standby->start; |
| 33 |
|
| 34 |
# We'll reset this timeout for each individual query we run. |
| 35 |
my $psql_timeout = IPC::Run::timer($PostgreSQL::Test::Utils::timeout_default); |
| 36 |
|
| 37 |
my %psql_primary = (stdin => '', stdout => '', stderr => ''); |
| 38 |
$psql_primary{run} = IPC::Run::start( |
| 39 |
[ |
| 40 |
'psql', '--no-psqlrc', '--no-align', |
| 41 |
'--file' => '-', |
| 42 |
'--dbname' => $node_primary->connstr('postgres') |
| 43 |
], |
| 44 |
'<' => \$psql_primary{stdin}, |
| 45 |
'>' => \$psql_primary{stdout}, |
| 46 |
'2>' => \$psql_primary{stderr}, |
| 47 |
$psql_timeout); |
| 48 |
|
| 49 |
my %psql_standby = ('stdin' => '', 'stdout' => '', 'stderr' => ''); |
| 50 |
$psql_standby{run} = IPC::Run::start( |
| 51 |
[ |
| 52 |
'psql', '--no-psqlrc', '--no-align', |
| 53 |
'--file' => '-', |
| 54 |
'--dbname' => $node_standby->connstr('postgres') |
| 55 |
], |
| 56 |
'<' => \$psql_standby{stdin}, |
| 57 |
'>' => \$psql_standby{stdout}, |
| 58 |
'2>' => \$psql_standby{stderr}, |
| 59 |
$psql_timeout); |
| 60 |
|
| 61 |
|
| 62 |
# Create template database with a table that we'll update, to trigger dirty |
| 63 |
# rows. Using a template database + preexisting rows makes it a bit easier to |
| 64 |
# reproduce, because there's no cache invalidations generated. |
| 65 |
|
| 66 |
$node_primary->safe_psql('postgres', |
| 67 |
"CREATE DATABASE conflict_db_template OID = 50000;"); |
| 68 |
$node_primary->safe_psql( |
| 69 |
'conflict_db_template', q[ |
| 70 |
CREATE TABLE large(id serial primary key, dataa text, datab text); |
| 71 |
INSERT INTO large(dataa, datab) SELECT g.i::text, 1 FROM generate_series(1, 4000) g(i);] |
| 72 |
); |
| 73 |
$node_primary->safe_psql('postgres', |
| 74 |
"CREATE DATABASE conflict_db TEMPLATE conflict_db_template OID = 50001;"); |
| 75 |
|
| 76 |
$node_primary->safe_psql( |
| 77 |
'postgres', q[ |
| 78 |
CREATE EXTENSION pg_prewarm; |
| 79 |
CREATE TABLE replace_sb(data text); |
| 80 |
INSERT INTO replace_sb(data) SELECT random()::text FROM generate_series(1, 15000);] |
| 81 |
); |
| 82 |
|
| 83 |
$node_primary->wait_for_catchup($node_standby); |
| 84 |
|
| 85 |
# Use longrunning transactions, so that AtEOXact_SMgr doesn't close files |
| 86 |
send_query_and_wait(\%psql_primary, q[BEGIN;], qr/BEGIN/m); |
| 87 |
send_query_and_wait(\%psql_standby, q[BEGIN;], qr/BEGIN/m); |
| 88 |
|
| 89 |
# Cause lots of dirty rows in shared_buffers |
| 90 |
$node_primary->safe_psql('conflict_db', "UPDATE large SET datab = 1;"); |
| 91 |
|
| 92 |
# Now do a bunch of work in another database. That will end up needing to |
| 93 |
# write back dirty data from the previous step, opening the relevant file |
| 94 |
# descriptors |
| 95 |
cause_eviction(\%psql_primary, \%psql_standby); |
| 96 |
|
| 97 |
# drop and recreate database |
| 98 |
$node_primary->safe_psql('postgres', "DROP DATABASE conflict_db;"); |
| 99 |
$node_primary->safe_psql('postgres', |
| 100 |
"CREATE DATABASE conflict_db TEMPLATE conflict_db_template OID = 50001;"); |
| 101 |
|
| 102 |
verify($node_primary, $node_standby, 1, "initial contents as expected"); |
| 103 |
|
| 104 |
# Again cause lots of dirty rows in shared_buffers, but use a different update |
| 105 |
# value so we can check everything is OK |
| 106 |
$node_primary->safe_psql('conflict_db', "UPDATE large SET datab = 2;"); |
| 107 |
|
| 108 |
# Again cause a lot of IO. That'll again write back dirty data, but uses newly |
| 109 |
# opened file descriptors, so we don't confuse old files with new files despite |
| 110 |
# recycling relfilenodes. |
| 111 |
cause_eviction(\%psql_primary, \%psql_standby); |
| 112 |
|
| 113 |
verify($node_primary, $node_standby, 2, |
| 114 |
"update to reused relfilenode (due to DB oid conflict) is not lost"); |
| 115 |
|
| 116 |
|
| 117 |
$node_primary->safe_psql('conflict_db', "VACUUM FULL large;"); |
| 118 |
$node_primary->safe_psql('conflict_db', "UPDATE large SET datab = 3;"); |
| 119 |
|
| 120 |
verify($node_primary, $node_standby, 3, "restored contents as expected"); |
| 121 |
|
| 122 |
# Test for old filehandles after moving a database in / out of tablespace |
| 123 |
$node_primary->safe_psql('postgres', |
| 124 |
q[CREATE TABLESPACE test_tablespace LOCATION '']); |
| 125 |
|
| 126 |
# cause dirty buffers |
| 127 |
$node_primary->safe_psql('conflict_db', "UPDATE large SET datab = 4;"); |
| 128 |
# cause files to be opened in backend in other database |
| 129 |
cause_eviction(\%psql_primary, \%psql_standby); |
| 130 |
|
| 131 |
# move database back / forth |
| 132 |
$node_primary->safe_psql('postgres', |
| 133 |
'ALTER DATABASE conflict_db SET TABLESPACE test_tablespace'); |
| 134 |
$node_primary->safe_psql('postgres', |
| 135 |
'ALTER DATABASE conflict_db SET TABLESPACE pg_default'); |
| 136 |
|
| 137 |
# cause dirty buffers |
| 138 |
$node_primary->safe_psql('conflict_db', "UPDATE large SET datab = 5;"); |
| 139 |
cause_eviction(\%psql_primary, \%psql_standby); |
| 140 |
|
| 141 |
verify($node_primary, $node_standby, 5, "post move contents as expected"); |
| 142 |
|
| 143 |
$node_primary->safe_psql('postgres', |
| 144 |
'ALTER DATABASE conflict_db SET TABLESPACE test_tablespace'); |
| 145 |
|
| 146 |
$node_primary->safe_psql('conflict_db', "UPDATE large SET datab = 7;"); |
| 147 |
cause_eviction(\%psql_primary, \%psql_standby); |
| 148 |
$node_primary->safe_psql('conflict_db', "UPDATE large SET datab = 8;"); |
| 149 |
$node_primary->safe_psql('postgres', 'DROP DATABASE conflict_db'); |
| 150 |
$node_primary->safe_psql('postgres', 'DROP TABLESPACE test_tablespace'); |
| 151 |
|
| 152 |
$node_primary->safe_psql('postgres', 'REINDEX TABLE pg_database'); |
| 153 |
|
| 154 |
|
| 155 |
# explicitly shut down psql instances gracefully - to avoid hangs |
| 156 |
# or worse on windows |
| 157 |
$psql_primary{stdin} .= "\\q\n"; |
| 158 |
$psql_primary{run}->finish; |
| 159 |
$psql_standby{stdin} .= "\\q\n"; |
| 160 |
$psql_standby{run}->finish; |
| 161 |
|
| 162 |
$node_primary->stop(); |
| 163 |
$node_standby->stop(); |
| 164 |
|
| 165 |
# Make sure that there weren't crashes during shutdown |
| 166 |
|
| 167 |
command_like( |
| 168 |
[ 'pg_controldata', $node_primary->data_dir ], |
| 169 |
qr/Database cluster state:\s+shut down\n/, |
| 170 |
'primary shut down ok'); |
| 171 |
command_like( |
| 172 |
[ 'pg_controldata', $node_standby->data_dir ], |
| 173 |
qr/Database cluster state:\s+shut down in recovery\n/, |
| 174 |
'standby shut down ok'); |
| 175 |
done_testing(); |
| 176 |
|
| 177 |
sub verify |
| 178 |
{ |
| 179 |
my ($primary, $standby, $counter, $message) = @_; |
| 180 |
|
| 181 |
my $query = |
| 182 |
"SELECT datab, count(*) FROM large GROUP BY 1 ORDER BY 1 LIMIT 10"; |
| 183 |
is($primary->safe_psql('conflict_db', $query), |
| 184 |
"$counter|4000", "primary: $message"); |
| 185 |
|
| 186 |
$primary->wait_for_catchup($standby); |
| 187 |
is($standby->safe_psql('conflict_db', $query), |
| 188 |
"$counter|4000", "standby: $message"); |
| 189 |
} |
| 190 |
|
| 191 |
sub cause_eviction |
| 192 |
{ |
| 193 |
my ($psql_primary, $psql_standby) = @_; |
| 194 |
|
| 195 |
send_query_and_wait( |
| 196 |
$psql_primary, |
| 197 |
q[SELECT SUM(pg_prewarm(oid)) warmed_buffers FROM pg_class WHERE pg_relation_filenode(oid) != 0;], |
| 198 |
qr/warmed_buffers/m); |
| 199 |
|
| 200 |
send_query_and_wait( |
| 201 |
$psql_standby, |
| 202 |
q[SELECT SUM(pg_prewarm(oid)) warmed_buffers FROM pg_class WHERE pg_relation_filenode(oid) != 0;], |
| 203 |
qr/warmed_buffers/m); |
| 204 |
} |
| 205 |
|
| 206 |
# Send query, wait until string matches |
| 207 |
sub send_query_and_wait |
| 208 |
{ |
| 209 |
my ($psql, $query, $untl) = @_; |
| 210 |
|
| 211 |
# For each query we run, we'll restart the timeout. Otherwise the timeout |
| 212 |
# would apply to the whole test script, and would need to be set very high |
| 213 |
# to survive when running under Valgrind. |
| 214 |
$psql_timeout->reset(); |
| 215 |
$psql_timeout->start(); |
| 216 |
|
| 217 |
# send query |
| 218 |
$$psql{stdin} .= $query; |
| 219 |
$$psql{stdin} .= "\n"; |
| 220 |
|
| 221 |
# wait for query results |
| 222 |
$$psql{run}->pump_nb(); |
| 223 |
while (1) |
| 224 |
{ |
| 225 |
last if $$psql{stdout} =~ /$untl/; |
| 226 |
|
| 227 |
if ($psql_timeout->is_expired) |
| 228 |
{ |
| 229 |
BAIL_OUT("aborting wait: program timed out\n" |
| 230 |
. "stream contents: >>$$psql{stdout}<<\n" |
| 231 |
. "pattern searched for: $untl\n"); |
| 232 |
return 0; |
| 233 |
} |
| 234 |
if (not $$psql{run}->pumpable()) |
| 235 |
{ |
| 236 |
BAIL_OUT("aborting wait: program died\n" |
| 237 |
. "stream contents: >>$$psql{stdout}<<\n" |
| 238 |
. "pattern searched for: $untl\n"); |
| 239 |
return 0; |
| 240 |
} |
| 241 |
$$psql{run}->pump(); |
| 242 |
} |
| 243 |
|
| 244 |
$$psql{stdout} = ''; |
| 245 |
|
| 246 |
return 1; |
| 247 |
} |