|
| 1 | +# Test remove of temporary files after a crash. |
| 2 | +use strict; |
| 3 | +use warnings; |
| 4 | +use PostgresNode; |
| 5 | +use TestLib; |
| 6 | +use Test::More; |
| 7 | +use Config; |
| 8 | +use Time::HiRes qw(usleep); |
| 9 | + |
| 10 | +plan tests => 9; |
| 11 | + |
| 12 | + |
| 13 | +# To avoid hanging while expecting some specific input from a psql |
| 14 | +# instance being driven by us, add a timeout high enough that it |
| 15 | +# should never trigger even on very slow machines, unless something |
| 16 | +# is really wrong. |
| 17 | +my $psql_timeout = IPC::Run::timer(60); |
| 18 | + |
| 19 | +my $node = get_new_node('node_crash'); |
| 20 | +$node->init(); |
| 21 | +$node->start(); |
| 22 | + |
| 23 | +# By default, PostgresNode doesn't restart after crash |
| 24 | +# Reduce work_mem to generate temporary file with a few number of rows |
| 25 | +$node->safe_psql( |
| 26 | + 'postgres', |
| 27 | + q[ALTER SYSTEM SET remove_temp_files_after_crash = on; |
| 28 | + ALTER SYSTEM SET log_connections = 1; |
| 29 | + ALTER SYSTEM SET work_mem = '64kB'; |
| 30 | + ALTER SYSTEM SET restart_after_crash = on; |
| 31 | + SELECT pg_reload_conf();]); |
| 32 | + |
| 33 | +# create table, insert rows |
| 34 | +$node->safe_psql( |
| 35 | + 'postgres', |
| 36 | + q[CREATE TABLE tab_crash (a text); |
| 37 | + INSERT INTO tab_crash (a) SELECT gen_random_uuid() FROM generate_series(1, 500);]); |
| 38 | + |
| 39 | +# Run psql, keeping session alive, so we have an alive backend to kill. |
| 40 | +my ($killme_stdin, $killme_stdout, $killme_stderr) = ('', '', ''); |
| 41 | +my $killme = IPC::Run::start( |
| 42 | + [ |
| 43 | + 'psql', '-X', '-qAt', '-v', 'ON_ERROR_STOP=1', '-f', '-', '-d', |
| 44 | + $node->connstr('postgres') |
| 45 | + ], |
| 46 | + '<', |
| 47 | + \$killme_stdin, |
| 48 | + '>', |
| 49 | + \$killme_stdout, |
| 50 | + '2>', |
| 51 | + \$killme_stderr, |
| 52 | + $psql_timeout); |
| 53 | + |
| 54 | +# Get backend pid |
| 55 | +$killme_stdin .= q[ |
| 56 | +SELECT pg_backend_pid(); |
| 57 | +]; |
| 58 | +ok(pump_until($killme, \$killme_stdout, qr/[[:digit:]]+[\r\n]$/m), |
| 59 | + 'acquired pid for SIGKILL'); |
| 60 | +my $pid = $killme_stdout; |
| 61 | +chomp($pid); |
| 62 | +$killme_stdout = ''; |
| 63 | +$killme_stderr = ''; |
| 64 | + |
| 65 | +# Run the query that generates a temporary file and that will be killed before |
| 66 | +# it finishes. Since the query that generates the temporary file does not |
| 67 | +# return before the connection is killed, use a SELECT before to trigger |
| 68 | +# pump_until. |
| 69 | +$killme_stdin .= q[ |
| 70 | +BEGIN; |
| 71 | +SELECT $$in-progress-before-sigkill$$; |
| 72 | +WITH foo AS (SELECT a FROM tab_crash ORDER BY a) SELECT a, pg_sleep(1) FROM foo; |
| 73 | +]; |
| 74 | +ok(pump_until($killme, \$killme_stdout, qr/in-progress-before-sigkill/m), |
| 75 | + 'select in-progress-before-sigkill'); |
| 76 | +$killme_stdout = ''; |
| 77 | +$killme_stderr = ''; |
| 78 | + |
| 79 | +# Wait some time so the temporary file is generated by SELECT |
| 80 | +usleep(10_000); |
| 81 | + |
| 82 | +# Kill with SIGKILL |
| 83 | +my $ret = TestLib::system_log('pg_ctl', 'kill', 'KILL', $pid); |
| 84 | +is($ret, 0, 'killed process with KILL'); |
| 85 | + |
| 86 | +# Close psql session |
| 87 | +$killme->finish; |
| 88 | + |
| 89 | +# Wait till server restarts |
| 90 | +$node->poll_query_until('postgres', 'SELECT 1', '1'); |
| 91 | + |
| 92 | +# Check for temporary files |
| 93 | +is($node->safe_psql( |
| 94 | + 'postgres', |
| 95 | + 'SELECT COUNT(1) FROM pg_ls_dir($$base/pgsql_tmp$$)'), |
| 96 | + qq(0), 'no temporary files'); |
| 97 | + |
| 98 | +# |
| 99 | +# Test old behavior (don't remove temporary files after crash) |
| 100 | +# |
| 101 | +$node->safe_psql( |
| 102 | + 'postgres', |
| 103 | + q[ALTER SYSTEM SET remove_temp_files_after_crash = off; |
| 104 | + SELECT pg_reload_conf();]); |
| 105 | + |
| 106 | +# Restart psql session |
| 107 | +($killme_stdin, $killme_stdout, $killme_stderr) = ('', '', ''); |
| 108 | +$killme->run(); |
| 109 | + |
| 110 | +# Get backend pid |
| 111 | +$killme_stdin .= q[ |
| 112 | +SELECT pg_backend_pid(); |
| 113 | +]; |
| 114 | +ok(pump_until($killme, \$killme_stdout, qr/[[:digit:]]+[\r\n]$/m), |
| 115 | + 'acquired pid for SIGKILL'); |
| 116 | +$pid = $killme_stdout; |
| 117 | +chomp($pid); |
| 118 | +$killme_stdout = ''; |
| 119 | +$killme_stderr = ''; |
| 120 | + |
| 121 | +# Run the query that generates a temporary file and that will be killed before |
| 122 | +# it finishes. Since the query that generates the temporary file does not |
| 123 | +# return before the connection is killed, use a SELECT before to trigger |
| 124 | +# pump_until. |
| 125 | +$killme_stdin .= q[ |
| 126 | +BEGIN; |
| 127 | +SELECT $$in-progress-before-sigkill$$; |
| 128 | +WITH foo AS (SELECT a FROM tab_crash ORDER BY a) SELECT a, pg_sleep(1) FROM foo; |
| 129 | +]; |
| 130 | +ok(pump_until($killme, \$killme_stdout, qr/in-progress-before-sigkill/m), |
| 131 | + 'select in-progress-before-sigkill'); |
| 132 | +$killme_stdout = ''; |
| 133 | +$killme_stderr = ''; |
| 134 | + |
| 135 | +# Wait some time so the temporary file is generated by SELECT |
| 136 | +usleep(10_000); |
| 137 | + |
| 138 | +# Kill with SIGKILL |
| 139 | +$ret = TestLib::system_log('pg_ctl', 'kill', 'KILL', $pid); |
| 140 | +is($ret, 0, 'killed process with KILL'); |
| 141 | + |
| 142 | +# Close psql session |
| 143 | +$killme->finish; |
| 144 | + |
| 145 | +# Wait till server restarts |
| 146 | +$node->poll_query_until('postgres', 'SELECT 1', '1'); |
| 147 | + |
| 148 | +# Check for temporary files -- should be there |
| 149 | +is($node->safe_psql( |
| 150 | + 'postgres', |
| 151 | + 'SELECT COUNT(1) FROM pg_ls_dir($$base/pgsql_tmp$$)'), |
| 152 | + qq(1), 'one temporary file'); |
| 153 | + |
| 154 | +# Restart should remove the temporary files |
| 155 | +$node->restart(); |
| 156 | + |
| 157 | +# Check the temporary files -- should be gone |
| 158 | +is($node->safe_psql( |
| 159 | + 'postgres', |
| 160 | + 'SELECT COUNT(1) FROM pg_ls_dir($$base/pgsql_tmp$$)'), |
| 161 | + qq(0), 'temporary file was removed'); |
| 162 | + |
| 163 | +$node->stop(); |
| 164 | + |
| 165 | +# Pump until string is matched, or timeout occurs |
| 166 | +sub pump_until |
| 167 | +{ |
| 168 | + my ($proc, $stream, $untl) = @_; |
| 169 | + $proc->pump_nb(); |
| 170 | + while (1) |
| 171 | + { |
| 172 | + last if $$stream =~ /$untl/; |
| 173 | + if ($psql_timeout->is_expired) |
| 174 | + { |
| 175 | + diag("aborting wait: program timed out"); |
| 176 | + diag("stream contents: >>", $$stream, "<<"); |
| 177 | + diag("pattern searched for: ", $untl); |
| 178 | + |
| 179 | + return 0; |
| 180 | + } |
| 181 | + if (not $proc->pumpable()) |
| 182 | + { |
| 183 | + diag("aborting wait: program died"); |
| 184 | + diag("stream contents: >>", $$stream, "<<"); |
| 185 | + diag("pattern searched for: ", $untl); |
| 186 | + |
| 187 | + return 0; |
| 188 | + } |
| 189 | + $proc->pump(); |
| 190 | + } |
| 191 | + return 1; |
| 192 | +} |
0 commit comments