database: add failover-safe pool defaults and pgxpool health checks for Postgres/AlloyDB#490
database: add failover-safe pool defaults and pgxpool health checks for Postgres/AlloyDB#490stevemsmith wants to merge 4 commits intomoov-io:masterfrom
Conversation
…oyDB During AlloyDB maintenance switchovers (< 1s downtime), services using database/sql with pgx fail to recover because: 1. No connection pool limits are set by default, so dead connections persist indefinitely 2. No retry logic exists for transient connection errors This adds: - DefaultPostgresConnectionsConfig() with MaxLifetime=5m, MaxIdleTime=30s to ensure dead connections are evicted quickly after failover - ApplyPostgresConnectionsConfig() that fills in safe defaults when services don't explicitly configure pool settings - IsRetryablePostgresError() to classify transient PG/network errors - RetryPostgres() for services to wrap critical DB operations Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Switch from sql.Open("pgx") to pgxpool.NewWithConfig() wrapped with
stdlib.OpenDBFromPool(). This gives us pgxpool's HealthCheckPeriod
(set to 5s) which proactively pings idle connections and evicts dead
ones — the most important fix for surviving AlloyDB maintenance
switchovers. The return type remains *sql.DB so no downstream changes
are needed.
Also cleans up the dialer TODO (dialer lifecycle is now tied to the
pool) and removes the unused pgx.ParseConfig import path.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
For sub-second AlloyDB switchovers, 1s health checks detect and evict dead connections faster with negligible overhead. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly improves the robustness of database connections for Postgres and AlloyDB by implementing proactive health checks, establishing intelligent default connection pool settings, and providing utilities to automatically retry operations affected by transient connection issues. These changes aim to enhance service stability and recovery during database failovers or brief network disruptions, without requiring modifications to existing application code. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces robust failover handling for Postgres/AlloyDB connections by switching to pgxpool, which enables proactive health checks to evict dead connections. It also adds sensible default connection pool settings tuned for failover scenarios and an opt-in retry mechanism for transient connection errors. The changes are well-implemented, non-breaking for consumers of the library, and thoroughly tested. My feedback includes a couple of suggestions to improve code conciseness and strengthen test assertions.
|
CI is failing on a pre-existing vulnerability in |
- Collapse switch cases in IsRetryablePostgresError for readability - Make context cancellation test more specific (assert context.Canceled and exact call count) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Can we try this in some apps before merging? |
|
I'll work with gosec/golangci-lint on getting the linter fix released. |
Summary
sql.Open("pgx")topgxpoolwithstdlib.OpenDBFromPool()— enablesHealthCheckPeriod(1s) to proactively evict dead connections after a failover, while keeping the*sql.DBreturn type so no downstream changes are neededMaxLifetime=5m,MaxIdleTime=30s,MaxOpen=25,MaxIdle=5) that are applied automatically when services don't explicitly configure themIsRetryablePostgresError()andRetryPostgres()utilities for services that want to retry transient connection errors on in-flight queriesProblem
During AlloyDB monthly maintenance switchovers (< 1s downtime), Go services using this library fail to recover because:
database/sqlwith pgx as a driver has no background connection health checks. After a switchover, dead connections sit in the pool and get handed to the application, causing errors.ConnectionsConfigdefaults to all zero values, meaningdatabase/sqlholds connections forever. Dead connections from before the switchover are never evicted.Changes
postgres.gosql.Open("pgx", connStr)withpgxpool.NewWithConfig()+stdlib.OpenDBFromPool()HealthCheckPeriod = 1s— pgxpool pings idle connections every second and evicts dead ones before the app sees themgetAlloyDBConnectorConnStr()→buildAlloyDBPoolConfig()to return*pgxpool.Configinstead of a connection stringIsRetryablePostgresError()— classifies PG error codes (57P01admin_shutdown,57P03cannot_connect_now,08xxxconnection class) and network errors (EOF, broken pipe, connection reset) as retryableRetryPostgres()— opt-in retry wrapper with linear backoff for critical DB operationsmodel_config.goDefaultPostgresConnectionsConfig()with failover-safe defaultsdatabase.goApplyPostgresConnectionsConfig()that fills in defaults for zero-valued config fieldsNew()now uses this instead of the genericApplyConnectionsConfigNon-breaking
New()is still*sql.DB— no downstream changes neededgetPostgresConnStr()is unchanged (just moved in file)pgxpoolandstdlibare both part ofpgx/v5Test plan
IsRetryablePostgresError— all PG error codes, network errors, non-retryable casesRetryPostgres— success, retry-then-succeed, non-retryable short-circuit, context cancellation, attempt exhaustionDefaultPostgresConnectionsConfigandApplyPostgresConnectionsConfiggo build ./database/...andgo vet ./database/...pass🤖 Generated with Claude Code