Documentation
¶
Overview ¶
Package pgvector implements Core vector-store capabilities with PostgreSQL's pgvector extension. The host owns the pgx pool. InitializeSchema optionally provisions the extension, schema, four current columns, and selected ANN index.
Current columns are id BYTEA PRIMARY KEY, content TEXT NOT NULL, metadata BYTEA NOT NULL (under MetadataColumn), and embedding VECTOR(N) NOT NULL. Native schema inspection establishes width. Dimensions only seeds a new column. Binary ID equality preserves Core identity; metadata stores complete Core JSON bytes. Recreate former text-ID/JSONB tables and reindex authoritative documents. No compatibility adapter or automatic schema change is provided.
postgres.DistanceCosine selects <=>, postgres.DistanceL2 selects <->, and postgres.DistanceIP selects <#>. IndexHNSW and IndexIVFFlat create native indexes with the selected opclass; IndexNone uses native exact scanning. DistanceMetric is a query choice. It does not override native schema or index configuration.
Core filter.Match selects metadata in a serializable source snapshot. Predicate deletion holds native row locks through commit. Index prepares every vector and commits the whole request atomically; conflicts and native failures are errors. Unfiltered Search ranks directly with native distance. Filtered Search ranks the selected binary IDs; its full-source read preserves Core semantics rather than using SQL metadata indexes. Both validate returned rows before MinScore and reject hybrid mode. Content cannot contain NUL; binary identity and encoded metadata can. See the parent postgres package for shared guarantees.
Native extension contracts: https://github.com/pgvector/pgvector.
Index ¶
- Constants
- type IndexType
- type Store
- func (s *Store) DeleteIDs(ctx context.Context, ids []string) error
- func (s *Store) DeleteWhere(ctx context.Context, predicate filter.Predicate) error
- func (s *Store) Index(ctx context.Context, request *vectorstore.IndexRequest) error
- func (s *Store) Search(ctx context.Context, request *vectorstore.SearchRequest) (*vectorstore.SearchResponse, error)
- type StoreConfig
Examples ¶
Constants ¶
const ( DefaultSchemaName = "public" DefaultTableName = "vector_store" DefaultMetadataColumn = "metadata" DefaultIndexSuffix = "_embedding_idx" )
const Provider = "PgVector"
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
func NewStore ¶
func NewStore(ctx context.Context, config StoreConfig) (*Store, error)
Example ¶
package main
import (
"context"
"os"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/Tangerg/scope/core/document"
"github.com/Tangerg/scope/core/embedding"
"github.com/Tangerg/scope/core/vectorstore"
"github.com/Tangerg/scope/vectorstores/postgres/pgvector"
)
type documentBatcher struct{}
func (d documentBatcher) Batch(_ context.Context, documents []*document.Document) ([][]*document.Document, error) {
return [][]*document.Document{documents}, nil
}
func main() {
ctx := context.Background()
pool, err := pgxpool.New(ctx, os.Getenv("SCOPE_PGVECTOR_DSN"))
if err != nil {
panic(err)
}
defer pool.Close()
model := embedding.ModelFunc(func(_ context.Context, request *embedding.Request) (*embedding.Response, error) {
outputs := make([]*embedding.Output, len(request.Texts))
for index := range outputs {
outputs[index] = &embedding.Output{Embedding: []float64{1, 0}}
}
return embedding.NewResponse(outputs, nil)
})
store, err := pgvector.NewStore(ctx, pgvector.StoreConfig{Pool: pool, SchemaName: "scope", TableName: "documents", EmbeddingModel: model, DocumentBatcher: documentBatcher{}, Dimensions: 2, InitializeSchema: true})
if err != nil {
panic(err)
}
if err = store.Index(ctx, &vectorstore.IndexRequest{Documents: []*document.Document{{ID: "one", Text: "current record"}}}); err != nil {
panic(err)
}
if _, err = store.Search(ctx, &vectorstore.SearchRequest{Query: "current"}); err != nil {
panic(err)
}
if err = store.DeleteIDs(ctx, []string{"one"}); err != nil {
panic(err)
}
}
Output:
func (*Store) DeleteWhere ¶
func (*Store) Index ¶
func (s *Store) Index(ctx context.Context, request *vectorstore.IndexRequest) error
func (*Store) Search ¶
func (s *Store) Search(ctx context.Context, request *vectorstore.SearchRequest) (*vectorstore.SearchResponse, error)
type StoreConfig ¶
type StoreConfig struct {
Pool *pgxpool.Pool
SchemaName string
TableName string
IndexName string
MetadataColumn string
EmbeddingModel embedding.Model
DocumentBatcher vectorstore.Batcher
// Dimensions seeds only a newly created native column; existing native width is authoritative.
Dimensions int
DistanceMetric postgres.DistanceMetric
IndexType IndexType
InitializeSchema bool
}
func (StoreConfig) Validate ¶
func (s StoreConfig) Validate() error