Skip to content

@anvia/pgvector ​

@anvia/pgvector stores Anvia embedded documents in PostgreSQL using the pgvector extension. It is useful when relational application data and retrieval data should share one operational database.

Install ​

sh
pnpm add @anvia/pgvector @anvia/core @anvia/openai pg pgvector

The ESM package includes pg and pgvector and peers with @anvia/core >=0.7.1 <1.0.0.

Store and search documents ​

ts
import { embedDocuments } from '@anvia/core/embeddings'
import { PgVectorStore } from '@anvia/pgvector'
import { OpenAIClient } from '@anvia/openai'

const openai = new OpenAIClient({
  apiKey: process.env.OPENAI_API_KEY,
})
const embeddings = openai.embeddingModel('text-embedding-3-small')
const sourceDocuments = [
  {
    id: 'password-reset',
    text: 'Password reset links expire after 30 minutes.',
    tenantId: 'acme',
  },
]

const documents = await embedDocuments(embeddings, sourceDocuments, {
  id: (document) => document.id,
  content: (document) => document.text,
  metadata: (document) => ({ tenantId: document.tenantId }),
})

const store = await PgVectorStore.connect({
  connectionString: process.env.DATABASE_URL,
  tableName: 'support_docs',
  vectorSize: 1536,
})

await store.upsertDocuments(documents)

const results = await store.index(embeddings).search({
  query: 'How do I reset a password?',
  topK: 5,
})

You can inject any compatible pg client or pool instead of a connection string.

Schema and index ownership ​

The default createIfMissing: true creates the vector extension and a table with ID, logical document ID, JSONB document, JSONB metadata, and the configured vector column. connect() always validates the stored vector dimension.

The adapter does not create an HNSW or IVFFlat index. For production:

  1. Create the extension and table in a migration.
  2. Add the pgvector index appropriate for the selected distance and workload.
  3. Deploy the schema before the application.
  4. Set createIfMissing: false at runtime.

Use cosine, l2, or innerProduct consistently between the adapter and the database index.

Production patterns ​

  • Reuse an application-managed pool and close it through the application lifecycle.
  • Validate the query plan after adding an ANN index.
  • Keep table names static; the adapter validates and quotes qualified identifiers.
  • Keep metadata keys outside the reserved __anvia_ prefix.
  • Design JSONB or expression indexes for metadata filters used frequently.

Read Embeddings and Metadata filters for the SDK workflow.

Reference ​

Built for Anvia.