Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pgvector restore WithConnectionURL option #832

Merged
merged 1 commit into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion vectorstores/pgvector/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ func WithCollectionTableName(name string) Option {
}
}

// WithConnectionURL is an option for specifying the Postgres connection URL. Either this
// or WithConn must be used.
func WithConnectionURL(connectionURL string) Option {
return func(p *Store) {
p.connURL = connectionURL
}
}

// WithConn is an option for specifying the Postgres connection.
// From pgx doc: it is not safe for concurrent usage.Use a connection pool to manage access
// to multiple database connections from multiple goroutines.
Expand Down Expand Up @@ -106,7 +114,7 @@ func applyClientOptions(opts ...Option) (Store, error) {
opt(o)
}

if o.conn == nil {
if o.conn == nil && o.connURL == "" {
return Store{}, fmt.Errorf("%w: missing postgres connection", ErrInvalidOptions)
}

Expand Down
23 changes: 23 additions & 0 deletions vectorstores/pgvector/pgvector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"io"
"strings"

"github.com/google/uuid"
Expand Down Expand Up @@ -45,9 +46,14 @@ type PGXConn interface {
SendBatch(ctx context.Context, batch *pgx.Batch) pgx.BatchResults
}

type CloseNoErr interface {
Close()
}

// Store is a wrapper around the pgvector client.
type Store struct {
embedder embeddings.Embedder
connURL string
conn PGXConn
embeddingTableName string
collectionTableName string
Expand All @@ -73,6 +79,12 @@ func New(ctx context.Context, opts ...Option) (Store, error) {
if err != nil {
return Store{}, err
}
if store.conn == nil {
store.conn, err = pgx.Connect(ctx, store.connURL)
if err != nil {
return Store{}, err
}
}
if err = store.conn.Ping(ctx); err != nil {
return Store{}, err
}
Expand All @@ -82,6 +94,17 @@ func New(ctx context.Context, opts ...Option) (Store, error) {
return store, nil
}

// Close closes the connection.
func (s Store) Close() error {
if closer, ok := s.conn.(io.Closer); ok {
return closer.Close()
}
if closer, ok := s.conn.(CloseNoErr); ok {
closer.Close()
}
return nil
}

func (s *Store) init(ctx context.Context) error {
tx, err := s.conn.Begin(ctx)
if err != nil {
Expand Down
Loading