pgvector

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package pgvector implements vector-store capabilities with the pgvector PostgreSQL extension. Documents live in a regular PostgreSQL table with a typed `vector(N)` column; metadata is stored in `jsonb`. Documents containing media are rejected before embedding or writes; this store persists text and metadata only. Index batches commit independently, so an error can leave earlier batches stored.

Requirements: PostgreSQL 13+ with the `vector` extension installed (the store runs `CREATE EXTENSION IF NOT EXISTS vector` under StoreConfig.InitializeSchema = true).

Distance metrics — three of pgvector's six operators are exposed:

  • DistanceCosine (`<=>`) — cosine distance, `vector_cosine_ops`
  • DistanceL2 (`<->`) — Euclidean distance, `vector_l2_ops`
  • DistanceIP (`<#>`) — negative inner product, `vector_ip_ops`

Index types: HNSW (default, best query perf) / IVFFlat (faster builds) / none (exact sequential-scan). Vector binding uses pgvector-go's typed [pgvec.Vector]; the connection is a standard pgx pool.

Metadata filters compile to parameterized SQL; values flow through `$N` placeholders, while JSON numbers and booleans use explicit PostgreSQL casts.

See https://github.com/pgvector/pgvector for the extension docs.

Index

Constants

View Source
const (
	DefaultSchemaName     = "public"
	DefaultTableName      = "vector_store"
	DefaultMetadataColumn = "metadata"
	DefaultIndexSuffix    = "_embedding_idx"
)

Exported defaults keep constructor behavior visible and overridable.

View Source
const Provider = "PgVector"

Provider is the stable backend name for host-side attribution.

Variables

This section is empty.

Functions

This section is empty.

Types

type DistanceMetric

type DistanceMetric string

DistanceMetric selects the query operator and ANN-index opclass.

const (
	// DistanceCosine uses cosine distance (`<=>` and vector_cosine_ops).
	DistanceCosine DistanceMetric = "cosine"

	// DistanceL2 uses Euclidean distance (`<->` and vector_l2_ops).
	DistanceL2 DistanceMetric = "l2"

	// DistanceIP uses negative inner product (`<#>` and vector_ip_ops).
	DistanceIP DistanceMetric = "ip"
)

func (DistanceMetric) String

func (d DistanceMetric) String() string

func (DistanceMetric) Valid

func (d DistanceMetric) Valid() bool

type IndexType

type IndexType string

IndexType selects the ANN index created during schema initialization.

const (
	IndexHNSW    IndexType = "hnsw"
	IndexIVFFlat IndexType = "ivfflat"
	IndexNone    IndexType = "none"
)

These are the provider values this adapter recognizes.

func (IndexType) String

func (i IndexType) String() string

func (IndexType) Valid

func (i IndexType) Valid() bool

type Store

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

Store implements vector-store capabilities with PostgreSQL and pgvector.

func NewStore

func NewStore(ctx context.Context, config StoreConfig) (*Store, error)

NewStore performs schema setup during construction, which is why it takes a context: a store returned before its backing schema exists would fail on the first index rather than at wiring, where the misconfiguration actually is.

func (*Store) DeleteIDs

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

func (*Store) DeleteWhere

func (s *Store) DeleteWhere(ctx context.Context, predicate filter.Predicate) error

func (*Store) Index

func (s *Store) Index(ctx context.Context, request *vectorstore.IndexRequest) error

func (*Store) Search

type StoreConfig

type StoreConfig struct {
	Pool             *pgxpool.Pool
	SchemaName       string
	TableName        string
	IndexName        string
	MetadataColumn   string
	EmbeddingModel   embedding.Model
	DocumentBatcher  vectorstore.Batcher
	Dimensions       int
	DistanceMetric   DistanceMetric
	IndexType        IndexType
	InitializeSchema bool
}

StoreConfig names every dependency explicitly rather than defaulting a client or connection, so a store cannot be built against a service the caller did not choose.

func (StoreConfig) Validate

func (s StoreConfig) Validate() error

Jump to

Keyboard shortcuts

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