kqueue backend for pgbench

Started by Roman Khapov29 days ago8 messageshackers
Jump to latest
#1Roman Khapov
rkhapov@yandex-team.ru

Hi hackers!

While working on [1]https://github.com/yandex/odyssey I run performance tests on different environments,
including laptop with macOS. pgbench is usually chosen for this,
but on one of the last runs I discovered that it uses select() on macOS (which lacks
the ppoll() syscall), which caps the max amount of clients at 1024 (FD_SETSIZE).

This can be fixed with new implementation of socket_set based on kqueue().
The only wrinkle is fact that select() and ppoll() is stateless, while kqueue() is not
(registration persists in kernel across kevent() calls), so wait_on_socket_set() must be
written in harder way than other implementations:
- disable stale registrations: EV_DISABLE for every fd to drain error for closed fds
- enable current set with EV_ENABLE, call kevent()
- report results (save flags in socket_set->input_ready) + preserving the set for the next iteration

POC-patch attached, any feedback would be appreciated!

[1]: https://github.com/yandex/odyssey

--
Best regards,
Roman Khapov

Attachments:

t254386_1
0001-pgbench-add-kqueue-implementation-of-the-socket-set-.patchapplication/octet-stream; name=0001-pgbench-add-kqueue-implementation-of-the-socket-set-.patch; x-unix-mode=0644Download+232-7
#2Kirill Reshke
reshkekirill@gmail.com
In reply to: Roman Khapov (#1)
Re: kqueue backend for pgbench

On Mon, 26 Oct 2026 at 11:42, Roman Khapov <rkhapov@yandex-team.ru> wrote:

Hi hackers!

While working on [1] I run performance tests on different environments,
including laptop with macOS. pgbench is usually chosen for this,
but on one of the last runs I discovered that it uses select() on macOS (which lacks
the ppoll() syscall), which caps the max amount of clients at 1024 (FD_SETSIZE).

Hi! pgbench with this many clients is an uncommon case, but I can see
the benefit in stress-testing connection allocation, which in case of
connection pooler is of interest. So overall +1 for supporting this

This can be fixed with new implementation of socket_set based on kqueue().
The only wrinkle is fact that select() and ppoll() is stateless, while kqueue() is not
(registration persists in kernel across kevent() calls), so wait_on_socket_set() must be
written in harder way than other implementations:
- disable stale registrations: EV_DISABLE for every fd to drain error for closed fds
- enable current set with EV_ENABLE, call kevent()
- report results (save flags in socket_set->input_ready) + preserving the set for the next iteration

POC-patch attached, any feedback would be appreciated!

Tested on my mac, it works.
Didn't look much in code detail here, but the nested stale-fd loop is
O(nregistered * (num of EV_ERROR)) . Probably fine at pgbench scale.

[1] - https://github.com/yandex/odyssey

--
Best regards,
Roman Khapov

--
Best regards,
Kirill Reshke

#3Yura Sokolov
y.sokolov@postgrespro.ru
In reply to: Kirill Reshke (#2)
Re: kqueue backend for pgbench

28.10.2026 08:12, Kirill Reshke wrote:

On Mon, 26 Oct 2026 at 11:42, Roman Khapov <rkhapov@yandex-team.ru> wrote:

Hi hackers!

While working on [1] I run performance tests on different environments,
including laptop with macOS. pgbench is usually chosen for this,
but on one of the last runs I discovered that it uses select() on macOS (which lacks
the ppoll() syscall), which caps the max amount of clients at 1024 (FD_SETSIZE).

Hi! pgbench with this many clients is an uncommon case, but I can see
the benefit in stress-testing connection allocation, which in case of
connection pooler is of interest. So overall +1 for supporting this

We regularly test with couple of thousand clients. But we have to set -j
matching number of -c because otherwise pgbench is bottleneck itself.

Partially it is because of using ppoll, partially because some algorithms
are O(n) on every wakeup in pgbench.

pgbench definitely needs more love.

--
regards
Yura Sokolov aka funny-falcon

#4Roman Khapov
rkhapov@yandex-team.ru
In reply to: Yura Sokolov (#3)
Re: kqueue backend for pgbench

We regularly test with couple of thousand clients. But we have to set -j
matching number of -c because otherwise pgbench is bottleneck itself.

To achieve max TPS we do the same thing, but sometimes there must be
clients >> threads, ex: testing the connection overhead itself and pool management
cost in poolers :)

pgbench definitely needs more love.

fully agree :)

--
Best regards,
Roman Khapov

#5Roman Khapov
rkhapov@yandex-team.ru
In reply to: Kirill Reshke (#2)
Re: kqueue backend for pgbench

Tested on my mac, it works.

thanks!

Didn't look much in code detail here, but the nested stale-fd loop is
O(nregistered * (num of EV_ERROR)) . Probably fine at pgbench scale.

Yes, the cost is O(nregistered * nclosed), however this is the worst case where all clients
are closed (the test end or the server crash).

The only case I doubt is -C/--connect test. On the other hand
there is sync call of PQconnectdb(), and the kernel usually hands
back the same fd number, in which case EV_ADD just
re-attaches the knote silently, without EV_ERROR.

Ofc this can be fixed with some algo based on sort+merge, but for now I prefer
to keep simple solution; that can be changed later.

In longer term, it might be worth redesigning the way fds are added
and deleted in threadRun loop, in the same spirit as the O(n)-per-wakeup issues
Yura mentioned above.

--
Best regards,
Roman Khapov

#6Jeff Anton
antonpgsql@hesiod.org
In reply to: Roman Khapov (#1)
Re: kqueue backend for pgbench

This is a lot of work which probably is not needed.

I don't think you understand select and the supporting fd_set type.

I'll include the NOTES from Freebsd on the select(2) man page....

NOTES
     The default size of FD_SETSIZE is currently 1024.  In order to
     accommodate programs which might potentially use a larger number
of open
     files with select(), it is possible to increase this size by
having the
     program define FD_SETSIZE before the inclusion of any header which
     includes <sys/types.h>.

     If nfds is greater than the number of open files, select() is not
     guaranteed to examine the unused file descriptors.  For historical
     reasons, select() will always examine the first 256 descriptors.

Another solution to this is to have code which does not use fd_set but
instead uses its own method of building and using bit arrays. I have
done this in my professional development.

Jeff Anton

PS: For those of you who do not know me, I was the first lead programmer
on the Postgres project at U.C.Berkeley a very long time ago. You can
blame me (or praise me) for many of the internals such as memory contexts.

Show quoted text

On 10/25/26 23:41, Roman Khapov wrote:

Hi hackers!

While working on [1] I run performance tests on different environments,
including laptop with macOS. pgbench is usually chosen for this,
but on one of the last runs I discovered that it uses select() on macOS (which lacks
the ppoll() syscall), which caps the max amount of clients at 1024 (FD_SETSIZE).

This can be fixed with new implementation of socket_set based on kqueue().
The only wrinkle is fact that select() and ppoll() is stateless, while kqueue() is not
(registration persists in kernel across kevent() calls), so wait_on_socket_set() must be
written in harder way than other implementations:
- disable stale registrations: EV_DISABLE for every fd to drain error for closed fds
- enable current set with EV_ENABLE, call kevent()
- report results (save flags in socket_set->input_ready) + preserving the set for the next iteration

POC-patch attached, any feedback would be appreciated!

[1] - https://github.com/yandex/odyssey

--
Best regards,
Roman Khapov

#7Roman Khapov
rkhapov@yandex-team.ru
In reply to: Jeff Anton (#6)
Re: kqueue backend for pgbench

Hi! Thanks for the review!

Another solution to this is to have code which does not use fd_set but instead uses its own method of building and using bit arrays. I have done this in my professional development.
I'll include the NOTES from Freebsd on the select(2) man page....

I rechecked the docs and the kernel impl example [0]https://elixir.bootlin.com/freebsd/v15.1/source/sys/kern/sys_generic.c#L1189.
There is some thought about I thinking:
- max absolute fd number will be limited by FD_SETSIZE, which is
defined at compile time, would it be a fragile solution for runtime anyway?
The the implementation of bit string must rely on architecture-depended things
(ex: _DARWIN_UNLIMITED_SELECT), which will lead to a patch
similar in size to the proposed?
- the cost of operations will have O(max_fd) instead of O(nfd),
which is not critical for current implementation of pgbench, but in future may
lead to refactor of struct socket_set or the code below
- kevent already used in backend code base for cases of several fds,
proposed patch would be just continuation of [1]https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=60e612b602999e670f2d57a01e52799eaa903ca9 which have same style
to waiteventset.c

[0]: https://elixir.bootlin.com/freebsd/v15.1/source/sys/kern/sys_generic.c#L1189
[1]: https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=60e612b602999e670f2d57a01e52799eaa903ca9

--
Best regards,
Roman Khapov

#8Roman Khapov
rkhapov@yandex-team.ru
In reply to: Roman Khapov (#1)
Re: kqueue backend for pgbench

POC-patch attached, any feedback would be appreciated!

I did some additional performance tests of the patch and found
a degradation with small client counts and trivially simple queries; select()
executes more ~5-8% tps transactions than v1 kevent() implementation.

This is due to the unconditional phase-1 disable/GC sync, which
costs an extra kevent() call per wait. In most cases it can be
skipped though: if the fd set is unchanged since the previous wait,
the kernel-side registrations are already in place, and we only need
to re-submit the enable changes (they are what re-attaches a knote
whose fd was closed and reused for another connection in between)
and wait for events. Attached v2 introduces this optimization.

Some measurements from my laptop (macOS, local server,
prepared protocol, medians of 3-4 runs, delta is v2 relative to select()):
select() v1 v2 delta
SELECT 1, -c 32 -j 1 ~110k ~101k ~106k -4%
SELECT 1, -c 128 -j 8 ~99k ~83k ~103k +4%
-S, -c 32 -j 32 ~96k ~89k ~99k +3%
-S, -c 512 -j 1 ~77k ~76k ~76k -1%
-S, -c 512 -j 8 ~71k ~72k ~73k +3%
-C, -c 128 -j 1 ~358 ~357 ~359 0%
-C -R, -c 128 -j 1 ~358 ~356 ~359 0%
-C -R, -c 128 -j 8 ~502 ~496 ~509 +1%
(values are tps)

With this, v2 matches select() wherever select() can run at all.

--
Best regards,
Roman Khapov

Attachments:

t254386_8
v2-0001-pgbench-add-kqueue-implementation-of-the-socket-s.patchapplication/octet-stream; name=v2-0001-pgbench-add-kqueue-implementation-of-the-socket-s.patch; x-unix-mode=0644Download+238-7