From: Peter Eisentraut <peter@eisentraut.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: psql: Stop scripts on Ctrl+C while waiting for COPY or prompt in
Date: Mon, 09 Nov 2026 09:27:25 +0000
Message-ID: <E1xQLev-000000000Rt-3X7r@gemulon.postgresql.org> (raw)
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@jeltef.nl>
Discussion: https://www.postgresql.org/message-id/flat/DEY0N7FS8NCU.1F7QXGG37MQ09@jeltef.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(-)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: peter@eisentraut.org
Subject: Re: pgsql: psql: Stop scripts on Ctrl+C while waiting for COPY or prompt in
In-Reply-To: <E1xQLev-000000000Rt-3X7r@gemulon.postgresql.org>
References: <E1xQLev-000000000Rt-3X7r@gemulon.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox