Embeddings and pgvector, Explained With a Real Example
Your users rarely type the words your data uses. Someone asks to “speed up our ecommerce site”, and the answer is filed under “Make my Shopify store load faster”. The two mean the same thing but share almost no words, so a keyword search finds nothing.
Search by meaning fixes that. It is built on embeddings, and it can live in the Postgres database you already run.
A real case: proposal tips from a freelancer’s own wins
A job-alert app I built for freelancers helps them write proposals. When a new job comes in, it finds the five past proposals closest to it in meaning, each marked won, replied or sent. An AI reads those five and writes a short list of tips, with a note on how sure the advice is.
With keyword search, the two store-speed jobs above look unrelated, and the AI has no history to learn from. With search by meaning, they match. So the tips come from the freelancer’s own history with jobs like this one, not from general advice anyone could get.
An embedding, in plain words
Imagine scoring every job on three things, from 0 to 1: how much it is about online stores, about speed, and about design.
| Job | Online store | Speed | Design |
|---|---|---|---|
| Make my Shopify store load faster | 0.9 | 0.9 | 0.1 |
| Speed up our ecommerce site | 0.8 | 0.9 | 0.2 |
| Design a logo for my bakery | 0.1 | 0.0 | 0.9 |
The first two rows look alike even though the words don’t. The third looks nothing like them. That is the whole idea: similar meaning gives similar numbers.
A real embedding works the same way, with two differences:
- It has far more numbers. The model I use, OpenAI’s
text-embedding-3-small, gives 1,536 for every piece of text. - Nobody chooses what each number means. The model learned them by reading a huge amount of text. You only ever compare whole lists.
Four choices that decide if it works
1. What you turn into numbers decides what “similar” means. For each past proposal I embed one block of text: the job title, the job description, the proposal that was sent, the notes saved with it, and what happened to it. So “similar” means “a similar job, answered in a similar way”. If I only embedded job titles, two very different jobs both called “Website help” would look like twins.
2. One model on both sides. The stored items and the new search must be turned into numbers by the same model. Numbers from two models are like two maps drawn at different scales: you can’t measure a distance across them. Models get replaced every year or so, so I store the numbers per model. A new model can be built next to the old one, compared on real proposals, and switched on with nothing going offline.
3. The same access rules as any other search. Each user only ever searches their own proposals. Search by meaning is not a back door around who can see what. More on that: Row-level security in Postgres: when to turn it on.
4. Keep it fresh. A stored embedding is a snapshot. When a proposal changes, for example a client replies or the job is won, its text is rebuilt and turned into numbers again. If that step fails, it is recorded and a nightly job retries it, so the search never quietly drifts away from the real data.
When to skip it
- Exact facts. “Jobs over $5,000” or “job ID 12345” are filters, not meaning. Normal search does those, next to the meaning search.
- Telling good from bad. Embeddings find what is about the same thing, not what is right. A similar proposal can be a losing one. That is why each match carries its outcome.
- Very short text. One or two words give the model little to go on, and the matches get noisy.
What to ask your developer
- What text gets turned into numbers for each item, and why that text?
- What happens when the embedding model is replaced?
- Can one customer’s search ever return another customer’s data?
- When a record changes, how is it updated, and what happens if that fails?
If your users search help articles, past tickets, products or documents, search by meaning finds what they mean, not just the words they typed. For the other AI features I build, see What I Can Build With AI.
You don’t need to know how any of this works inside. I choose what gets embedded, keep it in the database you already run, and make sure each user only searches their own data. The code is in the Technical tab.
The short version
Search by meaning turns text into numbers, so similar meaning gives similar numbers. Choose carefully what you embed, use one model on both sides, keep each user’s search to their own data, and update the numbers when records change. With pgvector, it lives in the Postgres database you already run.
What do your users search for that your app can’t find today?
Stack: Postgres with pgvector, OpenAI embeddings, a chat model for the writing.
Where the numbers come from
You send text to an embedding model and get the numbers back. One call per piece of text:
const response = await fetch('https://api.openai.com/v1/embeddings', {
method: 'POST',
headers: { Authorization: `Bearer ${apiKey}`, 'Content-Type': 'application/json' },
body: JSON.stringify({ model: 'text-embedding-3-small', input: text }),
});
const embedding: number[] = (await response.json()).data[0].embedding; // 1,536 numbers
Embedding models and chat models are different tools. The embedding model only turns text into numbers. A chat model, like ChatGPT or Claude, reads the matches afterwards and writes the advice. Claude doesn’t make embeddings at all, which is why my search uses OpenAI even where the writing can fall back to Claude.
How “close” is measured. Picture each list of numbers as an arrow. The usual measure is cosine distance: how far apart two arrows point, ignoring their length. 0 means they point the same way (very similar), 1 means nothing in common, 2 means opposite directions. It is often flipped into a similarity score, 1 - distance, so higher means more alike.
Storing them: pgvector
pgvector is a Postgres extension. It adds a vector column type and the operators to compare vectors. So the embeddings live in the same database as everything else, with the same backups, permissions and joins.
My table keeps one row per proposal per model:
CREATE TABLE proposal_learning_embeddings (
record_id uuid NOT NULL REFERENCES proposal_learning_records(id) ON DELETE CASCADE,
model text NOT NULL, -- which model made these numbers
dimensions integer NOT NULL, -- how many numbers that model gives
embedding vector(1536) NOT NULL,
PRIMARY KEY (record_id, model)
);
A new model’s numbers can be built in the background next to the old ones, compared on real proposals, and switched on by changing which model the search asks for.
Searching
pgvector’s <=> operator gives the cosine distance between two vectors. Sort by it, take the top five, and you have your closest matches:
SELECT record.content,
CASE WHEN proposal.closed THEN 'won'
WHEN proposal.replied_at IS NOT NULL THEN 'replied'
ELSE 'sent' END AS outcome,
proposal.revenue,
stored.embedding <=> $2::vector AS distance
FROM proposal_learning_records AS record
JOIN proposals AS proposal
ON proposal.id = record.proposal_id AND proposal.account_id = record.account_id
JOIN proposal_learning_embeddings AS stored
ON stored.record_id = record.id AND stored.model = $3
WHERE record.account_id = $1
ORDER BY stored.embedding <=> $2::vector
LIMIT 5;
Three things in there are easy to miss:
WHERE record.account_id = $1: a user only ever searches their own proposals.stored.model = $3: only numbers from the same model are compared.- The outcome and revenue come from the live proposal row, not from the embedded text. The text was a snapshot taken when it was embedded. If a proposal is marked won tomorrow, the search reports “won” right away.
Making it fast
Comparing a new job against every stored row works fine for thousands of rows. For millions, you want an index. pgvector offers HNSW:
CREATE INDEX ON proposal_learning_embeddings
USING hnsw (embedding vector_cosine_ops);
Think of HNSW as a map with shortcuts. Instead of checking every row, it hops between neighbours towards the closest ones. It is approximate: it trades a small chance of missing a close match for a big gain in speed.
One thing to know: with an approximate index, filters like account_id are applied after the index has picked its candidates. If one account owns a small slice of a big table, the search can come back with fewer than five rows. The pgvector docs cover the settings that fix this.
The full request
- The app writes up the new job the same way it wrote up past proposals.
- The same model turns it into numbers.
- The search finds the five closest past proposals, each marked won, replied or sent.
- A chat model reads those five, plus overall numbers like how many proposals got replies or won, and writes the advice.
This pattern is called RAG, retrieval-augmented generation: look things up first, then generate. Nothing is retrained. The model reads fresh context on every request.
When a proposal changes, its text block is rebuilt and re-embedded. A failed step is recorded, and a nightly job retries it, up to five times.
More Posts
All posts
November 25, 2026
Why Your App Went Offline on Vercel's Free Plan (and How to Stay Under the Limits)
Vercel's free plan pauses every site on the account when one monthly limit runs out. What the limits mean in plain words, what uses them up, and the fixes, from a real case.
November 24, 2026
Why Your Project Starts With a Small Paid Step: Requirements and an Estimate Before the Build
A price given before the work is measured is a guess, and someone pays for it later. What a small paid first step gets you: written decisions, a cost range and a time range, and the choice to stop there.