Add `PostgresStepStore` and `PostgresMediaStore`, the hosted Postgres backends for messages and media
- Closes #8991
StepPersistence has in-memory, file, SQLite and Mongo backends, while Memory already pairs its SQLite default with a hosted Postgres option. This adds the missing half of that pairing for messages, as specified in #8991: PostgresStepStore, plus PostgresMediaStore for the media_store='auto' default every other step store has.
The implementation copies the load-bearing patterns from PostgresMemoryStore (caller-owned asyncpg-compatible pool behind a local Protocol, _TABLE_RE-validated table= prefix, _ensure_schema behind an anyio.Lock, connection.transaction()) and preserves SqliteStepStore's semantic contracts, with the issue-preferred ON CONFLICT DO NOTHING idempotency in place of SQLite's trigger approach.
New public surface
pydantic_ai_harness.step_persistence.PostgresStepStorepydantic_ai_harness.media.PostgresMediaStorepydantic_ai_harness.media.PostgresPool/PostgresConnectionā driver-neutral protocols, re-exported fromstep_persistence; the harness takes on no driver dependency.
User-visible behavior
pool = await asyncpg.create_pool('postgresql://...') # caller-owned
persistence = StepPersistence(store=PostgresStepStore(pool), agent_name='support')
Events and snapshots are ordered by a BIGINT GENERATED ALWAYS AS IDENTITY sequence that keeps counting when ctx.run_step resets; large blobs externalize to {table}_media on the same pool by default.
Verification
src/pydantic_ai_harness/integration_tests/postgres/test_live_postgres.pyā behavior suite against a real server. The matchingharness-postgres-integrationCI job (postgres:17service; not path-gated, runs undercoverage run, merges into thecoveragejob and the final gate, not inallowed-skipsfor code PRs, per the issue's testing decision) lives in commit 65753ce20 but is not in this diff: the.githubDirectory Guard only accepts workflow changes from maintainers, so it needs to be carried forward in a separate PR (see the guard thread below). Until then, nothing on this PR runs the live suite in CI.tests/harness/step_persistence/test_postgres.py,tests/harness/media/test_postgres.pyā construction and validation on the ordinary matrix legs, no server required.- Locally: full suite green against
postgres:17viamake integration-postgres.
Two heads-ups for reviewers, both pre-existing on main and left to maintainer decisions rather than changed here:
- root
[tool.coverage.run].includedoes not yet measuresrc/pydantic_ai_harness/**, so the coverage the CI job uploads contains no harness lines until that include is widened (flagged on the issue). PostgresMemoryStore's_TABLE_REhas the same uppercase-folding exposure this PR closes for the new stores ('Orders'aliases'orders'on unquoted identifiers), but it has released users, so tightening it is a compatibility decision.
What changes for existing users
Nothing ā purely additive.
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver. (None.)
- PR title is fit for the release changelog.
š¤ Generated with Claude Code