Database / pgvector basics Interview Questions
What is vector quantisation and how does pgvector support binary quantisation?
Vector quantisation compresses full-precision vectors into more compact representations, trading some precision for dramatically reduced storage and faster distance computations. pgvector supports binary quantisation via the bit type.
-- Binary quantisation: convert float vectors to binary (0/1 per dimension) -- Each dimension becomes 1 bit instead of 32 bits = 32x compression! -- Store binary quantised vectors: CREATE TABLE items_binary ( id BIGSERIAL PRIMARY KEY, content TEXT, embedding VECTOR(1536), -- keep original for reranking embedding_bin BIT(1536) -- binary quantised for fast coarse search ); -- Quantise: positive values -> 1, negative/zero -> 0 UPDATE items_binary SET embedding_bin = ( SELECT string_agg( CASE WHEN val > 0 THEN '1' ELSE '0' END, '' ORDER BY ordinality )::bit(1536) FROM unnest(embedding::float4[]) WITH ORDINALITY AS t(val, ordinality) ); -- Two-stage retrieval with binary quantisation: -- Stage 1: Fast coarse search on binary vectors (Hamming distance) CREATE INDEX ON items_binary USING hnsw (embedding_bin bit_hamming_ops); WITH candidates AS ( SELECT id, embedding_bin <~> '[...]'::bit(1536) AS hamming_dist FROM items_binary ORDER BY hamming_dist LIMIT 100 -- get 100 candidates quickly from binary index ) -- Stage 2: Rerank top candidates using full-precision cosine distance SELECT i.id, i.content, i.embedding <=> '[...]' AS cosine_dist FROM items_binary i JOIN candidates c ON i.id = c.id ORDER BY cosine_dist LIMIT 5; -- return final top-5 after precise reranking
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...
