tencent cloud

TencentDB for PostgreSQL

DocumentationTencentDB for PostgreSQLAI PracticeHigh-Performance Vector Search Based on PostgreSQL + pgvectorscale

High-Performance Vector Search Based on PostgreSQL + pgvectorscale

PDF
Focus Mode
Font Size
Last updated: 2026-05-06 16:53:37
This document introduces AI practices for high-performance vector search based on PostgreSQL + pgvectorscale.

Overview

pgvectorscale is a PostgreSQL vector search enhancement extension designed specifically for AI application scenarios. It implements efficient approximate nearest neighbor (ANN) indexes based on the Microsoft DiskANN algorithm. As a supplement to pgvector, it provides a vector index solution with lower memory consumption and higher search efficiency for scenarios such as large model RAG retrieval, semantic search, intelligent recommendation, and multimodal AI applications. This helps you build vector storage and search capabilities for AI intelligent applications in a one-stop manner within PostgreSQL.
TencentDB for PostgreSQL now supports the pgvectorscale v0.9.0 extension (which depends on pgvector v0.8.2). You do not need to compile or install it separately. You can directly enable and use it in the console.

Applicable Scenarios

Semantic search (RAG) in large AI model applications.
Similarity search for multimodal data such as images/audio/video.
Candidate set recall for recommendation systems.
Efficient search for ultra-large-scale vector datasets (millions of vectors or more).

1. Environment Preparation

Version Requirement

Component
Version Requirement
TencentDB for PostgreSQL
For version 14 and above, upgrade to the latest kernel minor version. For the upgrade procedure, see Upgrade Kernel Minor Version.
pgvector extension
0.8.2 or above
pgvectorscale extension
0.9.0 or above

Installing an Extension

pgvectorscale depends on the pgvector extension and must be installed in sequence:
-- Step 1: Install the pgvector extension (skip this step if it is already installed)
CREATE EXTENSION IF NOT EXISTS vector;

-- Step 2: Install the pgvectorscale extension
CREATE EXTENSION IF NOT EXISTS vectorscale;
After installation is complete, you can verify it using the following command:
SELECT extname, extversion FROM pg_extension
WHERE extname IN ('vector', 'vectorscale');

2. Quick Start

Creating a Vector Table

-- Create a document table containing 128-dimensional vectors.
CREATE TABLE documents (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
content TEXT,
embedding vector(128) -- Adjust the vector dimension according to your actual model.
);
Notes:
The vector dimension must match the output dimension of the large model you are using. Common model dimensions: OpenAI text-embedding-3-small (1536 dimensions), BGE-M3 (1024 dimensions), ChatGLM Embedding (1024 dimensions).

Creating a DiskANN Index

pgvectorscale provides the diskann index type, which supports three distance metrics:
-- Cosine distance (most commonly used, suitable for semantic search)
CREATE INDEX idx_doc_cosine ON documents
USING diskann (embedding vector_cosine_ops);

-- Euclidean distance (L2)
CREATE INDEX idx_doc_l2 ON documents
USING diskann (embedding vector_l2_ops);

-- Inner product distance
CREATE INDEX idx_doc_ip ON documents
USING diskann (embedding vector_ip_ops);

Inserting Vector Data

-- Insert a single data record
INSERT INTO documents (title, content, embedding)
VALUES (
'TencentDB for PostgreSQL Introduction',
'PostgreSQL is a powerful, open-source relational database...',
'[0.1, 0.2, -0.3, ...]'::vector
);

-- Batch insertion (recommended to use COPY or multi-row INSERT)
INSERT INTO documents (title, content, embedding) VALUES
('Document 1', 'Content 1', '[0.1, 0.2, ...]'::vector),
('Document 2', 'Content 2', '[0.3, 0.4, ...]'::vector);

Vector Similarity Search

-- Query the Top 5 most similar documents using cosine distance
SELECT id, title, embedding <=> '[0.1, 0.2, ...]'::vector AS distance
FROM documents
ORDER BY embedding <=> '[0.1, 0.2, ...]'::vector
LIMIT 5;

-- L2 distance query
SELECT id, title, embedding <-> '[0.1, 0.2, ...]'::vector AS distance
FROM documents
ORDER BY embedding <-> '[0.1, 0.2, ...]'::vector
LIMIT 5;

-- Inner product query
SELECT id, title, embedding <#> '[0.1, 0.2, ...]'::vector AS distance
FROM documents
ORDER BY embedding <#> '[0.1, 0.2, ...]'::vector
LIMIT 5;
Distance operator description:
Operator
Distance Type
Scenarios
<=>
cosine distance
Semantic similarity search (Recommended)
<->
Euclidean distance (L2)
Distance calculation in coordinate space
<#>
negative inner product
Dot product similarity

3. Index Parameter Tuning

The DiskANN index supports the following configurable parameters to balance query accuracy and performance:

Core Parameters

Parameter
Default Value
Description
num_neighbors
32
The number of neighbors for each node in the graph. Increasing this value can improve Recall but will increase the index size and build time.
search_list_size
100
The size of the candidate list during a search. Increasing this value can improve Recall but will increase query latency.

