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

pgvector 0.8+ HNSW Iterative Index Scan Memory Budget Guardrail

Enforces strict session-level memory caps and scan bounds during pgvector 0.8+ iterative HNSW index scans on PostgreSQL 17. Highly selective relational WHERE clauses combined with nearest-neighbor vector operators cause unbounded graph traversal and candidate set allocation in backend worker memory, triggering OOM panics unless guarded by hnsw.iterative_scan and work_mem safeguards.

Prohibited Anti-Pattern: Unbounded Iterative HNSW Graph Traversals Triggering Postgres Worker OOM Panics

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: pgvector 0.8+ HNSW Iterative Index Scan Memory Budget Guardrail - Enforces strict session-level memory caps and scan bounds during pgvector 0.8+ iterative HNSW index scans on PostgreSQL 17. Highly selective relational WHERE clauses combined with nearest-neighbor vector operators cause unbounded graph traversal and candidate set allocation in backend worker memory, triggering OOM panics unless guarded by hnsw.iterative_scan and work_mem safeguards.
globs: src/db/**/*.ts, src/repositories/**/*.ts, src/services/vector/**/*.ts, src/**/*.sql
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pgvector-hnsw-iterative-scan-memory-budget"
---

# pgvector 0.8+ HNSW Iterative Index Scan Memory Budget Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Iterative HNSW Graph Traversals Triggering Postgres Worker OOM Panics

## System Constraints
You are a Principal Engineer enforcing strict memory guardrails for pgvector 0.8+ HNSW iterative scans in PostgreSQL 17. You must ensure that any filtered vector search executes within an explicit transaction or session context setting hnsw.iterative_scan, hnsw.max_scan_tuples, and bounded work_mem to prevent backend process eviction.

## Strict Guidelines
- Engineers MUST ALWAYS explicitly set `hnsw.iterative_scan = 'relaxed'` or `'strict'` alongside `hnsw.max_scan_tuples` within a local transaction when issuing vector searches with relational scalar predicates.
- Engineers MUST NOT allow default unbound `hnsw.max_scan_tuples = 0` on queries containing high-selectivity relational WHERE filters.
- Code MUST isolate iterative vector queries inside scoped transactions using `SET LOCAL` or dedicated pooled client sessions to prevent leaking altered planner parameters.
- Services MUST catch `57014` (query_canceled) and `53200` (out_of_memory) errors and fall back to exact scalar pre-filtering with sequential scans or exact KNN when iterative scan bounds are reached.
- Engineers MUST NEVER execute filtered ANN queries using connection-wide global overrides without resetting `hnsw.ef_search` and `hnsw.max_scan_tuples` to safe baseline limits.

## Bad vs Good Implementations
### Unbounded HNSW Filtering vs Guarded Iterative Scan in Postgres 17

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Executes an iterative HNSW search without limiting `hnsw.max_scan_tuples` or configuring `hnsw.iterative_scan`. When predicates filter out most nodes, pgvector keeps scanning the index graph until exhaustion, causing backend worker OOM kill and dropping client connections.
```typescript
import { sql } from 'kysely';
import { db } from '../database';

export async function searchSimilarDocuments(tenantId: string, embedding: number[], limit: number) {
  // Anti-pattern: No session-scoped memory guardrails or iteration limit.
  // When tenant_id matches <0.01% of rows, pgvector 0.8+ iterative scan traverses
  // the entire HNSW graph, causing massive candidate queue allocation and backend OOM.
  const vectorLiteral = `[${embedding.join(',')}]`;
  return await db.selectFrom('documents')
    .selectAll()
    .where('tenant_id', '=', tenantId)
    .where('is_archived', '=', false)
    .orderBy(sql`embedding <=> ${vectorLiteral}::vector`)
    .limit(limit)
    .execute();
}
```

