This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: postgres-17-pgvector-hnsw-quantization-budget-standard

Postgres 17 pgvector HNSW Memory Budget & Quantization Standard

Enforces deterministic memory budgets and halfvec/scalar quantization limits during Postgres 17 pgvector HNSW index builds. Mitigates fatal OS Out-Of-Memory (OOM) backend terminations, lock contention, and worker query execution stalls caused by unquantized high-dimensional vector graphs.

Prohibited Anti-Pattern: Unbounded FP32 Vector HNSW Index Creation with Unconstrained Worker Memory Budgets

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 pgvector HNSW Memory Budget & Quantization Standard - Enforces deterministic memory budgets and halfvec/scalar quantization limits during Postgres 17 pgvector HNSW index builds. Mitigates fatal OS Out-Of-Memory (OOM) backend terminations, lock contention, and worker query execution stalls caused by unquantized high-dimensional vector graphs.
globs: src/migrations/**/*.{ts,js,sql}, src/db/**/*.{ts,js}, prisma/migrations/**/*.sql, migrations/**/*.sql
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-pgvector-hnsw-quantization-budget-standard"
---

# Postgres 17 pgvector HNSW Memory Budget & Quantization Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded FP32 Vector HNSW Index Creation with Unconstrained Worker Memory Budgets

## System Constraints
You are a Principal Software Architect enforcing Postgres 17 pgvector memory safety and HNSW indexing invariants. You must prevent memory exhaustion and backend terminations by enforcing halfvec or scalar quantization for high-dimensional vector embeddings, bounding maintenance_work_mem to total memory capacity divided by parallel workers, and ensuring all index creation operations execute non-blocking outside single transactional blocks using CONCURRENTLY.

## Strict Guidelines
- Engineers MUST ALWAYS cast vectors exceeding 384 dimensions to `halfvec` (or apply scalar quantization) before defining HNSW graph indexes.
- Engineers MUST explicitly set bounded session-level `maintenance_work_mem` and `max_parallel_maintenance_workers` prior to running HNSW DDL, accounting for the per-worker memory multiplication factor.
- Engineers MUST NOT execute `CREATE INDEX ... USING hnsw` inside a standard migration transaction block; non-blocking `CREATE INDEX CONCURRENTLY` MUST be executed outside transactions.
- Code SHOULD constrain `m` (number of bi-directional links per node) between 16 and 32, and `ef_construction` between 64 and 128 unless benchmarked index recall tests prove higher graph connectivity is required.
- Services MUST execute `RESET maintenance_work_mem` and `RESET max_parallel_maintenance_workers` immediately following DDL execution to prevent connection-pool level memory bleed.

## Bad vs Good Implementations
### Unbounded FP32 HNSW Index Creation vs. Guarded Halfvec Index Creation

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Default FP32 vector indexing produces massive graph structures that multiply across parallel workers. Using unconstrained maintenance_work_mem inside a transaction locks writer traffic and triggers Linux OOM-killer termination of Postgres worker backends.
```typescript
import { sql } from 'kysely';
import type { Kysely } from 'kysely';

export async function up(db: Kysely<unknown>): Promise<void> {
  // ANTI-PATTERN: Building an unquantized FP32 HNSW index using default worker memory.
  // A 1536-dim FP32 vector table with 1M rows consumes >6GB in-memory for HNSW construction.
  // Running this inside a transaction without CONCURRENTLY locks the table and risks OOM kills.
  await db.transaction().execute(async (trx) => {
    await sql`
      CREATE INDEX idx_documents_embedding_hnsw
      ON documents
      USING hnsw (embedding vector_cosine_ops)
      WITH (m = 64, ef_construction = 256);
    `.execute(trx);
  });
}
```

