postgres

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package postgres is the optional Postgres implementation of brain.Store.

Hosts build a pool with PoolConfig and construct a Store with New(pool), then inject it into brain.NewEngine. PoolConfig sets search_path to the tacklr schema, then public, and rejects port 6543 (a transaction-mode pooler). pgx prepared statements need a direct connection or a session-mode pooler on port 5432. Call Setup once per database. Tables live in Store.Schema (default "tacklr"): objects, object_kinds, and session_messages.

Lexical search uses a stored tsvector, search_tsv, on objects. Title is weight A, summary B, and body C. A partial GIN index covers live parts. Results order by ts_rank_cd with normalization 32. The query is websearch_to_tsquery (every term). When that page is shorter than requested, a second query ORs the same terms.

Other object indexes: namespace GIN (jsonb @>), parent btree (list children), a partial kind btree for live parents, and HNSW for vectors. Session messages are a small per-session set and are not indexed beyond their unique keys. Property, trigram, and created/updated indexes are not created: those queries do not use them. pg_trgm and pgvector are always created.

Query/Exec emit otelpgx spans under the caller context after telemetry.Init. Package brain does not import this package. MemoryStore stays in brain for tests and offline hosts.

Index

Constants

View Source
const DefaultEmbeddingDim = 1536

DefaultEmbeddingDim is the pgvector column size when Store.EmbeddingDim is 0.

View Source
const DefaultSchema = "tacklr"

DefaultSchema is the Postgres schema for tacklr tables when Store.Schema is empty.

Variables

This section is empty.

Functions

func PoolConfig added in v0.4.1

func PoolConfig(connString, schema string) (*pgxpool.Config, error)

PoolConfig parses connString and sets SearchPath on every connection. Port 6543 is rejected: that port is a transaction-mode pooler, and pgx's prepared statements need a direct connection or a session-mode pooler (port 5432).

func SearchPath added in v0.4.1

func SearchPath(schema string) (string, error)

SearchPath is the session search_path: the tacklr schema, then public.

Types

type PgxDB

type PgxDB interface {
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
	Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
}

PgxDB is satisfied by *pgx.Conn and *pgxpool.Pool.

type Store

type Store struct {
	// EmbeddingDim is the pgvector size Setup uses. Zero means DefaultEmbeddingDim.
	EmbeddingDim int
	// Schema is the Postgres schema for tacklr tables. Empty means DefaultSchema.
	Schema string
	// contains filtered or unexported fields
}

Store is the optional Postgres brain.Store. Hosts inject it into NewEngine. Call Setup once per database. Setup does not migrate an existing vector column.

Schema is the Postgres schema for objects, object_kinds, and session_messages. Empty means DefaultSchema. Build the pool with PoolConfig so each connection starts with SearchPath. Use a direct connection or a session-mode pooler (port 5432). Port 6543 is a transaction-mode pooler and breaks pgx prepared statements.

func New

func New(db PgxDB) (*Store, error)

New wraps a pgx pool or connection. Query/Exec emit otelpgx client spans as children of ctx (after telemetry.Init).

func (*Store) DeleteSessionMessages added in v0.4.0

func (s *Store) DeleteSessionMessages(ctx context.Context, sessionID string) error

DeleteSessionMessages implements brain.ObjectWriter.

func (*Store) Get

func (s *Store) Get(ctx context.Context, scope brain.Scope, id uuid.UUID) (brain.Object, error)

Get implements ObjectReader.

func (*Store) GetByProperty

func (s *Store) GetByProperty(ctx context.Context, scope brain.Scope, key, value string) (brain.Object, error)

GetByProperty implements ObjectLister.

func (*Store) GetKind

func (s *Store) GetKind(ctx context.Context, kind string) (brain.ObjectKind, error)

GetKind implements KindReader.

func (*Store) GetMany

func (s *Store) GetMany(ctx context.Context, scope brain.Scope, ids []uuid.UUID) ([]brain.Object, error)

GetMany implements ObjectReader.

func (*Store) KindsWithObjects

func (s *Store) KindsWithObjects(ctx context.Context, scope brain.Scope) ([]string, error)

KindsWithObjects implements ObjectLister.

func (*Store) ListByKind

func (s *Store) ListByKind(ctx context.Context, scope brain.Scope, kind string, limit int) ([]brain.Object, error)

ListByKind implements ObjectLister (first-class objects only).

func (*Store) ListChildren

func (s *Store) ListChildren(ctx context.Context, scope brain.Scope, parentID uuid.UUID) ([]brain.Object, error)

ListChildren implements ObjectReader.

func (*Store) ListKinds

func (s *Store) ListKinds(ctx context.Context) ([]brain.ObjectKind, error)

ListKinds implements KindReader.

func (*Store) Put

func (s *Store) Put(ctx context.Context, obj brain.Object) error

Put implements ObjectWriter (full-column upsert; clears soft-delete on revive).

func (*Store) PutKind

func (s *Store) PutKind(ctx context.Context, k brain.ObjectKind) error

PutKind implements KindWriter.

func (*Store) SaveSessionMessages added in v0.4.0

func (s *Store) SaveSessionMessages(ctx context.Context, host brain.Namespace, sessionID, generationKey string, messages []brain.SessionMessage) (int, error)

SaveSessionMessages implements brain.ObjectWriter.

func (*Store) SearchLexical

func (s *Store) SearchLexical(ctx context.Context, scope brain.Scope, query string, filters brain.Filter, k int) ([]brain.ScoredID, error)

SearchLexical implements PartSearcher over parts (parent_id set). Score is ts_rank_cd on search_tsv (title A, summary B, body C). websearch_to_tsquery runs first (every term). A short page then ORs those terms.

func (*Store) SearchTrigram

func (s *Store) SearchTrigram(ctx context.Context, scope brain.Scope, query string, filters brain.Filter, k int) ([]brain.ScoredID, error)

SearchTrigram implements PartSearcher using pg_trgm similarity over parts.

func (*Store) SearchVector

func (s *Store) SearchVector(ctx context.Context, scope brain.Scope, embedding []float32, filters brain.Filter, k int) ([]brain.ScoredID, error)

SearchVector implements PartSearcher using pgvector cosine distance over parts.

func (*Store) Setup

func (s *Store) Setup(ctx context.Context, kinds ...brain.KindSpec) error

Setup creates the tacklr schema, extensions, tables, and indexes (idempotent), then upserts kinds into object_kinds. EmbeddingDim is the pgvector size and must match the embedder; zero uses DefaultEmbeddingDim. This does not migrate an existing vector column to a new size.

Lexical search uses the stored search_tsv column. pg_trgm and pgvector are created. Vector indexes are HNSW.

func (*Store) SoftDelete

func (s *Store) SoftDelete(ctx context.Context, scope brain.Scope, id uuid.UUID) error

SoftDelete implements ObjectWriter.

Jump to

Keyboard shortcuts

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