Skip to content

Commit 06ff064

Browse files
committed
Don't spuriously report FD_SETSIZE exhaustion on Windows.
Starting on 2023-08-03, this intermittently terminated a "pgbench -C" test in CI. It could affect a high-client-count "pgbench" without "-C". While parallel reindexdb and vacuumdb reach the same problematic check, sufficient client count and/or connection turnover is less plausible for them. Given the lack of examples from the buildfarm or from manual builds, reproducing this must entail rare operating system configurations. Also correct the associated error message, which was wrong for non-Windows. Back-patch to v12, where the pgbench check first appeared. While v11 vacuumdb has the problematic check, reaching it with typical vacuumdb usage is implausible. Reviewed by Thomas Munro. Discussion: https://postgr.es/m/CA+hUKG+JwvTNdcyJTriy9BbtzF1veSRQ=9M_ZKFn9_LqE7Kp7Q@mail.gmail.com
1 parent 0002feb commit 06ff064

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

src/bin/pgbench/pgbench.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7782,14 +7782,23 @@ clear_socket_set(socket_set *sa)
77827782
static void
77837783
add_socket_to_set(socket_set *sa, int fd, int idx)
77847784
{
7785+
/* See connect_slot() for background on this code. */
7786+
#ifdef WIN32
7787+
if (sa->fds.fd_count + 1 >= FD_SETSIZE)
7788+
{
7789+
pg_log_error("too many concurrent database clients for this platform: %d",
7790+
sa->fds.fd_count + 1);
7791+
exit(1);
7792+
}
7793+
#else
77857794
if (fd < 0 || fd >= FD_SETSIZE)
77867795
{
7787-
/*
7788-
* Doing a hard exit here is a bit grotty, but it doesn't seem worth
7789-
* complicating the API to make it less grotty.
7790-
*/
7791-
pg_fatal("too many client connections for select()");
7796+
pg_log_error("socket file descriptor out of range for select(): %d",
7797+
fd);
7798+
pg_log_error_hint("Try fewer concurrent database clients.");
7799+
exit(1);
77927800
}
7801+
#endif
77937802
FD_SET(fd, &sa->fds);
77947803
if (fd > sa->maxfd)
77957804
sa->maxfd = fd;

src/fe_utils/parallel_slot.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,41 @@ connect_slot(ParallelSlotArray *sa, int slotno, const char *dbname)
295295
slot->connection = connectDatabase(sa->cparams, sa->progname, sa->echo, false, true);
296296
sa->cparams->override_dbname = old_override;
297297

298-
if (PQsocket(slot->connection) >= FD_SETSIZE)
299-
pg_fatal("too many jobs for this platform");
298+
/*
299+
* POSIX defines FD_SETSIZE as the highest file descriptor acceptable to
300+
* FD_SET() and allied macros. Windows defines it as a ceiling on the
301+
* count of file descriptors in the set, not a ceiling on the value of
302+
* each file descriptor; see
303+
* https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-select
304+
* and
305+
* https://learn.microsoft.com/en-us/windows/win32/api/winsock/ns-winsock-fd_set.
306+
* We can't ignore that, because Windows starts file descriptors at a
307+
* higher value, delays reuse, and skips values. With less than ten
308+
* concurrent file descriptors, opened and closed rapidly, one can reach
309+
* file descriptor 1024.
310+
*
311+
* Doing a hard exit here is a bit grotty, but it doesn't seem worth
312+
* complicating the API to make it less grotty.
313+
*/
314+
#ifdef WIN32
315+
if (slotno >= FD_SETSIZE)
316+
{
317+
pg_log_error("too many jobs for this platform: %d", slotno);
318+
exit(1);
319+
}
320+
#else
321+
{
322+
int fd = PQsocket(slot->connection);
323+
324+
if (fd >= FD_SETSIZE)
325+
{
326+
pg_log_error("socket file descriptor out of range for select(): %d",
327+
fd);
328+
pg_log_error_hint("Try fewer jobs.");
329+
exit(1);
330+
}
331+
}
332+
#endif
300333

301334
/* Setup the connection using the supplied command, if any. */
302335
if (sa->initcmd)

0 commit comments

Comments
 (0)