PostgreSQL as a Vector Database
{"all_nodes":"PHASE 1 fill complete; deterministic lint clean (2026-10-12)"}local graph
PostgreSQL as a Vector Database — Joachim Nygaard
Section titled “PostgreSQL as a Vector Database — Joachim Nygaard”chapter_01
Section titled “chapter_01”node-book_postgresql-as-a-vector-database_chapter-01-preface — Preface
chapter_02
Section titled “chapter_02”node-book_postgresql-as-a-vector-database_chapter-02-foundations-of-vector-search-in-postgresql — Foundations of Vector Search in PostgreSQL
chapter_03
Section titled “chapter_03”node-book_postgresql-as-a-vector-database_chapter-03-getting-started-with-pgvector — Getting Started with pgvector
chapter_04
Section titled “chapter_04”node-book_postgresql-as-a-vector-database_chapter-04-building-ai-driven-applications-with-pgvector — Building AI-Driven Applications with pgvector
chapter_05
Section titled “chapter_05”node-book_postgresql-as-a-vector-database_chapter-05-retrieval-augmented-generation-rag-on-postgresql — Retrieval-Augmented Generation (RAG) on PostgreSQL
chapter_06
Section titled “chapter_06”node-book_postgresql-as-a-vector-database_chapter-06-scaling-and-optimizing-vector-search — Scaling and Optimizing Vector Search
chapter_07
Section titled “chapter_07”node-book_postgresql-as-a-vector-database_chapter-07-multimodal-vector-search-in-postgresql — Multimodal Vector Search in PostgreSQL
chapter_08
Section titled “chapter_08”node-book_postgresql-as-a-vector-database_chapter-08-hybrid-retrieval-and-advanced-patterns — Hybrid Retrieval and Advanced Patterns
chapter_09
Section titled “chapter_09”node-book_postgresql-as-a-vector-database_chapter-09-productionizing-pgvector-applications — Productionizing pgvector Applications
Title Page
Section titled “Title Page”PostgreSQL as a Vector Database
Section titled “PostgreSQL as a Vector Database”Advanced pgvector for AI, RAG, and Multimodal LLMs
Section titled “Advanced pgvector for AI, RAG, and Multimodal LLMs”
[!visual-conversion] geometric elephant with network nodes and arrows Cropped from the scan (PDF page 001): geometric elephant with network nodes and arrows
Joachim Nygaard
# Copyright © 2025 by Joachim Nygaard
All rights reserved.
No part of this publication may be reproduced, distributed, or transmitted in any form or by any means, including photocopying, recording, or other electronic or mechanical methods, without the prior written permission of the publisher, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law.Table of Contents
Section titled “Table of Contents”Table of Contents
Section titled “Table of Contents”Preface
Section titled “Preface”17
1. Foundations of Vector Search in PostgreSQL
Section titled “1. Foundations of Vector Search in PostgreSQL”20
1.1 The Evolution of Search: From Keyword to Embeddings
Section titled “1.1 The Evolution of Search: From Keyword to Embeddings”20
1.2 PostgreSQL as a General-Purpose Database and its Strengths
Section titled “1.2 PostgreSQL as a General-Purpose Database and its Strengths”26
1.3 Why pgvector? Positioning Against Dedicated Vector Databases
Section titled “1.3 Why pgvector? Positioning Against Dedicated Vector Databases”31
1.4 Core Concepts: Embeddings, Distance Metrics, and Index Structures
Section titled “1.4 Core Concepts: Embeddings, Distance Metrics, and Index Structures”37
2. Getting Started with pgvector
Section titled “2. Getting Started with pgvector”46
2.1 Installing and Configuring pgvector Extension
Section titled “2.1 Installing and Configuring pgvector Extension”46
2.2 Vector Data Types and Storage in PostgreSQL
Section titled “2.2 Vector Data Types and Storage in PostgreSQL”55
2.3 Indexing Strategies: IVFFlat, HNSW, and Brute-Force
Section titled “2.3 Indexing Strategies: IVFFlat, HNSW, and Brute-Force”65
2.4 Benchmarking Basic Vector Queries
Section titled “2.4 Benchmarking Basic Vector Queries”74
3. Building AI-Driven Applications with pgvector
Section titled “3. Building AI-Driven Applications with pgvector”86
3.1 Ingesting and Storing Embeddings from OpenAI, HuggingFace, and Others
Section titled “3.1 Ingesting and Storing Embeddings from OpenAI, HuggingFace, and Others”86
3.2 Designing Efficient Schemas for Hybrid Data + Vector Search
Section titled “3.2 Designing Efficient Schemas for Hybrid Data + Vector Search”96
3.3 Combining SQL and Vector Queries for Rich Results
Section titled “3.3 Combining SQL and Vector Queries for Rich Results”106
3.4 Practical Walkthrough: Question-Answering with pgvector
Section titled “3.4 Practical Walkthrough: Question-Answering with pgvector”119
4. Retrieval-Augmented Generation (RAG) on PostgreSQL
Section titled “4. Retrieval-Augmented Generation (RAG) on PostgreSQL”130
4.1 RAG Architecture and Data Flow Explained
Section titled “4.1 RAG Architecture and Data Flow Explained”131
4.2 Document Preprocessing, Chunking, and Embedding Pipelines
Section titled “4.2 Document Preprocessing, Chunking, and Embedding Pipelines”139
4.3 Low-Latency Query Execution and Search Optimizations
Section titled “4.3 Low-Latency Query Execution and Search Optimizations”140
148
4.4 Implementing RAG with LangChain, LlamaIndex, and pgvector 156
- Scaling and Optimizing Vector Search 166
5.1 Index Tuning and Query Optimization Techniques 167
5.2 Parallelization, Caching, and Materialized Views 176
5.3 Sharding and Partitioning Strategies for Large Datasets 186
5.4 Monitoring, Observability, and Cost Control 195
- Multimodal Vector Search in PostgreSQL 204
6.1 Beyond Text: Image, Audio, and Video Embeddings 205
6.2 Designing Unified Schemas for Multimodal Data 217
6.3 Cross-Modal Retrieval: Text-to-Image and Image-to-Text Search 229
6.4 Real-World Use Case: Enterprise Knowledge Hub 240
- Hybrid Retrieval and Advanced Patterns 253
7.1 Combining BM25 and pgvector for Hybrid Search 254
7.2 Metadata Filtering, Ranking, and Post-Processing 265
7.3 Integrating Structured + Unstructured Data Queries 279
7.4 Advanced Scenarios: Agents, Tool-Use, and Workflow Orchestration 288
- Productionizing pgvector Applications 299
8.1 Deployment Models: On-Prem, Cloud, and Kubernetes Operators 300
8.2 Security, Authentication, and Access Control for AI Workloads 307
8.3 CI/CD Pipelines and Schema Migrations for Vector-Enhanced Apps 317
8.4 Future Trends: Graph-RAG, Multimodal Fusion, and Real-Time Retrieval 328
4