shared-pool-pattern

Share one PostgreSQL pool across FastAPI services with schema-isolated managers.

Updated Jul 28, 2025
One-click install
npx skills add https://github.com/juanre/pgdbm --skill shared-pool-pattern
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: shared-pool-pattern
Source: https://github.com/juanre/pgdbm/tree/main/skills/shared-pool-pattern
Command: npx skills add https://github.com/juanre/pgdbm --skill shared-pool-pattern

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides a proven pattern for sharing a single database connection pool across multiple services within a single application, reducing resource usage and connection overhead while preserving service isolation.

Core Features & Use Cases

  • Single Shared Pool: Creates one pool at startup that is reused by all services.
  • Schema-Isolated Managers: Each service gets its own manager with a dedicated database schema to avoid table conflicts.
  • Per-Service Migrations: Independent migrations run per service module to keep schemas up to date.
  • FastAPI Integration: Demonstrates how to wire the shared pool into FastAPI lifespans and dependency injections.

Quick Start

Initialize a shared pool at application startup, then create per-service managers with distinct schemas and run migrations.

Frequently Asked Questions about shared-pool-pattern

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I share a single database connection pool across multiple FastAPI services?

Create a shared pool at application startup using create_shared_pool, then instantiate per-service AsyncDatabaseManager instances with distinct schemas. Each service reuses the single pool while maintaining isolation through dedicated database schemas, reducing connection overhead across your multi-service app.

Can I run independent migrations for each service with a shared database pool?

Yes. Assign unique module_names to each service's AsyncDatabaseManager so migrations run per-service schema without conflicts. This pattern lets you maintain separate migration histories and schemas while sharing the underlying connection pool.

What's the best way to handle schema isolation in a multi-service Python application?

Schema isolation separates tables by service within a single database. Each AsyncDatabaseManager gets its own schema, preventing table name collisions and keeping services independent while benefiting from a shared pool's resource efficiency.

How do I integrate a shared database pool into FastAPI lifespans and dependency injection?

Initialize the shared pool during FastAPI startup via lifespan context managers, then inject per-service managers through FastAPI dependencies. This ensures proper pool creation, cleanup, and availability across all service endpoints.

Does the shared pool pattern work with async database operations in FastAPI?

Yes. The pattern uses AsyncDatabaseManager instances designed for async workflows, integrating seamlessly with FastAPI's async/await model and lifespan management for non-blocking database access across services.

What happens to connection efficiency when multiple services share a single PostgreSQL pool?

A shared pool reduces total connection count to the database, lowering memory overhead and connection limits strain. Instead of each service maintaining its own pool, all services reuse connections from one pool, improving resource utilization in multi-service deployments.