đź’ˇ Found of the day: "INSERT ... ON CONFLICT" in Postgres
Part of my project’s RAG pipeline is written in raw SQL. I wanted to stop branching on “does that vector already exist?” and found a much cleaner way: one atomic upsert that inserts new rows and updates existing ones.
INSERT INTO bl_docs_embeddings (id, embedding, doc_chunk, context)
VALUES ($1, $2, $3, $4)
ON CONFLICT (id) DO UPDATE SET
embedding = EXCLUDED.embedding,
doc_chunk = EXCLUDED.doc_chunk,
context = EXCLUDED.context;
EXCLUDED is the row you tried to insert. No IF, no SELECT first, and no race when two workers handle the same doc at once.
Try it out and share your own finding of the day 👇
commit dee1e67