This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: pgvector-hnsw-shared-buffer-pinning-invariant

Postgres 17 pgvector HNSW Dedicated Buffer Pool and Memory Prewarming Invariant

Enforces deterministic resident-memory retention for high-dimensional pgvector HNSW indices to prevent catastrophic NVMe paging spikes during concurrent approximate nearest neighbor (ANN) scans. Mandates strict shared_buffers sizing, explicit pg_prewarm integration during deployment pipelines, and session-level work_mem/maintenance_work_mem calibration.

Prohibited Anti-Pattern: Unpinned Cold-Index Paging Spikes and Unbounded Random Read Degradation

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 pgvector HNSW Dedicated Buffer Pool and Memory Prewarming Invariant - Enforces deterministic resident-memory retention for high-dimensional pgvector HNSW indices to prevent catastrophic NVMe paging spikes during concurrent approximate nearest neighbor (ANN) scans. Mandates strict shared_buffers sizing, explicit pg_prewarm integration during deployment pipelines, and session-level work_mem/maintenance_work_mem calibration.
globs: src/database/**/*.ts, src/database/**/*.sql, migrations/**/*.ts, migrations/**/*.sql, src/infrastructure/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pgvector-hnsw-shared-buffer-pinning-invariant"
---

# Postgres 17 pgvector HNSW Dedicated Buffer Pool and Memory Prewarming Invariant

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unpinned Cold-Index Paging Spikes and Unbounded Random Read Degradation

## System Constraints
You are a Principal Database Architect enforcing strict memory residency invariants for Postgres 17 pgvector HNSW operations. HNSW graph traversals execute non-sequential, pointer-chasing access patterns across high-dimensional vector spaces. Any eviction of HNSW index pages from Postgres shared_buffers triggers random I/O latency amplification across NVMe arrays, collapsing p99 query performance. You must mandate migration scripts and database access layers verify index size versus shared_buffers capacity, enforce immediate buffer hydration via pg_prewarm, configure query-level maintenance barriers, and prevent table scans from evicting vector graph pages.

## Strict Guidelines
- Engineers MUST ALWAYS prewarm HNSW vector indices into shared_buffers using `pg_prewarm(index_name, 'buffer')` immediately post-migration and inside automated instance startup hooks.
- Engineers MUST NOT create an HNSW index with `m` and `ef_construction` parameters that calculate to an aggregate index footprint exceeding 40% of the allocated `shared_buffers`.
- Engineers MUST configure `effective_cache_size` and `shared_buffers` appropriately in connection bootstrap options before executing high-concurrency vector similarity probes.
- Code MUST execute vector search operations using bounded `hnsw.ef_search` values set within an explicit session or localized transaction context rather than broad cluster defaults.
- Migrations MUST NEVER run `CREATE INDEX ... USING hnsw` without explicitly verifying that `maintenance_work_mem` is tuned sufficiently high (e.g., >= 2GB) to construct optimal graph topology without spilling to disk.
- Services MUST monitor index cache hit ratios via `pg_statio_user_indexes` and emit alerts if the cache hit ratio for HNSW relations falls below 99.5%.

## Bad vs Good Implementations
### HNSW Migration and Runtime Prewarming Pattern

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Creating an HNSW index without configuring maintenance memory leads to poor graph connectivity, and omitting pg_prewarm leaves the graph cold on disk. Subsequent vector queries trigger severe I/O stalls fetching unpinned index pages from NVMe.
```typescript
import { Kysely, sql } from 'kysely';

export async function up(db: Kysely<unknown>): Promise<void> {
  // Anti-pattern: Default maintenance memory, missing pg_prewarm extension,
  // and lack of shared_buffers verification allows cache eviction on cold boot.
  await sql`
    CREATE INDEX idx_documents_embedding_hnsw
    ON document_embeddings
    USING hnsw (embedding vector_cosine_ops)
    WITH (m = 16, ef_construction = 64);
  `.execute(db);
}
```

#### Production Standard (Correct)
> **Standard**: Applies explicit `maintenance_work_mem` for the build, constructs a balanced HNSW graph, immediately hydrates Postgres shared buffers using `pg_prewarm`, and validates relation footprint to ensure cache residency.
```typescript
import { Kysely, sql } from 'kysely';

export async function up(db: Kysely<unknown>): Promise<void> {
  // Verify extension availability
  await sql`CREATE EXTENSION IF NOT EXISTS pg_prewarm;`.execute(db);

  // Execute in isolated block with tuned build memory to avoid disk-spill graph degradation
  await sql`SET LOCAL maintenance_work_mem = '4GB';`.execute(db);
  
  await sql`
    CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_documents_embedding_hnsw
    ON document_embeddings
    USING hnsw (embedding vector_cosine_ops)
    WITH (m = 32, ef_construction = 128);
  `.execute(db);

  // Pin the HNSW index into shared_buffers immediately after creation
  await sql`
    SELECT pg_prewarm('idx_documents_embedding_hnsw', 'buffer', 'main');
  `.execute(db);

  // Assert index is fully retained in buffers
  const stats = await sql<{ size: string }>`
    SELECT pg_size_pretty(pg_relation_size('idx_documents_embedding_hnsw')) as size;
  `.execute(db);
  
  if (!stats.rows[0]?.size) {
    throw new Error('Failed to verify HNSW index sizing post-prewarm.');
  }
}
```

