{"slug": "fixing-error-column-cannot-have-more-than-2000-dimensions-in-pgvector-hnsw", "title": "Fixing ERROR: column cannot have more than 2000 dimensions in pgvector HNSW", "summary": "A developer documented why pgvector's HNSW and ivfflat indexes reject embedding columns above 2,000 dimensions, tracing the limit to PostgreSQL's 8 KB page size, where a 3,072-dimension FP32 vector consumes 12,288 bytes per tuple. The writeup presents three fixes: casting to halfvec FP16 expression indexes (raising the cap to 4,000 dimensions), Matryoshka truncation with L2 re-normalization, and a vendor embedding service, Voxell Forge, that returns server-side normalized vectors sized for native pgvector indexes.", "body_md": "If you migrate a PostgreSQL Retrieval-Augmented Generation (RAG) table from `1536`-dimensional embeddings to `3072`-dimensional vectors (` text-embedding-3-large` or `gemini-embedding-001`) and build an HNSW index, PostgreSQL immediately aborts DDL execution:\n\n```\nERROR: column cannot have more than 2000 dimensions for hnsw index\n```\n\nThe same hard ceiling hits `ivfflat` (`ERROR: column cannot have more than 2000 dimensions for ivfflat index`). Here is why PostgreSQL enforces that page-size limit and the three production ways to fix it without losing retrieval recall.\n\n`vector` Indexes at 2,000 Dimensions\nPostgreSQL uses a fixed `8 KB` (`8,192`-byte) heap and index page size by default. An uncompressed 32-bit float (`vector(3072)`) consumes `3,072 * 4 = 12,288` bytes (`12 KB`) per tuple before index graph pointers and tuple headers. Because a single index tuple must fit inside an `8 KB` page without TOAST out-of-line indirection during HNSW graph traversal, `pgvector` caps standard FP32 `vector` index columns at `2,000` dimensions (`8,000` bytes).\n\n`halfvec` FP16 Expression Indexing (`pgvector >= 0.7.0`)\nCasting `vector(3072)` to 16-bit floats (`halfvec(3072)`) halves storage to `6,144` bytes per tuple, raising the HNSW limit to `4,000` dimensions:\n\n```\nCREATE INDEX CONCURRENTLY idx_chunks_emb_hnsw_fp16\nON doc_chunks\nUSING hnsw ((embedding::halfvec(3072)) halfvec_cosine_ops)\nWITH (m = 16, ef_construction = 128);\n\n-- Every SELECT ORDER BY must repeat the exact cast so the planner uses the index:\nSELECT chunk_id, content\nFROM doc_chunks\nORDER BY (embedding::halfvec(3072)) <=> $1::halfvec(3072)\nLIMIT 20;\n```\n\nWatch out for query planner drift: if an ORM, **Model Context Protocol (MCP)** tool server, or **Agent2Agent (A2A)** service queries `embedding <=> $1` without the explicit `::halfvec(3072)` cast, PostgreSQL silently falls back to a sequential scan.\n\n`<= 2000d`)\nMatryoshka Representation Learning (MRL) concentrates coarse-to-fine semantic variance into leading vector coordinates so you can store native `vector(1024)` or `vector(1536)` columns directly in `pgvector` HNSW with zero cast gymnastics.\n\nHowever, truncating a `3072d` vector on the client without L2 re-normalization breaks inner-product indexes. In empirical probes on `gemini-embedding-001` (`3072d` truncated to `768d`), the prefix L2 norm drops to `0.5841`, collapsing unnormalized dot products from `0.8111` to `0.2793`.\n\n[**Voxell Forge**](https://voxell.ai/forge/) handles both paths cleanly:\n\n`turbo` (`1024d` native, `$0/forever`):` pgvector` FP32 `vector(1024)` HNSW natively at `4 KB` per vector with zero casting.`pro` (`2560d` native, `$0.25/1M` tokens):`1536d`) with automatic unit-norm L2 re-normalization, or indexes at full `halfvec(2560)` (`5 KB` per tuple). Paired with `C_corpus` zero-LLM ingest conditioning, `Pro` reaches `99.04%` Stage-1 `Recall@20` and `0.8849 nDCG@10` with two-stage `Max` neural reranking.\n\n``` python\nfrom openai import OpenAI\n\nclient = OpenAI(base_url=\"https://api.voxell.ai/v1\", api_key=\"FORGE_API_KEY\")\n\n# Returns server-side L2-normalized 1536d vectors ready for native pgvector HNSW\nres = client.embeddings.create(\n    model=\"pro\",\n    input=[\"PostgreSQL HNSW page layout and tuple headers\"],\n    dimensions=1536,\n)\n```\n\n| Configuration | Bytes / Vector | Raw Vector RAM (10M) | Native `vector` HNSW | Recall & Rerank Notes | \n|---|---|---|---|---|\n| `vector(3072)` FP32 | `12,288 B` | `122.9 GB` | Fails ( `> 2000d` ) | Sequential scan only unless cast | \n| `halfvec(2560)` FP16 (`Forge Pro` ) | `5,120 B` | `51.2 GB` | Supported ( `<= 4000d` ) | `99.04% Recall@20` (`C_corpus` ) +`0.8849 nDCG@10` (`Max` ) | \n| `vector(1536)` FP32 MRL (`Forge Pro` ) | `6,144 B` | `61.4 GB` | Supported ( `<= 2000d` ) | Server-side L2 re-normalized unit vector | \n| `vector(1024)` FP32 (`Forge Turbo` ) | `4,096 B` | `41.0 GB` | Supported ( `<= 2000d` ) | `$0/forever` embedding tier |", "url": "https://wpnews.pro/news/fixing-error-column-cannot-have-more-than-2000-dimensions-in-pgvector-hnsw", "canonical_source": "https://dev.to/jcorners68/fixing-error-column-cannot-have-more-than-2000-dimensions-in-pgvector-hnsw-3dj5", "published_at": "2026-12-03 20:43:05+00:00", "updated_at": "2026-12-03 20:46:58.944577+00:00", "lang": "en", "topics": ["ai-infrastructure", "ai-tools", "large-language-models"], "entities": ["pgvector", "PostgreSQL", "Voxell Forge", "text-embedding-3-large", "gemini-embedding-001", "Model Context Protocol", "Agent2Agent"], "also_reported_by": [], "takeaway": "Casting a 3,072-dimension vector column to halfvec in an HNSW expression index raises pgvector's 2,000-dimension cap to 4,000, but every ORDER BY query must repeat the exact cast or PostgreSQL silently falls back to a sequential scan.", "recommendation": {"advice": "When indexing embeddings above 2,000 dimensions in pgvector, build a halfvec expression index and repeat the identical cast in every query, or the planner will silently sequential-scan.", "playbook": "rag-retrieval", "playbook_url": "https://wpnews.pro/playbooks/rag-retrieval"}, "alternates": {"html": "https://wpnews.pro/news/fixing-error-column-cannot-have-more-than-2000-dimensions-in-pgvector-hnsw", "markdown": "https://wpnews.pro/news/fixing-error-column-cannot-have-more-than-2000-dimensions-in-pgvector-hnsw.md", "text": "https://wpnews.pro/news/fixing-error-column-cannot-have-more-than-2000-dimensions-in-pgvector-hnsw.txt", "jsonld": "https://wpnews.pro/news/fixing-error-column-cannot-have-more-than-2000-dimensions-in-pgvector-hnsw.jsonld"}}