sqlstore

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: LGPL-3.0 Imports: 13 Imported by: 0 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 sqlstore implements vector.Store on database/sql for the databases a Dialect describes. The public backends, vector/sqlitevec and vector/pgvec, are this package with their own Dialect and their own check of the database they get.

Every index has a table set of its own, named by internal/tablename: docs (one row per document and its content hash), chunks (one row per chunk, its text and its vector) and meta (the dimensions of the vectors and the model of each collection). Only writes create the tables; a read of an index without them finds nothing. Searches are exact: the chunks of the scope are ranked by cosine distance and the best one of each document kept, with a window function.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dialect

type Dialect struct {
	// Name is the database's name in error messages.
	Name string
	// Numbered makes the placeholders $1, $2… instead of ?.
	Numbered bool
	// Schema returns the statements that create the docs, chunks and meta
	// tables of an index, given their names, for vectors of dims dimensions.
	// The meta table comes last: its existence says the others exist.
	Schema func(docs, chunks, meta string, dims int) []string
	// The tables of an index are created and dropped in one transaction, and
	// LockDDL, when not empty, is its first statement: a lock, bound to the
	// meta table's name, that makes two processes creating or dropping an
	// index's tables wait for each other. BeginDDL, when not empty, starts
	// that transaction on a connection of its own instead of BeginTx (SQLite's
	// BEGIN IMMEDIATE, which takes the write lock at once, so the busy timeout
	// covers the wait for it and no other writer can slip in between).
	LockDDL  string
	BeginDDL string
	// Legacy is the suffix of a table that only the schema before the meta
	// table had. An index that has it and no meta table is refused with an
	// error that says how to migrate.
	Legacy string
	// TableExists is a query that answers one row with one boolean: whether
	// the table named by its one parameter exists.
	TableExists string
	// ListTables is a query that answers the names of the tables whose name
	// starts with "ss_".
	ListTables string
	// Distance returns the expression of the cosine distance between two
	// vector expressions: a stored embedding and a query vector.
	Distance func(embedding, query string) string
	// Queries returns the WITH clause that defines the table q(i, v), which
	// turns the one argument EncodeQueries makes of the query vectors into
	// one row per vector: its index i, from 0, and the vector v. The clause
	// binds it to its one placeholder, so a search of any number of vectors
	// takes one parameter, and q must be computed once per statement, not
	// once per chunk. dims is the store's.
	Queries       func(dims int) string
	EncodeQueries func([][]float32) any
	// IDs is the subquery of the ids in the JSON array bound to its one
	// placeholder.
	IDs string
	// Encode turns a vector into the argument the embedding column is bound
	// to, and Decode turns what the column scans into back into a vector.
	Encode func([]float32) any
	Decode func(src any) ([]float32, error)
	// MissingTable reports whether err says a statement named a table that
	// does not exist: another Store, in this process or another, dropped the
	// index after this one found its tables.
	MissingTable func(err error) bool
	// LockRows, when not empty, is the clause that makes a SELECT lock the
	// rows it reads (FOR UPDATE): a delete takes the docs rows' locks with it
	// first, in id order, the order Upsert takes them, so the two never
	// deadlock whatever order the delete's plan visits the rows in. A
	// database that runs one writer at a time (SQLite) needs none.
	// ShareRows is the clause of a shared row lock (FOR SHARE), which a
	// writer takes on its collection's model record (see claimModel).
	LockRows  string
	ShareRows string
	// Retryable, when set, reports whether err is a transient failure of a
	// write transaction (a deadlock, a serialization failure) that is worth
	// running the transaction again for.
	Retryable func(err error) bool
}

Dialect holds what differs between the SQL databases a Store runs on. The statements are written once, with ? placeholders, and are otherwise plain SQL that these databases share.

type Store

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

Store implements vector.Store on SQL. It is safe for concurrent use as far as its *sql.DB is.

func New

func New(db *sql.DB, d Dialect, dims int, generation string) (*Store, error)

New returns a Store over db for vectors of dims dimensions, keeping its tables in generation (see tablename.For). It does not look at the database, which its caller knows how to check.

func (*Store) Chunks

func (s *Store) Chunks(ctx context.Context, c vector.Coord, id string) ([]vector.Chunk, error)

Chunks implements vector.Store.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, c vector.Coord, ids ...string) error

Delete implements vector.Store in one transaction (see deleteWhere), which locks the documents in the order Upsert does, so the two never deadlock; a write transaction that fails with a deadlock or serialization error anyway runs again, up to maxAttempts times.

func (*Store) DeleteCollection

func (s *Store) DeleteCollection(ctx context.Context, c vector.Coord) error

DeleteCollection implements vector.Store as Delete does, for every document of c, and drops the collection's model record in the same transaction when no partition of it holds documents any more.

func (*Store) DeleteIndex

func (s *Store) DeleteIndex(ctx context.Context, index string) error

DeleteIndex implements vector.Store by dropping the index's tables, those of the schema before the meta table included, all in one DDL transaction: no other Store ever sees some of them gone and the rest still there, and a failure part of the way drops none, so no later write can bring back a meta table over old documents. They are dropped in the order every write transaction locks them, meta, docs, chunks, so a drop waiting for a writer never deadlocks with it.

func (*Store) Docs

func (s *Store) Docs(ctx context.Context, c vector.Coord, after string, limit int) ([]vector.DocInfo, error)

Docs implements vector.Store.

func (*Store) DropGeneration

func (s *Store) DropGeneration(ctx context.Context, generation string) error

DropGeneration drops every table of generation, all its indexes', so that a migration can retire the generation it moved away from. It refuses the store's own generation, whose tables it is using.

func (*Store) Hashes

func (s *Store) Hashes(ctx context.Context, c vector.Coord, ids ...string) (map[string]string, error)

Hashes implements vector.Store.

func (*Store) Model

func (s *Store) Model(ctx context.Context, c vector.Coord) (string, error)

Model implements vector.Store.

func (*Store) Search

func (s *Store) Search(ctx context.Context, c vector.Coord, q vector.Query) ([]vector.Match, error)

Search implements vector.Store with one exact query, whatever the number of query vectors: every chunk in scope is scored by its smallest distance to any of them, in one scan of the chunks, the best chunk of each document (the lowest Seq on a tie) is kept, and the q.Limit best of those are returned, each with the first query vector at that distance. The cosine distance (0 = identical, 2 = opposite) is reported as 1-distance, so that higher is better.

func (*Store) Upsert

func (s *Store) Upsert(ctx context.Context, c vector.Coord, model string, docs ...vector.Doc) error

Upsert implements vector.Store. Every document is written in one transaction, in the order of their ids so that two concurrent writes of the same documents queue instead of deadlocking, and each starts with its docs row: that row's lock makes a second writer of the same document wait until the first is done, so the two never leave two sets of chunks. The first statement of the transaction is a write, so SQLite takes its write lock as the transaction begins and a busy timeout covers the wait for it.

Jump to

Keyboard shortcuts

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