Skip to main content

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 string
  • DATABASE_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

metapg_pool-0.3.0.tar.gz (76.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

metapg_pool-0.3.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

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

Hashes for metapg_pool-0.3.0.tar.gz
Algorithm Hash digest
SHA256 610cfb5ebb61e44a532e0c8c6e0061cc9d1031784defd409134d06946c7ab14b
MD5 1976833daba9e3abf08191e42c5b4413
BLAKE2b-256 8e25701bf881521b1a45a60959d8f97cb2470fa7ba3dbb31e3734e1da617284d

See more details on using hashes here.

File details

Details for the file metapg_pool-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for metapg_pool-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 102a126eb1b5568f038a58ec5a4086a43a6056fd94e2c5b92aa160eddb79d429
MD5 f7e5f1e279d151a67d5e9efca5be4764
BLAKE2b-256 cb686c529e06880dadcdd47707f4b884162264fd1cfb0b9ec1883acc9120e2bc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page