fix: keep postgres connections alive with heartbeat#797
Merged
harshsingh1002 merged 4 commits intomainfrom Sep 19, 2025
Merged
Conversation
shaspitz
reviewed
Sep 19, 2025
shaspitz
approved these changes
Sep 19, 2025
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.
Harden Postgres connection handling to avoid insert timeouts after idle periods.
Details
Pool settings:
MaxOpenConns=25, MaxIdleConns=20 to keep a warm stash.
ConnMaxLifetime=0 disables age-based churn.
ConnMaxIdleTime=0 prevents idle reaping. Ensures the pool never drops healthy connections.
Heartbeat goroutine:
Runs db.PingContext every 2 minutes.
Keeps at least one connection active, preventing k8s/LB/NAT or Postgres from closing all idle sessions.
Interval chosen < common infra idle timeout (~10m).
Why
Without this, long quiet gaps drained the pool; the next insert had to open a new conn (dial + TLS + auth) inside the 5s insert deadline → context deadline exceeded.
These changes ensure a live connection is always available, so inserts stay within the 5s budget even after idle periods.