Database / pgvector basics Interview Questions
How do you store and query vector embeddings with pgvector in a real schema design?
A well-designed schema stores embeddings close to the source data they represent, includes metadata for filtering, and separates concerns cleanly. Here are common production schema patterns.
-- Pattern 1: Embedding column on the same table as the content CREATE TABLE articles ( id BIGSERIAL PRIMARY KEY, title TEXT NOT NULL, body TEXT NOT NULL, author_id BIGINT REFERENCES users(id), category TEXT, published_at TIMESTAMPTZ, is_published BOOLEAN DEFAULT FALSE, embedding VECTOR(1536) -- text-embedding-3-small ); -- Pattern 2: Separate embeddings table (useful when embedding -- the same content with multiple models or at multiple granularities) CREATE TABLE article_chunks ( id BIGSERIAL PRIMARY KEY, article_id BIGINT REFERENCES articles(id) ON DELETE CASCADE, chunk_index INTEGER, -- which chunk within the article chunk_text TEXT, embedding VECTOR(1536), model_name TEXT DEFAULT 'text-embedding-3-small' ); -- Pattern 3: Multi-modal embeddings (different models on different columns) CREATE TABLE products ( id BIGSERIAL PRIMARY KEY, name TEXT, description TEXT, image_url TEXT, text_embedding VECTOR(1536), -- text-embedding-3-small on description image_embedding VECTOR(512) -- CLIP on product image ); -- Indexes on each: CREATE INDEX ON articles USING hnsw (embedding vector_cosine_ops); CREATE INDEX ON article_chunks USING hnsw (embedding vector_cosine_ops); CREATE INDEX ON products USING hnsw (text_embedding vector_cosine_ops); CREATE INDEX ON products USING hnsw (image_embedding vector_cosine_ops);
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...
