Re: Don't use the deprecated and insecure PQcancel in our frontend tools anymore

Heikki Linnakangas <hlinnaka@iki.fi>

From: Heikki Linnakangas <hlinnaka@iki.fi>
To: Jelte Fennema-Nio <postgres@jeltef.nl>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>, Alvaro Herrera <alvherre@alvh.no-ip.org>, Jacob Champion <jacob.champion@enterprisedb.com>
Date: 2026-07-07T14:52:12Z
Lists: pgsql-hackers
On 07/07/2026 10:54, Jelte Fennema-Nio wrote:
> On Tue Jul 7, 2026 at 12:12 AM CEST, Heikki Linnakangas wrote:
>> This relies on the signal to interrupt select(), but I'm afraid that's 
>> not guaranteed on all platforms. Also, there's a race condition if the 
>> signal arrives *just* before you call select(). That's what the "self- 
>> pipe hack" is for, see comments at waiteventset.c.
> 
> Ugh yes you're right. The comment above that block told me otherwise and
> I didn't question it. Attached is a new version that improves the
> comment and starts using the same 1 second poll on all platforms.

Yeah, I guess that works.. But It's a little unsatisfactory to have to 
poll, where we didn't poll before. I started to go down the rabbit hole, 
here are the options I can think of:

a) switch to pselect() where available, fall back to select() with 
timeout. We don't currently use pselect() anywhere, so this needs a new 
configure check.

b) switch to ppoll() where available, fall back to select() with 
timeout. ppoll() is already used in pgbench, and is a better interface 
in general. However, it's different from select(), so the fallback code 
would differ more.

c) switch to ppoll() where available, fall back to poll() with timeout. 
Use WaitForMultipleObjectsEx() on Windows (it has neither ppoll() nor 
poll()). The fallback would look more similar to the main code path than 
with b), but the Windows implementation would be even more different. We 
could actually eliminate the polling in Windows too, if we added an 
Event object for the cancellation too.

d) like c), but write a helper function to implement poll()/ppoll() on 
Windows using WaitForMultipleObjectsEx(). Could be used in more places then.

d) seems like the nicest option in the long run, but takes a little 
effort, and I don't have a Windows system to test on currently.


While poking around, I noticed that we actually already have a win32 
implementation of select() using WaitForMultipleObjectsEx(), in 
src/backend/port/win32/socket.c. But AFAICS it's not used anywhere.

Other prior art: In libpq we have PQsocketPoll(), which uses poll() or 
select(), but it only operates on a single socket.

I wonder if we could bump up our minimum portability requirements, and 
e.g. require ppoll() on all all non-Windows systems.

- Heikki