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

pgvector 0.8 HNSW Index Deterministic Mmap and Buffer Prewarming Boundary

Enforces deterministic prewarming of pgvector HNSW index pages into Postgres shared_buffers and OS page cache before routing production vector search traffic. Eliminates cold-start synchronous page faults, disk I/O thrashing, and p99 tail latency spikes during deployments, failovers, and index maintenance.

Prohibited Anti-Pattern: Routing Production Search Queries to Cold HNSW Indexes Causing Page Fault Storms and Disk I/O Thrashing

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: pgvector 0.8 HNSW Index Deterministic Mmap and Buffer Prewarming Boundary - Enforces deterministic prewarming of pgvector HNSW index pages into Postgres shared_buffers and OS page cache before routing production vector search traffic. Eliminates cold-start synchronous page faults, disk I/O thrashing, and p99 tail latency spikes during deployments, failovers, and index maintenance.
globs: src/**/*.service.ts, src/**/*.repository.ts, src/infra/**/*.ts, src/health/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pgvector-hnsw-mmap-prewarm-boundary"
---

# pgvector 0.8 HNSW Index Deterministic Mmap and Buffer Prewarming Boundary

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Routing Production Search Queries to Cold HNSW Indexes Causing Page Fault Storms and Disk I/O Thrashing

## System Constraints
You are a Principal Software Architect enforcing pgvector 0.8 HNSW Index Mmap Prewarming Boundaries. You mandate that no backend service or container reports healthy or marks traffic readiness until target pgvector HNSW index relations are deterministically prefaulted into PostgreSQL shared_buffers and the OS buffer cache via pg_prewarm or deterministic probe sets. Cold disk reads during graph traversal trigger cascading thread stalls under concurrent production load. You must enforce explicit prewarm gating in lifecycle hooks and readiness endpoints.

## Strict Guidelines
- Engineers MUST ALWAYS invoke `pg_prewarm` or execute deterministic synthetic warmup probes across HNSW indexes before declaring a node or container ready for production vector queries.
- Engineers MUST NOT consider a generic `SELECT 1` database health check sufficient for vector-serving workloads.
- Readiness probes SHOULD verify that relation pages for target HNSW indexes reside in `pg_buffercache` or execute warmup queries until vector latency matches warm-cache SLAs.
- Services MUST configure `maintenance_work_mem` and `hnsw.ef_search` within optimal bounds during warmup transitions to avoid worker thread memory exhaustion.
- Components NEVER route un-hedged, un-warmed production nearest-neighbor queries immediately after database replica promotion, index recreation (REINDEX CONCURRENTLY), or container restarts.

## Bad vs Good Implementations
### HNSW Index Readiness Probe & Prewarming Boundary

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The readiness check only runs 'SELECT 1;', signalling readiness while HNSW index graph pages remain entirely on cold disk storage. First wave of production traffic incurs synchronous disk reads during vector graph traversal, creating thread pool exhaustion and p99 latency spikes.
```typescript
import { Pool } from 'pg';

export class ColdVectorHealthService {
  constructor(private readonly db: Pool) {}

  // BAD: Superficial healthcheck routes live queries to cold HNSW graph pages on disk
  public async checkReadiness(): Promise<{ ready: boolean }> {
    try {
      await this.db.query('SELECT 1;');
      return { ready: true };
    } catch (err) {
      return { ready: false };
    }
  }

  public async search(embedding: number[]): Promise<any[]> {
    // Triggers severe synchronous page faults and p99 latency spikes (seconds vs ms)
    const query = `
      SELECT id, content
      FROM documents
      ORDER BY embedding <=> $1::vector
      LIMIT 10;
    `;
    const res = await this.db.query(query, [JSON.stringify(embedding)]);
    return res.rows;
  }
}
```

