Module github.com/bide-ai/bide/store/postgres
Go module github.com/bide-ai/bide/store/postgres; all modules.
store/postgres
github.com/bide-ai/bide/store/postgres · pkg.go.dev · Stability: core
Package postgres is a Postgres-backed agent.Store for high-availability durable agents: shared Postgres storage lets any node resume any run, so a dead worker's in-flight agents pick up elsewhere.
Variables
var ErrSchemaMissing: ErrSchemaMissing is wrapped by the error Open, New or OpenReadOnly return when the store's schema, or a part of it (the pinned schema, a table, the next_seq function, a lease index), does not exist and the store was not allowed to create it: it was opened WithoutMigration, or read only.var ErrSchemaVersion: ErrSchemaVersion is wrapped by the error returned when the database's schema version is not the one this version of the package uses.
type EventStore
type EventStore struct {
// contains filtered or unexported fields
}EventStore is a Postgres-backed audit.EventStore: the durable copy of a run's event trail (see audit.PersistJournal and audit.LoadEventLog), in the bide_events table (see WithTablePrefix and WithSchema).
Methods
func (e *EventStore) Append(ctx context.Context, runID string, seq int, leaf []byte) error: Append implements audit.EventStore.func (e *EventStore) Load(ctx context.Context, runID string) ([][]byte, error): Load implements audit.EventStore.
type MigrationResult
type MigrationResult struct {
Schema string
Before int
After int
Created []string
Statements []string
DryRun bool
}MigrationResult is what a migration did, or with WithDryRun would have done.
Constructors and functions
func Migrate(ctx context.Context, dsn string, opts ...Option) (MigrationResult, error): Migrate creates or checks the store's schema at dsn (a pgx DSN, as Open takes) as the role in the DSN, which needs the privileges of the schema's owner (CREATE on the schema), and reports the schema version before and after.func MigrateDB(ctx context.Context, db *sql.DB, opts ...Option) (MigrationResult, error): MigrateDB is Migrate over db, which the caller opened (with the pgx driver) and closes, as New is to Open.
type Option
type Option interface {
// contains filtered or unexported methods
}Option configures Open and New.
Constructors and functions
func WithDryRun() Option: WithDryRun makes Migrate and MigrateDB run the migration in a transaction and roll it back: the result lists the statements and the versions as if it had committed, the role is shown to hold the privileges the migration needs, and nothing is left behind.func WithSchema(name string) Option: WithSchema pins the schema the store's tables and next_seq function are in, instead of discovering it through the search path at every Open.func WithTablePrefix(prefix string) Option: WithTablePrefix names the tables with prefix instead of "bide_": prefix+"steps", prefix+"leases", prefix+"events" and prefix+"schema_version".func WithoutMigration() Option: WithoutMigration makes Open and New verify the schema instead of creating it.
type Store
type Store struct {
// contains filtered or unexported fields
}Store is a Postgres-backed agent.Store.
Constructors and functions
func New(ctx context.Context, db *sql.DB, opts ...Option) (*Store, error): New returns a Store over db, which the caller opened (with the pgx driver) and closes, and ensures the schema exists, or with WithoutMigration verifies it (see Open).func Open(ctx context.Context, dsn string, opts ...Option) (*Store, error): Open connects to Postgres via a pgx DSN (e.g. "postgres://user:pass@host:5432/db") and ensures the schema exists, creating it when it does not; with WithoutMigration it only verifies it, for a role with no DDL rights (see Migrate).func OpenReadOnly(ctx context.Context, dsn string, opts ...Option) (*Store, error): OpenReadOnly connects to the Postgres journal at dsn (a pgx DSN, as Open takes) for reading only.
Methods
func (s *Store) AcquireLease(ctx context.Context, runID, holder string, ttl time.Duration) (bool, error): AcquireLease implements agent.Leaser: claim runID for holder until now()+ttl.func (s *Store) Close() error: Close closes the connection pool Open opened.func (s *Store) EventStore() *EventStore: EventStore returns the audit.EventStore over s's database.func (s *Store) Get(ctx context.Context, runID, name string) (agent.Entry, bool, error): Get implements agent.Store.func (s *Store) Holds(ctx context.Context) ([]agent.Hold, error): Holds implements agent.Holder.func (s *Store) Insert(ctx context.Context, runID, name string, data []byte) (agent.Entry, bool, error): Insert implements agent.Store.func (s *Store) LiftHold(ctx context.Context, id string) (bool, error): LiftHold implements agent.Holder.func (s *Store) Load(ctx context.Context, runID string, after int64) iter.Seq2[agent.Entry, error]: Load implements agent.Store.func (s *Store) PlaceHold(ctx context.Context, h agent.Hold) error: PlaceHold implements agent.Holder with one statement that inserts a row per run the hold names, only if no hold has the ID.func (s *Store) ReapLeases(ctx context.Context, ended []string) (int, error): ReapLeases implements agent.Leaser: one DELETE of the lapsed leases whose run is finished (holds an entry named in ended), not in the steps table, or a session's or a sub-agent's (its ID contains '>'), which checks the expiry itself, so a lease taken or renewed meanwhile is kept.func (s *Store) Redact(ctx context.Context, runID, name string, current, tombstone []byte) (bool, error): Redact implements agent.Redactor by calling the redaction function (see redactVersion), which replaces the entry only if it holds current, the run is over, no lease on it is live, no run above it is still being driven, and it is not a session's run, and builds the tombstone itself.func (s *Store) ReleaseLease(ctx context.Context, runID, holder string) error: ReleaseLease implements agent.Leaser: relinquish runID if held by holder (a no-op otherwise).func (s *Store) RenewLease(ctx context.Context, runID, holder string, ttl time.Duration) (bool, error): RenewLease implements agent.Leaser: extend holder's still-live lease on runID.func (s *Store) Runs(ctx context.Context, f agent.RunFilter) iter.Seq2[string, error]: Runs implements agent.Lister.