Stage 5 track
Track D: AI & Modern PostgreSQL
Goal: Use PostgreSQL where the new workloads live — vector search with pgvector, hybrid retrieval, data design for RAG, time-series and geospatial at a glance, and the hosted platforms — shipping semantic product search as your capstone. Time: 4–6 weeks alongside the capstone · Builds on: Parts 14 and 17 especially
Who this track is for
You're building AI-adjacent features — semantic search, RAG, recommendations — and want them on the database you already trust instead of a fleet of specialised stores. Part 14 was your favourite; this is its sequel.
The path
1. Embeddings, explained like data. An embedding model turns text (or images) into a vector of floats — hundreds to thousands of dimensions — where similar meaning lands nearby. "Similar" becomes a distance between vectors, and "find related things" becomes a nearest-neighbour query. That's the entire magic: meaning, made indexable.
2. pgvector basics. pgvector adds a vector(n) column type and the distance operators — <-> L2 (Euclidean), <=> cosine distance, <#> negative inner product (per its own documentation). Nearest neighbours is then plain SQL, composable with everything you know:
SELECT name FROM products
ORDER BY embedding <=> $1 -- the query's embedding, as a parameter
LIMIT 10;
(pgvector is an extension to install — CREATE EXTENSION vector — not core Postgres; most hosted platforms ship it.)
3. Vector indexes. Exact nearest-neighbour means scanning every vector; the approximate indexes trade a sliver of recall for orders of magnitude of speed. HNSW: graph-based, best query speed/recall, buildable on an empty table, costlier to build and store. IVFFlat: clusters vectors into lists — cheaper, but it picks its cluster centres from your data, so build it after loading, and re-evaluate after bulk changes. Part 17's discipline applies verbatim: measure recall and latency on your vectors before choosing.
4. Hybrid search. Pure vector search misses exact tokens ("model 7745", SKUs, names); pure keyword search misses meaning. Production search is both: Part 14's full-text query and a vector query, results fused (commonly reciprocal rank fusion — score by summed inverse ranks). You already own both halves; hybrid is a UNION and a formula.
5. Data design for RAG. Retrieval-augmented generation is a database problem wearing an AI costume: chunk documents (by structure, not blind length), store chunk text + embedding + metadata (source, section, date, tenant), filter by metadata in the same query as the vector ordering — Part 14's "columns for the backbone" checklist, again — and keep embeddings fresh when source text changes (Part 13's triggers, or a job queue from Track B marking rows stale). Multi-tenant RAG wants Track B's RLS on the chunks table.
6. Time-series data. Append-only measurements are Postgres-native territory: partition by month (Track C's partitioning) so old data drops instantly, BRIN indexes (Part 17 — finally on data that's actually append-ordered, where their kilobytes shine), and aggregates materialized (Part 13). TimescaleDB packages exactly these ideas as an extension when you want them automated.
7. Geospatial at a glance. PostGIS adds geometry types, spatial operators and GiST-indexed queries ("stores within 2 km") — the most battle-tested Postgres extension in existence. Know it exists and what shape of problem it owns; its depth is its own course.
8. Hosted PostgreSQL for AI apps. Supabase (Postgres + auth + APIs, pgvector included) and Neon (serverless Postgres — storage/compute split, branch-per-pull-request databases) change provisioning, not principles. Every lesson in this course applies unchanged on both; evaluate them on Track C's questions (backups, PITR, extensions, limits).
Capstone: semantic product search
Over shopdb's catalogue (scale product names/descriptions up with the Tune It generator):
- An
embedding vector(n)column on products, filled via any embedding model — a local sentence-transformers model is fine (and free); record which model and dimension. - An HNSW index; measured latency and spot-checked recall vs the exact scan.
- Hybrid search: full-text + vector with rank fusion, behind one SQL function (Part 13).
- Metadata filters that compose: category and price bounds in the same query as the semantic ordering.
- A freshness trigger: editing a product's text marks its embedding stale for a re-embed job.
Done when: "warm drink maker" finds the coffee maker without the words warm or drink appearing anywhere in its name, "modle 7745" (typo and all) still finds the product — and both answers return in milliseconds with the filters applied.