#### Production Standard (Correct)
> **Standard**: Wraps the query in a transactional scope using `SET LOCAL` to activate pgvector 0.8's iterative scan while constraining candidate visits with `hnsw.max_scan_tuples = 20000` and setting a strict statement timeout to avoid run-away memory allocation.
```typescript
import { sql } from 'kysely';
import { db } from '../database';

export async function searchSimilarDocuments(tenantId: string, embedding: number[], limit: number) {
  const vectorLiteral = `[${embedding.join(',')}]`;
  
  return await db.transaction().execute(async (trx) => {
    // Bound the graph traversal and enforce memory limit inside the transaction boundary
    await sql`SET LOCAL hnsw.iterative_scan = 'relaxed'`.execute(trx);
    await sql`SET LOCAL hnsw.max_scan_tuples = 20000`.execute(trx);
    await sql`SET LOCAL hnsw.ef_search = 100`.execute(trx);
    await sql`SET LOCAL statement_timeout = '2500ms'`.execute(trx);

    return await trx.selectFrom('documents')
      .selectAll()
      .where('tenant_id', '=', tenantId)
      .where('is_archived', '=', false)
      .orderBy(sql`embedding <=> ${vectorLiteral}::vector`)
      .limit(limit)
      .execute();
  });
}
```

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 Iterative Index Scan Memory Budget Guardrail

pgvector 0.8+ HNSW Iterative Index Scan Memory Budget Guardrail defines an industrial-grade behavioral standard for backend engineering. Enforces strict session-level memory caps and scan bounds during pgvector 0.8+ iterative HNSW index scans on PostgreSQL 17. Highly selective relational WHERE clauses combined with nearest-neighbor vector operators cause unbounded graph traversal and candidate set allocation in backend worker memory, triggering OOM panics unless guarded by hnsw.iterative_scan and work_mem safeguards. 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 Iterative HNSW Graph Traversals Triggering Postgres Worker OOM Panics 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 Iterative HNSW Graph Traversals Triggering Postgres Worker OOM PanicsCritical
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 explicitly set `hnsw.iterative_scan = 'relaxed'` or `'strict'` alongside `hnsw.max_scan_tuples` within a local transaction when issuing vector searches with relational scalar predicates.

Engineers MUST ALWAYS explicitly set `hnsw.iterative_scan = 'relaxed'` or `'strict'` alongside `hnsw.max_scan_tuples` within a local transaction when issuing vector searches with relational scalar predicates.

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 allow default unbound `hnsw.max_scan_tuples = 0` on queries containing high-selectivity relational WHERE filters.

Engineers MUST NOT allow default unbound `hnsw.max_scan_tuples = 0` on queries containing high-selectivity relational WHERE filters.

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 MUST isolate iterative vector queries inside scoped transactions using `SET LOCAL` or dedicated pooled client sessions to prevent leaking altered planner parameters.

Code MUST isolate iterative vector queries inside scoped transactions using `SET LOCAL` or dedicated pooled client sessions to prevent leaking altered planner parameters.

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 catch `57014` (query_canceled) and `53200` (out_of_memory) errors and fall back to exact scalar pre-filtering with sequential scans or exact KNN when iterative scan bounds are reached.

Services MUST catch `57014` (query_canceled) and `53200` (out_of_memory) errors and fall back to exact scalar pre-filtering with sequential scans or exact KNN when iterative scan bounds are reached.

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

Engineers MUST NEVER execute filtered ANN queries using connection-wide global overrides without resetting `hnsw.ef_search` and `hnsw.max_scan_tuples` to safe baseline limits.

Engineers MUST NEVER execute filtered ANN queries using connection-wide global overrides without resetting `hnsw.ef_search` and `hnsw.max_scan_tuples` to safe baseline limits.

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 Iterative Index Scan Memory Budget Guardrail critical for AI coding assistants?

Enforces strict session-level memory caps and scan bounds during pgvector 0.8+ iterative HNSW index scans on PostgreSQL 17. Highly selective relational WHERE clauses combined with nearest-neighbor vector operators cause unbounded graph traversal and candidate set allocation in backend worker memory, triggering OOM panics unless guarded by hnsw.iterative_scan and work_mem safeguards. 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 Iterative HNSW Graph Traversals Triggering Postgres Worker OOM Panics) 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