fastpg
An async PostgreSQL client for Python, powered by Rust.
- Python
- Rust
- PyO3
- PostgreSQL
- asyncio
- msgspec
Rows straight into types
Database drivers usually return tuples or records, and the application then copies
them into its own models. fastpg removes that step. Results always use PostgreSQL’s
binary format, decoded in Rust, and into= builds msgspec structs or pydantic models
directly, with no intermediate dicts or tuples.
uv add fastpg
Install it from PyPI. fastpg targets Python 3.14 on macOS and Linux.
import fastpg
import msgspec
class Person(msgspec.Struct):
name: str
age: int
async with await fastpg.connect(dsn) as conn:
person = await conn.select_one(
t"SELECT name, age FROM people WHERE id = {person_id}",
into=Person,
)
Column-to-field binding and type checks run once per statement and type, not once per
row, and raise before any row is decoded. A NULL in a non-optional field, or a text
column bound to an int field, is a clear error naming the column and the field.
Python 3.14 all the way down
Every query method accepts a PEP 750 template string. Interpolations become bound
parameters, never SQL text, so queries are injection-safe by construction, and nested
templates compose. The whole package passes pyright --strict.
Pipelining without the head-of-line blocking
Concurrent queries are pipelined over a single connection, but PostgreSQL runs one
connection’s queries strictly in order, so one slow query can stall everything behind
it. fastpg.Pool routes each shared query to an idle connection where it can, and
otherwise to the connection with the least expected queued work, using a moving
average of each statement’s observed runtime.
All I/O runs on the asyncio loop thread itself: there is no tokio runtime and no thread hand-off per query.
Performance
On one CPU, through a pool of 16 connections, fastpg serves around 2.5 times as many
lookups per second as asyncpg, fetches large results at over twice the row rate, and
bulk-loads with binary COPY at 1.25M rows per second.
Read the performance results for the complete methodology, numbers, and caveats.
The trade-off
fastpg is an early, tested prototype with deliberate limits. It runs on Unix only, binds each connection to the event loop that opened it, and always uses named prepared statements, so it is incompatible with pgbouncer in transaction-pooling mode. Arrays, intervals, and range types are not decoded yet.