Fixed: Cancelling Postgres Queries

We’ve fixed an issue where the Fly Proxy was rejecting CancelRequest messages through the pg_tls handler. This Postgres feature lets you cancel a query by opening a new connection and sending a CancelRequest instead of a StartupPacket. Until now, this confused the Proxy, and the message did not make it through to your database.

The main places you’re likely to see an improvement are:

  1. If you’re in a psql session, hitting Ctrl+C while running a query sends a CancelRequest.
  2. Golang’s lib/pq driver automatically sends a CancelRequest for any context cancellation or timeout.
  3. Other drivers that support explicit query cancellation, like Python’s connection.cancel() or Ruby’s PG::Connection#cancel, send CancelRequests.

Without the CancelRequest getting through, Postgres kept toiling away on those queries. Not nice!

This fix works for anything going through the pg_tls handler in the Proxy. That includes unmanaged Postgres instances and direct connections to Fly Managed Postgres (MPG). Connections to MPG going through the connection pooler weren’t affected.

Bonus fix: in today’s flyctl we added proper Ctrl+C handling for the psql session that fly mpg connect spins up. Now if you’re running a query there you can hit Ctrl+C, and it will (a) cancel the query, and (b) not quit flyctl.

4 Likes