Sub topic 1
What embeddings are and why similarity search needs a database
Prerequisites
You should know basic SQL, including SELECT, WHERE, ORDER BY, and LIMIT. You should also understand that PostgreSQL stores structured data in tables. A general idea of text or image embeddings is helpful, but this chapter explains the working model.
Embeddings turn meaning into numbers
An embedding is a list of numbers that represents an item, such as a sentence, document, image, or product. An embedding model creates the list from the item's content.
Items with similar meaning tend to produce vectors that are close together in a mathematical space. A vector is an ordered list of numbers. For example, a small demonstration model might represent three texts like this:
| Text | Example vector |
|---|---|
| "How do I reset my password?" | `[0.81, 0.12, 0.44]` |
| "I forgot my account password." | `[0.79, 0.15, 0.41]` |
| "How much does shipping cost?" | `[0.10, 0.92, 0.23]` |
These values are illustrative. Real embedding models often produce hundreds or thousands of dimensions. Each dimension does not usually correspond to a human-readable feature. The whole vector carries the representation.
Similarity search compares a query vector with stored vectors and returns the closest items. A user can search for “password recovery” and find a document containing “resetting account credentials,” even when the exact words differ.
Why put vectors in PostgreSQL?
A separate vector service can search embeddings, but your application still needs related data. A document has an owner, permissions, source URL, creation date, and other metadata. PostgreSQL can keep the embedding and that metadata in one row.
This gives a query a single transaction and a familiar filtering model:
- Search by semantic similarity.
- Restrict results to one tenant or category.
- Check permissions with ordinary SQL.
- Return document content and metadata together.
- Update or delete the document and its embedding consistently.
Without database support, an application may need to search vectors in one system and fetch matching records from another. That creates synchronization work and can make authorization errors easier to introduce.
The pgvector PostgreSQL extension adds a vector data type and operators for comparing vectors. It also provides indexes for larger collections. The basic path is:
flowchart TD
A[User query] --> B[Embedding model]
B --> C[Query vector]
C --> D[PostgreSQL with pgvector]
D --> E[Compare with stored vectors]
E --> F[Apply metadata and permission filters]
F --> G[Return nearest matching rows]Similarity is a distance decision
A search system needs a rule for deciding whether two vectors are close. Common choices include Euclidean, or L2, distance; cosine distance; and inner product.
The correct choice depends on the embedding model and how it was trained. Cosine distance focuses on the angle between vectors. It is often useful when vector length should not affect the result. L2 distance measures geometric separation. Inner product can rank vectors by both direction and magnitude.
Do not select a metric only because its name is familiar. Use the model documentation, then test results on representative queries. The metric affects ranking, indexes, and the meaning of your scores.
Database search is more than nearest neighbors
A production query rarely asks only for the closest vectors. It may require documents from one customer, only published records, or only rows the current user may read. PostgreSQL can combine vector ordering with WHERE conditions and joins.
For a small table, an exact search can compare the query with every stored vector and sort the results. As the table grows, this becomes expensive. pgvector's approximate indexes reduce the work by examining a subset of possible neighbors, trading some recall for speed. Recall is the fraction of the true nearest neighbors that the search returns.
The later chapters build the SQL step by step, then compare exact results with HNSW and IVFFlat index results.
Exercise
Why might an application store an embedding and its document permissions in the same PostgreSQL row?
Show answer
PostgreSQL can apply semantic search and permission filters in one query, keeping the returned content consistent with the authorized metadata.