A pgvector distance query fails with 'operator does not exist'
ERROR: operator does not exist: <type> <-> <type>Short answer
The operator error means Postgres found no distance operator for the two types on either side. Either the two sides are different vector types (vector against halfvec, or a plain numeric array), or the extension's operators live in a schema that is not on the search_path. Cast one side to match, or qualify the operator.
Fix it fast
1Find out which cause you have
Ask Postgres for the type of the column and the schema the extension lives in. The error names both operand types; this shows which side is not a vector.
SELECT a.attname, format_type(a.atttypid, a.atttypmod) FROM pg_attribute a WHERE a.attrelid = 'items'::regclass AND a.attname = 'embedding'; SELECT e.extname, n.nspname FROM pg_extension e JOIN pg_namespace n ON n.oid = e.extnamespace WHERE e.extname = 'vector';2Match what you see, then jump to its fix
- 1
The error lists two different types, such as vector on one side and halfvec (or a numeric array) on the other.The two sides are different vector typesFix - 2
It fails only from application code that binds the embedding as a parameter.The query value is passed as a typed array, not as a vectorFix - 3
Both sides are vector, the first-step query shows the extension in a schema such as extensions, and that schema is not in search_path.The extension's operators are in a schema the session cannot seeFix
- 1
What does the error look like?
ERROR: operator does not exist: <type> <-> <type>SELECT ... ORDER BY embedding <=> $1 fails when the parameter is an array
Cause 1 of 3
The two sides are different vector types
1Check
pgvector has separate types: vector, halfvec, sparsevec and bit, each with its own operator classes. A column of one type compared to a literal or cast of another type finds no match.
SELECT pg_typeof(embedding) FROM items LIMIT 1;2Fix
Make both sides the same type. The README casts the column to halfvec in a half-precision query, and casts a double precision array with ::vector.
-- column stored as vector, query as half-precision
SELECT * FROM items ORDER BY embedding::halfvec(3) <-> '[1,2,3]' LIMIT 5;3Verify
The query returns rows ordered by distance.
SELECT id FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;Confirmed by raw.githubusercontent.com
Cause 2 of 3
The query value is passed as a typed array, not as a vector
1Check
The README writes query vectors as text like '[3,1,2]'. Postgres gives an untyped literal the type of the other operand, which finds the vector operator. A parameter that your driver already typed as an array (for example float8[]) is no longer untyped, so it does not.
SELECT pg_typeof('{0.1,0.2,0.3}'::float8[]) AS array_type, pg_typeof('[0.1,0.2,0.3]'::vector) AS vector_type;2Fix
Cast the parameter to vector in SQL, or send it as the text form '[0.1,0.2,0.3]'.
SELECT id FROM items ORDER BY embedding <-> $1::vector LIMIT 5;3Verify
Run the query from the application with a real embedding.
SELECT id FROM items ORDER BY embedding <-> '[0.1,0.2,0.3]'::vector LIMIT 5;Confirmed by postgresql.org
Cause 3 of 3
The extension's operators are in a schema the session cannot see
1Check
Operator names are found through the search path just like table names. If the extension went into another schema, an unqualified <-> is not found.
SHOW search_path;2Fix
Add the extension's schema to the search_path, or write the operator in its qualified form.
SET search_path TO public, extensions;
-- or, for one expression
SELECT id FROM items
ORDER BY embedding OPERATOR(extensions.<->) '[3,1,2]' LIMIT 5;3Verify
The unqualified query now runs.
SELECT id FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;Confirmed by postgresql.org
How do I stop it coming back?
- Store and query embeddings with the same type (vector everywhere, or halfvec everywhere).
- Cast bound parameters to vector in the SQL text, so the driver's array type does not matter.
- Check SHOW search_path in the application's own connection, not only in your admin session.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Databases
Postgres cannot find the vector type for pgvector
ERROR: type "vector" does not exist
Installing pgvector on the server is not enough.
3 causes, each with a fixDatabases
DBeaver cannot run pg_restore: the utility is not found in the client home
Utility 'pg_restore' not found in client home '/usr/lib' (/usr/lib)
DBeaver runs pg_restore from a local PostgreSQL client folder; it does not bundle one.
3 causes, each with a fixDatabases
DBeaver cannot download the PostgreSQL driver from Maven
The PostgreSQL driver files are missing and the download does not complete
DBeaver downloads the PostgreSQL JDBC driver from a Maven repository the first time you connect.
3 causes, each with a fixDatabases
python manage.py migrate finishes but the tables for my app are missing
relation "myapp_book" does not exist
migrate only applies migration files that exist.
3 causes, each with a fixDatabases
Django save fails on PostgreSQL: null value in a column violates a not-null constraint
django.db.utils.IntegrityError: null value in column "price" of relation "shop_product" violates not-null constraint
A model field is NOT NULL in the database unless it has null=True.
3 causes, each with a fixDatabases
Django migration on PostgreSQL stops with 'pending trigger events'
django.db.utils.OperationalError: cannot ALTER TABLE "mytable" because it has pending trigger events
PostgreSQL runs a Django migration in one transaction.
2 causes, each with a fix