Message-ID: From: "beikov (@beikov)" To: "pgjdbc/pgjdbc" Date: Fri, 27 Mar 2026 11:59:49 +0000 Subject: Re: [pgjdbc/pgjdbc] PR #4010: [#2325] Execute queries as pipelined when running on virtual thread In-Reply-To: References: List-Id: X-GitHub-Author-Login: beikov X-GitHub-Comment-Id: 4142106500 X-GitHub-Comment-Type: issue_comment X-GitHub-Edited-At: 2026-03-27T12:01:03Z X-GitHub-Issue: 4010 X-GitHub-Repo: pgjdbc/pgjdbc X-GitHub-Type: comment X-GitHub-Url: https://github.com/pgjdbc/pgjdbc/pull/4010#issuecomment-4142106500 Content-Type: text/plain; charset=utf-8 The design is pretty simple. First, the code extracted the queues of the `QueryExecutor` into a wrapper object and passes that to all methods instead of relying on the shared state on the `QueryExecutor`. That way, a high level operation can isolate all necessary queue objects when multiple threads call into the `QueryExecutor`. Next, the new `executePipelined` method was introduced that creates isolated queues and splits its work into 3 phases: 1. While holding a write lock, acquire a "ticket" and send operations to the server 2. Lock and wait until it's the turn of the "ticket" 3. Process results When done, it signals that the next "ticket" can start it's work. This way, we can send multiple operations to the server in a pipelined fashion, but process results in FIFO order. With e.g. virtual threads, when a read blocks a VT1, another virtual thread VT2 can send further commands before it enters Phase 2 and waits for its turn. As long as VT1 is waiting, further virtual threads can send even further commands. Once VT1 is woken up, it needs to process the results and complete before further operations can be sent by other virtual threads. This means that new operations can only be added while the thread whose turn it is waits for data in Phase 3. To achieve better pipeline usage, we'd need to defer the waking of the thread whose turn it is until all other virtual threads submitted their operations, but I don't know if there is a good way to do that. Maybe someone has an idea how to achieve better utilization?