Parameter Configuration Examples

-- High Recall configuration (suitable for scenarios requiring high accuracy)
CREATE INDEX idx_high_recall ON documents
USING diskann (embedding vector_cosine_ops)
WITH (num_neighbors = 64, search_list_size = 200);

-- High Throughput configuration (suitable for scenarios requiring low latency)
CREATE INDEX idx_low_latency ON documents
USING diskann (embedding vector_cosine_ops)
WITH (num_neighbors = 32, search_list_size = 80);

-- Large Dataset configuration (millions of vectors or more)
CREATE INDEX idx_large_scale ON documents
USING diskann (embedding vector_cosine_ops)
WITH (num_neighbors = 50, search_list_size = 150);

Tuning Suggestion

Data Scale
num_neighbors
search_list_size
Expected Recall
< 100,000
32
100
≥ 95%
100,000 - 1,000,000
50
150
≥ 90%
> 1,000,000
64
200
≥ 85%
Notes:
The above parameters are recommended starting values. Their actual performance varies with data distribution, so you should test and adjust them based on your specific business scenarios.

4. DiskANN vs. HNSW Index Comparison

TencentDB for PostgreSQL supports both the HNSW index from pgvector and the DiskANN index from pgvectorscale. The following is a performance comparison based on actual tests (128-dimensional vectors, 2000 data points):

Selection Recommendations

Scenario
Recommended Index
Reason
Dataset size < 1 million, latency-sensitive
HNSW
Fast construction, low query latency
Dataset size > 1 million
DiskANN
Low memory footprint, disk-friendly
Memory resources are constrained
DiskANN
Storage footprint is about 60% of HNSW's.
Frequent data updates
DiskANN
Supports concurrent inserts and mixed read/write operations.
Requires extremely high Recall (>99%).
HNSW
Slightly better precision

5. Typical Application Scenarios

RAG Knowledge Base Search

Suitable for Retrieval-Augmented Generation (RAG) in large model applications:
-- Create a knowledge base table
CREATE TABLE knowledge_base (
id SERIAL PRIMARY KEY,
doc_title TEXT,
chunk_text TEXT,
embedding vector(1024), -- Using the BGE-M3 model as an example
metadata JSONB
);

-- Create a DiskANN index
CREATE INDEX idx_kb_diskann ON knowledge_base
USING diskann (embedding vector_cosine_ops)
WITH (num_neighbors = 50, search_list_size = 150);

-- Search for the 5 document fragments most relevant to the question
SELECT chunk_text, metadata,
embedding <=> $1::vector AS distance
FROM knowledge_base
ORDER BY embedding <=> $1::vector
LIMIT 5;

Product Recommendation System

-- Product vector table
CREATE TABLE product_embeddings (
product_id BIGINT PRIMARY KEY,
product_name TEXT,
category TEXT,
embedding vector(256)
);

-- Similar product search filtered by category
SELECT product_id, product_name,
embedding <=> $1::vector AS distance
FROM product_embeddings
WHERE category = 'Electronics'
ORDER BY embedding <=> $1::vector
LIMIT 20;

Multimodal Search (Image Search)

-- Image vector table
CREATE TABLE image_vectors (
image_id BIGINT PRIMARY KEY,
image_url TEXT,
description TEXT,
embedding vector(512) -- CLIP model output
);

CREATE INDEX idx_img_diskann ON image_vectors
USING diskann (embedding vector_cosine_ops);

-- Search for images using an image
SELECT image_id, image_url, description,
embedding <=> $1::vector AS distance
FROM image_vectors
ORDER BY embedding <=> $1::vector
LIMIT 10;

6. Index Maintenance

Index Rebuilding

After significant data changes occur, it is recommended to periodically reindex to maintain optimal performance:
-- Reindex
REINDEX INDEX idx_doc_cosine;

-- Or perform concurrent rebuilding (non-blocking to queries)
REINDEX INDEX CONCURRENTLY idx_doc_cosine;

Monitoring Index Status

-- View index size
SELECT pg_size_pretty(pg_relation_size('idx_doc_cosine')) AS index_size;

-- View index usage
SELECT indexrelname, idx_scan, idx_tup_read, idx_tup_fetch
FROM pg_stat_user_indexes
WHERE indexrelname LIKE '%diskann%';

7. Precautions

Extension dependencies: pgvectorscale depends on pgvector. Before uninstalling pgvector, you must first uninstall pgvectorscale.
Data type: The DiskANN index only supports floating-point vector data.
Dimension limitation: The vector dimension must match the large model output dimension and cannot be changed after creation.
Concurrency safety: The DiskANN index supports concurrent inserts and mixed read/write operations without requiring additional locks.
Index building: Building indexes on large datasets requires a significant amount of time. It is recommended to perform this operation during off-peak business hours.
Embedding: The Embedding field must be generated by you using a large model.

Appendix: Distance Functions

Distance Type
Operator
Index Operator Class
Applicable Function
cosine distance
<=>
vector_cosine_ops
cosine_distance()
Euclidean distance
<->
vector_l2_ops
l2_distance()
negative inner product
<#>
vector_ip_ops
inner_product()

Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback