| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test remove of temporary files after a crash. |
| 5 |
use strict; |
| 6 |
use warnings FATAL => 'all'; |
| 7 |
use PostgreSQL::Test::Cluster; |
| 8 |
use PostgreSQL::Test::Utils; |
| 9 |
use Test::More; |
| 10 |
use Config; |
| 11 |
|
| 12 |
if ($Config{osname} eq 'MSWin32') |
| 13 |
{ |
| 14 |
plan skip_all => 'tests hang on Windows'; |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
my $psql_timeout = IPC::Run::timer($PostgreSQL::Test::Utils::timeout_default); |
| 19 |
|
| 20 |
my $node = PostgreSQL::Test::Cluster->new('node_crash'); |
| 21 |
$node->init(); |
| 22 |
$node->start(); |
| 23 |
|
| 24 |
# By default, PostgreSQL::Test::Cluster doesn't restart after crash |
| 25 |
# Reduce work_mem to generate temporary file with a few number of rows |
| 26 |
$node->safe_psql( |
| 27 |
'postgres', |
| 28 |
q[ALTER SYSTEM SET remove_temp_files_after_crash = on; |
| 29 |
ALTER SYSTEM SET log_connections = receipt; |
| 30 |
ALTER SYSTEM SET work_mem = '64kB'; |
| 31 |
ALTER SYSTEM SET restart_after_crash = on; |
| 32 |
SELECT pg_reload_conf();]); |
| 33 |
|
| 34 |
# create table, insert rows |
| 35 |
$node->safe_psql('postgres', q[CREATE TABLE tab_crash (a integer UNIQUE);]); |
| 36 |
|
| 37 |
# Run psql, keeping session alive, so we have an alive backend to kill. |
| 38 |
my ($killme_stdin, $killme_stdout, $killme_stderr) = ('', '', ''); |
| 39 |
my $killme = IPC::Run::start( |
| 40 |
[ |
| 41 |
'psql', '--no-psqlrc', '--quiet', '--no-align', '--tuples-only', |
| 42 |
'--set' => 'ON_ERROR_STOP=1', |
| 43 |
'--file' => '-', |
| 44 |
'--dbname' => $node->connstr('postgres') |
| 45 |
], |
| 46 |
'<' => \$killme_stdin, |
| 47 |
'>' => \$killme_stdout, |
| 48 |
'2>' => \$killme_stderr, |
| 49 |
$psql_timeout); |
| 50 |
|
| 51 |
# Get backend pid |
| 52 |
$killme_stdin .= q[ |
| 53 |
SELECT pg_backend_pid(); |
| 54 |
]; |
| 55 |
ok( pump_until( |
| 56 |
$killme, $psql_timeout, \$killme_stdout, qr/[[:digit:]]+[\r\n]$/m), |
| 57 |
'acquired pid for SIGKILL'); |
| 58 |
my $pid = $killme_stdout; |
| 59 |
chomp($pid); |
| 60 |
$killme_stdout = ''; |
| 61 |
$killme_stderr = ''; |
| 62 |
|
| 63 |
# Open a 2nd session that will block the 1st one, using the UNIQUE constraint. |
| 64 |
# This will prevent removal of the temporary file created by the 1st session. |
| 65 |
my ($killme_stdin2, $killme_stdout2, $killme_stderr2) = ('', '', ''); |
| 66 |
my $killme2 = IPC::Run::start( |
| 67 |
[ |
| 68 |
'psql', '--no-psqlrc', '--quiet', '--no-align', '--tuples-only', |
| 69 |
'--set' => 'ON_ERROR_STOP=1', |
| 70 |
'--file' => '-', |
| 71 |
'--dbname' => $node->connstr('postgres') |
| 72 |
], |
| 73 |
'<' => \$killme_stdin2, |
| 74 |
'>' => \$killme_stdout2, |
| 75 |
'2>' => \$killme_stderr2, |
| 76 |
$psql_timeout); |
| 77 |
|
| 78 |
# Insert one tuple and leave the transaction open |
| 79 |
$killme_stdin2 .= q[ |
| 80 |
BEGIN; |
| 81 |
INSERT INTO tab_crash (a) VALUES(1); |
| 82 |
SELECT $$insert-tuple-to-lock-next-insert$$; |
| 83 |
]; |
| 84 |
pump_until($killme2, $psql_timeout, \$killme_stdout2, |
| 85 |
qr/insert-tuple-to-lock-next-insert/m); |
| 86 |
$killme_stdout2 = ''; |
| 87 |
$killme_stderr2 = ''; |
| 88 |
|
| 89 |
# Run the query that generates a temporary file and that will be killed before |
| 90 |
# it finishes. Since the query that generates the temporary file does not |
| 91 |
# return before the connection is killed, use a SELECT before to trigger |
| 92 |
# pump_until. |
| 93 |
$killme_stdin .= q[ |
| 94 |
BEGIN; |
| 95 |
SELECT $$in-progress-before-sigkill$$; |
| 96 |
INSERT INTO tab_crash (a) SELECT i FROM generate_series(1, 5000) s(i); |
| 97 |
]; |
| 98 |
ok( pump_until( |
| 99 |
$killme, $psql_timeout, |
| 100 |
\$killme_stdout, qr/in-progress-before-sigkill/m), |
| 101 |
'insert in-progress-before-sigkill'); |
| 102 |
$killme_stdout = ''; |
| 103 |
$killme_stderr = ''; |
| 104 |
|
| 105 |
# Wait until the batch insert gets stuck on the lock. |
| 106 |
$killme_stdin2 .= q[ |
| 107 |
DO $c$ |
| 108 |
DECLARE |
| 109 |
c INT; |
| 110 |
BEGIN |
| 111 |
LOOP |
| 112 |
SELECT COUNT(*) INTO c FROM pg_locks WHERE pid = ] . $pid |
| 113 |
. q[ AND NOT granted; |
| 114 |
IF c > 0 THEN |
| 115 |
EXIT; |
| 116 |
END IF; |
| 117 |
END LOOP; |
| 118 |
END; $c$; |
| 119 |
SELECT $$insert-tuple-lock-waiting$$; |
| 120 |
]; |
| 121 |
|
| 122 |
pump_until($killme2, $psql_timeout, \$killme_stdout2, |
| 123 |
qr/insert-tuple-lock-waiting/m); |
| 124 |
$killme_stdout2 = ''; |
| 125 |
$killme_stderr2 = ''; |
| 126 |
|
| 127 |
# Kill with SIGKILL |
| 128 |
my $ret = PostgreSQL::Test::Utils::system_log('pg_ctl', 'kill', 'KILL', $pid); |
| 129 |
is($ret, 0, 'killed process with KILL'); |
| 130 |
|
| 131 |
# Close that psql session |
| 132 |
$killme->finish; |
| 133 |
|
| 134 |
# Wait till the other session reports failure, ensuring that the postmaster |
| 135 |
# has noticed its dead child and begun a restart cycle. |
| 136 |
$killme_stdin2 .= qq[ |
| 137 |
SELECT pg_sleep($PostgreSQL::Test::Utils::timeout_default); |
| 138 |
]; |
| 139 |
ok( pump_until( |
| 140 |
$killme2, |
| 141 |
$psql_timeout, |
| 142 |
\$killme_stderr2, |
| 143 |
qr/WARNING: terminating connection because of crash of another server process|server closed the connection unexpectedly|connection to server was lost|could not send data to server/m |
| 144 |
), |
| 145 |
"second psql session died successfully after SIGKILL"); |
| 146 |
$killme2->finish; |
| 147 |
|
| 148 |
# Wait till server finishes restarting |
| 149 |
$node->poll_query_until('postgres', undef, ''); |
| 150 |
|
| 151 |
# Check for temporary files |
| 152 |
is( $node->safe_psql( |
| 153 |
'postgres', 'SELECT COUNT(1) FROM pg_ls_dir($$base/pgsql_tmp$$)'), |
| 154 |
qq(0), |
| 155 |
'no temporary files'); |
| 156 |
|
| 157 |
# |
| 158 |
# Test old behavior (don't remove temporary files after crash) |
| 159 |
# |
| 160 |
$node->safe_psql( |
| 161 |
'postgres', |
| 162 |
q[ALTER SYSTEM SET remove_temp_files_after_crash = off; |
| 163 |
SELECT pg_reload_conf();]); |
| 164 |
|
| 165 |
# Restart psql session |
| 166 |
($killme_stdin, $killme_stdout, $killme_stderr) = ('', '', ''); |
| 167 |
$killme->run(); |
| 168 |
|
| 169 |
# Get backend pid |
| 170 |
$killme_stdin .= q[ |
| 171 |
SELECT pg_backend_pid(); |
| 172 |
]; |
| 173 |
ok( pump_until( |
| 174 |
$killme, $psql_timeout, \$killme_stdout, qr/[[:digit:]]+[\r\n]$/m), |
| 175 |
'acquired pid for SIGKILL'); |
| 176 |
$pid = $killme_stdout; |
| 177 |
chomp($pid); |
| 178 |
$killme_stdout = ''; |
| 179 |
$killme_stderr = ''; |
| 180 |
|
| 181 |
# Restart the 2nd psql session |
| 182 |
($killme_stdin2, $killme_stdout2, $killme_stderr2) = ('', '', ''); |
| 183 |
$killme2->run(); |
| 184 |
|
| 185 |
# Insert one tuple and leave the transaction open |
| 186 |
$killme_stdin2 .= q[ |
| 187 |
BEGIN; |
| 188 |
INSERT INTO tab_crash (a) VALUES(1); |
| 189 |
SELECT $$insert-tuple-to-lock-next-insert$$; |
| 190 |
]; |
| 191 |
pump_until($killme2, $psql_timeout, \$killme_stdout2, |
| 192 |
qr/insert-tuple-to-lock-next-insert/m); |
| 193 |
$killme_stdout2 = ''; |
| 194 |
$killme_stderr2 = ''; |
| 195 |
|
| 196 |
# Run the query that generates a temporary file and that will be killed before |
| 197 |
# it finishes. Since the query that generates the temporary file does not |
| 198 |
# return before the connection is killed, use a SELECT before to trigger |
| 199 |
# pump_until. |
| 200 |
$killme_stdin .= q[ |
| 201 |
BEGIN; |
| 202 |
SELECT $$in-progress-before-sigkill$$; |
| 203 |
INSERT INTO tab_crash (a) SELECT i FROM generate_series(1, 5000) s(i); |
| 204 |
]; |
| 205 |
ok( pump_until( |
| 206 |
$killme, $psql_timeout, |
| 207 |
\$killme_stdout, qr/in-progress-before-sigkill/m), |
| 208 |
'insert in-progress-before-sigkill'); |
| 209 |
$killme_stdout = ''; |
| 210 |
$killme_stderr = ''; |
| 211 |
|
| 212 |
# Wait until the batch insert gets stuck on the lock. |
| 213 |
$killme_stdin2 .= q[ |
| 214 |
DO $c$ |
| 215 |
DECLARE |
| 216 |
c INT; |
| 217 |
BEGIN |
| 218 |
LOOP |
| 219 |
SELECT COUNT(*) INTO c FROM pg_locks WHERE pid = ] . $pid |
| 220 |
. q[ AND NOT granted; |
| 221 |
IF c > 0 THEN |
| 222 |
EXIT; |
| 223 |
END IF; |
| 224 |
END LOOP; |
| 225 |
END; $c$; |
| 226 |
SELECT $$insert-tuple-lock-waiting$$; |
| 227 |
]; |
| 228 |
|
| 229 |
pump_until($killme2, $psql_timeout, \$killme_stdout2, |
| 230 |
qr/insert-tuple-lock-waiting/m); |
| 231 |
$killme_stdout2 = ''; |
| 232 |
$killme_stderr2 = ''; |
| 233 |
|
| 234 |
# Kill with SIGKILL |
| 235 |
$ret = PostgreSQL::Test::Utils::system_log('pg_ctl', 'kill', 'KILL', $pid); |
| 236 |
is($ret, 0, 'killed process with KILL'); |
| 237 |
|
| 238 |
# Close that psql session |
| 239 |
$killme->finish; |
| 240 |
|
| 241 |
# Wait till the other session reports failure, ensuring that the postmaster |
| 242 |
# has noticed its dead child and begun a restart cycle. |
| 243 |
$killme_stdin2 .= qq[ |
| 244 |
SELECT pg_sleep($PostgreSQL::Test::Utils::timeout_default); |
| 245 |
]; |
| 246 |
ok( pump_until( |
| 247 |
$killme2, |
| 248 |
$psql_timeout, |
| 249 |
\$killme_stderr2, |
| 250 |
qr/WARNING: terminating connection because of crash of another server process|server closed the connection unexpectedly|connection to server was lost|could not send data to server/m |
| 251 |
), |
| 252 |
"second psql session died successfully after SIGKILL"); |
| 253 |
$killme2->finish; |
| 254 |
|
| 255 |
# Wait till server finishes restarting |
| 256 |
$node->poll_query_until('postgres', undef, ''); |
| 257 |
|
| 258 |
# Check for temporary files -- should be there |
| 259 |
is( $node->safe_psql( |
| 260 |
'postgres', 'SELECT COUNT(1) FROM pg_ls_dir($$base/pgsql_tmp$$)'), |
| 261 |
qq(1), |
| 262 |
'one temporary file'); |
| 263 |
|
| 264 |
# Restart should remove the temporary files |
| 265 |
$node->restart(); |
| 266 |
|
| 267 |
# Check the temporary files -- should be gone |
| 268 |
is( $node->safe_psql( |
| 269 |
'postgres', 'SELECT COUNT(1) FROM pg_ls_dir($$base/pgsql_tmp$$)'), |
| 270 |
qq(0), |
| 271 |
'temporary file was removed'); |
| 272 |
|
| 273 |
$node->stop(); |
| 274 |
|
| 275 |
done_testing(); |