pgvector

package
v0.0.0-...-7871f83 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package pgvector provides a PostgreSQL pgvector-backed VectorStore implementation.

pgvector is a PostgreSQL extension for vector similarity search. This implementation supports all core VectorStore operations including namespaced storage, metadata filtering, and index management.

Setup

Start PostgreSQL with pgvector using Docker:

docker run -p 5432:5432 -e POSTGRES_PASSWORD=password pgvector/pgvector:pg17

Basic Usage

store, err := pgvector.New(ctx, "postgres://user:pass@localhost:5432/db",
    pgvector.WithTableName("documents"),
    pgvector.WithDimensions(1536),
)
if err != nil {
    log.Fatal(err)
}
defer store.Close()

// Add documents
err = store.Add(ctx, []vectorstore.Document{
    {ID: "1", Content: "Hello", Embedding: vec1},
    {ID: "2", Content: "World", Embedding: vec2},
})

// Search
results, err := store.Search(ctx, queryVec, vectorstore.SearchOptions{K: 5})

Index Management

The pgvector store implements the Indexer interface:

// Create a table with vector column
err := store.CreateIndex(ctx, "products", 768, embedding.Cosine)

// List tables
tables, err := store.ListIndexes(ctx)

// Delete a table
err := store.DeleteIndex(ctx, "products")

Testing

For testing, use testcontainers to spin up a PostgreSQL instance:

import "github.com/testcontainers/testcontainers-go/modules/postgres"

container, _ := postgres.Run(ctx, "pgvector/pgvector:pg17",
    postgres.WithDatabase("testdb"),
    postgres.WithUsername("test"),
    postgres.WithPassword("test"),
)
connStr, _ := container.ConnectionString(ctx)
store, _ := New(ctx, connStr)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Options)

Option configures a Store.

func WithAutoCreateTable

func WithAutoCreateTable(auto bool) Option

WithAutoCreateTable enables automatic table creation.

func WithDimensions

func WithDimensions(dims int) Option

WithDimensions sets the vector dimensions.

func WithMetric

func WithMetric(metric embedding.Metric) Option

WithMetric sets the distance metric.

func WithTableName

func WithTableName(name string) Option

WithTableName sets the default table name.

type Options

type Options struct {
	// TableName is the default table to use. Default: "documents"
	TableName string

	// Dimensions is the vector dimensionality. Required for table creation.
	Dimensions int

	// Metric specifies the distance metric. Default: Cosine
	Metric embedding.Metric

	// AutoCreateTable creates the table if it doesn't exist.
	AutoCreateTable bool
}

Options configures the pgvector store.

type Pool

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

Pool defines the interface for PostgreSQL pool operations.

type Store

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

Store is a pgvector-backed VectorStore implementation.

func New

func New(ctx context.Context, connString string, optFns ...Option) (*Store, error)

New creates a new pgvector vector store by connecting to the given connection string.

func NewFromPool

func NewFromPool(ctx context.Context, pool Pool, optFns ...Option) (*Store, error)

NewFromPool creates a new pgvector vector store with the provided pool.

func (*Store) Add

func (s *Store) Add(ctx context.Context, docs []vectorstore.Document, optFns ...func(*vectorstore.AddOptions)) error

Add inserts or updates documents in the store.

func (*Store) Close

func (s *Store) Close() error

Close releases resources.

func (*Store) CreateIndex

func (s *Store) CreateIndex(ctx context.Context, name string, dims int, metric embedding.Metric) error

CreateIndex creates a new table with vector column.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, ids []string, namespace string) error

Delete removes documents by ID.

func (*Store) DeleteIndex

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

DeleteIndex removes a table and all its data.

func (*Store) ListIndexes

func (s *Store) ListIndexes(ctx context.Context) ([]string, error)

ListIndexes returns all tables with vector columns.

func (*Store) Search

func (s *Store) Search(ctx context.Context, queryEmbedding embedding.Vector, opts vectorstore.SearchOptions) ([]vectorstore.Document, error)

Search finds documents similar to the query embedding.

Jump to

Keyboard shortcuts

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