| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
use strict; |
| 5 |
use warnings FATAL => 'all'; |
| 6 |
use locale; |
| 7 |
|
| 8 |
use PostgreSQL::Test::Cluster; |
| 9 |
use PostgreSQL::Test::Utils; |
| 10 |
use Test::More; |
| 11 |
|
| 12 |
program_help_ok('psql'); |
| 13 |
program_version_ok('psql'); |
| 14 |
program_options_handling_ok('psql'); |
| 15 |
|
| 16 |
# Execute a psql command and check its output. |
| 17 |
sub psql_like |
| 18 |
{ |
| 19 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 20 |
|
| 21 |
my ($node, $sql, $expected_stdout, $test_name) = @_; |
| 22 |
|
| 23 |
my ($ret, $stdout, $stderr) = $node->psql('postgres', $sql); |
| 24 |
|
| 25 |
is($ret, 0, "$test_name: exit code 0"); |
| 26 |
is($stderr, '', "$test_name: no stderr"); |
| 27 |
like($stdout, $expected_stdout, "$test_name: matches"); |
| 28 |
|
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
# Execute a psql command and check that it fails and check the stderr. |
| 33 |
sub psql_fails_like |
| 34 |
{ |
| 35 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 36 |
|
| 37 |
my ($node, $sql, $expected_stderr, $test_name, $replication) = @_; |
| 38 |
|
| 39 |
# Use the context of a WAL sender, if requested by the caller. |
| 40 |
$replication = '' unless defined($replication); |
| 41 |
|
| 42 |
my ($ret, $stdout, $stderr) = |
| 43 |
$node->psql('postgres', $sql, replication => $replication); |
| 44 |
|
| 45 |
isnt($ret, 0, "$test_name: exit code not 0"); |
| 46 |
like($stderr, $expected_stderr, "$test_name: matches"); |
| 47 |
|
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
# test --help=foo, analogous to program_help_ok() |
| 52 |
foreach my $arg (qw(commands variables)) |
| 53 |
{ |
| 54 |
my ($stdout, $stderr); |
| 55 |
my $result; |
| 56 |
|
| 57 |
$result = IPC::Run::run [ 'psql', "--help=$arg" ], |
| 58 |
'>' => \$stdout, |
| 59 |
'2>' => \$stderr; |
| 60 |
ok($result, "psql --help=$arg exit code 0"); |
| 61 |
isnt($stdout, '', "psql --help=$arg goes to stdout"); |
| 62 |
is($stderr, '', "psql --help=$arg nothing to stderr"); |
| 63 |
} |
| 64 |
|
| 65 |
my $node = PostgreSQL::Test::Cluster->new('main'); |
| 66 |
$node->init(extra => [ '--locale=C', '--encoding=UTF8' ]); |
| 67 |
$node->append_conf( |
| 68 |
'postgresql.conf', q{ |
| 69 |
wal_level = 'logical' |
| 70 |
max_replication_slots = 4 |
| 71 |
max_wal_senders = 4 |
| 72 |
}); |
| 73 |
$node->start; |
| 74 |
|
| 75 |
psql_like($node, '\copyright', qr/Copyright/, '\copyright'); |
| 76 |
psql_like($node, '\help', qr/ALTER/, '\help without arguments'); |
| 77 |
psql_like($node, '\help SELECT', qr/SELECT/, '\help with argument'); |
| 78 |
|
| 79 |
# Test clean handling of unsupported replication command responses |
| 80 |
psql_fails_like( |
| 81 |
$node, |
| 82 |
'START_REPLICATION 0/0', |
| 83 |
qr/unexpected PQresultStatus: 8$/, |
| 84 |
'handling of unexpected PQresultStatus', 'database'); |
| 85 |
|
| 86 |
# test \timing |
| 87 |
psql_like( |
| 88 |
$node, |
| 89 |
'\timing on |
| 90 |
SELECT 1', |
| 91 |
qr/^1$ |
| 92 |
^Time: \d+[.,]\d\d\d ms/m, |
| 93 |
'\timing with successful query'); |
| 94 |
|
| 95 |
# test \timing with query that fails |
| 96 |
{ |
| 97 |
my ($ret, $stdout, $stderr) = |
| 98 |
$node->psql('postgres', "\\timing on\nSELECT error"); |
| 99 |
isnt($ret, 0, '\timing with query error: query failed'); |
| 100 |
like( |
| 101 |
$stdout, |
| 102 |
qr/^Time: \d+[.,]\d\d\d ms/m, |
| 103 |
'\timing with query error: timing output appears'); |
| 104 |
unlike( |
| 105 |
$stdout, |
| 106 |
qr/^Time: 0[.,]000 ms/m, |
| 107 |
'\timing with query error: timing was updated'); |
| 108 |
} |
| 109 |
|
| 110 |
# test that ENCODING variable is set and that it is updated when |
| 111 |
# client encoding is changed |
| 112 |
psql_like( |
| 113 |
$node, |
| 114 |
'\echo :ENCODING |
| 115 |
set client_encoding = LATIN1; |
| 116 |
\echo :ENCODING', |
| 117 |
qr/^UTF8$ |
| 118 |
^LATIN1$/m, |
| 119 |
'ENCODING variable is set and updated'); |
| 120 |
|
| 121 |
# test LISTEN/NOTIFY |
| 122 |
psql_like( |
| 123 |
$node, |
| 124 |
'LISTEN foo; |
| 125 |
NOTIFY foo;', |
| 126 |
qr/^Asynchronous notification "foo" received from server process with PID \d+\.$/, |
| 127 |
'notification'); |
| 128 |
|
| 129 |
psql_like( |
| 130 |
$node, |
| 131 |
"LISTEN foo; |
| 132 |
NOTIFY foo, 'bar';", |
| 133 |
qr/^Asynchronous notification "foo" with payload "bar" received from server process with PID \d+\.$/, |
| 134 |
'notification with payload'); |
| 135 |
|
| 136 |
# test behavior and output on server crash |
| 137 |
my ($ret, $out, $err) = $node->psql('postgres', |
| 138 |
"SELECT 'before' AS running;\n" |
| 139 |
. "SELECT pg_terminate_backend(pg_backend_pid());\n" |
| 140 |
. "SELECT 'AFTER' AS not_running;\n"); |
| 141 |
|
| 142 |
is($ret, 2, 'server crash: psql exit code'); |
| 143 |
like($out, qr/before/, 'server crash: output before crash'); |
| 144 |
ok($out !~ qr/AFTER/, 'server crash: no output after crash'); |
| 145 |
is( $err, |
| 146 |
'psql:<stdin>:2: FATAL: terminating connection due to administrator command |
| 147 |
psql:<stdin>:2: server closed the connection unexpectedly |
| 148 |
This probably means the server terminated abnormally |
| 149 |
before or while processing the request. |
| 150 |
psql:<stdin>:2: error: connection to server was lost', |
| 151 |
'server crash: error message'); |
| 152 |
|
| 153 |
# test \errverbose |
| 154 |
# |
| 155 |
# (This is not in the regular regression tests because the output |
| 156 |
# contains the source code location and we don't want to have to |
| 157 |
# update that every time it changes.) |
| 158 |
|
| 159 |
psql_like( |
| 160 |
$node, |
| 161 |
'SELECT 1; |
| 162 |
\errverbose', |
| 163 |
qr/^1\nThere is no previous error\.$/, |
| 164 |
'\errverbose with no previous error'); |
| 165 |
|
| 166 |
# There are three main ways to run a query that might affect |
| 167 |
# \errverbose: The normal way, piecemeal retrieval using FETCH_COUNT, |
| 168 |
# and using \gdesc. Test them all. |
| 169 |
|
| 170 |
like( |
| 171 |
( $node->psql( |
| 172 |
'postgres', |
| 173 |
"SELECT error;\n\\errverbose", |
| 174 |
on_error_stop => 0))[2], |
| 175 |
qr/\A^psql:<stdin>:1: ERROR: .*$ |
| 176 |
^LINE 1: SELECT error;$ |
| 177 |
^ *^.*$ |
| 178 |
^psql:<stdin>:2: error: ERROR: [0-9A-Z]{5}: .*$ |
| 179 |
^LINE 1: SELECT error;$ |
| 180 |
^ *^.*$ |
| 181 |
^LOCATION: .*$/m, |
| 182 |
'\errverbose after normal query with error'); |
| 183 |
|
| 184 |
like( |
| 185 |
( $node->psql( |
| 186 |
'postgres', |
| 187 |
"\\set FETCH_COUNT 1\nSELECT error;\n\\errverbose", |
| 188 |
on_error_stop => 0))[2], |
| 189 |
qr/\A^psql:<stdin>:2: ERROR: .*$ |
| 190 |
^LINE 1: SELECT error;$ |
| 191 |
^ *^.*$ |
| 192 |
^psql:<stdin>:3: error: ERROR: [0-9A-Z]{5}: .*$ |
| 193 |
^LINE 1: SELECT error;$ |
| 194 |
^ *^.*$ |
| 195 |
^LOCATION: .*$/m, |
| 196 |
'\errverbose after FETCH_COUNT query with error'); |
| 197 |
|
| 198 |
like( |
| 199 |
( $node->psql( |
| 200 |
'postgres', |
| 201 |
"SELECT error\\gdesc\n\\errverbose", |
| 202 |
on_error_stop => 0))[2], |
| 203 |
qr/\A^psql:<stdin>:1: ERROR: .*$ |
| 204 |
^LINE 1: SELECT error$ |
| 205 |
^ *^.*$ |
| 206 |
^psql:<stdin>:2: error: ERROR: [0-9A-Z]{5}: .*$ |
| 207 |
^LINE 1: SELECT error$ |
| 208 |
^ *^.*$ |
| 209 |
^LOCATION: .*$/m, |
| 210 |
'\errverbose after \gdesc with error'); |
| 211 |
|
| 212 |
# Check behavior when using multiple -c and -f switches. |
| 213 |
# Note that we cannot test backend-side errors as tests are unstable in this |
| 214 |
# case: IPC::Run can complain about a SIGPIPE if psql quits before reading a |
| 215 |
# query result. |
| 216 |
my $tempdir = PostgreSQL::Test::Utils::tempdir; |
| 217 |
$node->safe_psql('postgres', "CREATE TABLE tab_psql_single (a int);"); |
| 218 |
|
| 219 |
# Tests with ON_ERROR_STOP. |
| 220 |
$node->command_ok( |
| 221 |
[ |
| 222 |
'psql', |
| 223 |
'--no-psqlrc', |
| 224 |
'--single-transaction', |
| 225 |
'--set' => 'ON_ERROR_STOP=1', |
| 226 |
'--command' => 'INSERT INTO tab_psql_single VALUES (1)', |
| 227 |
'--command' => 'INSERT INTO tab_psql_single VALUES (2)', |
| 228 |
], |
| 229 |
'ON_ERROR_STOP, --single-transaction and multiple -c switches'); |
| 230 |
my $row_count = |
| 231 |
$node->safe_psql('postgres', 'SELECT count(*) FROM tab_psql_single'); |
| 232 |
is($row_count, '2', |
| 233 |
'--single-transaction commits transaction, ON_ERROR_STOP and multiple -c switches' |
| 234 |
); |
| 235 |
|
| 236 |
$node->command_fails( |
| 237 |
[ |
| 238 |
'psql', |
| 239 |
'--no-psqlrc', |
| 240 |
'--single-transaction', |
| 241 |
'--set' => 'ON_ERROR_STOP=1', |
| 242 |
'--command' => 'INSERT INTO tab_psql_single VALUES (3)', |
| 243 |
'--command' => "\\copy tab_psql_single FROM '$tempdir/nonexistent'" |
| 244 |
], |
| 245 |
'ON_ERROR_STOP, --single-transaction and multiple -c switches, error'); |
| 246 |
$row_count = |
| 247 |
$node->safe_psql('postgres', 'SELECT count(*) FROM tab_psql_single'); |
| 248 |
is($row_count, '2', |
| 249 |
'client-side error rolls back transaction, ON_ERROR_STOP and multiple -c switches' |
| 250 |
); |
| 251 |
|
| 252 |
# Tests mixing files and commands. |
| 253 |
my $copy_sql_file = "$tempdir/tab_copy.sql"; |
| 254 |
my $insert_sql_file = "$tempdir/tab_insert.sql"; |
| 255 |
append_to_file($copy_sql_file, |
| 256 |
"\\copy tab_psql_single FROM '$tempdir/nonexistent';"); |
| 257 |
append_to_file($insert_sql_file, 'INSERT INTO tab_psql_single VALUES (4);'); |
| 258 |
$node->command_ok( |
| 259 |
[ |
| 260 |
'psql', |
| 261 |
'--no-psqlrc', |
| 262 |
'--single-transaction', |
| 263 |
'--set' => 'ON_ERROR_STOP=1', |
| 264 |
'--file' => $insert_sql_file, |
| 265 |
'--file' => $insert_sql_file |
| 266 |
], |
| 267 |
'ON_ERROR_STOP, --single-transaction and multiple -f switches'); |
| 268 |
$row_count = |
| 269 |
$node->safe_psql('postgres', 'SELECT count(*) FROM tab_psql_single'); |
| 270 |
is($row_count, '4', |
| 271 |
'--single-transaction commits transaction, ON_ERROR_STOP and multiple -f switches' |
| 272 |
); |
| 273 |
|
| 274 |
$node->command_fails( |
| 275 |
[ |
| 276 |
'psql', |
| 277 |
'--no-psqlrc', |
| 278 |
'--single-transaction', |
| 279 |
'--set' => 'ON_ERROR_STOP=1', |
| 280 |
'--file' => $insert_sql_file, |
| 281 |
'--file' => $copy_sql_file |
| 282 |
], |
| 283 |
'ON_ERROR_STOP, --single-transaction and multiple -f switches, error'); |
| 284 |
$row_count = |
| 285 |
$node->safe_psql('postgres', 'SELECT count(*) FROM tab_psql_single'); |
| 286 |
is($row_count, '4', |
| 287 |
'client-side error rolls back transaction, ON_ERROR_STOP and multiple -f switches' |
| 288 |
); |
| 289 |
|
| 290 |
# Tests without ON_ERROR_STOP. |
| 291 |
# The last switch fails on \copy. The command returns a failure and the |
| 292 |
# transaction commits. |
| 293 |
$node->command_fails( |
| 294 |
[ |
| 295 |
'psql', |
| 296 |
'--no-psqlrc', |
| 297 |
'--single-transaction', |
| 298 |
'--file' => $insert_sql_file, |
| 299 |
'--file' => $insert_sql_file, |
| 300 |
'--command' => "\\copy tab_psql_single FROM '$tempdir/nonexistent'" |
| 301 |
], |
| 302 |
'no ON_ERROR_STOP, --single-transaction and multiple -f/-c switches'); |
| 303 |
$row_count = |
| 304 |
$node->safe_psql('postgres', 'SELECT count(*) FROM tab_psql_single'); |
| 305 |
is($row_count, '6', |
| 306 |
'client-side error commits transaction, no ON_ERROR_STOP and multiple -f/-c switches' |
| 307 |
); |
| 308 |
|
| 309 |
# The last switch fails on \copy coming from an input file. The command |
| 310 |
# returns a success and the transaction commits. |
| 311 |
$node->command_ok( |
| 312 |
[ |
| 313 |
'psql', |
| 314 |
'--no-psqlrc', |
| 315 |
'--single-transaction', |
| 316 |
'--file' => $insert_sql_file, |
| 317 |
'--file' => $insert_sql_file, |
| 318 |
'--file' => $copy_sql_file |
| 319 |
], |
| 320 |
'no ON_ERROR_STOP, --single-transaction and multiple -f switches'); |
| 321 |
$row_count = |
| 322 |
$node->safe_psql('postgres', 'SELECT count(*) FROM tab_psql_single'); |
| 323 |
is($row_count, '8', |
| 324 |
'client-side error commits transaction, no ON_ERROR_STOP and multiple -f switches' |
| 325 |
); |
| 326 |
|
| 327 |
# The last switch makes the command return a success, and the contents of |
| 328 |
# the transaction commit even if there is a failure in-between. |
| 329 |
$node->command_ok( |
| 330 |
[ |
| 331 |
'psql', |
| 332 |
'--no-psqlrc', |
| 333 |
'--single-transaction', |
| 334 |
'--command' => 'INSERT INTO tab_psql_single VALUES (5)', |
| 335 |
'--file' => $copy_sql_file, |
| 336 |
'--command' => 'INSERT INTO tab_psql_single VALUES (6)' |
| 337 |
], |
| 338 |
'no ON_ERROR_STOP, --single-transaction and multiple -c switches'); |
| 339 |
$row_count = |
| 340 |
$node->safe_psql('postgres', 'SELECT count(*) FROM tab_psql_single'); |
| 341 |
is($row_count, '10', |
| 342 |
'client-side error commits transaction, no ON_ERROR_STOP and multiple -c switches' |
| 343 |
); |
| 344 |
|
| 345 |
# Test \copy from with DEFAULT option |
| 346 |
$node->safe_psql( |
| 347 |
'postgres', |
| 348 |
"CREATE TABLE copy_default ( |
| 349 |
id integer PRIMARY KEY, |
| 350 |
text_value text NOT NULL DEFAULT 'test', |
| 351 |
ts_value timestamp without time zone NOT NULL DEFAULT '2022-07-05' |
| 352 |
)" |
| 353 |
); |
| 354 |
|
| 355 |
my $copy_default_sql_file = "$tempdir/copy_default.csv"; |
| 356 |
append_to_file($copy_default_sql_file, "1,value,2022-07-04\n"); |
| 357 |
append_to_file($copy_default_sql_file, "2,placeholder,2022-07-03\n"); |
| 358 |
append_to_file($copy_default_sql_file, "3,placeholder,placeholder\n"); |
| 359 |
|
| 360 |
psql_like( |
| 361 |
$node, |
| 362 |
"\\copy copy_default from $copy_default_sql_file with (format 'csv', default 'placeholder'); |
| 363 |
SELECT * FROM copy_default", |
| 364 |
qr/1\|value\|2022-07-04 00:00:00 |
| 365 |
2|test|2022-07-03 00:00:00 |
| 366 |
3|test|2022-07-05 00:00:00/, |
| 367 |
'\copy from with DEFAULT'); |
| 368 |
|
| 369 |
# Check \watch |
| 370 |
# Note: the interval value is parsed with locale-aware strtod() |
| 371 |
psql_like( |
| 372 |
$node, sprintf('SELECT 1 \watch c=3 i=%g', 0.01), |
| 373 |
qr/1\n1\n1/, '\watch with 3 iterations, interval of 0.01'); |
| 374 |
|
| 375 |
# Sub-millisecond wait works, equivalent to 0. |
| 376 |
psql_like( |
| 377 |
$node, sprintf('SELECT 1 \watch c=3 i=%g', 0.0001), |
| 378 |
qr/1\n1\n1/, '\watch with 3 iterations, interval of 0.0001'); |
| 379 |
|
| 380 |
# Test zero interval |
| 381 |
psql_like( |
| 382 |
$node, '\set WATCH_INTERVAL 0 |
| 383 |
SELECT 1 \watch c=3', |
| 384 |
qr/1\n1\n1/, '\watch with 3 iterations, interval of 0'); |
| 385 |
|
| 386 |
# Check \watch minimum row count |
| 387 |
psql_fails_like( |
| 388 |
$node, |
| 389 |
'SELECT 3 \watch m=x', |
| 390 |
qr/incorrect minimum row count/, |
| 391 |
'\watch, invalid minimum row setting'); |
| 392 |
|
| 393 |
psql_fails_like( |
| 394 |
$node, |
| 395 |
'SELECT 3 \watch m=1 min_rows=2', |
| 396 |
qr/minimum row count specified more than once/, |
| 397 |
'\watch, minimum rows is specified more than once'); |
| 398 |
|
| 399 |
psql_like( |
| 400 |
$node, |
| 401 |
sprintf( |
| 402 |
q{with x as ( |
| 403 |
select now()-backend_start AS howlong |
| 404 |
from pg_stat_activity |
| 405 |
where pid = pg_backend_pid() |
| 406 |
) select 123 from x where howlong < '2 seconds' \watch i=%g m=2}, 0.5), |
| 407 |
qr/^123$/, |
| 408 |
'\watch, 2 minimum rows'); |
| 409 |
|
| 410 |
# Check \watch errors |
| 411 |
psql_fails_like( |
| 412 |
$node, |
| 413 |
'SELECT 1 \watch -10', |
| 414 |
qr/incorrect interval value "-10"/, |
| 415 |
'\watch, negative interval'); |
| 416 |
psql_fails_like( |
| 417 |
$node, |
| 418 |
'SELECT 1 \watch 10ab', |
| 419 |
qr/incorrect interval value "10ab"/, |
| 420 |
'\watch, incorrect interval'); |
| 421 |
psql_fails_like( |
| 422 |
$node, |
| 423 |
'SELECT 1 \watch 10e400', |
| 424 |
qr/incorrect interval value "10e400"/, |
| 425 |
'\watch, out-of-range interval'); |
| 426 |
psql_fails_like( |
| 427 |
$node, |
| 428 |
'SELECT 1 \watch 1 1', |
| 429 |
qr/interval value is specified more than once/, |
| 430 |
'\watch, interval value is specified more than once'); |
| 431 |
psql_fails_like( |
| 432 |
$node, |
| 433 |
'SELECT 1 \watch c=1 c=1', |
| 434 |
qr/iteration count is specified more than once/, |
| 435 |
'\watch, iteration count is specified more than once'); |
| 436 |
|
| 437 |
# Check WATCH_INTERVAL |
| 438 |
psql_like( |
| 439 |
$node, |
| 440 |
'\echo :WATCH_INTERVAL |
| 441 |
\set WATCH_INTERVAL 10 |
| 442 |
\echo :WATCH_INTERVAL |
| 443 |
\unset WATCH_INTERVAL |
| 444 |
\echo :WATCH_INTERVAL', |
| 445 |
qr/^2$ |
| 446 |
^10$ |
| 447 |
^2$/m, |
| 448 |
'WATCH_INTERVAL variable is set and updated'); |
| 449 |
psql_fails_like( |
| 450 |
$node, |
| 451 |
'\set WATCH_INTERVAL 1e500', |
| 452 |
qr/is out of range/, |
| 453 |
'WATCH_INTERVAL variable is out of range'); |
| 454 |
psql_like($node, '\echo :WATCH_INTERVAL', |
| 455 |
qr/^2$/m, 'WATCH_INTERVAL variable was not altered'); |
| 456 |
|
| 457 |
# Test \g output piped into a program. |
| 458 |
# The program is perl -pe '' to simply copy the input to the output. |
| 459 |
my $g_file = "$tempdir/g_file_1.out"; |
| 460 |
my $perlbin = $^X; |
| 461 |
$perlbin =~ s!\\!/!g if $PostgreSQL::Test::Utils::windows_os; |
| 462 |
my $pipe_cmd = "$perlbin -pe '' >$g_file"; |
| 463 |
|
| 464 |
psql_like($node, "SELECT 'one' \\g | $pipe_cmd", qr//, "one command \\g"); |
| 465 |
my $c1 = slurp_file($g_file); |
| 466 |
like($c1, qr/one/); |
| 467 |
|
| 468 |
psql_like($node, "SELECT 'two' \\; SELECT 'three' \\g | $pipe_cmd", |
| 469 |
qr//, "two commands \\g"); |
| 470 |
my $c2 = slurp_file($g_file); |
| 471 |
like($c2, qr/two.*three/s); |
| 472 |
|
| 473 |
|
| 474 |
psql_like( |
| 475 |
$node, |
| 476 |
"\\set SHOW_ALL_RESULTS 0\nSELECT 'four' \\; SELECT 'five' \\g | $pipe_cmd", |
| 477 |
qr//, |
| 478 |
"two commands \\g with only last result"); |
| 479 |
my $c3 = slurp_file($g_file); |
| 480 |
like($c3, qr/five/); |
| 481 |
unlike($c3, qr/four/); |
| 482 |
|
| 483 |
psql_like($node, "copy (values ('foo'),('bar')) to stdout \\g | $pipe_cmd", |
| 484 |
qr//, "copy output passed to \\g pipe"); |
| 485 |
my $c4 = slurp_file($g_file); |
| 486 |
like($c4, qr/foo.*bar/s); |
| 487 |
|
| 488 |
# Test COPY within pipelines. These abort the connection from |
| 489 |
# the frontend so they cannot be tested via SQL. |
| 490 |
$node->safe_psql('postgres', 'CREATE TABLE psql_pipeline()'); |
| 491 |
my $log_location = -s $node->logfile; |
| 492 |
psql_fails_like( |
| 493 |
$node, |
| 494 |
qq{\\startpipeline |
| 495 |
COPY psql_pipeline FROM STDIN; |
| 496 |
SELECT 'val1'; |
| 497 |
\\syncpipeline |
| 498 |
\\endpipeline}, |
| 499 |
qr/COPY in a pipeline is not supported, aborting connection/, |
| 500 |
'COPY FROM in pipeline: fails'); |
| 501 |
$node->wait_for_log( |
| 502 |
qr/FATAL: .*terminating connection because protocol synchronization was lost/, |
| 503 |
$log_location); |
| 504 |
|
| 505 |
# Remove \syncpipeline here. |
| 506 |
psql_fails_like( |
| 507 |
$node, |
| 508 |
qq{\\startpipeline |
| 509 |
COPY psql_pipeline TO STDOUT; |
| 510 |
SELECT 'val1'; |
| 511 |
\\endpipeline}, |
| 512 |
qr/COPY in a pipeline is not supported, aborting connection/, |
| 513 |
'COPY TO in pipeline: fails'); |
| 514 |
|
| 515 |
psql_fails_like( |
| 516 |
$node, |
| 517 |
qq{\\startpipeline |
| 518 |
\\copy psql_pipeline from stdin; |
| 519 |
SELECT 'val1'; |
| 520 |
\\syncpipeline |
| 521 |
\\endpipeline}, |
| 522 |
qr/COPY in a pipeline is not supported, aborting connection/, |
| 523 |
'\copy from in pipeline: fails'); |
| 524 |
|
| 525 |
# Sync attempt after a COPY TO/FROM. |
| 526 |
psql_fails_like( |
| 527 |
$node, |
| 528 |
qq{\\startpipeline |
| 529 |
\\copy psql_pipeline to stdout; |
| 530 |
\\syncpipeline |
| 531 |
\\endpipeline}, |
| 532 |
qr/COPY in a pipeline is not supported, aborting connection/, |
| 533 |
'\copy to in pipeline: fails'); |
| 534 |
|
| 535 |
# Test that psql counts every COPY FROM STDIN in a query string when it has |
| •536 |
# to scan the string itself (as for -c). A COPY that is not the first |
| 537 |
# sub-command must still be recognized, otherwise psql treats the server's |
| 538 |
# COPY_IN response as unexpected and aborts the connection. |
| 539 |
$node->safe_psql('postgres', 'CREATE TABLE copy_stdin_count (a int)'); |
| 540 |
|
| 541 |
my @copy_stdin_cases = ( |
| 542 |
{ |
| 543 |
name => 'single COPY FROM STDIN', |
| 544 |
sql => 'COPY copy_stdin_count FROM STDIN', |
| 545 |
data => "10\n20\n\\.\n", |
| 546 |
rows => 2, |
| 547 |
}, |
| 548 |
{ |
| 549 |
name => 'COPY FROM STDIN after another command', |
| 550 |
sql => 'SELECT 1; COPY copy_stdin_count FROM STDIN', |
| 551 |
data => "30\n40\n\\.\n", |
| 552 |
rows => 2, |
| 553 |
}, |
| 554 |
{ |
| 555 |
name => 'two COPY FROM STDIN in one string', |
| 556 |
sql => 'COPY copy_stdin_count FROM STDIN; COPY copy_stdin_count FROM STDIN', |
| 557 |
data => "50\n\\.\n60\n\\.\n", |
| 558 |
rows => 2, |
| 559 |
}); |
| 560 |
|
| 561 |
foreach my $c (@copy_stdin_cases) |
| 562 |
{ |
| 563 |
$node->safe_psql('postgres', 'TRUNCATE copy_stdin_count'); |
| 564 |
|
| 565 |
my ($stdout, $stderr) = ('', ''); |
| 566 |
my $ret = IPC::Run::run( |
| 567 |
[ |
| 568 |
'psql', '-X', '-v' => 'ON_ERROR_STOP=1', |
| 569 |
'-d' => $node->connstr('postgres'), |
| 570 |
'-c' => $c->{sql} |
| 571 |
], |
| 572 |
'<' => \$c->{data}, |
| 573 |
'>' => \$stdout, |
| 574 |
'2>' => \$stderr); |
| 575 |
|
| 576 |
ok($ret, "$c->{name}: psql exits 0"); |
| 577 |
unlike($stderr, qr/unexpected COPY_IN result/, |
| 578 |
"$c->{name}: connection not aborted"); |
| 579 |
|
| 580 |
my $count = |
| 581 |
$node->safe_psql('postgres', 'SELECT count(*) FROM copy_stdin_count'); |
| 582 |
is($count, $c->{rows}, "$c->{name}: all rows loaded"); |
| 583 |
} |
| 584 |
|
| 585 |
psql_fails_like( |
| 586 |
$node, |
| 587 |
qq{\\restrict test |
| 588 |
\\! should_fail}, |
| 589 |
qr/backslash commands are restricted; only \\unrestrict is allowed/, |
| 590 |
'meta-command in restrict mode fails'); |
| 591 |
|
| 592 |
psql_fails_like( |
| 593 |
$node, |
| 594 |
qq{\\restrict test |
| 595 |
\\unrestrict `echo test`}, |
| 596 |
qr/wrong key/, |
| 597 |
'\unrestrict does not do backquote expansion'); |
| 598 |
|
| 599 |
done_testing(); |