Add `PostgresStepStore` and `PostgresMediaStore`, the hosted Postgres backends for messages and media
- Fixes #8991
Supersedes #9453: the issue-link guard closed it before GitHub had resolved the issue reference, and it can no longer be reopened because its head branch was deleted. Same commits, plus a merge of main.
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's comment on #9453). Until then, nothing on this PR runs the live suite in CI, so thecoveragegate fails here: the two new modules are only executed end to end by the live suite (38% and 27% from the no-server tests).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.
One heads-up for reviewers, pre-existing on main and left to a maintainer decision rather than changed here: PostgresMemoryStore's _TABLE_RE has 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