pgx icon indicating copy to clipboard operation
pgx copied to clipboard

pgxpool: accept pool_ping_timeout in the connection string

Open 1991santhu opened this issue 4 months ago • 0 comments

Every pool setting on pgxpool.Config has a matching pool_* connection string key, except PingTimeout. ParseConfig didn't know the key, so pgx.ParseConfig left it in RuntimeParams and it got sent to the server as a startup parameter. Postgres then refused every connection in the pool:

failed to connect to `user=pgx_md5 database=pgx_test`:
  127.0.0.1:55432 (127.0.0.1): server error: FATAL: unrecognized configuration parameter "pool_ping_timeout" (SQLSTATE 42704)

So it's not that the key was ignored. Setting it broke the pool outright, and there was no way to bound the ping from a URL at all.

That matters for #1889. Acquire pings a connection that has been idle for a second or more, and with PingTimeout at 0 that ping inherits the caller's context. If the caller passes a context with no deadline and the network path has gone away without an RST, which is what the reporters see on Aurora and behind PrivateLink, the read blocks for as long as the socket stays open and Acquire never returns. PingTimeout is the existing mitigation for that. It was only reachable if you built the config in Go, and most people configure a pool from a URL.

What changed:

  • ParseConfig consumes pool_ping_timeout as a duration string and rejects negative values.
  • The default is unchanged. Zero still means no timeout, so nothing moves for existing users.
  • Documented pool_min_idle_conns on ParseConfig too. It's been supported since MinIdleConns was added but was never in the list.

I stopped at making the knob reachable rather than picking a non-zero default. A default would change behaviour for everyone and that's your call, not mine. Happy to follow up with one if you want it.

Verified against Postgres 17 in Docker, macOS arm64, Go 1.26.5.

New and existing ping-timeout tests:

$ PGX_TEST_DATABASE="postgres://pgx_md5:[email protected]:55432/pgx_test" \
    go test ./pgxpool/ -run 'TestParseConfig|TestPoolAcquirePingTimeout' -v
--- PASS: TestParseConfigExtractsPingTimeout (0.00s)
--- PASS: TestParseConfigPingTimeoutDefaultsToZero (0.00s)
--- PASS: TestParseConfigExtractsPoolArguments (0.00s)
--- PASS: TestParseConfigRejectsInvalidPingTimeout (0.00s)
--- PASS: TestPoolAcquirePingTimeout (6.02s)
PASS
ok  	github.com/jackc/pgx/v5/pgxpool	6.225s

Whole package:

$ PGX_TEST_DATABASE="postgres://pgx_md5:[email protected]:55432/pgx_test" go test ./pgxpool/
ok  	github.com/jackc/pgx/v5/pgxpool	6.245s

The new tests fail on unpatched pool.go:

--- FAIL: TestParseConfigExtractsPingTimeout (0.00s)
    map[string]string{"pool_ping_timeout":"250ms"} should not contain "pool_ping_timeout"
--- FAIL: TestParseConfigRejectsInvalidPingTimeout (0.00s)
    pool_ping_timeout=abc should be rejected
    pool_ping_timeout=250 should be rejected
    pool_ping_timeout=-1s should be rejected

go vet ./pgxpool/ and gofmt -l pgxpool/ are both clean. Diff is two files, no lockfile or generated churn.


Reviewer notes (not for the PR body)

  • The FATAL: unrecognized configuration parameter output above is real. I reproduced it with a scratch module against the real server on unpatched code, then confirmed it goes away with the patch.
  • I deliberately did not add an integration test that puts the key in PGX_TEST_DATABASE. CI sets that variable in keyword/value form in one job and as a URI in another, so appending a parameter is not format-safe. The parse-level assertion that the key leaves RuntimeParams is what actually prevents the failure, and TestPoolAcquirePingTimeout already covers the behaviour once PingTimeout is set.
  • Governance check: the last 20 merged PRs on jackc/pgx are all from non-maintainers.
  • No PR references issue #1889.

1991santhu avatar Aug 05 '26 01:08 1991santhu