postgres

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

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

DB wraps a Pool and provides all Postgres operations for Eidetic.

func NewDB

func NewDB(ctx context.Context, dsn string) (*DB, error)

NewDB connects to Postgres and returns a DB. The pool is ready to use immediately; callers should defer DB.Close.

func NewDBFromPool added in v0.5.0

func NewDBFromPool(p Pool) *DB

NewDBFromPool creates a DB from an existing Pool implementation. Useful for testing with pgxmock.

func (*DB) BatchUpsertEntries

func (db *DB) BatchUpsertEntries(ctx context.Context, entries []domain.Entry) (int, error)

BatchUpsertEntries upserts entries in batches for better performance during reindex. Returns the number of successfully upserted entries.

func (*DB) Close

func (db *DB) Close()

Close releases pool connections.

func (*DB) Counts

func (db *DB) Counts(ctx context.Context) (total, embedded int64, err error)

Counts returns total entry count and count of embedded entries.

func (*DB) DeleteEntries added in v0.3.0

func (db *DB) DeleteEntries(ctx context.Context, ids []uuid.UUID) (int64, error)

DeleteEntries removes multiple entries by ID in a single batch.

func (*DB) DeleteEntry added in v0.3.0

func (db *DB) DeleteEntry(ctx context.Context, id uuid.UUID) error

DeleteEntry removes a single entry by ID.

func (*DB) GetRecent

func (db *DB) GetRecent(ctx context.Context, limit int, agentID string) ([]domain.Entry, error)

GetRecent returns the N most recent entries, optionally filtered by agentID.

func (*DB) GetUnembedded

func (db *DB) GetUnembedded(ctx context.Context) ([]domain.Entry, error)

GetUnembedded returns all entries that have no embedding, ordered by created_at ascending. Used at startup to re-queue pending embed jobs.

func (*DB) Healthy

func (db *DB) Healthy(ctx context.Context) bool

Healthy pings Postgres and returns true if reachable.

func (*DB) InsertEntry

func (db *DB) InsertEntry(ctx context.Context, e domain.Entry) (domain.InsertResult, error)

InsertEntry inserts a new entry (without an embedding). On duplicate (same agent_id + content_hash), it updates timestamp and tags instead, returning the existing row's ID and Deduplicated=true.

func (*DB) ListAllEntries added in v0.3.0

func (db *DB) ListAllEntries(ctx context.Context) ([]domain.Entry, error)

ListAllEntries returns all entries ordered by timestamp ascending.

func (*DB) Migrate added in v0.4.0

func (db *DB) Migrate(ctx context.Context, migrationDir fs.FS) error

Migrate applies any unapplied SQL migrations from the provided filesystem. Migration files must be named like "001_init.sql" — they are sorted lexicographically and executed in order. Each migration is expected to insert its version (filename without .sql) into the migrations table; already-applied versions are skipped.

Call once at startup, after NewDB, before serving traffic.

func (*DB) SearchSimilar

func (db *DB) SearchSimilar(ctx context.Context, p domain.SearchParams) ([]domain.SearchResult, error)

SearchSimilar runs a cosine-similarity search against embedded entries. When p.Hybrid is true and p.QueryText is set, it combines vector similarity with Postgres full-text ranking (ts_rank) using Reciprocal Rank Fusion, plus a small temporal decay boost for recency.

func (*DB) TruncateEntries

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

TruncateEntries removes all rows from the entries table. Used by reindex.

func (*DB) UpdateEmbedding

func (db *DB) UpdateEmbedding(ctx context.Context, id uuid.UUID, vec []float32) error

UpdateEmbedding stores the computed vector for an entry.

func (*DB) UpsertEntry

func (db *DB) UpsertEntry(ctx context.Context, e domain.Entry) error

UpsertEntry inserts or updates an entry identified by id. Used by reindex.

type Pool added in v0.5.0

type Pool interface {
	Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
	SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults
	Ping(ctx context.Context) error
	Close()
}

Pool is the interface satisfied by *pgxpool.Pool and pgxmock for testing.

Jump to

Keyboard shortcuts

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