Pipelines that feed a RAG index
For engineers
SQL, modeling, pipelines and quality checks that keep embeddings and queries trustworthy. Completing it counts as evidence for every listed skill.
The one-sentence idea: a RAG answer is only as good as the rows that reach the index, so the pipeline that selects, shapes, checks and embeds your data deserves the same care as the prompt, and every stage needs a test you can run without reading the answer.
The case
Dana runs data for Northwind, a SaaS company with a help centre. The support bot (any of Claude, ChatGPT or Gemini behind retrieval) must answer from the help articles of the customer's own plan and tenant. Articles live in Postgres. A nightly job copies changed articles into chunks, embeds them and writes the vectors to a pgvector table. A week after launch, three things go wrong: the bot quotes a price that was changed in March 2026, it cites a draft article, and one customer sees text from another tenant's private article.
None of these is a model problem. Each one entered the index through the pipeline.
Why it matters
A retrieval step cannot know that a row is stale, a draft or someone else's. It returns the nearest vectors. The model then writes fluent text around whatever it received. Fixing the prompt hides the symptom for one question and leaves the cause in the index.
How the work splits
- SQL decides what goes in. The extract query joined articles to every version of each article, so old text was indexed next to new text. Fix the join to the current version, filter on status and deleted rows, and test the query on a handful of articles whose answers you know.
- The model of the data decides what can go wrong. The chunks table had no tenant column, no embedding model name and no uniqueness rule. Add them, so a bad row is rejected and a model switch can happen side by side.
- The pipeline decides how reliably it runs. A retried task inserted its chunks twice. Make each task idempotent: re-running it for the same window gives the same table. Embed only chunks whose content hash changed.
- Quality checks decide when to stop. Compare row counts between stages, test for empty bodies and set a freshness limit. A failed check blocks the swap to the new index, not just the log line. Name an owner who gets paged.
- Embeddings decide what "near" means. Choose the model on your own labelled questions, not a leaderboard rank. Use the query mode of the model for questions and the passage mode for documents. Never mix vectors from two models in one index.
- Query handling decides what is searched for. Rewrite follow-ups into standalone questions, split comparisons into sub-queries, route counts to SQL and take the tenant filter from the signed-in session, never from the question text.
Before and after
Before, the extract was:
SELECT a.id, a.tenant_id, v.body
FROM articles a
JOIN article_versions v ON v.article_id = a.id
WHERE a.updated_at > :last_run;
After, it names the current version, the status and the delete flag:
SELECT a.id, a.tenant_id, v.body
FROM articles a
JOIN article_versions v ON v.id = a.current_version_id
WHERE a.updated_at > :last_run
AND a.status = 'published'
AND a.deleted_at IS NULL;
Five old versions of an article became one row, and drafts and deleted articles stopped reaching the index.
What goes wrong
- A fix in the wrong layer. Adding "ignore outdated articles" to the prompt does nothing when the old text and the new text are equally close to the question.
- Checks that run but do not block. A test that only writes a warning lets a half-empty index go live.
- An assistant's confident plan. Copilot, Cursor or ChatGPT can write the DAG, the migration or the filter extraction quickly, and each can contain a flaw that looks ordinary: a
now()where the run date belongs, an in-place type change on a live table, a tenant id read from the user's text. Review each against a known case before it ships.
The work in this scenario is to find the stage that caused a symptom, fix it there and add the check that would have caught it.
Steps
Fix the extract query
The bot quoted a price that changed in March 2026 because the extract joined every version of an article. Join the current version only, filter on status and deleted rows, and test the query on three articles whose latest text you know.
Extract query before the fix pipelines/extract_articles.sql
SELECT a.id, a.tenant_id, v.bodyFROM articles aJOIN article_versions v ON v.article_id = a.idWHERE a.updated_at > :last_run;Give the chunks table keys and a model column
A chunk row needs a tenant id, the embedding model that produced it and a uniqueness rule. Then a bad row is rejected by the database and a new model can be written next to the old one.
Chunks table after the change db/chunks.sql
CREATE TABLE chunks ( id uuid PRIMARY KEY, tenant_id uuid NOT NULL REFERENCES tenants(id), article_id bigint NOT NULL REFERENCES articles(id), chunk_no int NOT NULL, content_hash text NOT NULL, embedding_model text NOT NULL, embedding vector(1024) NOT NULL, UNIQUE (article_id, chunk_no, embedding_model));Make the nightly job safe to re-run
Key every write on article, chunk number and model, and use an upsert. Compute the extract window from the run's logical date, not from the clock. Embed only chunks whose content hash is new.
Block the index swap on a failed check
Reconcile counts between stages and fail the run before the new index replaces the live one. Give the dataset a named owner and write the check results where that owner will see them.
Stage reconciliation, one night Counts for the nightly run Stage Published articles Rows Source (Postgres) 41200 41200 Staged extract 41200 41200 Chunks written 38900 412000 Choose and test the embedding model on your questions
Collect 60 real support questions with the article that answers each. Score every candidate model by recall at 5 and MRR (mean reciprocal rank, the average of 1 divided by the rank of the first right result), and keep the old index until the new one wins.
Shape the question before it is searched
Rewrite follow-ups into standalone questions, extract product and version filters from the text, and add the tenant filter from the session. Route a question such as how many tickets are open to SQL, not to the document index.
Filters sent to the search search/filters.json
{ "tenant_id": "from-session", "product": "api", "version": "v3"}
Skills this scenario uses
Where to study
- PostgreSQL: Data definition (opens in a new tab)DocsFree
Keys, constraints and generated columns that make bad chunk rows impossible.
- Data tests (dbt) (opens in a new tab)DocsFree
Not-null, unique and accepted-values tests to run inside the pipeline.
- dbt model contracts (opens in a new tab)DocsFree
How a producer's schema promise is enforced before a build succeeds.
- Apache Airflow documentation (opens in a new tab)DocsFree
DAGs, retries, backfills and the logical date a task should use.
- OpenLineage (opens in a new tab)StandardFree
An open standard for tracing a wrong value back through each job that touched it.
- pgvector: open-source vector similarity search for Postgres (opens in a new tab)ToolFree
Vector columns, distance operators and indexes in Postgres.
- MTEB: Massive Text Embedding Benchmark (Hugging Face) (opens in a new tab)ToolFree
The public leaderboard to shortlist models before testing them on your own questions.
- Matryoshka Representation Learning (opens in a new tab)PaperFree
The idea behind truncating embedding dimensions with a measured loss in quality.