Database / pgvector basics Interview Questions
How does pgvector fit into a RAG (Retrieval-Augmented Generation) pipeline?
RAG (Retrieval-Augmented Generation) is a technique that improves LLM responses by retrieving relevant documents from a knowledge base and including them as context in the prompt. pgvector serves as the vector store component, storing document embeddings and enabling semantic retrieval.
| Stage | What happens | pgvector role |
|---|---|---|
| 1. Ingest | Split documents into chunks; embed each chunk | Store chunks + embeddings in vector table |
| 2. Retrieve | Embed user query; find similar chunks | KNN query returns top-k relevant chunks |
| 3. Generate | Inject retrieved chunks into LLM prompt | No role - LLM (OpenAI, Gemini, etc.) generates answer |
import psycopg2 from pgvector.psycopg2 import register_vector from openai import OpenAI conn = psycopg2.connect("postgresql://user:pass@localhost/mydb") register_vector(conn) cur = conn.cursor() oai = OpenAI() # STAGE 1: INGEST - embed and store documents documents = [ "pgvector is a PostgreSQL extension for vector search.", "HNSW indexes provide fast approximate nearest neighbour search.", "Cosine distance is commonly used for text embeddings.", ] for text in documents: emb = oai.embeddings.create( model="text-embedding-3-small", input=text ).data[0].embedding cur.execute( "INSERT INTO documents (content, embedding) VALUES (%s, %s)", (text, emb) ) conn.commit() # STAGE 2: RETRIEVE - semantic search for user query user_question = "What kind of index should I use for fast search?" q_emb = oai.embeddings.create( model="text-embedding-3-small", input=user_question ).data[0].embedding cur.execute( "SELECT content FROM documents ORDER BY embedding <=> %s LIMIT 3", (q_emb,) ) context_chunks = [r[0] for r in cur.fetchall()] # STAGE 3: GENERATE - pass context to LLM context = "\n".join(context_chunks) completion = oai.chat.completions.create( model="gpt-4o-mini", messages=[ {"role": "system", "content": f"Answer using this context:\n{context}"}, {"role": "user", "content": user_question} ] ) print(completion.choices[0].message.content)
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
