Skip to content

feat(postgres): share pg.Pool across services with same config#775

Merged
loopingz merged 1 commit intomainfrom
feat/postgres-shared-pool
May 9, 2026
Merged

feat(postgres): share pg.Pool across services with same config#775
loopingz merged 1 commit intomainfrom
feat/postgres-shared-pool

Conversation

@loopingz
Copy link
Copy Markdown
Owner

@loopingz loopingz commented May 9, 2026

Summary

  • Add a refcounted pool registry (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.
  • PostgresStore and PostgresQueueService acquire the shared pool when usePool: true; releases drop the refcount and the underlying pool ends only on the last release.
  • 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.

Why

Each postgres service (store, queue, pubsub) was instantiating its own pg.Pool even when configured against the same database. Workloads that run a couple of these per process were burning multiples of max connections for no functional reason. Sharing on identical config keeps the connection footprint flat.

Test plan

  • pnpm test in packages/postgres against a local postgres — 30/30 pass
  • pnpm run lint
  • pnpm run build (regenerated webda.module.json is in the diff)
  • CI green

🤖 Generated with Claude Code

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
Copy link
Copy Markdown

codecov Bot commented May 9, 2026

Codecov Report

❌ Patch coverage is 75.43860% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.91%. Comparing base (408e229) to head (52f8120).

Files with missing lines Patch % Lines
packages/postgres/src/pgpool.ts 83.87% 2 Missing and 3 partials ⚠️
packages/postgres/src/postgresstore.ts 50.00% 4 Missing and 1 partial ⚠️
packages/postgres/src/postgresqueue.ts 60.00% 2 Missing and 2 partials ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@loopingz loopingz merged commit 9f0155f into main May 9, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant