tencent cloud

TencentDB for PostgreSQL

pgvector Vector Extension Usage Guide

Download
Focus Mode
Font Size
Last updated: 2026-06-08 10:32:13

Overview

pgvector is an open-source vector similarity search extension for PostgreSQL, supporting both exact and approximate nearest neighbor search. TencentDB for PostgreSQL comes pre-installed with pgvector version 0.8.x, which provides the following core capabilities:
Vector Data Types: vector (single-precision), halfvec (half-precision), sparsevec (sparse vector).
Indexing Algorithms: HNSW (high-performance), IVFFlat (memory-friendly).
Distance Metrics: L2 Euclidean distance, cosine similarity, inner product, L1 Manhattan distance.

Prerequisites

The TencentDB for PostgreSQL instance version must be 12 or later.
You must have created a database and possess the CREATE EXTENSION privilege.

Installation and Enabling

-- Create the pgvector extension
CREATE EXTENSION IF NOT EXISTS vector;

-- Verify the installation
SELECT extversion FROM pg_extension WHERE extname = 'vector';

Vector Data Types

vector (Single-Precision Floating-Point)

The most commonly used vector type occupies 4 bytes per dimension.
-- Create a table with a vector column (1536-dimensional, compatible with common Embedding models).
CREATE TABLE documents (
id BIGSERIAL PRIMARY KEY,
content TEXT NOT NULL,
embedding vector(1536)
);

-- Insert vector data
INSERT INTO documents (content, embedding)
VALUES ('PostgreSQL is the most advanced open-source database', '[0.1, 0.2, 0.3, ...]'::vector(1536));
Note:
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).

halfvec (Half-Precision Floating-Point)

Each dimension occupies 2 bytes, making it suitable for large-scale vector storage (halving the space with minimal precision loss).
CREATE TABLE docs_halfvec (
id BIGSERIAL PRIMARY KEY,
embedding halfvec(1536)
);

sparsevec (Sparse Vector)

Suitable for high-dimensional sparse vectors (such as BM25 scores and SPLADE output).
CREATE TABLE docs_sparse (
id BIGSERIAL PRIMARY KEY,
embedding sparsevec(30000)
);

-- Use the {index:value}/dim format for sparse vectors.
INSERT INTO docs_sparse (embedding)
VALUES ('{1:0.5, 100:0.3, 999:0.8}/30000'::sparsevec);

Distance Operators

Operator
Meaning
Scenario
<->
L2 Euclidean distance
General similarity search
<=>
cosine distance
Text / image embedding (recommended)
<#>
negative inner product
Maximum inner product search for normalized vectors
<+>
L1 Manhattan distance
Special scenarios
-- Cosine similarity search (most commonly used)
SELECT id, content,
1 - (embedding <=> query_vec) AS similarity
FROM documents
ORDER BY embedding <=> query_vec
LIMIT 10;

-- L2 distance search
SELECT id, content
FROM documents
ORDER BY embedding <-> query_vec
LIMIT 10;

Creating Indexes

HNSW Index (Recommended)

HNSW (Hierarchical Navigable Small World) is a graph-based approximate search algorithm that offers high query performance and good recall.
-- Create an HNSW index (cosine distance)
CREATE INDEX ON documents
USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 200);

-- Create an HNSW index (L2 distance)
CREATE INDEX ON documents
USING hnsw (embedding vector_l2_ops)
WITH (m = 16, ef_construction = 200);
Parameter description:
Parameter
Default Value
Description
m
16
The maximum number of connections per node. A higher value increases precision but consumes more memory.
ef_construction
64
The search width during index construction. A larger value slows down the construction process but improves quality.
Query-time tuning:
-- Increase ef_search to improve recall (default: 40)
SET hnsw.ef_search = 100;

IVFFlat Index

IVFFlat is suitable for memory-sensitive scenarios and offers faster build speeds.
-- Data must exist before creation.
CREATE INDEX ON documents
USING ivfflat (embedding vector_cosine_ops)
WITH (lists = 100); -- The recommended value for lists is the square root of the number of rows.

-- Set the probe count during queries.
SET ivfflat.probes = 10;

Index Selection Recommendations

Scenario
Recommended Index
Reason
Dataset size < 1 million
HNSW
Fast queries, high recall
Dataset size > 1 million
Disk-friendly
Frequent inserts and updates
IVFFlat
Low incremental cost
Requires extremely high recall
HNSW (large ef_search)
Approaches exact search

Building a Semantic Search System

-- 1. Create a knowledge base table
CREATE TABLE knowledge_base (
id BIGSERIAL PRIMARY KEY,
title TEXT NOT NULL,
content TEXT NOT NULL,
embedding vector(1024),
metadata JSONB DEFAULT '{}',
created_at TIMESTAMPTZ DEFAULT NOW()
);

-- 2. Create an index
CREATE INDEX idx_kb_hnsw ON knowledge_base
USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 200);

-- 3. Semantic search (obtain the query vector using tencentdb_ai)
SELECT title, content,
1 - (embedding <=> query_embedding) AS similarity
FROM knowledge_base,
LATERAL (
SELECT tencentdb_ai.get_embedding('bge-m3', 'What is PostgreSQL?')
AS query_embedding
) q
WHERE embedding <=> query_embedding < 0.5
ORDER BY embedding <=> query_embedding
LIMIT 5;

Performance Tuning

Batch Import Optimization

-- Insert data first, then create the index (faster)
DROP INDEX IF EXISTS idx_kb_hnsw;
-- ... Batch COPY data ...
CREATE INDEX idx_kb_hnsw ON knowledge_base
USING hnsw (embedding vector_cosine_ops);

Vector Dimension Selection

Model
Level
Recommended Scenes
BGE-M3
1024
Chinese and English
text-embedding-3-small
1536
OpenAI ecosystem
Cohere embed-v3
1024
Multilingual
Custom Dimensionality Reduction
256 – 512
Ultra-large-scale data

Notes

Vectors in the same column must have the same dimension.
A full table scan may be faster when the data volume is < 1000 rows.
NULL vectors are not included in the index.
The HNSW index supports concurrent read and write operations.
It is recommended to apply a NOT NULL constraint to vector columns.

References

Help and Support

Was this page helpful?

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

Feedback