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