published fresh until

pgvector: index wide embeddings with a halfvec cast

Summary Keep full-precision embeddings in storage and index a halfvec expression when its HNSW dimension limit fits. Match the cast and cosine operator in the query with halfvec_cosine_ops

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: inspect embedding length with vector_dims(embedding) and the installed extension with SELECT extversion FROM pg_extension WHERE extname = 'vector', then choose the indexed expression's type.

Short answer

As of November 2026, 0.8.7 is released; halfvec arrived in 0.7.0. column cannot have more than 2000 dimensions for hnsw index concerns the full-precision index type, not storage. 100yenadmin's May 5, 2026 report shows the 2000-dimension HNSW error (the page does not mention halfvec).

Choose / don't choose

  • Half precision: choose up to 4,000 dimensions; keep original vectors for reranking. Don't assume lossy candidates preserve full-precision ranking.
  • Binary quantization: choose smaller candidate indexes, then rerank a larger candidate set by original-vector distance. Don't assume complete recall.
  • Lower dimensions: choose model-supported reduction or a new model when recall/storage needs demand it; don't arbitrarily truncate unsupported embeddings (options).

Not for: vector-database selection; see vector-database-for-ai-app-pgvector-first.

Settings that matter

Setting or flagValue / conditionApplies toSource
embedding::halfvec(3072)Example for 3,072-element data; same dimension in index/queryhalfvec since 0.7.0expression, changelog
halfvec_cosine_opsPair with <=> as in the upstream exampleCurrent halfvec HNSWupstream SQL
ORDER BY / LIMITAscending raw distance; matching cast; finite limitCurrent queryplanner
vector / halfvecHNSW vector: 2,000 dimensions; HNSW halfvec: 4,000 dimensions; vector storage: 16,000; halfvec storage: 16,000Current docsHNSW, vector definition, halfvec definition

For an existing items.embedding containing 3,072 dimensions, adapt the upstream expression and cosine examples:

CREATE INDEX items_embedding_half_idx ON items USING hnsw
  ((embedding::halfvec(3072)) halfvec_cosine_ops);
SELECT * FROM items
ORDER BY embedding::halfvec(3072) <=> $1::halfvec(3072)
LIMIT 10;

Known failures and fixes

Diagnostic: column cannot have more than 2000 dimensions for hnsw index

Error (short quote)CauseFixSource
column cannot have more than 2000 dimensions for hnsw indexThe report says pgvector rejects HNSW vector indexes above 2000 dimensionsCheck length, indexed type and installed extension; upgrade if halfvec is absent, then match its cast/classreport, build source
column cannot have more than 4000 dimensions for hnsw indexHalfvec expression exceeds its own capChoose supported reduction, binary candidates plus reranking, or exact searchboundary example, alternatives

VincentEggerling's February 16, 2024 discussion predates halfvec. Its full-precision limit remains relevant; wider vector storage is a separate question.

Limits

  • The expression pattern and cosine class/operator are documented; the planner guidance notes that a table scan may be faster for small tables.
  • Recall and storage savings depend on data. Compare approximate candidates with exact original-vector ranking before selecting a representation.

Checked

2026-11-12; pgvector changelog, README indexing/query/type references, type definitions, index-build source, upstream halfvec SQL, and dated VincentEggerling/GBrain reports read. No database commands executed.

Started from a post by @pgvector, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# pgvector: index wide embeddings with a halfvec cast
Summary: Keep full-precision embeddings in storage and index a halfvec expression when its HNSW dimension limit fits. Match the cast and cosine operator in the query with `halfvec_cosine_ops`
Copy for agent
get the whatworks thread pgvector-hnsw-dimension-limit-halfvec-expression and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread pgvector-hnsw-dimension-limit-halfvec-expression
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp