Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions docs/integration/connection-pool.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Connection Pooling

Connection pooling allows you to reuse database connections across multiple operations, reducing the overhead of establishing new connections. This is particularly useful in web applications where many concurrent requests need database access.

## Usage

Pass a `ConnectionPool` (or `AsyncConnectionPool`) to `PgDb` instead of a raw connection:

```{.python continuation fixture:postgres_container}
from psycopg_pool import ConnectionPool
from pydantic import BaseModel
from typing import Annotated

from embar.column.common import Integer, Text
from embar.config import EmbarConfig
from embar.db.pg import PgDb
from embar.table import Table


class User(Table):
embar_config: EmbarConfig = EmbarConfig(table_name="users")
id: Integer = Integer(primary=True)
name: Text = Text()


# Create a connection pool
pool = ConnectionPool("postgres://pg:pw@localhost:25432/db", open=True)

# Pass the pool to PgDb
db = PgDb(pool)

# Run migrations
db.migrate([User]).run()

# Insert a user
db.insert(User).values(User(id=1, name="Alice")).run()

# Query it back
class UserRead(BaseModel):
id: Annotated[int, User.id]
name: Annotated[str, User.name]

users = db.select(UserRead).from_(User).run()
print(users)
# [UserRead(id=1, name='Alice')]

# Clean up
pool.close()
```

## Unopened Pools

If you create a pool with `open=False`, it will be automatically opened on first use:

```python notest
pool = ConnectionPool("postgres://...", open=False)
db = PgDb(pool)
# Pool is opened automatically when the first query runs
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ nav:
- Raw SQL: query/raw-sql.md

- Integration:
- Connection pool: integration/connection-pool.md
- Migrations: migrations.md
- Types: types.md

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ requires-python = ">=3.14"

dependencies = [
"psycopg[binary]>=3.2.11",
"psycopg-pool>=3.3.0",
"pydantic>=2.12.4",
]

Expand Down
Loading