Async and sync PostgreSQL connection pool for multi-database applications
Project description
metapg.pool
High-performance PostgreSQL connection pool with async and sync support
metapg.pool provides async and sync PostgreSQL connection pooling built on psycopg3. It supports multiple databases, smart connection reuse, and context-aware operations.
Installation
pip install metapg.pool
Quick Start
import metapg.pool
# Initialize pool
metapg.pool.init_pool(dsn="postgresql://localhost/mydb", db_name="main")
# Async usage
async with metapg.pool.cursor() as cur:
await cur.execute("SELECT * FROM users")
users = await cur.fetchall()
# Sync usage (same interface!)
with metapg.pool.cursor() as cur:
cur.execute("SELECT * FROM users")
users = cur.fetchall()
# Transactions
async with metapg.pool.transaction():
async with metapg.pool.cursor() as cur:
await cur.execute("INSERT INTO users (name) VALUES (%s)", ("Alice",))
Features
- 🎯 Smart Interface - Same API for both async and sync operations
- ⚡ High Performance - Built on psycopg3 with efficient connection pooling
- 🎛️ Multi-Database - Manage multiple PostgreSQL databases with named pools
- 🧠 Context-Aware - Smart connection reuse with contextvars
- 🔒 Thread-Safe - Safe for use in threaded applications
- ⚙️ Zero-Config - Works out of the box with sensible defaults
API Reference
Pool Management
# Initialize pools (creates both async and sync pools)
metapg.pool.init_pool(
dsn="postgresql://user:pass@host:port/dbname",
db_name="default",
min_size=1,
max_size=20
)
# Get existing pool
pool = metapg.pool.get_pool("default")
# Close specific pool
await metapg.pool.close_pool("default")
# Close all pools
await metapg.pool.close_all_pools()
Database Operations
# Smart cursor (adapts to sync/async context)
async with metapg.pool.cursor("db_name") as cur:
await cur.execute("SELECT * FROM table")
results = await cur.fetchall()
# Direct connection access
async with metapg.pool.connection("db_name") as conn:
async with conn.cursor() as cur:
await cur.execute("SELECT 1")
# Transactions
async with metapg.pool.transaction("db_name"):
async with metapg.pool.cursor() as cur:
await cur.execute("INSERT INTO table VALUES (%s)", (value,))
Environment Variables
DATABASE_URL- Default database connection stringDATABASE_URL_{NAME}- Connection string for named database (e.g.,DATABASE_URL_ANALYTICS)
License
MIT License - see LICENSE file for details.
Part of metapg
This package is part of the metapg metapackage for PostgreSQL operations.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file metapg_pool-0.3.0.tar.gz.
File metadata
- Download URL: metapg_pool-0.3.0.tar.gz
- Upload date:
- Size: 76.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
610cfb5ebb61e44a532e0c8c6e0061cc9d1031784defd409134d06946c7ab14b
|
|
| MD5 |
1976833daba9e3abf08191e42c5b4413
|
|
| BLAKE2b-256 |
8e25701bf881521b1a45a60959d8f97cb2470fa7ba3dbb31e3734e1da617284d
|
File details
Details for the file metapg_pool-0.3.0-py3-none-any.whl.
File metadata
- Download URL: metapg_pool-0.3.0-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
102a126eb1b5568f038a58ec5a4086a43a6056fd94e2c5b92aa160eddb79d429
|
|
| MD5 |
f7e5f1e279d151a67d5e9efca5be4764
|
|
| BLAKE2b-256 |
cb686c529e06880dadcdd47707f4b884162264fd1cfb0b9ec1883acc9120e2bc
|