13 · Repeated-PK Service Lookups (identity_map_read)#
← All benchmarks · Related field note: Field Note 1 · Unit of Work & Identity Map
Run three independent service helpers (e.g., auth check, pricing tier, and audit banner) inside one request transaction, where each helper looks up the same 30 guests by primary key (90 logical PK lookups total).
This is a genuine, everyday architectural trade-off where Identity Map ORMs outshine pgdorm:
- Identity Map ORMs (SQLAlchemy 2 and SQLModel using
AsyncSession.get()) query PostgreSQL on pass 1 (30rows), retain the loaded instances in the session's Identity Map, and serve passes 2 and 3 straight from RAM in 0 extra round trips. - Stateless ORMs (
pgdorm, Django, Tortoise, Piccolo, Peewee, SQLDataclass) intentionally keep no hidden session cache. Naive service layers that repeatedly query by PK hit PostgreSQL all 90 times.
Chart & metrics#
| ORM | Instr (Client + PG) | RTTs | Server ms | ms (med) |
|---|---|---|---|---|
| SQLAlchemy 2 | 46.3M (26.8M + 19.5M) | 32 | 0.45 ms | 16.74 ms |
| SQLModel | 47.7M (27.6M + 20.1M) | 32 | 0.48 ms | 18.15 ms |
| SQLDataclass | 109.9M (53.2M + 56.7M) | 94 | 1.13 ms | 37.02 ms |
| Piccolo ORM | 119.6M (64.5M + 55.1M) | 93 | 1.08 ms | 36.64 ms |
| pgdorm | 125.8M (70.0M + 55.8M) | 91 | 1.45 ms | 40.50 ms |
| Peewee | 125.9M (72.2M + 53.7M) | 92 | 1.09 ms | 39.80 ms |
| Tortoise ORM | 128.0M (72.8M + 55.2M) | 93 | 1.12 ms | 37.78 ms |
| Django ORM | 139.4M (76.5M + 62.9M) | 92 | 1.25 ms | 43.88 ms |
Ferris' basement: Why pgdorm stays stateless — and the idiomatic pattern
pgdorm omits an Identity Map on purpose so queries never return stale cached rows after concurrent updates or RETURNING mutations, and memory never balloons across long-lived loops (Field Note 1). When multiple service helpers need the same entities in a single request, fetch them once at the request boundary (e.g., pg.select(StayGuest).where(StayGuest.stay_guest_id.in_any(ids)) in 1 RTT) and pass the immutable slotted models down explicitly.
Contender implementations#
Every tab below is AST-extracted directly from bench/adapters/ — zero hand-copied snippets.
pgdorm 0.10.0 (pgdorm-wire (Rust)) · 91 RTTs · 70.0M instr · 40.50 ms · pgdorm_adapter.py
async def repeated_pk_service_reads(self, ids: list[int]) -> list[str]:
assert self._pool is not None
out: list[str] = []
async with self._pool.transaction() as tx:
for _layer in range(3):
for gid in ids:
row = await tx.one(pg.select(StayGuest).where(StayGuest.stay_guest_id == gid))
out.append(row.name)
return out
SQLAlchemy 2 2.1.9 (asyncpg) · 32 RTTs · 26.8M instr · 16.74 ms · sqlalchemy_adapter.py
async def repeated_pk_service_reads(self, ids: list[int]) -> list[str]:
assert self._session_factory is not None
out: list[str] = []
kept: list[SaStayGuest] = []
async with self._session_factory() as s:
for _layer in range(3):
for gid in ids:
row = await s.get(SaStayGuest, gid)
assert row is not None
kept.append(row)
out.append(row.name)
return out
SQLModel 0.0.50 (asyncpg) · 32 RTTs · 27.6M instr · 18.15 ms · sqlmodel_adapter.py
async def repeated_pk_service_reads(self, ids: list[int]) -> list[str]:
assert self._session_factory is not None
out: list[str] = []
kept: list[SmStayGuest] = []
async with self._session_factory() as s:
for _layer in range(3):
for gid in ids:
row = await s.get(SmStayGuest, gid)
assert row is not None
kept.append(row)
out.append(row.name)
return out
SQLDataclass 0.4.3 (psycopg3 (sync)) · 94 RTTs · 53.2M instr · 37.02 ms · sqldataclass_adapter.py
async def repeated_pk_service_reads(self, ids: list[int]) -> list[str]:
assert self._engine is not None
tbl = SdStayGuest.__table__
out: list[str] = []
with self._engine.connect() as conn:
for _layer in range(3):
for gid in ids:
rows = SdStayGuest.load_all(conn, where=tbl.c.stay_guest_id == gid)
out.append(rows[0].name)
return out
Django ORM 6.1.3 (psycopg3) · 92 RTTs · 76.5M instr · 43.88 ms · django_adapter.py
async def repeated_pk_service_reads(self, ids: list[int]) -> list[str]:
_, _, _, _, guest_m, _, _, _ = build_django_stay_models()
out: list[str] = []
with transaction.atomic():
for _layer in range(3):
for gid in ids:
out.append(guest_m.objects.get(pk=gid).name)
return out
Tortoise ORM 1.1.9 (asyncpg) · 93 RTTs · 72.8M instr · 37.78 ms · tortoise_adapter.py
Piccolo ORM 1.39.0 (asyncpg) · 93 RTTs · 64.5M instr · 36.64 ms · piccolo_adapter.py
async def repeated_pk_service_reads(self, ids: list[int]) -> list[str]:
_, _, _, _, guest_t, _, _, _ = self._stay_tables
out: list[str] = []
async with guest_t._meta.db.transaction():
for _layer in range(3):
for gid in ids:
row = await guest_t.objects().where(guest_t.stay_guest_id == gid).first()
assert row is not None
out.append(str(row.name))
return out
Peewee 4.5.3 (psycopg3) · 92 RTTs · 72.2M instr · 39.80 ms · peewee_adapter.py
async def repeated_pk_service_reads(self, ids: list[int]) -> list[str]:
assert self._db is not None
_, _, _, _, guest_m, _, _, _ = self._stay_models
out: list[str] = []
with self._db.atomic():
for _layer in range(3):
for gid in ids:
out.append(str(guest_m.get_by_id(gid).name))
return out