feat(postgres): share pg.Pool across services with same config#775
Merged
feat(postgres): share pg.Pool across services with same config#775
Conversation
Add a refcounted pool registry keyed by stable JSON hash of the pg config so multiple postgres services targeting the same database share connections instead of each spinning up their own pool. PostgresStore and PostgresQueueService acquire the shared pool when `usePool: true`; PostgresPubSubService keeps a dedicated pg.Client for LISTEN (subscription is bound to a single connection — a pool would silently rotate and drop it) but its publish path lazily acquires the shared pool, so subscribe-only services pay zero pool cost. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #775 +/- ##
==========================================
+ Coverage 73.88% 73.91% +0.02%
==========================================
Files 228 229 +1
Lines 15552 15603 +51
Branches 3778 3788 +10
==========================================
+ Hits 11491 11533 +42
- Misses 2976 2982 +6
- Partials 1085 1088 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Summary
pgpool.ts) keyed by stable JSON hash of the pg config so multiple postgres services targeting the same database share connections instead of each spinning up their own pool.PostgresStoreandPostgresQueueServiceacquire the shared pool whenusePool: true; releases drop the refcount and the underlying pool ends only on the last release.PostgresPubSubServicekeeps a dedicatedpg.Clientfor LISTEN (subscription is bound to a single connection — a pool would silently rotate and drop it), but its publish path lazily acquires the shared pool so subscribe-only services pay zero pool cost.Why
Each postgres service (store, queue, pubsub) was instantiating its own
pg.Pooleven when configured against the same database. Workloads that run a couple of these per process were burning multiples ofmaxconnections for no functional reason. Sharing on identical config keeps the connection footprint flat.Test plan
pnpm testinpackages/postgresagainst a local postgres — 30/30 passpnpm run lintpnpm run build(regeneratedwebda.module.jsonis in the diff)🤖 Generated with Claude Code