postgres

package
v0.5.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package postgres keeps sessions durable in Postgres: the session Store (checkpoints and tool-call bodies), SecretStorage (the users' delegated tokens, sealed before they are written) and the EventLog.

The host owns the connection pool and passes it in; the three share it, and can share it with brain/postgres. Every table is qualified by the schema, so the pool needs no search_path. Run DB.Setup on each deploy.

Index

Constants

View Source
const DefaultSchema = "tacklr"

DefaultSchema holds the tables when Options.Schema is empty. It is the schema brain/postgres uses too.

Variables

This section is empty.

Functions

func Configure

func Configure(cfg *pgxpool.Config)

Configure sets the tracer of a host's pool config, before pgxpool.NewWithConfig: a span for each statement under its caller's context, so a turn's trace shows its checkpoint, call and event writes. A pool that brain/postgres.Configure set up has it already.

Types

type DB

type DB struct {
	// contains filtered or unexported fields
}

DB is the session tables in one schema of the host's pool.

func New

func New(pool *pgxpool.Pool, opts Options) (*DB, error)

New returns the session tables of opts.Schema in pool. It does no I/O.

func (*DB) EventLog

func (db *DB) EventLog() *EventLog

EventLog returns the session EventLog.

func (*DB) Secrets

func (db *DB) Secrets(sealer session.Sealer) *Secrets

Secrets returns the session SecretStorage, sealing every bag with sealer.

func (*DB) Setup

func (db *DB) Setup(ctx context.Context) error

Setup creates the schema and the session tables. Every statement is idempotent, so run it on each deploy; concurrent runs (hosts and workers that start together) wait for each other on an advisory lock.

func (*DB) Store

func (db *DB) Store() *Store

Store returns the session Store.

type EventLog

type EventLog struct {
	// contains filtered or unexported fields
}

EventLog is session.EventLog on Postgres. Each session's events are numbered by a counter row that an Append bumps and writes from in one statement, so sequences have no gaps. An Append also NOTIFYs the session's id (never the event: a NOTIFY payload is at most 8000 bytes), and one listening connection per EventLog wakes the subscribers, which then read what they have not seen. Close the EventLog to stop its listener.

func (*EventLog) Append

func (l *EventLog) Append(ctx context.Context, sessionID session.ID, topic string, ev session.Event) error

Append implements session.EventLog.

func (*EventLog) Close

func (l *EventLog) Close()

Close stops the listener and ends every tail of this EventLog.

func (*EventLog) CloseSession

func (l *EventLog) CloseSession(ctx context.Context, sessionID session.ID) error

CloseSession implements session.EventLog. It deletes the session's log, so a later session can reuse the id, and ends its tails in every process.

func (*EventLog) Head

func (l *EventLog) Head(ctx context.Context, sessionID session.ID) (session.Seq, error)

Head implements session.EventLog. An unknown session is at 0.

func (*EventLog) Subscribe

func (l *EventLog) Subscribe(ctx context.Context, sessionID session.ID, after session.Seq) (<-chan session.Event, error)

Subscribe implements session.EventLog. It replays the events after after, then tails: each subscriber reads at its own pace, so a slow one misses nothing and never blocks Append. Retry entries are not sent. The channel closes when ctx ends, the session is closed, or the EventLog is.

func (*EventLog) Trim

func (l *EventLog) Trim(ctx context.Context, sessionID session.ID, through session.Seq) error

Trim implements session.EventLog. The head row keeps the numbering.

type Options

type Options struct {
	// Schema holds the session tables. Default DefaultSchema.
	Schema string
}

Options configures New. Zero fields take the defaults.

type Secrets

type Secrets struct {
	// contains filtered or unexported fields
}

Secrets is session.SecretStorage on Postgres for the session-scoped tokens a host passes on CreateSession, Prompt and Resume (an end user's Drive or Graph OAuth tokens, per-session object-store credentials). Each session's bag is sealed before it is written, so the database never holds a plaintext token. Service-to-service credentials do not belong here: the host passes those to the providers it builds.

func (*Secrets) Delete

func (s *Secrets) Delete(ctx context.Context, sessionID session.ID) error

Delete implements session.SecretStorage.

func (*Secrets) Get

func (s *Secrets) Get(ctx context.Context, sessionID session.ID) (session.Secrets, error)

Get implements session.SecretStorage.

func (*Secrets) Put

func (s *Secrets) Put(ctx context.Context, sessionID session.ID, secrets session.Secrets) error

Put implements session.SecretStorage: a bag with bindings replaces the session's; one without (drop-only) changes nothing.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is session.Store on Postgres: one row per checkpoint row, and one per session whose revision a Save compares and bumps in the transaction that writes its changes.

func (*Store) Calls

func (s *Store) Calls(ctx context.Context, sessionID session.ID, callKeys []string) (map[string][]byte, error)

Calls implements session.Store in one query for every key.

func (*Store) ClearCalls

func (s *Store) ClearCalls(ctx context.Context, sessionID session.ID) error

ClearCalls implements session.Store.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, sessionID session.ID) error

Delete implements session.Store: the checkpoint and every call go in one statement.

func (*Store) Load

func (s *Store) Load(ctx context.Context, sessionID session.ID) ([]session.CheckpointEntry, session.Revision, error)

Load implements session.Store: the revision and the rows read in one snapshot of the database.

func (*Store) PutCall

func (s *Store) PutCall(ctx context.Context, sessionID session.ID, callKey string, body []byte) error

PutCall implements session.Store: a Put of a key replaces it.

func (*Store) Revision

func (s *Store) Revision(ctx context.Context, sessionID session.ID) (session.Revision, error)

Revision implements session.Store.

func (*Store) Save

func (s *Store) Save(ctx context.Context, sessionID session.ID, changes []session.CheckpointChange, expected session.Revision) (session.Revision, error)

Save implements session.Store. It writes only when the checkpoint is at expected ("" when there is none yet), so of two saves from one revision exactly one wins and the other gets session.ErrStaleCheckpoint. The revision and the changes commit in one transaction.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL