Skip to main content

PG 向量存储

🌐 PG Vector Store

PgVector 类提供使用 PostgreSQLpgvector 扩展的向量搜索功能。它在你现有的 PostgreSQL 数据库中提供强大的向量相似性搜索能力。

🌐 The PgVector class provides vector search using PostgreSQL with pgvector extension. It provides robust vector similarity search capabilities within your existing PostgreSQL database.

构造函数选项
Direct link to 构造函数选项

🌐 Constructor Options

connectionString?:

string
PostgreSQL connection URL

host?:

string
PostgreSQL server host

port?:

number
PostgreSQL server port

database?:

string
PostgreSQL database name

user?:

string
PostgreSQL user

password?:

string
PostgreSQL password

ssl?:

boolean | ConnectionOptions
Enable SSL or provide custom SSL configuration

schemaName?:

string
The name of the schema you want the vector store to use. Will use the default schema if not provided.

max?:

number
Maximum number of pool connections (default: 20)

idleTimeoutMillis?:

number
Idle connection timeout in milliseconds (default: 30000)

pgPoolOptions?:

PoolConfig
Additional pg pool configuration options

构造函数示例
Direct link to 构造函数示例

🌐 Constructor Examples

连接字符串
Direct link to 连接字符串

🌐 Connection String

import { PgVector } from "@mastra/pg";

const vectorStore = new PgVector({
id: 'pg-vector',
connectionString: "postgresql://user:password@localhost:5432/mydb",
});

主机/端口/数据库配置
Direct link to 主机/端口/数据库配置

🌐 Host/Port/Database Configuration

const vectorStore = new PgVector({
id: 'pg-vector',
host: "localhost",
port: 5432,
database: "mydb",
user: "postgres",
password: "password",
});

高级配置
Direct link to 高级配置

🌐 Advanced Configuration

const vectorStore = new PgVector({
id: 'pg-vector',
connectionString: "postgresql://user:password@localhost:5432/mydb",
schemaName: "custom_schema",
max: 30,
idleTimeoutMillis: 60000,
pgPoolOptions: {
connectionTimeoutMillis: 5000,
allowExitOnIdle: true,
},
});

方法
Direct link to 方法

🌐 Methods

createIndex()
Direct link to createIndex()

indexName:

string
Name of the index to create

dimension:

number
Vector dimension (must match your embedding model)

metric?:

'cosine' | 'euclidean' | 'dotproduct'
= cosine
Distance metric for similarity search

indexConfig?:

IndexConfig
= { type: 'ivfflat' }
Index configuration

buildIndex?:

boolean
= true
Whether to build the index

IndexConfig
Direct link to IndexConfig

type:

'flat' | 'hnsw' | 'ivfflat'
= ivfflat
Index type
string

flat:

flat
Sequential scan (no index) that performs exhaustive search.

ivfflat:

ivfflat
Clusters vectors into lists for approximate search.

hnsw:

hnsw
Graph-based index offering fast search times and high recall.

ivf?:

IVFConfig
IVF configuration
object

lists?:

number
Number of lists. If not specified, automatically calculated based on dataset size. (Minimum 100, Maximum 4000)

hnsw?:

HNSWConfig
HNSW configuration
object

m?:

number
Maximum number of connections per node (default: 8)

efConstruction?:

number
Build-time complexity (default: 32)

内存要求
Direct link to 内存要求

🌐 Memory Requirements

HNSW 索引在构建过程中需要大量共享内存。对于 10 万个向量:

🌐 HNSW indexes require significant shared memory during construction. For 100K vectors:

  • 小尺寸(64d):默认设置下约为60MB
  • 中等维度(256d):在默认设置下约为180MB
  • 大尺寸(384d+):使用默认设置约250MB以上

较高的 M 值或 efConstruction 值将显著增加内存需求。如有必要,请调整系统的共享内存限制。

🌐 Higher M values or efConstruction values will increase memory requirements significantly. Adjust your system's shared memory limits if needed.

upsert()
Direct link to upsert()

indexName:

string
Name of the index to upsert vectors into

vectors:

number[][]
Array of embedding vectors

metadata?:

Record<string, any>[]
Metadata for each vector

ids?:

string[]
Optional vector IDs (auto-generated if not provided)

query()
Direct link to query()

indexName:

string
Name of the index to query

vector:

number[]
Query vector

topK?:

number
= 10
Number of results to return

filter?:

Record<string, any>
Metadata filters

includeVector?:

boolean
= false
Whether to include the vector in the result

minScore?:

number
= 0
Minimum similarity score threshold

options?:

{ ef?: number; probes?: number }
Additional options for HNSW and IVF indexes
object

ef?:

number
HNSW search parameter

probes?:

number
IVF search parameter

listIndexes()
Direct link to listIndexes()

返回一个由索引名称组成的字符串数组。

🌐 Returns an array of index names as strings.

describeIndex()
Direct link to describeIndex()

indexName:

string
Name of the index to describe

返回:

🌐 Returns:

interface PGIndexStats {
dimension: number;
count: number;
metric: "cosine" | "euclidean" | "dotproduct";
type: "flat" | "hnsw" | "ivfflat";
config: {
m?: number;
efConstruction?: number;
lists?: number;
probes?: number;
};
}

deleteIndex()
Direct link to deleteIndex()

indexName:

string
Name of the index to delete

updateVector()
Direct link to updateVector()

通过 ID 或元数据过滤器更新单个向量。必须提供 idfilter 中的一个,但不能同时提供两者。

🌐 Update a single vector by ID or by metadata filter. Either id or filter must be provided, but not both.

indexName:

string
Name of the index containing the vector

id?:

string
ID of the vector to update (mutually exclusive with filter)

filter?:

Record<string, any>
Metadata filter to identify vector(s) to update (mutually exclusive with id)

update:

{ vector?: number[]; metadata?: Record<string, any>; }
Object containing the vector and/or metadata to update

通过 ID 或筛选条件更新现有向量。更新对象中必须至少提供向量或元数据中的一项。

🌐 Updates an existing vector by ID or filter. At least one of vector or metadata must be provided in the update object.

// Update by ID
await pgVector.updateVector({
indexName: "my_vectors",
id: "vector123",
update: {
vector: [0.1, 0.2, 0.3],
metadata: { label: "updated" },
},
});

// Update by filter
await pgVector.updateVector({
indexName: "my_vectors",
filter: { category: "product" },
update: {
metadata: { status: "reviewed" },
},
});

deleteVector()
Direct link to deleteVector()

indexName:

string
Name of the index containing the vector

id:

string
ID of the vector to delete

根据指定索引通过 ID 删除单个向量。

🌐 Deletes a single vector by ID from the specified index.

await pgVector.deleteVector({ indexName: "my_vectors", id: "vector123" });

deleteVectors()
Direct link to deleteVectors()

通过 ID 或元数据过滤器删除多个向量。必须提供 idsfilter 中的一个,但不能同时提供两者。

🌐 Delete multiple vectors by IDs or by metadata filter. Either ids or filter must be provided, but not both.

indexName:

string
Name of the index containing the vectors to delete

ids?:

string[]
Array of vector IDs to delete (mutually exclusive with filter)

filter?:

Record<string, any>
Metadata filter to identify vectors to delete (mutually exclusive with ids)

disconnect()
Direct link to disconnect()

关闭数据库连接池。使用存储完成后应调用此方法。

🌐 Closes the database connection pool. Should be called when done using the store.

buildIndex()
Direct link to buildIndex()

indexName:

string
Name of the index to define

metric?:

'cosine' | 'euclidean' | 'dotproduct'
= cosine
Distance metric for similarity search

indexConfig:

IndexConfig
Configuration for the index type and parameters

使用指定的度量和配置构建或重建索引。创建新索引之前会删除任何已有索引。

🌐 Builds or rebuilds an index with specified metric and configuration. Will drop any existing index before creating the new one.

// Define HNSW index
await pgVector.buildIndex("my_vectors", "cosine", {
type: "hnsw",
hnsw: {
m: 8,
efConstruction: 32,
},
});

// Define IVF index
await pgVector.buildIndex("my_vectors", "cosine", {
type: "ivfflat",
ivf: {
lists: 100,
},
});

// Define flat index
await pgVector.buildIndex("my_vectors", "cosine", {
type: "flat",
});

响应类型
Direct link to 响应类型

🌐 Response Types

查询结果以此格式返回:

🌐 Query results are returned in this format:

interface QueryResult {
id: string;
score: number;
metadata: Record<string, any>;
vector?: number[]; // Only included if includeVector is true
}

错误处理
Direct link to 错误处理

🌐 Error Handling

该存储会抛出可以被捕获的类型化错误:

🌐 The store throws typed errors that can be caught:

try {
await store.query({
indexName: "index_name",
queryVector: queryVector,
});
} catch (error) {
if (error instanceof VectorStoreError) {
console.log(error.code); // 'connection_failed' | 'invalid_dimension' | etc
console.log(error.details); // Additional error context
}
}

索引配置指南
Direct link to 索引配置指南

🌐 Index Configuration Guide

性能优化
Direct link to 性能优化

🌐 Performance Optimization

IVF平面调谐
Direct link to IVF平面调谐

🌐 IVFFlat Tuning

  • lists 参数:设置为 sqrt(n) * 2,其中 n 是向量的数量
  • 更多列表 = 更高精度但构建时间更慢
  • 列表越少 = 构建越快,但可能准确性较低

HNSW 调优
Direct link to HNSW 调优

🌐 HNSW Tuning

  • m 参数
    • 8-16:准确度中等,内存力较低
    • 16-32:高精度,中等内存
    • 32-64:极高精度,高内存
  • efConstruction
    • 32-64:构建快,质量好
    • 64-128:构建较慢,但质量更好
    • 128-256:最慢的构建,最佳质量

索引重建行为
Direct link to 索引重建行为

🌐 Index Recreation Behavior

系统会自动检测配置更改,并且仅在必要时重建索引:

🌐 The system automatically detects configuration changes and only rebuilds indexes when necessary:

  • 相同配置:索引保持不变(不重新创建)
  • 配置已更改:索引已被删除并重建
  • 这可以防止因不必要的索引重建而导致的性能问题

最佳实践
Direct link to 最佳实践

🌐 Best Practices

  • 定期评估你的索引配置以确保最佳性能。
  • 根据数据集大小和查询要求调整参数,如 listsm
  • 使用 describeIndex() 监控索引性能 以跟踪使用情况
  • 定期重建索引以保持效率,尤其是在数据发生重大变化后

直接访问池
Direct link to 直接访问池

🌐 Direct Pool Access

PgVector 类将其底层的 PostgreSQL 连接池作为公共字段公开:

🌐 The PgVector class exposes its underlying PostgreSQL connection pool as a public field:

pgVector.pool; // instance of pg.Pool

这可以实现高级用法,例如运行直接的 SQL 查询、管理事务或监控连接池状态。直接使用连接池时:

🌐 This enables advanced usage such as running direct SQL queries, managing transactions, or monitoring pool state. When using the pool directly:

  • 你有责任在使用后释放客户端(client.release())。
  • 在调用 disconnect() 之后,连接池仍然可访问,但新的查询将会失败。
  • 直接访问会绕过 PgVector 方法提供的任何验证或事务逻辑。

这个设计支持高级用例,但需要用户仔细管理资源。

🌐 This design supports advanced use cases but requires careful resource management by the user.

使用示例
Direct link to 使用示例

🌐 Usage Example

使用 fastembed 的本地嵌入
Direct link to 使用 fastembed 的本地嵌入

🌐 Local embeddings with fastembed

嵌入是由内存的 semanticRecall 用来按意义(而非关键词)检索相关信息的数字向量。这一设置使用 @mastra/fastembed 来生成向量嵌入。

🌐 Embeddings are numeric vectors used by memory's semanticRecall to retrieve related messages by meaning (not keywords). This setup uses @mastra/fastembed to generate vector embeddings.

安装 fastembed 以开始:

🌐 Install fastembed to get started:

npm install @mastra/fastembed@latest

将以下内容添加到你的代理中:

🌐 Add the following to your agent:

src/mastra/agents/example-pg-agent.ts
import { Memory } from "@mastra/memory";
import { Agent } from "@mastra/core/agent";
import { PostgresStore, PgVector } from "@mastra/pg";
import { fastembed } from "@mastra/fastembed";

export const pgAgent = new Agent({
id: "pg-agent",
name: "PG Agent",
instructions:
"You are an AI agent with the ability to automatically recall memories from previous interactions.",
model: "openai/gpt-5.1",
memory: new Memory({
storage: new PostgresStore({
id: 'pg-agent-storage',
connectionString: process.env.DATABASE_URL!,
}),
vector: new PgVector({
id: 'pg-agent-vector',
connectionString: process.env.DATABASE_URL!,
}),
embedder: fastembed,
options: {
lastMessages: 10,
semanticRecall: {
topK: 3,
messageRange: 2,
},
},
}),
});

🌐 Related