pgsql: psql: Stop scripts on Ctrl+C while waiting for COPY or prompt in

From: Peter Eisentraut <peter(at)eisentraut(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: psql: Stop scripts on Ctrl+C while waiting for COPY or prompt in
Date: 2026-11-09 09:27:25
Message-ID: E1xQLev-000000000Rt-3X7r@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

psql: Stop scripts on Ctrl+C while waiting for COPY or prompt input

When running a script non-interactively, pressing Ctrl+C while a query
is running aborts the whole script and psql exits with code 3. But when
psql was instead waiting for input, e.g. COPY FROM STDIN data via
\copy ... from pstdin, or input for \prompt or \password, pressing
Ctrl+C only aborted that single command and the rest of the script
continued running. For example with this script:

\copy t1 from pstdin
select now();

Pressing Ctrl+C while psql is waiting for COPY data would abort the
COPY, but then still run the SELECT and exit successfully.

The reason for this inconsistency was that psql_cancel_callback() only
set cancel_pressed when it did not longjmp out of waiting for input.
That's fine for the longjmp target in MainLoop(), which itself already
stops a non-interactive script when psql gets interrupted while waiting
for the next line of input. But the longjmp targets in handleCopyIn()
and pg_get_line_append() (used by simple_prompt_extended()) simply
return to their callers, which then have no way of knowing that the
user wanted to stop the script.

This fixes that by always setting cancel_pressed before doing the
longjmp. Interactive sessions are not affected, because MainLoop()
clears cancel_pressed again for those.

Author: Jelte Fennema-Nio <postgres(at)jeltef(dot)nl>
Discussion: https://www.postgresql.org/message-id/flat/DEY0N7FS8NCU(dot)1F7QXGG37MQ09(at)jeltef(dot)nl

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/8b529705d153f6bc592f874a9f2dd47be8fa0589

Modified Files
--------------
src/bin/psql/common.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Robert Haas 2026-11-09 11:15:04 Re: pgsql: ci: Enable PCH in mingw cross job
Previous Message Aleksander Alekseev 2026-11-09 08:26:50 Re: pgsql: Make GetSnapshotData() more resilient on out-of-memory errors