From the title I was hoping for this being hacky on the server application side, like how it aborts and clears the memory for a running query. Still an interesting read. Just wondering, why can't the TCP connection of the query not be used to send a cancellation request? Why does it have the be out of band?
The way CTRL-C in Postgres CLI cancels queries is incredibly hack-y
11–20 of 50 posts
Re: The way CTRL-C in Postgres CLI cancels queries is incredibly hack-y
#12I think I can understand why this wasn’t addressed for so long: in the vast majority of cases if your db is exposed on a network level to untrusted sources, then you probably have far bigger problems?
Re: The way CTRL-C in Postgres CLI cancels queries is incredibly hack-y
#13TCP has an "urgent data" feature that might have been used for this kind of thing, used for Ctrl-C in telnet, etc. It can be used to bypass any pending send buffer and received by the server ahead of any unread data.
Re: The way CTRL-C in Postgres CLI cancels queries is incredibly hack-y
#14Re: The way CTRL-C in Postgres CLI cancels queries is incredibly hack-y
#15From the title I was hoping for this being hacky on the server application side, like how it aborts and clears the memory for a running query. Still an interesting read. Just wondering, why can't the TCP connection of the query not be used to send a cancellation request? Why does it have the be out of band?
Because Postgres is a very old codebase and was written in a style that assumes there are no threads, and thus there's nothing to listen for a cancellation packet whilst work is getting done. A lot of UNIXes had very poor support for threads for a long time and so this kind of multi-process architecture is common in old codebases. The TCP URG bit came out of this kind of problem. It triggers a SIGURG signal on UNIX w…
Re: The way CTRL-C in Postgres CLI cancels queries is incredibly hack-y
#16From the title I was hoping for this being hacky on the server application side, like how it aborts and clears the memory for a running query. Still an interesting read. Just wondering, why can't the TCP connection of the query not be used to send a cancellation request? Why does it have the be out of band?
Because then the cancellation request would get queued behind any other data that's in flight from the client to the server. In the worst case the TCP buffers are full, and the client cannot even send the request until the server processes some of the existing data that's in-flight.
At the receiver, a signal handler must be used, which will be invoked when an urgent packet is received, with SIGURG.
Re: The way CTRL-C in Postgres CLI cancels queries is incredibly hack-y
#17Re: The way CTRL-C in Postgres CLI cancels queries is incredibly hack-y
#18[flagged]
I've never had to connect to PostGres in an adversarial environment. I've been at work or at home and I connected to PostGres instances owned by me or my employer. If I tried to connect to my work instance from a coffee shop, the first thing I'd do would be to log in to a VPN. That's your multiplexed protocol layer right there: the security happens at the network layer and your cancel happens at the application layer.
This is a different situation from websites. I connect to websites owned by third parties all the time, and I want my communication there to be encrypted at the application layer.
Re: The way CTRL-C in Postgres CLI cancels queries is incredibly hack-y
#19From the title I was hoping for this being hacky on the server application side, like how it aborts and clears the memory for a running query. Still an interesting read. Just wondering, why can't the TCP connection of the query not be used to send a cancellation request? Why does it have the be out of band?
Because Postgres is a very old codebase and was written in a style that assumes there are no threads, and thus there's nothing to listen for a cancellation packet whilst work is getting done. A lot of UNIXes had very poor support for threads for a long time and so this kind of multi-process architecture is common in old codebases. The TCP URG bit came out of this kind of problem. It triggers a SIGURG signal on UNIX w…
https://datatracker.ietf.org/doc/html/rfc6093:
“it is strongly recommended that applications do not employ urgent indications. Nevertheless, urgent indications are still retained as a mandatory part of the TCP protocol to support the few legacy applications that employ them. However, it is expected that even these applications will have difficulties in environments with middleboxes.”