| From: | Yura Sokolov <y(dot)sokolov(at)postgrespro(dot)ru> |
|---|---|
| To: | "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Hannu Krosing <hannuk(at)google(dot)com>, Бондарь Дмитрий <d(dot)bondar(at)postgrespro(dot)ru>, Roman Khapov <rkhapov(at)yandex-team(dot)ru> |
| Subject: | Another pgbench refactoring. |
| Date: | 2026-12-06 16:39:08 |
| Message-ID: | 0224db79-5304-4dce-9410-3de55328d1ab@postgrespro.ru |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Good day.
Here is my attempt to make good for pgbench.
My goals were:
- make it faster
- make it use epoll and easier extend to kqueue
- make it state machine more readable and easier to extend
- make query preparation and connection establishing asynchronous
To be honestly, first two goals are not quite satisfied:
- it may be faster or slower. Usually it consumes a bit more CPU. But if
there's enough CPU it could be a bit more rapid.
- epoll gives nothing for speed. It is considerably slower when most
sockets are active. That is why it is not the default, but could be
switched on with command line argument or environment variable. It could
give more stable latency though (lesser stddev).
What I really like about work done is switch from explicit state machine to
stackless coroutine style based on Duff Device. Connection state (CState)
become coroutine with stack of invoked "functions", each is written as Duff
Device using macroses. Since there's limited different "functions" all
their state is kept in CState itself. There is stack for 4 coroutines calls
and it is enough for current version.
This coroutine style makes easy to refactor connection state handling and
convert synchronous routines (connection establishing and query
preparation) into asynchronous. In consequence it allows to establish
connections in parallel at benchmark start.
Patches:
0001 - just change `progress` line argument to fractional. It isn't
important for following changes but could be used to view difference in
behaviour (benchmark warming time). And it is useful anyway for pgbench
wrappers to have more precise stats that could be bound to events in
PostgreSQL log for example.
0002 - instead of iterations over states to determine current execution
conditions, it introduces readiness queue, timer's heap and move socket
registration into advanceConnectionState.
CStates are registered in socket sets as long as they doesn't sleep in
throttling or \sleep command since advanceConnectionState usually doesn't
return until it sends command. And even at transaction end it's readiness
forces threadRun to skip check of socket_set therefore no meaningful
behaviour is changed.
0003 - add epoll backend for sockets and runtime selection of available
backends. I believe it would be ease to extend for kqueue.
Sleeping doesn't remove socket from socket_set if backend is epoll since it
epoll_ctl is costly and leaving socket registered is cheap. There's no
reading expected usually.
Well, while I'm writting this letter I recognize there could be \sleep
while pipeline is enabled. In this case there could be a problem. If
patchset will gain interest it would be easy to fix: either force exclusion
from socket_set in META_SLEEP, or forbid META_SLEEP while in pipeline mode.
0004 - tiny refactoring to prepare for coroutine transformation.
0005 - transformation to coroutine style. Waiting for result, waiting for
abort and discardUntilSync are already transformed as asynchronous
subroutines which are invoked with CORO_CALL from scriptHandling routine
(it is mostly renamed advanceConnectionState which name is reused for tiny
wrapper).
0006 - make query preparation asynchronous. And pipeline preparation reuses
single query preparation forming deepest coroutine call stack:
scriptHandling -> metaCommandHandling -> preparePipelineHandling ->
prepareCommandHandling.
0007 - make connection establishing asynchronous. And make connection
establishing parallel in common mode (ie without `-C` flag).
0008 - introduce "low latency" mode: check socket states even there are
connections "ready for execution". Usually it is situation when there's
bunch of connections in transition "End of Transaction" -> "Choose Script".
When there're not enough CPU it slows down things measurably, but reduces
reported query latency. When there's plenty of CPU it could even speedup
things slightly.
Patchset is written with help of local LLM. It is my largest experience
with LLM. Though all patches are reviewed manually and fixed if necessary.
There were several refactorings to make patches concise, so I did my best
to make them clear and understandable.
Commit 0005 - coroutine transformation - were written in this shape by
hands and then modified by LLM only to handle other commits movements.
Ok, commit messages and code comments are generated at whole. But checked.
Concurrent works:
- Pgbench: remove synchronous prepare
https://www.postgresql.org/message-id/flat/2da56b59-acb6-4ee3-af6b-6e800115dc7f%40postgrespro.ru
- kqueue backend for pgbench
https://www.postgresql.org/message-id/flat/5EF180BD-3951-4E8C-903D-D91EF54A338D%40yandex-team.ru
- Refactor pgbench to make future improvements easier
https://www.postgresql.org/message-id/flat/CAMT0RQT1BMSKqgO31QJw4razSDruW_HX6G-R_Xr7yXsfMM_H2g%40mail.gmail.com
Problem of first patch (synchronous prepare) is completely solved in this
patchset.
I believe this patchset will make solving of two other issues (kqueue and
preparation for future improvments) easier to solve.
--
regards
Yura Sokolov aka funny-falcon
| Attachment | Content-Type | Size |
|---|---|---|
| v1-0001-Allow-fractional-seconds-in-pgbench-progress.patch | text/x-patch | 5.8 KB |
| v1-0002-pgbench-rework-client-bookkeeping-in-the-thread-e.patch | text/x-patch | 41.9 KB |
| v1-0003-pgbench-add-epoll-based-wait-backend-selectable-a.patch | text/x-patch | 25.1 KB |
| v1-0004-pgbench-extract-false-branch-skipping-separate-me.patch | text/x-patch | 13.7 KB |
| v1-0005-pgbench-replace-the-connection-state-machine-with.patch | text/x-patch | 48.5 KB |
| v1-0006-pgbench-async-query-preparation.patch | text/x-patch | 15.7 KB |
| v1-0007-pgbench-asynchronous-connection.patch | text/x-patch | 15.2 KB |
| v1-0008-pgbench-add-a-low-latency-mode.patch | text/x-patch | 9.7 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Mihail Nikalayeu | 2026-12-06 16:50:00 | Re: REPACK enhancements |
| Previous Message | Shubh Mehta | 2026-12-06 16:23:48 | Re: [PATCH] psql: Add tab completion for \dO, \dc and \det |