#### Production Standard (Correct)
> **Standard**: Enforces a strict prewarming boundary. Uses PostgreSQL's 'pg_prewarm' to populate shared_buffers with HNSW index pages, executes a deterministic probe to prime OS page caches, and blocks the service readiness probe until cache warming succeeds.
```typescript
import { Pool } from 'pg';

export class PrewarmedVectorHealthService {
  private isWarmed = false;
  private warmingPromise: Promise<void> | null = null;

  constructor(private readonly db: Pool) {}

  public async checkReadiness(): Promise<{ ready: boolean; reason?: string }> {
    if (!this.isWarmed) {
      if (!this.warmingPromise) {
        this.warmingPromise = this.prewarmHnswIndex();
      }
      return { ready: false, reason: 'HNSW index is prewarming into memory' };
    }
    return { ready: true };
  }

  private async prewarmHnswIndex(): Promise<void> {
    const client = await this.db.connect();
    try {
      // 1. Prefault index blocks into PostgreSQL shared_buffers via pg_prewarm
      await client.query(`
        SELECT pg_prewarm(
          'idx_documents_embedding_hnsw',
          'buffer',
          'main'
        );
      `);

      // 2. Synthetic probe pass to warm CPU caches and ef_search traversals
      await client.query(`
        SET LOCAL hnsw.ef_search = 40;
        SELECT id FROM documents
        ORDER BY embedding <=> (SELECT embedding FROM documents LIMIT 1)
        LIMIT 10;
      `);

      this.isWarmed = true;
    } catch (error) {
      this.isWarmed = false;
      throw error;
    } finally {
      client.release();
    }
  }

  public async search(embedding: number[]): Promise<any[]> {
    if (!this.isWarmed) {
      throw new Error('ServiceUnavailable: Vector index prewarm boundary not established.');
    }
    const query = `
      SELECT id, content
      FROM documents
      ORDER BY embedding <=> $1::vector
      LIMIT 10;
    `;
    const res = await this.db.query(query, [JSON.stringify(embedding)]);
    return res.rows;
  }
}
```

Recommended MCP Servers (mcp-serverlist.com)

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

Architectural Specification & Invariant Rationale

Architectural Invariant Design: pgvector 0.8 HNSW Index Deterministic Mmap and Buffer Prewarming Boundary

pgvector 0.8 HNSW Index Deterministic Mmap and Buffer Prewarming Boundary defines an industrial-grade behavioral standard for backend engineering. Enforces deterministic prewarming of pgvector HNSW index pages into Postgres shared_buffers and OS page cache before routing production vector search traffic. Eliminates cold-start synchronous page faults, disk I/O thrashing, and p99 tail latency spikes during deployments, failovers, and index maintenance. 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: Routing Production Search Queries to Cold HNSW Indexes Causing Page Fault Storms and Disk I/O Thrashing 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 DriftRouting Production Search Queries to Cold HNSW Indexes Causing Page Fault Storms and Disk I/O ThrashingCritical
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 invoke `pg_prewarm` or execute deterministic synthetic warmup probes across HNSW indexes before declaring a node or container ready for production vector queries.

Engineers MUST ALWAYS invoke `pg_prewarm` or execute deterministic synthetic warmup probes across HNSW indexes before declaring a node or container ready for production vector queries.

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 consider a generic `SELECT 1` database health check sufficient for vector-serving workloads.

Engineers MUST NOT consider a generic `SELECT 1` database health check sufficient for vector-serving workloads.

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

Readiness probes SHOULD verify that relation pages for target HNSW indexes reside in `pg_buffercache` or execute warmup queries until vector latency matches warm-cache SLAs.

Readiness probes SHOULD verify that relation pages for target HNSW indexes reside in `pg_buffercache` or execute warmup queries until vector latency matches warm-cache SLAs.

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 configure `maintenance_work_mem` and `hnsw.ef_search` within optimal bounds during warmup transitions to avoid worker thread memory exhaustion.

Services MUST configure `maintenance_work_mem` and `hnsw.ef_search` within optimal bounds during warmup transitions to avoid worker thread memory exhaustion.

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 route un-hedged, un-warmed production nearest-neighbor queries immediately after database replica promotion, index recreation (REINDEX CONCURRENTLY), or container restarts.

Components NEVER route un-hedged, un-warmed production nearest-neighbor queries immediately after database replica promotion, index recreation (REINDEX CONCURRENTLY), or container restarts.

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 Index Deterministic Mmap and Buffer Prewarming Boundary critical for AI coding assistants?

Enforces deterministic prewarming of pgvector HNSW index pages into Postgres shared_buffers and OS page cache before routing production vector search traffic. Eliminates cold-start synchronous page faults, disk I/O thrashing, and p99 tail latency spikes during deployments, failovers, and index maintenance. 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 (Routing Production Search Queries to Cold HNSW Indexes Causing Page Fault Storms and Disk I/O Thrashing) 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