### Scoped Query Execution with Bounded Search Memory and Plan Check

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Relying on default global settings risks unbounded search traversal, while executing without verifying shared buffer usage exposes queries to random storage latency when pages fall out of cache.
```typescript
import { Pool } from 'pg';

export async function querySimilarVectors(pool: Pool, queryVector: number[]): Promise<any[]> {
  // Anti-pattern: Missing transaction-isolated hnsw.ef_search;
  // fails to guard against cold buffers or unbounded graph traversing.
  const res = await pool.query(
    'SELECT id, content FROM document_embeddings ORDER BY embedding <=> $1::vector LIMIT 10;',
    [JSON.stringify(queryVector)]
  );
  return res.rows;
}
```

#### Production Standard (Correct)
> **Standard**: Wraps the similarity scan in a READ ONLY transaction, tunes `hnsw.ef_search` locally to bound memory and traversal overhead, and isolates configuration state per checkout.
```typescript
import { Pool, PoolClient } from 'pg';

export async function querySimilarVectors(pool: Pool, queryVector: number[]): Promise<any[]> {
  const client: PoolClient = await pool.connect();
  try {
    await client.query('BEGIN READ ONLY;');
    
    // Bound the graph traversal within the session/transaction boundary
    await client.query('SET LOCAL hnsw.ef_search = 100;');
    
    const formattedVector = `[${queryVector.join(',')}]`;
    
    const result = await client.query(
      `SELECT id, content 
       FROM document_embeddings 
       ORDER BY embedding <=> $1::vector 
       LIMIT 10;`,
      [formattedVector]
    );
    
    await client.query('COMMIT;');
    return result.rows;
  } catch (err) {
    await client.query('ROLLBACK;');
    throw err;
  } 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: Postgres 17 pgvector HNSW Dedicated Buffer Pool and Memory Prewarming Invariant

Postgres 17 pgvector HNSW Dedicated Buffer Pool and Memory Prewarming Invariant defines an industrial-grade behavioral standard for backend engineering. Enforces deterministic resident-memory retention for high-dimensional pgvector HNSW indices to prevent catastrophic NVMe paging spikes during concurrent approximate nearest neighbor (ANN) scans. Mandates strict shared_buffers sizing, explicit pg_prewarm integration during deployment pipelines, and session-level work_mem/maintenance_work_mem calibration. 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: Unpinned Cold-Index Paging Spikes and Unbounded Random Read Degradation 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 DriftUnpinned Cold-Index Paging Spikes and Unbounded Random Read DegradationCritical
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 prewarm HNSW vector indices into shared_buffers using `pg_prewarm(index_name, 'buffer')` immediately post-migration and inside automated instance startup hooks.

Engineers MUST ALWAYS prewarm HNSW vector indices into shared_buffers using `pg_prewarm(index_name, 'buffer')` immediately post-migration and inside automated instance startup hooks.

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 create an HNSW index with `m` and `ef_construction` parameters that calculate to an aggregate index footprint exceeding 40% of the allocated `shared_buffers`.

Engineers MUST NOT create an HNSW index with `m` and `ef_construction` parameters that calculate to an aggregate index footprint exceeding 40% of the allocated `shared_buffers`.

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 configure `effective_cache_size` and `shared_buffers` appropriately in connection bootstrap options before executing high-concurrency vector similarity probes.

Engineers MUST configure `effective_cache_size` and `shared_buffers` appropriately in connection bootstrap options before executing high-concurrency vector similarity probes.

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 MUST execute vector search operations using bounded `hnsw.ef_search` values set within an explicit session or localized transaction context rather than broad cluster defaults.

Code MUST execute vector search operations using bounded `hnsw.ef_search` values set within an explicit session or localized transaction context rather than broad cluster defaults.

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

Migrations MUST NEVER run `CREATE INDEX ... USING hnsw` without explicitly verifying that `maintenance_work_mem` is tuned sufficiently high (e.g., >= 2GB) to construct optimal graph topology without spilling to disk.

Migrations MUST NEVER run `CREATE INDEX ... USING hnsw` without explicitly verifying that `maintenance_work_mem` is tuned sufficiently high (e.g., >= 2GB) to construct optimal graph topology without spilling to disk.

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.
6

Services MUST monitor index cache hit ratios via `pg_statio_user_indexes` and emit alerts if the cache hit ratio for HNSW relations falls below 99.5%.

Services MUST monitor index cache hit ratios via `pg_statio_user_indexes` and emit alerts if the cache hit ratio for HNSW relations falls below 99.5%.

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 Dedicated Buffer Pool and Memory Prewarming Invariant critical for AI coding assistants?

Enforces deterministic resident-memory retention for high-dimensional pgvector HNSW indices to prevent catastrophic NVMe paging spikes during concurrent approximate nearest neighbor (ANN) scans. Mandates strict shared_buffers sizing, explicit pg_prewarm integration during deployment pipelines, and session-level work_mem/maintenance_work_mem calibration. 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 (Unpinned Cold-Index Paging Spikes and Unbounded Random Read Degradation) 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