Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

A pgvector distance query fails with 'operator does not exist'

ErrorERROR: 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.

  • Applies to PostgreSQL 16, PostgreSQL 17, pgvector 0.8
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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';
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2It 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. 3Both 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

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

Did this fix it?

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

Did this fix it?

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

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.