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);
More Related questions...