Sub topic 4
The vector type and fixed dimensions
Prerequisites and scope
This chapter assumes you can write basic SQL (CREATE TABLE, INSERT, SELECT), use psql, and know that an embedding is an array of numbers produced by a model. It also assumes the vector extension is already enabled in your database, which the previous chapter covered. Here you will learn what the vector column type stores, why its dimension is fixed, and which errors you are likely to meet.
What the vector type is
The vector type stores one embedding as an ordered list of single-precision floating-point numbers (4 bytes each, called float4 or real in PostgreSQL). You declare the number of elements, called the dimension, in parentheses:
CREATE TABLE items (
id bigserial PRIMARY KEY,
content text NOT NULL,
embedding vector(3)
);vector(3) means every value in this column must have exactly three elements. Real embedding models produce many more. For example, a model might output 384, 768, 1536, or 3072 numbers per input. The dimension in your column must match the model you use.
The text form of a vector is a bracketed, comma-separated list, such as '[1,2,3]'. PostgreSQL reads that string and converts it to the vector type when you insert it or compare it with a vector column.
INSERT INTO items (content, embedding) VALUES
('a', '[1,2,3]'),
('b', '[4,5,6]'),
('c', '[1,1,1]');You can inspect the stored values with an ordinary SELECT:
SELECT id, content, embedding FROM items;The output shows each vector in the same bracketed form.
Why the dimension is fixed
The distance operators compare two vectors element by element. L2 distance, for instance, subtracts matching positions, squares the differences, sums them, and takes a square root. That calculation has no meaning if one vector has three positions and the other has five. Position 1 of one embedding must mean the same thing as position 1 of the other, and that is only true when both came from the same model.
A fixed dimension has three practical effects:
- PostgreSQL can reject a malformed value at insert time, before it pollutes the table.
- Indexes can assume every entry has the same size and layout.
- Storage per row is predictable.
The dimension is part of the column definition, much like varchar(50) or numeric(10,2). It is a constraint on what the column accepts. It is not a hint.
Storage size
Each vector value uses 4 bytes per element plus 8 bytes of overhead. A 1536-dimension vector therefore takes about 4 × 1536 + 8 = 6152 bytes. This matters for two reasons.
First, one PostgreSQL page is 8 KB, and a row that large is close to the point where PostgreSQL moves large values out of the main table into TOAST storage. Keep that in mind when you estimate table size, since a million 1536-dimension vectors need roughly 6 GB before indexes.
Second, the vector type has a dimension limit. A column of type vector can hold up to 16,000 dimensions, but an index on a vector column supports at most 2,000 dimensions. If your embeddings are larger than 2,000, you can store them but not index them with a plain vector index. The final chapters cover halfvec and other representations that raise or sidestep this limit.
| Property | Value for `vector` |
|---|---|
| Element type | Single-precision float (4 bytes) |
| Storage per value | 4 × dimensions + 8 bytes |
| Maximum dimensions in a column | 16,000 |
| Maximum dimensions in an index | 2,000 |
Dimension errors you will see
The most common error is inserting a vector of the wrong length. With the table above:
INSERT INTO items (content, embedding) VALUES ('d', '[1,2]');PostgreSQL rejects the row with an error such as expected 3 dimensions, not 2. The error is useful. It usually means the application is calling a different embedding model than the one the schema was designed for.
A second error appears when you compare vectors of different lengths:
SELECT embedding <-> '[1,2]' FROM items;This fails with a message such as different vector dimensions 3 and 2. Comparison is checked at query time, so a query vector from the wrong model fails loudly instead of returning nonsense.
A third case is more subtle. If you declare the column as plain vector with no dimension, PostgreSQL accepts values of any length in the same column:
CREATE TABLE loose_items (
id bigserial PRIMARY KEY,
embedding vector
);This is allowed, and each row can have a different length. It looks flexible, but it has costs:
- Comparing a query vector with a row of another length raises an error partway through a scan.
- You cannot build a normal vector index on a column with no declared dimension, because the index needs a fixed size.
Prefer vector(N) for any column you plan to search.
Choosing and changing the dimension
Pick the dimension from your embedding model's documentation, then confirm it by measuring one real output. In Python, len(embedding) gives the count. Do not guess from the model name.
Some models let you request a shorter output. A shorter vector uses less storage and searches faster, but it carries less information. If you use that option, treat the chosen length as part of the schema and use it for every row and every query.
Changing the dimension later is expensive. Switching models means every stored vector has to be recomputed, because vectors from different models are not comparable even when their lengths match. Two models that both output 768 numbers still place their outputs in unrelated spaces.
You can alter the column type if you have already re-embedded the data into a new column or table:
ALTER TABLE items ADD COLUMN embedding_v2 vector(768);The safer migration is to add a new column, fill it with the new model's output, build the index on it, switch queries over, and then drop the old column. This keeps the old column usable while you backfill.
Other value rules
A vector value must contain finite numbers. PostgreSQL rejects NaN and infinity inside a vector, and it rejects a vector with zero elements. Values are stored as float4, so a number like 0.1234567891 loses digits beyond roughly seven significant figures. This is normal for embeddings, whose precision is rarely meaningful past that point.
NULL is allowed in a vector column unless you add NOT NULL. A row with a NULL embedding is skipped by distance ordering in indexed queries, so it never appears as a neighbor. If every row should be searchable, declare the column NOT NULL:
CREATE TABLE docs (
id bigserial PRIMARY KEY,
body text NOT NULL,
embedding vector(3) NOT NULL
);You can also cast explicitly, for example when a value arrives as an array:
SELECT '[1,2,3]'::vector(3);
SELECT ARRAY[1,2,3]::real[]::vector(3);The cast to vector(3) applies the same dimension check, so a wrong-length value fails there too.
Checking a column's dimension
To see the declared dimension of a column, ask the catalog:
SELECT format_type(atttypid, atttypmod) AS declared_type
FROM pg_attribute
WHERE attrelid = 'items'::regclass
AND attname = 'embedding';This returns vector(3). To find the dimension of a stored value, use the vector_dims function:
SELECT id, vector_dims(embedding) AS dims FROM items;This is a quick way to audit a table you did not create, especially a column declared without a dimension.
Exercises
Exercise
A table has a column embedding vector(768). Your application starts sending 1536-element query vectors after a model change. What happens when you run ORDER BY embedding <-> $1, and what is the correct fix?
Show answer
The query fails with a "different vector dimensions 768 and 1536" error, because the operator requires equal lengths. The fix is to re-embed the stored data with the new model into a column declared vector(1536), not to cast or truncate the query. Vectors from different models are not comparable even when their lengths match.
Exercise
You need to index embeddings that are 3072 dimensions long. Can you create an HNSW index on a vector(3072) column? Why or why not?
Show answer
No. A vector column can hold up to 16,000 dimensions, but an index on a vector column supports at most 2,000. You could store the data but not index it as vector. Options include a smaller output dimension from the model or a more compact type such as halfvec, which is covered later.