IVFFlat is an approximate vector index for PostgreSQL with pgvector. It clusters similar embeddings during index creation, then searches only clusters near the query instead of comparing every stored vector. section 5.4.3
This design trades some recall for substantially faster similarity queries. A relevant vector can be missed when its cluster is not searched, although the practical accuracy loss is described as small. The lists parameter controls the number of clusters: more lists can accelerate searches while increasing the chance of omission. section 5.4.3
Use IVFFlat with these operational constraints:
- Create the index after ingesting a representative first batch of documents; an empty table cannot provide useful cluster structure. section 5.4.3
- Start with lists = 100 for collections up to a few million vectors, then evaluate the resulting speed and recall. section 5.4.3
- Store embeddings alongside chunk text and metadata, and expose ranked results with scores, document IDs, and metadata through a vector-store interface. section 5.7
PostgreSQL index definition
CREATE INDEX idx_chunks_embedding ON chunks USING ivfflat (embedding vector_cosine_ops) WITH (lists = 100); section 5.4.3
Listing 5.18 PostgreSQL schema for vector storage with pgvector
CREATE EXTENSION IF NOT EXISTS vector; #A
CREATE TABLE chunks (
chunk_id VARCHAR(255) PRIMARY KEY,
document_id VARCHAR(255) NOT NULL, #B
index_name VARCHAR(255) NOT NULL, #B
chunk_text TEXT NOT NULL,
embedding vector(1536), #C
metadata JSONB DEFAULT '{}', #D
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_chunks_document ON chunks(document_id);
CREATE INDEX idx_chunks_index ON chunks(index_name);
CREATE INDEX idx_chunks_metadata
➥ON chunks USING gin (metadata); #E
CREATE INDEX idx_chunks_embedding ON chunks
USING ivfflat (embedding vector_cosine_ops)
WITH (lists = 100); #FInspect the complete pgvector schema, including the embedding column, metadata fields, supporting indexes, and IVFFlat definition. section 5.4.3

