Database / pgvector basics Interview Questions
What data types does pgvector provide and how do you define vector columns?
pgvector introduces several new data types to PostgreSQL for storing vector data. The primary type is vector, with additional types for half-precision and binary vectors added in later releases.
| Type | Storage | Precision | Max dimensions | Use case |
|---|---|---|---|---|
| vector(n) | 4 bytes per dimension | 32-bit float (single precision) | 16,000 | Standard embeddings (OpenAI, Cohere, etc.) |
| halfvec(n) | 2 bytes per dimension | 16-bit float (half precision) | 16,000 | Reduced storage; slight precision trade-off |
| bit(n) | ~1 bit per dimension | Binary (0 or 1) | 64,000 | Binary quantised embeddings; very compact |
| sparsevec(n) | Only non-zero values stored | 32-bit float | 1,000,000 | Sparse vectors (most dimensions are 0) |
-- Basic vector column (fixed dimensions) CREATE TABLE documents ( id BIGSERIAL PRIMARY KEY, content TEXT, embedding VECTOR(1536) -- 1536-dim for text-embedding-3-small ); -- Half-precision to save storage (2x space reduction) CREATE TABLE documents_small ( id BIGSERIAL PRIMARY KEY, embedding HALFVEC(1536) ); -- Binary vectors (very compact; special distance operators) CREATE TABLE binary_items ( id BIGSERIAL PRIMARY KEY, embedding BIT(1536) ); -- Variable dimensions (no dimension constraint) CREATE TABLE flexible ( id BIGSERIAL PRIMARY KEY, embedding VECTOR -- accepts any dimension ); -- NOTE: indexes on variable-dim columns require expression/partial indexes
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...
