Forward libpq TCP keepalive / tcp_user_timeout parameters in the PostgreSQL drivers - #7409
Draft
benconda wants to merge 1 commit into
Draft
Forward libpq TCP keepalive / tcp_user_timeout parameters in the PostgreSQL drivers#7409benconda wants to merge 1 commit into
benconda wants to merge 1 commit into
Conversation
…drivers Add support for the libpq connection keywords keepalives, keepalives_idle, keepalives_interval, keepalives_count, tcp_user_timeout and connect_timeout in both the pdo_pgsql and pgsql drivers, in the same style as the existing ssl* keywords. These bound how long a connection can block when a TCP session is silently dropped, which otherwise hangs until the kernel exhausts net.ipv4.tcp_retries2 (~15 min by default). Refs doctrine#7408
benconda
force-pushed
the
pgsql-tcp-keepalives
branch
from
June 17, 2026 13:34
f8c1369 to
1a8b607
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #7408.
Summary
The PostgreSQL drivers build their connection string from a fixed whitelist of parameters and silently drop everything else, so libpq's TCP-resilience keywords cannot be configured through DBAL today. This adds support for:
keepaliveskeepalives_idlekeepalives_intervalkeepalives_counttcp_user_timeoutconnect_timeoutin both PostgreSQL drivers, in the same explicit style already used for the
ssl*/gssencmodekeywords.Why
When a TCP connection to PostgreSQL is silently cut mid-query (flaky inter-DC link, stateful firewall dropping an idle entry, LB reset without RST), the PHP process blocks in libpq's
recv()until the kernel gives up — with the Linux defaultnet.ipv4.tcp_retries2 = 15that is ~15 minutes per stuck request, which can exhaust the worker/FPM pool. These libpq keywords bound dead-peer detection to a few seconds, but there was previously no way to set them via DBAL. See #7408 for the full context (real-world incident + measurements).Changes
Driver\PDO\PgSQL\Driver::constructPdoDsn()— append the keywords to the PDO DSN.Driver\PgSQL\Driver::constructConnectionString()— add the keywords to the nativepg_connectconnection string.Verification
phpcs(Doctrine CS) andphpstanpass on the changed files.pgsql:host=...;...;keepalives=1;keepalives_idle=10;keepalives_interval=5;keepalives_count=3;tcp_user_timeout=15000;connect_timeout=5;Notes
4.4.x; glad to retarget to whichever branch you prefer for a feature.