<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Aug 5, 2024 at 4:55 PM Tatsuo Ishii <<a href="mailto:ishii@postgresql.org">ishii@postgresql.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi Usama,<br>
<br>
Thank you for the patch! I will look into the patch but it may take a<br>
while due to the size of patch.<br></blockquote><div><br></div><div>Thanks, Ishii San.<br>you are right,, the patch is quite a huge one. I tried to divide it into more chinks, but unfortunately couldn't manage to do that as things were so tied to each other.</div><div><br></div><div>Best regards</div><div>Muhammad Usama</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
Best reagards,<br>
--<br>
Tatsuo Ishii<br>
SRA OSS LLC<br>
English: <a href="https://url.avanan.click/v2/___http://www.sraoss.co.jp/index_en/___.YXAzOnBlcmNvbmE6YTpnOjM4NjU0NTI5OGQ5ZTE2ZjA4YTU4N2YyMDM3MWQ2N2Q5OjY6YTU4NTo2YzMyMzBlYjc2NTA2ZjZiNWI0MWRjYzY4MjYzYmEzZDZlYzU2YjMyMWNkMTlhODlhMzdhM2I4ZDM3ZmYxYzBmOnA6VDpO" rel="noreferrer" target="_blank">https://url.avanan.click/v2/___http://www.sraoss.co.jp/index_en/___.YXAzOnBlcmNvbmE6YTpnOjM4NjU0NTI5OGQ5ZTE2ZjA4YTU4N2YyMDM3MWQ2N2Q5OjY6YTU4NTo2YzMyMzBlYjc2NTA2ZjZiNWI0MWRjYzY4MjYzYmEzZDZlYzU2YjMyMWNkMTlhODlhMzdhM2I4ZDM3ZmYxYzBmOnA6VDpO</a><br>
Japanese:<a href="https://url.avanan.click/v2/___http://www.sraoss.co.jp___.YXAzOnBlcmNvbmE6YTpnOjM4NjU0NTI5OGQ5ZTE2ZjA4YTU4N2YyMDM3MWQ2N2Q5OjY6ZGNhZTpiMzI3MTdmYTM5NWU3NjRhZDFkMzUyNGE1YWYzOGIxZTkxZDkzMDk0MGRmZTIwYTM5NzJkMGU1NjNlMTBjYjljOnA6VDpO" rel="noreferrer" target="_blank">https://url.avanan.click/v2/___http://www.sraoss.co.jp___.YXAzOnBlcmNvbmE6YTpnOjM4NjU0NTI5OGQ5ZTE2ZjA4YTU4N2YyMDM3MWQ2N2Q5OjY6ZGNhZTpiMzI3MTdmYTM5NWU3NjRhZDFkMzUyNGE1YWYzOGIxZTkxZDkzMDk0MGRmZTIwYTM5NzJkMGU1NjNlMTBjYjljOnA6VDpO</a><br>
<br>
> Hi Pgpool-II Community,<br>
> <br>
> I've been working on global connection pooling for Pgpool-II.<br>
> Traditionally, Pgpool-II supports local connection pooling, where each<br>
> child process maintains its own connection pool. However, this approach has<br>
> several limitations:<br>
> <br>
> -- Inefficiency in Connection Utilization: There are always (max_pool - 1)<br>
> * num_init_children pooled connections that remain unusable. This<br>
> inefficiency requires setting a higher number for max_connections on<br>
> PostgreSQL than needed.<br>
> -- Random Child Process Selection: Since child process selection for new<br>
> client connections is random, we can't ensure that a child with a<br>
> particular backend connection gets the incoming connection. This often<br>
> results in existing pooled connections being dropped and new ones created,<br>
> especially with multiple DB-USER pairs.<br>
> -- Limited Pooling Modes: The current architecture restricts connection<br>
> pooling to session pooling only.<br>
> -- Backend Connection Requirement: The per-child connection pool<br>
> architecture requires more backend connections than the application needs,<br>
> countering the purpose of a connection pooler.<br>
> <br>
> I've added a new global connection pooling option to Pgpool-II to address<br>
> these issues.<br>
> <br>
> Global Connection Pooling<br>
> <br>
> In Pgpool-II, global connection pooling means the main process maintains<br>
> the entire connection pool instead of individual child processes. Pooled<br>
> connections are leased to child processes as needed. This approach<br>
> addresses the shortcomings mentioned above and enhances Pgpool-II's<br>
> connection pooling capabilities, offering features like load balancing and<br>
> failover.<br>
> <br>
> Technical Challenges<br>
> <br>
> Implementing global connection pooling in Pgpool-II's multi-process<br>
> architecture required significant technical effort, particularly in<br>
> managing backend sockets between Pgpool children. We also ensured minimal<br>
> performance impact when pooled processes were acquired or released.<br>
> <br>
> Benefits<br>
> <br>
> -- Reduced max_connections Requirement: We need fewer max_connections on<br>
> PostgreSQL side to handle the same number of application connections,<br>
> improving overall performance.<br>
> -- Elimination of Wasted Connections: No more unusable pooled connections.<br>
> -- Efficient Connection Utilization: Pooled connections are leased to child<br>
> processes with precise backend connection requirements.<br>
> -- Fine-Grained Control: We can set maximum connections for specific<br>
> DB-USER pairs and implement separate bucket sizes.<br>
> -- Support for Transaction Pooling: The new architecture allows<br>
> implementing transaction pooling modes.<br>
> <br>
> Architecture Notes<br>
> <br>
> New Configuration Parameters:<br>
> <br>
> -- connection_pool_type: Allows selection between classic and global<br>
> connection pool strategies for Pgpool-II.<br>
> -- max_pool_size: Configures the size of the global connection pool.<br>
> <br>
> Internals<br>
> <br>
> -- Main Process Pool Management: The Pgpool-II main process maintains the<br>
> global connection pool in shared memory and leases connections to child<br>
> processes upon request.<br>
> -- Unix Domain Sockets: The main process uses Unix domain sockets to<br>
> receive client requests and transfer backend sockets to and from child<br>
> processes.<br>
> -- Backend Connection Handling: The main process never creates or<br>
> terminates backend connections, ensuring it never blocks and continues<br>
> serving children's requests. When a child process leases an empty pool<br>
> slot, it connects and authenticates the backend and returns it to the main<br>
> pool after use. Thus, the child processes handle all backend communication,<br>
> while the main process manages the registry and connected sockets.<br>
> -- Efficient Data Transfer: Only sockets are transferred to child processes<br>
> through IPC sockets, while other connection data is read and written<br>
> directly by the child process in shared memory. This speeds up the lease<br>
> and return operations.<br>
> -- Minimal Locking: Since the main process moderates pooled connections, no<br>
> locks are required on the connection pool.<br>
> -- Failover Handling: In the event of a failover, the main process marks<br>
> affected connections as need_cleanup. The actual cleanup occurs when the<br>
> pooled connection is leased to a child process, which processes the cleanup.<br>
> -- New Backend Node Addition: When a new backend node is added, the child<br>
> process checks the backend_status of each node in the pooled connection and<br>
> establishes any missing backend node connections as needed.<br>
> -- Consistent Interface: Pgpool's internal interface to the connection pool<br>
> remains consistent for both classic and global connection pools. The code<br>
> calls connection pool functions that internally invoke the respective<br>
> function pointers for classic or global pooling, based on the currently<br>
> active connection pool.<br>
> -- Shared Memory Management: Enhanced shared memory management to<br>
> accommodate the connection pool data and support efficient data operations.<br>
> -- Concurrency Handling: The architecture ensures concurrency control,<br>
> allowing multiple child processes to request and release pooled connections<br>
> efficiently without significant performance penalties.<br>
> <br>
> Additional Changes<br>
> <br>
> -- Enhanced Regression Tests: Allow selection of connection pool mode for<br>
> regression testing<br>
> -- Shared Memory Data: Moved all connection pool-related data to shared<br>
> memory.<br>
> -- Updated Structures: Renamed and restructured connection-related data for<br>
> clarity.<br>
> -- Compilation Fixes: Fixed issues with compilation when cassert is enabled.<br>
> <br>
> Remaining TODOs<br>
> <br>
> -- Handle the drop database command.<br>
> -- Implement recycling of idle database connections.<br>
> -- Restructure SSL context handling for pooled connections to resolve<br>
> issues with second-time use.<br>
> -- Documentation and remove any remaining debugging or dead code.<br>
> <br>
> Next Steps<br>
> <br>
> Please review the global connection pool's architecture, design, and<br>
> functionality. Your feedback is invaluable for thorough testing and<br>
> refinement. The next step is to implement transaction connection pooling,<br>
> fine-grained control over pooled connections, and potentially a pre-warm<br>
> pool feature.<br>
> <br>
> You can find the detailed implementation and patch set attached or see the<br>
> implementation at<br>
> <a href="https://url.avanan.click/v2/___https://github.com/codeforall/pgpool2/tree/global_cp___.YXAzOnBlcmNvbmE6YTpnOjM4NjU0NTI5OGQ5ZTE2ZjA4YTU4N2YyMDM3MWQ2N2Q5OjY6YWJhNDoxMjZmNWQyMDY4YjE1MTM2MmZjNTBlNjZkODc1NmZjMzdhN2JiYTUxMDA3N2U5NTg2YTEyNTc5OGU1ZTMxZDU1OnA6VDpO" rel="noreferrer" target="_blank">https://url.avanan.click/v2/___https://github.com/codeforall/pgpool2/tree/global_cp___.YXAzOnBlcmNvbmE6YTpnOjM4NjU0NTI5OGQ5ZTE2ZjA4YTU4N2YyMDM3MWQ2N2Q5OjY6YWJhNDoxMjZmNWQyMDY4YjE1MTM2MmZjNTBlNjZkODc1NmZjMzdhN2JiYTUxMDA3N2U5NTg2YTEyNTc5OGU1ZTMxZDU1OnA6VDpO</a><br>
> <br>
> <br>
> Thank you for your time and consideration.<br>
> Best Regards<br>
> Muhammad Usama<br>
</blockquote></div></div>