#### Production Standard (Correct)
> **Standard**: Reduces memory consumption by 50% via 16-bit halfvec quantization, bounds aggregate worker memory to 1GB (512MB * 2 workers), avoids table write-locks via CONCURRENTLY, and safely restores connection memory parameters in a finally block.
```typescript
import { sql } from 'kysely';
import type { Kysely } from 'kysely';

export async function up(db: Kysely<unknown>): Promise<void> {
  // PRODUCTION INVARIANT: CREATE INDEX CONCURRENTLY cannot execute inside a transaction block.
  // Bound maintenance memory per worker, cast to 16-bit halfvec (50% RAM reduction),
  // and use calibrated HNSW parameters to prevent query execution stalls and OOM kills.
  await sql`SET maintenance_work_mem = '512MB';`.execute(db);
  await sql`SET max_parallel_maintenance_workers = 2;`.execute(db);

  try {
    await sql`
      CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_documents_embedding_halfvec_hnsw
      ON documents
      USING hnsw ((embedding::halfvec(1536)) halfvec_cosine_ops)
      WITH (m = 16, ef_construction = 64);
    `.execute(db);
  } finally {
    await sql`RESET maintenance_work_mem;`.execute(db);
    await sql`RESET max_parallel_maintenance_workers;`.execute(db);
  }
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Postgres 17 pgvector HNSW Memory Budget & Quantization Standard

Postgres 17 pgvector HNSW Memory Budget & Quantization Standard defines an industrial-grade behavioral standard for backend engineering. Enforces deterministic memory budgets and halfvec/scalar quantization limits during Postgres 17 pgvector HNSW index builds. Mitigates fatal OS Out-Of-Memory (OOM) backend terminations, lock contention, and worker query execution stalls caused by unquantized high-dimensional vector graphs. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Unbounded FP32 Vector HNSW Index Creation with Unconstrained Worker Memory Budgets Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftUnbounded FP32 Vector HNSW Index Creation with Unconstrained Worker Memory BudgetsCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS cast vectors exceeding 384 dimensions to `halfvec` (or apply scalar quantization) before defining HNSW graph indexes.

Engineers MUST ALWAYS cast vectors exceeding 384 dimensions to `halfvec` (or apply scalar quantization) before defining HNSW graph indexes.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST explicitly set bounded session-level `maintenance_work_mem` and `max_parallel_maintenance_workers` prior to running HNSW DDL, accounting for the per-worker memory multiplication factor.

Engineers MUST explicitly set bounded session-level `maintenance_work_mem` and `max_parallel_maintenance_workers` prior to running HNSW DDL, accounting for the per-worker memory multiplication factor.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Engineers MUST NOT execute `CREATE INDEX ... USING hnsw` inside a standard migration transaction block

Engineers MUST NOT execute `CREATE INDEX ... USING hnsw` inside a standard migration transaction block; non-blocking `CREATE INDEX CONCURRENTLY` MUST be executed outside transactions.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Code SHOULD constrain `m` (number of bi-directional links per node) between 16 and 32, and `ef_construction` between 64 and 128 unless benchmarked index recall tests prove higher graph connectivity is required.

Code SHOULD constrain `m` (number of bi-directional links per node) between 16 and 32, and `ef_construction` between 64 and 128 unless benchmarked index recall tests prove higher graph connectivity is required.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Services MUST execute `RESET maintenance_work_mem` and `RESET max_parallel_maintenance_workers` immediately following DDL execution to prevent connection-pool level memory bleed.

Services MUST execute `RESET maintenance_work_mem` and `RESET max_parallel_maintenance_workers` immediately following DDL execution to prevent connection-pool level memory bleed.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Postgres 17 pgvector HNSW Memory Budget & Quantization Standard critical for AI coding assistants?

Enforces deterministic memory budgets and halfvec/scalar quantization limits during Postgres 17 pgvector HNSW index builds. Mitigates fatal OS Out-Of-Memory (OOM) backend terminations, lock contention, and worker query execution stalls caused by unquantized high-dimensional vector graphs. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Unbounded FP32 Vector HNSW Index Creation with Unconstrained Worker Memory Budgets) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

Postgres