CREATE EXTENSION privilege.-- Create the pgvector extensionCREATE EXTENSION IF NOT EXISTS vector;-- Verify the installationSELECT extversion FROM pg_extension WHERE extname = 'vector';
-- 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 dataINSERT INTO documents (content, embedding)VALUES ('PostgreSQL is the most advanced open-source database', '[0.1, 0.2, 0.3, ...]'::vector(1536));
CREATE TABLE docs_halfvec (id BIGSERIAL PRIMARY KEY,embedding halfvec(1536));
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);
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 similarityFROM documentsORDER BY embedding <=> query_vecLIMIT 10;-- L2 distance searchSELECT id, contentFROM documentsORDER BY embedding <-> query_vecLIMIT 10;
-- Create an HNSW index (cosine distance)CREATE INDEX ON documentsUSING hnsw (embedding vector_cosine_ops)WITH (m = 16, ef_construction = 200);-- Create an HNSW index (L2 distance)CREATE INDEX ON documentsUSING hnsw (embedding vector_l2_ops)WITH (m = 16, ef_construction = 200);
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. |
-- Increase ef_search to improve recall (default: 40)SET hnsw.ef_search = 100;
-- Data must exist before creation.CREATE INDEX ON documentsUSING 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;
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 |
-- 1. Create a knowledge base tableCREATE 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 indexCREATE INDEX idx_kb_hnsw ON knowledge_baseUSING 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 similarityFROM knowledge_base,LATERAL (SELECT tencentdb_ai.get_embedding('bge-m3', 'What is PostgreSQL?')AS query_embedding) qWHERE embedding <=> query_embedding < 0.5ORDER BY embedding <=> query_embeddingLIMIT 5;
-- 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_baseUSING hnsw (embedding vector_cosine_ops);
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 |
Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback