Add `PostgresStepStore` and `PostgresMediaStore`, the hosted Postgres backends for messages and media

Closed
RS
2mo ago
+2454-48

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.PostgresStepStore
  • pydantic_ai_harness.media.PostgresMediaStore
  • pydantic_ai_harness.media.PostgresPool / PostgresConnection — driver-neutral protocols, re-exported from step_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 matching harness-postgres-integration CI job (postgres:17 service; not path-gated, runs under coverage run, merges into the coverage job and the final gate, not in allowed-skips for code PRs, per the issue's testing decision) lives in commit 65753ce20 but is not in this diff: the .github Directory 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:17 via make 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].include does not yet measure src/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_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