Visitar URL original
Cancelling query send does not cancel query · Issue #1607 · dbcli/pgcli · GitHub
Skip to content

Cancelling query send does not cancel query #1607

Description

@PadenZach

I'd expect ctrl-c or ctrl-d that cancels a query in the foreground, to also cancel that query on the backend (the postgres server).

for example:

postgres> SELECT some_long_running_query();
^C^Ccancelled query
postgres> select 1;
sending query failed: another command is already in progress
Time: 0.004s
postgres>
A transaction is ongoing. Choose `c` to COMMIT, `r` to ROLLBACK, `a` to abort exit, `force` to exit anyway. [a]: a
postgres> select 1;
A transaction is ongoing. Choose `c` to COMMIT, `r` to ROLLBACK, `a` to abort exit, `force` to exit anyway. [a]: r
sending query failed: another command is already in progress
another command is already in progress
another command is already in progress
another command is already in progress
Time: 0.004s

Activity

  1. devadathanmb commented on Jul 4, 2026

    @devadathanmb
    Contributor

    This seems to be already handled by psycopg, the driver pgcli uses to talk to Postgres.

    When you hit Ctrl-C during a query, psycopg catches it and sends a real cancel request to the server, then drains the resulting "canceling statement due to user request" error off the socket before letting the interrupt reach pgcli (see the source). That's essentially the same way psql also handles Ctrl-C. pgcli doesn't do anything extra on top of this, it just lets that exception bubble up and prints "cancelled query", the actual cancellation happens inside psycopg.

    I tried to reproduce this issue on the latest release and couldn't. The query gets cancelled server-side correctly every time I've tested it.

    There's one case where it could theoretically break: pressing Ctrl-C a second time before the first cancel finishes draining. That second drain is only guarded against a QueryCanceled error, so a second KeyboardInterrupt landing there escapes uncaught, before the pending result is fully read off the socket, and the connection is left desynced (next query fails with another command is already in progress until you reconnect). That window is roughly the cancel request's round trip time, which would be wider against a remote or high-latency database than on localhost.

    Which pgcli version are you on, and are you connecting directly or through something like PgBouncer or an SSH tunnel? Did you press Ctrl-C more than once when this happened?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions