# MP112: warn-hnsw-build-memory **Severity**: Warning | **Auto-fixable**: No | **Tier**: Free (needs `--database-url`) ## What It Detects An HNSW index build on a table of a million rows or more while `maintenance_work_mem` is below 1 GB. ## Why It's Dangerous pgvector builds the HNSW graph in `maintenance_work_mem`. While the graph fits, the build is fast. Once it does not, pgvector logs `hnsw graph no longer fits into maintenance_work_mem after N tuples` and finishes the rest on a much slower path. The statement is identical either way, which is what makes this hard to catch. A build that took minutes in staging can run for hours in production purely because the setting is lower there, and nothing in the migration file hints at it. `maintenance_work_mem` can be raised for the session that runs the build, so this is usually the cheapest fix available. ## Requires Production Context The rule needs both numbers: the row count from `pg_class`, and `maintenance_work_mem` from `pg_settings`. Without `--database-url` it has neither and stays silent. ### About the threshold "A million rows and under 1 GB" is a heuristic, not a computed graph size. The actual memory the graph needs depends on the vector dimensions and on `m`, and MigrationPilot does not attempt to derive it — a number that looks precise and is wrong would be worse than a rule that fires on the shape of the problem. The message reports the row count and the current setting and leaves the arithmetic to the server, which will tell you in the log if the graph did not fit. ## Bad Example ```sql -- items has 8M rows; maintenance_work_mem is 64MB CREATE INDEX idx_items_embedding ON items USING hnsw (embedding vector_cosine_ops) WITH (m = 16, ef_construction = 64); ``` ## Good Example ```sql SET maintenance_work_mem = '8GB'; SET max_parallel_maintenance_workers = 7; CREATE INDEX idx_items_embedding ON items USING hnsw (embedding vector_cosine_ops) WITH (m = 16, ef_construction = 128); ``` Watch the server log during the build. If the "no longer fits" notice appears, the rest of the build was on the slow path and more memory would have helped. `max_parallel_maintenance_workers` is capped by `max_parallel_workers`, so raise that too if you want more than a couple of workers on the build. ## Auto-fix This rule is not auto-fixable. How much memory to give the build depends on the machine, and setting it on someone's behalf could push the server into swap. ## Configuration ```yaml rules: MP112: false # disable MP112: severity: critical # change severity ``` ## Related Rules - [MP109](./MP109.md) require-vector-index-params -- setting m and ef_construction deliberately - [MP050](./MP050.md) prefer-hnsw-over-ivfflat -- which access method to use - [MP104](./MP104.md) warn-long-index-build -- build duration for ordinary indexes