Conversation
|
Ah wow, this is amazing! @cesco69 just curious, in your example: Afaiu if ...except that you have a test showing this is not the case ( I really thought the pg docs said that So I guess if there is no open transaction, everything is fine. 👍 Speaking of transactions, I see you have a test for it, but does the Like if I have two in-flight requests, I wouldn't want user1 issuing a Or I guess that's just on me to use a This is really great! 🎉 |
Thanks! 🙏 You're right about error isolation, each query gets its own sync message, so errors don't cascade outside of transactions. The postgres docs refer to aborting "the current transaction", but in autocommit mode (no explicit BEGIN), each statement is its own transaction. And yes, for transactions you'd use pool.connect() to get a dedicated client, same as without pipeline mode. Pipeline mode doesn't change connection ownership semantics, it just optimizes the wire protocol for that connection. I have add more tests! |
This PR implements pipeline mode, allowing multiple queries to be sent to the PostgreSQL server without waiting for the results of previous queries. This significantly reduces network latency, especially for batch operations or high-latency connections.
Usage
Enabling Pipeline Mode
Pipeline mode is opt-in and can be enabled when creating a client or pool:
When pipeline mode is enabled, you can submit multiple queries and they will be sent to the server immediately:
Backpressure
When submitting a large number of queries in pipeline mode, you may want to limit how many queries are pending at once to prevent memory issues. The client supports configurable backpressure:
When the number of pending queries reaches
pipelineMaxQueries, new queries are queued internally and will be sent once space becomes available. The client emits events to help you monitor backpressure:You can also check the current pipeline depth:
The
pipelineDrainevent is emitted when the pending query count drops below 75% ofpipelineMaxQueries(the "low water mark").Error Isolation
If one query fails in pipeline mode, other queries continue to execute:
Prepared Statements in Pipeline Mode
Prepared statements work in pipeline mode, including concurrent queries with the same statement name:
Restrictions and Limitations
pg-native, would require:Note: Multi-statement queries (e.g.,
SELECT 1; SELECT 2) are rejected by PostgreSQL when using the extended query protocol, which pipeline mode uses. This is a PostgreSQL limitation.Transaction Behavior
Errors inside a transaction will abort subsequent queries until
ROLLBACK. This is standard PostgreSQL behavior. If you sendCOMMITon an aborted transaction, PostgreSQL automatically converts it toROLLBACK:When to Use Pipeline Mode
Pipeline mode is most beneficial when:
Related Issues
Benchmark
I have also updated the benchmark b189ed5
Raw Output