Improve the granularity of PQsocketPoll's timeout parameter?
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-hackers@lists.postgresql.org
Cc: "Tristan Partin" <tristan@partin.io>, Robert Haas <robertmhaas@gmail.com>,
Dominique Devienne <ddevienne@gmail.com>
Date: 2024-06-10T21:39:35Z
Lists: pgsql-hackers
Attachments
- pqsocketpoll-in-microseconds-v1.patch (text/x-diff) patch v1
In [1] Dominique Devienne complained that PQsocketPoll would be far more useful to him if it had better-than-one-second timeout resolution. I initially pushed back on that on the grounds that post-beta1 is a bit late to be redefining public APIs. Which it is, but if we don't fix it now then we'll be stuck supporting that API indefinitely. And it's not like one-second resolution is great for our internal usage either --- for example, I see that psql is now doing end_time = time(NULL) + 1; rc = PQsocketPoll(sock, forRead, !forRead, end_time); which claims to be waiting one second, but actually it's waiting somewhere between 0 and 1 second. So I thought I'd look into whether we can still change it without too much pain, and I think we can. The $64 question is how to represent the end_time if not as time_t. The only alternative POSIX offers AFAIK is gettimeofday's "struct timeval", which is painful to compute with and I don't think it's native on Windows. What I suggest is that we use int64 microseconds since the epoch, which is the same idea as the backend's TimestampTz except I think we'd better use the Unix epoch not 2000-01-01. Then converting code is just a matter of changing variable types and adding some zeroes to constants. The next question is how to spell "int64" in libpq-fe.h. As a client-exposed header, the portability constraints on it are pretty stringent, so even in 2024 I'm loath to make it depend on <stdint.h>; and certainly depending on our internal int64 typedef won't do. What I did in the attached is to write "long long int", which is required to be at least 64 bits by C99. Other opinions are possible of course. Lastly, we need a way to get current time in this form. My first draft of the attached patch had the callers calling gettimeofday and doing arithmetic from that, but it seems a lot better to provide a function that just parallels time(2). BTW, I think this removes the need for libpq-fe.h to #include <time.h>, but I didn't remove that because it seems likely that some callers are indirectly relying on it to be present. Removing it wouldn't gain very much anyway. Thoughts? regards, tom lane [1] https://www.postgresql.org/message-id/CAFCRh-8hf%3D7V8UoF63aLxSkeFmXX8-1O5tRxHL61Pngb7V9rcw%40mail.gmail.com
Commits
-
Improve the granularity of PQsocketPoll's timeout parameter.
- 105024a47238 17.0 landed