Documentation
¶
Overview ¶
Package pgvector implements Recall's context-aware Backend on PostgreSQL with the pgvector extension. Each Recall collection is one table, created on the first write with the dimension of the first vector written to it, in a schema of its own (recall by default) beside two bookkeeping tables: recall_collections, which maps collection names to tables and counts each collection's writes, and recall_leases, which holds agent leases.
Postgres gives this backend everything Recall can use: exact listings from one snapshot, a per-collection revision that changes on every write (the watermark behind Recall's cached reads), row locks for agent leases, and full-text search over each event's text, fused with the vector search.
Every write increments its collection's revision under a row lock, which serializes writers to one collection. Agent memory is written at the pace of a conversation, where that costs nothing; a bulk loader would notice.
Each collection's row in recall_collections carries the storage format its table was written in. A Recall that finds a newer format than it knows refuses the collection instead of misreading it.
Index ¶
- Constants
- Variables
- func TableName(collection string) string
- type Backend
- func (b *Backend) AcquireLease(ctx context.Context, collection, name, holder string, ttl time.Duration) (recall.LeaseGrant, error)
- func (b *Backend) Close()
- func (b *Backend) Create(ctx context.Context, collection, id string, values []float32, ...) error
- func (b *Backend) Get(ctx context.Context, collection string, ids []string) ([]recall.StoredVector, error)
- func (b *Backend) List(ctx context.Context, collection string, filter map[string]any, limit int) ([]recall.StoredVector, int, error)
- func (b *Backend) Pool() *pgxpool.Pool
- func (b *Backend) Put(ctx context.Context, collection, id string, values []float32, ...) error
- func (b *Backend) PutFenced(ctx context.Context, collection, id string, values []float32, ...) error
- func (b *Backend) ReleaseLease(ctx context.Context, collection, name, holder string, epoch uint64) error
- func (b *Backend) RenewLease(ctx context.Context, collection, name, holder string, epoch uint64, ...) (recall.LeaseGrant, error)
- func (b *Backend) Search(ctx context.Context, collection string, values []float32, k int, ...) ([]recall.Hit, error)
- func (b *Backend) SearchText(ctx context.Context, collection, text string, k int, filter map[string]any) ([]recall.Hit, error)
- func (b *Backend) Unavailable(err error) bool
- func (b *Backend) Watermark(ctx context.Context, collection string) (string, error)
- type Config
Constants ¶
const DefaultSchema = "recall"
DefaultSchema is where the tables live unless Config.Schema says otherwise.
const StorageFormat = 1
StorageFormat is the newest collection layout this package reads and writes. A collection without a recorded format is format 1.
Variables ¶
var ErrNewerFormat = errors.New("pgvector: the collection was written by a newer Recall")
ErrNewerFormat reports a collection written by a newer Recall, in a layout this one cannot read safely.
Functions ¶
func TableName ¶
TableName is the identifier a collection's table gets: the name itself when it is already a plain lower-case identifier of at most 48 characters, and otherwise a cleaned form with a hash suffix, so distinct collections never share a table. The mapping is also recorded in recall_collections.
Types ¶
type Backend ¶
type Backend struct {
// contains filtered or unexported fields
}
Backend is a reusable PostgreSQL client, safe for concurrent calls.
func New ¶
New validates the configuration and creates a backend. It opens no connection; the first operation does, and creates the schema and bookkeeping tables if they are missing.
func (*Backend) AcquireLease ¶
func (b *Backend) AcquireLease(ctx context.Context, collection, name, holder string, ttl time.Duration) (recall.LeaseGrant, error)
AcquireLease takes an agent's lease unless a live holder has it. The epoch only ever grows: a fresh lease starts at 1, and a lease taken over after its holder let it lapse continues from where the chain was.
func (*Backend) Close ¶
func (b *Backend) Close()
Close releases the pool the backend opened. It does nothing for a pool supplied through Config.
func (*Backend) Create ¶ added in v0.26.0
func (b *Backend) Create(ctx context.Context, collection, id string, values []float32, metadata map[string]any, fence *recall.Fence) error
Create inserts only when the id holds no record, in the same transaction as the lease check when a fence is given. A taken id is reported as recall.ErrRecordExists with nothing written.
func (*Backend) Get ¶
func (b *Backend) Get(ctx context.Context, collection string, ids []string) ([]recall.StoredVector, error)
Get reads events by id in request order; unknown ids are omitted.
func (*Backend) List ¶
func (b *Backend) List(ctx context.Context, collection string, filter map[string]any, limit int) ([]recall.StoredVector, int, error)
List returns up to limit exact matches, in id order, and the exact total, both from one snapshot.
func (*Backend) Pool ¶
Pool is the connection pool the backend uses, for callers that want to query the tables themselves.
func (*Backend) Put ¶
func (b *Backend) Put(ctx context.Context, collection, id string, values []float32, metadata map[string]any) error
Put upserts one immutable event ID, creating the collection on first use, and moves the collection's revision in the same transaction.
func (*Backend) PutFenced ¶ added in v0.21.0
func (b *Backend) PutFenced(ctx context.Context, collection, id string, values []float32, metadata map[string]any, fence recall.Fence) error
PutFenced is Put applied only while the agent's lease is still the writer's: the lease row is read under FOR SHARE in the write's own transaction, so a takeover waits for the write to commit or the write sees the takeover, never both. A fence that no longer holds is reported as recall.ErrLeaseLost with nothing written.
func (*Backend) ReleaseLease ¶
func (b *Backend) ReleaseLease(ctx context.Context, collection, name, holder string, epoch uint64) error
ReleaseLease ends a held lease at once, so the next Acquire need not wait for it to lapse. A lease already past the given epoch is left alone.
func (*Backend) RenewLease ¶
func (b *Backend) RenewLease(ctx context.Context, collection, name, holder string, epoch uint64, ttl time.Duration) (recall.LeaseGrant, error)
RenewLease extends a held lease and advances its epoch. A lease whose chain has moved past the given epoch was lost to another holder.
func (*Backend) Search ¶
func (b *Backend) Search(ctx context.Context, collection string, values []float32, k int, filter map[string]any) ([]recall.Hit, error)
Search returns the k nearest events by cosine distance. Score is 1 minus the distance.
func (*Backend) SearchText ¶
func (b *Backend) SearchText(ctx context.Context, collection, text string, k int, filter map[string]any) ([]recall.Hit, error)
SearchText ranks events by PostgreSQL full-text match of the query's words against the event text, any word matching. Score is ts_rank_cd; Distance is 1/(1+Score). A query with no searchable words returns no hits.
func (*Backend) Unavailable ¶ added in v0.28.0
Unavailable reports an error from a server that could not answer: no connection, a shutdown or failover, no resources left, or a transaction it asks to have run again. A refused login or a bad statement is an answer.
func (*Backend) Watermark ¶
Watermark is the collection's revision, which moves on every write, behind a nonce chosen when the collection was created, so a collection dropped and made again never repeats a revision. A collection nothing has written to has a watermark too, which its first write changes.
type Config ¶
type Config struct {
// URL is a PostgreSQL connection string, such as
// postgres://user:password@localhost:5432/recall. libpq's PG* environment
// variables fill in what it leaves out.
URL string
// Password overrides the connection string's password when set.
Password string
// Schema is the schema the tables are created in; empty means DefaultSchema.
Schema string
// TextSearchConfig is the PostgreSQL text search configuration the event
// text is indexed with, such as english (the default) or simple. Tables
// keep the configuration they were created with.
TextSearchConfig string
// Pool is a connection pool to use instead of opening one from URL. The
// backend never closes a supplied pool.
Pool *pgxpool.Pool
}
Config selects a database and how the backend lays its tables out.