Sub topic 5
Inserting and Updating Vectors
This chapter assumes that you know basic SQL and PostgreSQL, and that you already understand that an embedding is a numeric representation of text, an image, or another object. You will use the vector type from the pgvector extension.
Create a table with an embedding column
A vector column stores an ordered list of numbers. The number in parentheses declares its dimension, or length. A vector(3) column accepts exactly three values per row.
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE items (
id bigserial PRIMARY KEY,
content text NOT NULL,
embedding vector(3)
);The embedding column is nullable because the definition does not include NOT NULL. This can be useful when you insert content before generating its embedding. If every row must have an embedding, declare it as embedding vector(3) NOT NULL.
The dimension is part of the table's data contract. An embedding generated by a model with 768 dimensions cannot be inserted into vector(3). Choose the dimension that matches the model you use, and use the same model consistently for related rows.
Insert vectors
The simplest SQL representation is a quoted list inside square brackets:
INSERT INTO items (content, embedding) VALUES
('a', '[1,2,3]'),
('b', '[4,5,6]'),
('c', '[1,1,1]');PostgreSQL converts the text representation to the vector type because it knows the destination column's type. You can also cast a value explicitly:
INSERT INTO items (content, embedding)
VALUES ('another item', '[2,0,5]'::vector);The values keep their order. The first number is coordinate one, the second is coordinate two, and so on. Do not sort or round the coordinates yourself. The embedding model produces a particular vector, and changing its values can change search results.
Update an existing vector
Use a normal UPDATE statement when you regenerate an embedding or correct an import:
UPDATE items
SET embedding = '[2,2,1]'::vector
WHERE id = 1;Always include a precise WHERE clause. Without it, PostgreSQL updates every row:
UPDATE items
SET embedding = NULL
WHERE id = 1;Setting the value to NULL removes the embedding while preserving the row. This is different from storing a zero vector such as [0,0,0]. A zero vector is still a real vector and can have different behavior with similarity metrics.
The usual data flow is:
Check dimensions and missing values
You can inspect stored values with a query:
SELECT id, content, embedding
FROM items
WHERE embedding IS NOT NULL;To reject missing vectors, add a constraint after ensuring existing rows are complete:
ALTER TABLE items
ALTER COLUMN embedding SET NOT NULL;This command fails if any existing row still contains NULL. Fill or remove those rows before applying the constraint.
When inserting from application code, use a parameterized query rather than constructing SQL by string concatenation. The client library can adapt a numeric array to a vector value, which avoids quoting errors and SQL injection risks.
Exercise
What happens when you insert four coordinates into an embedding vector(3) column, and how should you handle a model that produces four-dimensional embeddings?
Show answer
PostgreSQL rejects the insert because the dimensions do not match. Use a column declared as vector(4), or choose a model that produces three-dimensional embeddings. Do not silently discard a coordinate.