This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: pgvector-08-hnsw-mmap-resident-ceiling

pgvector 0.8 HNSW Memory-Mapped Vector Resident Ceiling and Quantization Invariant

Enforces strict scalar quantization thresholds (SQ8/halfvec) and kernel-level resident set size ceilings for pgvector 0.8 HNSW indexes running on PostgreSQL 17. Prevents unconstrained virtual memory page faulting and Linux kernel OOM panics caused by concurrent high-dimensional approximate nearest neighbor (ANN) searches exhausting cgroup memory allocations.

Prohibited Anti-Pattern: Unquantized 1536+ dimension HNSW index instantiation without explicit work_mem ceilings and mmap page budgeting.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: pgvector 0.8 HNSW Memory-Mapped Vector Resident Ceiling and Quantization Invariant - Enforces strict scalar quantization thresholds (SQ8/halfvec) and kernel-level resident set size ceilings for pgvector 0.8 HNSW indexes running on PostgreSQL 17. Prevents unconstrained virtual memory page faulting and Linux kernel OOM panics caused by concurrent high-dimensional approximate nearest neighbor (ANN) searches exhausting cgroup memory allocations.
globs: src/database/**/*.ts, src/migrations/**/*.sql, src/repositories/**/*.ts, src/config/database.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pgvector-08-hnsw-mmap-resident-ceiling"
---

# pgvector 0.8 HNSW Memory-Mapped Vector Resident Ceiling and Quantization Invariant

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unquantized 1536+ dimension HNSW index instantiation without explicit work_mem ceilings and mmap page budgeting.

## System Constraints
You are a Principal Engineer enforcing pgvector 0.8 HNSW Memory-Mapped Vector Resident Ceiling invariants. Mandate scalar quantization (halfvec or 8-bit integer quantization) for vector dimensionality >= 768, enforce dynamic maintenance_work_mem boundaries during index construction, and mandate explicit hnsw.ef_search limits inside transactional query contexts to guarantee deterministic resident memory consumption under high concurrency.

## Strict Guidelines
- Engineers MUST ALWAYS enforce scalar quantization or halfvec types for any vector dimension >= 768 in pgvector 0.8+ schemas.
- Engineers MUST NOT construct HNSW indexes using standard 32-bit floating point vectors without pre-allocating dedicated cgroup page buffer capacity exceeding the full graph footprint.
- Code SHOULD dynamically configure `hnsw.ef_search` per transaction scope to bound CPU cache line traversal and page faults during peak read concurrency.
- Services MUST execute migration-time DDL inside explicit session boundaries setting `maintenance_work_mem` and `max_parallel_maintenance_workers` to avoid host node starvation.
- Components NEVER execute unbounded vector similarity queries without strict top-K limit clauses and index probe budgets.

## Bad vs Good Implementations
### HNSW Index Creation and Query Execution with Quantization Guardrails

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Executes 1536-dimensional float32 vector comparisons without scoping session-level memory flags. In high-concurrency environments, unbounded parallel page faults on the mmapped HNSW graph rapidly exhaust host cgroup memory, triggering the Linux OOM killer.
```typescript
import { Pool } from 'pg';

export async function searchVectors(pool: Pool, queryVector: number[]): Promise<any[]> {
  // Anti-pattern: Default float32 vector, unbounded ef_search, unconstrained memory allocation
  const query = `
    SELECT id, payload, embedding <=> $1::vector AS distance
    FROM document_embeddings
    ORDER BY distance ASC
    LIMIT 20;
  `;
  const { rows } = await pool.query(query, [JSON.stringify(queryVector)]);
  return rows;
}
```

#### Production Standard (Correct)
> **Standard**: Uses halfvec (FP16 quantization) to reduce index graph resident memory footprint by 50%, wraps search in a read-only transaction, and caps hnsw.ef_search to strictly bound the page working set per query worker.
```typescript
import { Pool, PoolClient } from 'pg';

export async function searchVectors(pool: Pool, queryVector: number[]): Promise<any[]> {
  const client: PoolClient = await pool.connect();
  try {
    await client.query('BEGIN READ ONLY;');
    // Invariant: Bound graph descent scope to constrain resident working set per worker
    await client.query('SET LOCAL hnsw.ef_search = 40;');
    
    // Invariant: Cast query vector to halfvec to leverage pgvector 0.8 quantized storage footprint
    const query = `
      SELECT id, payload, (embedding_half <=> $1::halfvec) AS distance
      FROM document_embeddings
      ORDER BY distance ASC
      LIMIT 20;
    `;
    
    const { rows } = await client.query(query, [JSON.stringify(queryVector)]);
    await client.query('COMMIT;');
    return rows;
  } catch (error) {
    await client.query('ROLLBACK;');
    throw error;
  } finally {
    client.release();
  }
}
```

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: pgvector 0.8 HNSW Memory-Mapped Vector Resident Ceiling and Quantization Invariant

pgvector 0.8 HNSW Memory-Mapped Vector Resident Ceiling and Quantization Invariant defines an industrial-grade behavioral standard for backend engineering. Enforces strict scalar quantization thresholds (SQ8/halfvec) and kernel-level resident set size ceilings for pgvector 0.8 HNSW indexes running on PostgreSQL 17. Prevents unconstrained virtual memory page faulting and Linux kernel OOM panics caused by concurrent high-dimensional approximate nearest neighbor (ANN) searches exhausting cgroup memory allocations. 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: Unquantized 1536+ dimension HNSW index instantiation without explicit work_mem ceilings and mmap page budgeting. 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 DriftUnquantized 1536+ dimension HNSW index instantiation without explicit work_mem ceilings and mmap page budgeting.Critical
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 enforce scalar quantization or halfvec types for any vector dimension >= 768 in pgvector 0.8+ schemas.

Engineers MUST ALWAYS enforce scalar quantization or halfvec types for any vector dimension >= 768 in pgvector 0.8+ schemas.

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 NOT construct HNSW indexes using standard 32-bit floating point vectors without pre-allocating dedicated cgroup page buffer capacity exceeding the full graph footprint.

Engineers MUST NOT construct HNSW indexes using standard 32-bit floating point vectors without pre-allocating dedicated cgroup page buffer capacity exceeding the full graph footprint.

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

Code SHOULD dynamically configure `hnsw.ef_search` per transaction scope to bound CPU cache line traversal and page faults during peak read concurrency.

Code SHOULD dynamically configure `hnsw.ef_search` per transaction scope to bound CPU cache line traversal and page faults during peak read concurrency.

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

Services MUST execute migration-time DDL inside explicit session boundaries setting `maintenance_work_mem` and `max_parallel_maintenance_workers` to avoid host node starvation.

Services MUST execute migration-time DDL inside explicit session boundaries setting `maintenance_work_mem` and `max_parallel_maintenance_workers` to avoid host node starvation.

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

Components NEVER execute unbounded vector similarity queries without strict top-K limit clauses and index probe budgets.

Components NEVER execute unbounded vector similarity queries without strict top-K limit clauses and index probe budgets.

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 pgvector 0.8 HNSW Memory-Mapped Vector Resident Ceiling and Quantization Invariant critical for AI coding assistants?

Enforces strict scalar quantization thresholds (SQ8/halfvec) and kernel-level resident set size ceilings for pgvector 0.8 HNSW indexes running on PostgreSQL 17. Prevents unconstrained virtual memory page faulting and Linux kernel OOM panics caused by concurrent high-dimensional approximate nearest neighbor (ANN) searches exhausting cgroup memory allocations. 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 (Unquantized 1536+ dimension HNSW index instantiation without explicit work_mem ceilings and mmap page budgeting.) 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

pgvector