ExAgent. Store. Postgres
(ExAgent v3.0.0)
View Source
A durable ExAgent.Store backed by PostgreSQL, for resume across crashes and
multiple nodes.
Stability — Stable.
Snapshots are stored as JSON (the same strict serialization ExAgent.Store.ETS
uses), never raw Erlang terms — so nothing non-serializable (pids,
closures) can land in the database. The store is DB-free by default in the
rest of exAgent: this module needs ecto_sql + postgrex, declared as optional
dependencies that a host app adds when it wants a durable store.
JSON does not redact secrets present in application data. Ordinary snapshots
retain their single-writer contract. Continuation commands use one row
transaction and receipts; they do not make external effects transactional.
Real SQL recovery/locking qualification is separate from offline protocol tests.
Wiring
The store takes the host app's Ecto.Repo as its config (the host owns the
database connection — exAgent never does):
# 1) add deps in your app:
# {:ecto_sql, "~> 3.0"}, {:postgrex, "~> 0.19"}
# 2) in a transactional Ecto migration:
# def up, do: ExAgent.Migration.up(version: 1)
# def down, do: ExAgent.Migration.down(version: 0)
# 3) use it:
ExAgent.AgentSupervisor.start_agent(
agent: agent_template,
agent_id: "dm",
store: {ExAgent.Store.Postgres, MyApp.Repo}
)The snapshots table is exagent_snapshots by default; override it with
{ExAgent.Store.Postgres, {MyApp.Repo, table: "my_snapshots"}}.
Both Store configurations read inline and fragmented rows. After installing
the unified migration, use
{MyApp.Repo, segments: true} to write large records and snapshots as
immutable fragments.
Server and Session validate descriptor options before starting.
Unified migration 1 includes Store SQL 4 for continuation transitions and
run pages. If the header column or its index is missing, they return
{:error, {:store_migration_required, 4}}. Rebuild the derived headers on
existing tables with rebuild_run_headers/1.
Durable turn admission requires SQL migration 5. It indexes one row per receipt, with the expiry watermark and receipt committed together.
The supervised recovery sweep requires SQL migration 6. The index uses
effective claim horizons and headers for uncertain and ready records.
Run check_recovery_schema/1 after upgrading writers and migrating an
existing table.
Supervise ExAgent.Store.Postgres.Maintenance after the Repo to reclaim
expired tombstones and retired records. It uses the retention indexes
installed by the unified migration.
Why not own the database?
A library shouldn't own your connection pool, credentials or migrations. The repo comes from your app; exAgent only issues parameterized SQL against one table.
Summary
Deprecated PostgreSQL migrations
Replay a historical host migration using Store SQL 1, 2, 4, 5 and 6. Retained through 3.x for 2.x host migration files.
PostgreSQL statements retained for host migrations written against the
per-Store SQL sequence. Use ExAgent.Migration for new migrations.
Version 1 creates the snapshots table. Its rollback deletes that table and its contents.
Version 2 adds a generated revision and a partial index. Its rollback removes
only those derived objects and their table-specific helper.
Version 3 adds the optional fragmented layout and rejects existing segment
columns with incompatible types. Its rollback requires a table with no
fragmented documents.
Functions
Verify SQL migration 6 and matching claimed roots and lease projections.
If a table has open timers, verify its SQL 7 timer index. This read-only
check uses existing run headers and lease keys. It reports the first
orphaned or unexpected lease's physical key. Use verify_run_headers/1
to detect imported journals without headers.
Backfill continuation headers after applying unified migration 1 to an existing table. This requires the projection introduced by Store SQL 4. Reads one journal at a time through the configured layout and locks its root while repairing the header and seeding a missing lease projection. It leaves any renewed projection, the journal bytes, its revision, fragments and update time intact. If interrupted, rerun the operation.
Read each eligible journal and compare its projected header to the current stored header. This is a read-only table scan with bounded journal reads. Run after backfill and before starting the writers, or as an explicit integrity check. Stops at the first unreadable journal or mismatched header.
Deprecated PostgreSQL migrations
Replay a historical host migration using Store SQL 1, 2, 4, 5 and 6. Retained through 3.x for 2.x host migration files.
Apply ExAgent.Migration for a fresh installation or to finish a 2.x upgrade.
The unified migration installs the complete 3.0 schema, including fragments
(Store SQL 3) and the timer index (Store SQL 7). Backfill existing journals
with rebuild_run_headers/1 as described by ExAgent.Migration.
When replaying this helper, use Ecto migration locking. Concurrent first
creation can return unique_violation and a repeat after commit is safe.
PostgreSQL statements retained for host migrations written against the
per-Store SQL sequence. Use ExAgent.Migration for new migrations.
Version 1 creates the snapshots table. Its rollback deletes that table and its contents.
Version 2 adds a generated revision and a partial index. Its rollback removes
only those derived objects and their table-specific helper.
Version 3 adds the optional fragmented layout and rejects existing segment
columns with incompatible types. Its rollback requires a table with no
fragmented documents.
Version 4 adds a continuation header column and a scoped key index. Apply it after version 2. Its rollback removes only the derived objects.
Version 5 adds indexed receipt rows. Its rollback requires a table with no indexed admission data, including expiry watermarks. Version 6 indexes effective lease horizons and ready or uncertain headers for supervised recovery. Update writers and run the header backfill to seed missing projections before applying version 6. Its rollback removes the index and its scope function. Unified schema version 1 applies Store SQL 7, which indexes pending timers. Roll back SQL 7 before SQL 6.
Functions
Verify SQL migration 6 and matching claimed roots and lease projections.
If a table has open timers, verify its SQL 7 timer index. This read-only
check uses existing run headers and lease keys. It reports the first
orphaned or unexpected lease's physical key. Use verify_run_headers/1
to detect imported journals without headers.
Backfill continuation headers after applying unified migration 1 to an existing table. This requires the projection introduced by Store SQL 4. Reads one journal at a time through the configured layout and locks its root while repairing the header and seeding a missing lease projection. It leaves any renewed projection, the journal bytes, its revision, fragments and update time intact. If interrupted, rerun the operation.
Run with the same Repo and table as the writers.
Read each eligible journal and compare its projected header to the current stored header. This is a read-only table scan with bounded journal reads. Run after backfill and before starting the writers, or as an explicit integrity check. Stops at the first unreadable journal or mismatched header.
A missing SQL 4 column or index returns {:error, {:store_migration_required, 4}}.
A journal without a header returns
{:error, {:run_header_missing, %{namespace: namespace, id: id}}}.
The identity comes from the validated journal key.