pgvector

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: Apache-2.0 Imports: 18 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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.

Index

Constants

View Source
const DefaultSchema = "recall"

DefaultSchema is where the tables live unless Config.Schema says otherwise.

Variables

This section is empty.

Functions

func TableName

func TableName(collection string) string

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

func New(cfg Config) (*Backend, error)

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) 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

func (b *Backend) Pool() *pgxpool.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) Watermark

func (b *Backend) Watermark(ctx context.Context, collection string) (string, error)

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.

Jump to

Keyboard shortcuts

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