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

PostgreSQL 17 pgvector HNSW Shared-Buffer Working Set Isolation

High-dimensional HNSW index graph traversals produce non-sequential page access patterns across multi-gigabyte working sets that evict hot OLTP relation pages from Postgres shared_buffers. In hybrid vector-relational workloads, HNSW query execution must be isolated through dedicated connection pools with strict session-level memory caps, materialized pre-filtering, or separated storage partitions to prevent cache thrashing and tail-latency amplification.

Prohibited Anti-Pattern: Unconstrained In-Line HNSW Graph Traversal in Core OLTP Shared Buffers

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 pgvector HNSW Shared-Buffer Working Set Isolation - High-dimensional HNSW index graph traversals produce non-sequential page access patterns across multi-gigabyte working sets that evict hot OLTP relation pages from Postgres shared_buffers. In hybrid vector-relational workloads, HNSW query execution must be isolated through dedicated connection pools with strict session-level memory caps, materialized pre-filtering, or separated storage partitions to prevent cache thrashing and tail-latency amplification.
globs: src/db/**/*.ts, src/repositories/**/*.ts, src/services/**/*.ts, migrations/**/*.sql
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pgvector-hnsw-shared-buffer-isolation-guardrail"
---

# PostgreSQL 17 pgvector HNSW Shared-Buffer Working Set Isolation

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unconstrained In-Line HNSW Graph Traversal in Core OLTP Shared Buffers

## System Constraints
You are a Principal Database Architect and Performance Engineer enforcing memory isolation standards for PostgreSQL 17 pgvector deployments. You must ensure that HNSW index traversals do not evict mission-critical OLTP relation pages from shared_buffers. Mandate explicit query-level work_mem containment, index sizing validations (ensuring hnsw indices fit in designated OS cache or shared_buffers), iterative pre-filtering before vector distance evaluation, and routing to read-replicas or dedicated vector pools.

## Strict Guidelines
- Engineers MUST ALWAYS isolate HNSW similarity searches from primary OLTP connection pools by using dedicated read-replica pools or discrete connection pools with scoped session parameters.
- Engineers MUST set session-scoped 'hnsw.ef_search' dynamically based on strict latency/recall trade-offs instead of relying on unbounded global configuration defaults.
- Engineers MUST NEVER execute broad un-indexed relational JOINs directly across massive HNSW search results in a single non-partitioned transaction.
- Queries executing vector searches alongside relational filters MUST use scalar pre-filtering with index intersections or CTE-fenced subqueries to prune the graph traversal candidates before distance calculations occur.
- Database migrations MUST explicitly set HNSW storage parameters ('m' and 'ef_construction') with calculated index size budgets to ensure the index working set does not exceed available shared_buffers or OS page cache capacity.

## Bad vs Good Implementations
### Hybrid Vector Search Execution: Unbounded OLTP Eviction vs. Scoped Session Isolation with CTE Pre-Filtering

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Executing vector similarity searches directly on the core OLTP pool with default session parameters causes non-sequential page requests through the HNSW graph, evicting hot relational tuples from shared_buffers. Combining 'WHERE tenant_id' without a composite index or strict CTE boundaries leads to post-filtering, which traverses excessive graph nodes and amplifies buffer churn.
```typescript
import { Pool } from 'pg';

const oltpPool = new Pool({ connectionString: process.env.DATABASE_URL });

export async function searchSimilarDocuments(tenantId: string, embedding: number[], limit: number) {
  // Anti-Pattern: Direct query on main OLTP pool
  // Lacks local work_mem containment and forces HNSW traversal over unpartitioned tenant data
  // Evicts hot OLTP B-tree and table pages from shared_buffers
  const query = `
    SELECT id, tenant_id, title, embedding <=> $1::vector AS distance
    FROM documents
    WHERE tenant_id = $2
    ORDER BY embedding <=> $1::vector
    LIMIT $3;
  `;
  const client = await oltpPool.connect();
  try {
    const res = await client.query(query, [JSON.stringify(embedding), tenantId, limit]);
    return res.rows;
  } finally {
    client.release();
  }
}
```

#### Production Standard (Correct)
> **Standard**: The query executes against an isolated replica connection pool, setting localized transaction parameters ('hnsw.ef_search', 'work_mem') within a read-only transaction. A MATERIALIZED CTE or partitioned subquery bounds the candidate space before vector distance computation, preventing HNSW graph traversal thrashing in shared_buffers.
```typescript
import { Pool, PoolClient } from 'pg';

// Isolated read-replica or dedicated vector search connection pool
const vectorReplicaPool = new Pool({
  connectionString: process.env.VECTOR_READ_REPLICA_URL,
  max: 10,
  idleTimeoutMillis: 10000,
});

export async function searchSimilarDocuments(
  tenantId: string,
  embedding: number[],
  limit: number
) {
  const client: PoolClient = await vectorReplicaPool.connect();
  try {
    await client.query('BEGIN READ ONLY');
    // Constrain graph traversal depth and local worker memory
    await client.query('SET LOCAL hnsw.ef_search = 40');
    await client.query("SET LOCAL work_mem = '64MB'");

    // CTE fences scalar filter first, drastically reducing HNSW traversal working set
    const query = `
      WITH scoped_candidates AS MATERIALIZED (
        SELECT id, embedding, title
        FROM documents
        WHERE tenant_id = $1
      )
      SELECT id, title, embedding <=> $2::vector AS distance
      FROM scoped_candidates
      ORDER BY embedding <=> $2::vector
      LIMIT $3;
    `;
    const res = await client.query(query, [tenantId, JSON.stringify(embedding), limit]);
    await client.query('COMMIT');
    return res.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: PostgreSQL 17 pgvector HNSW Shared-Buffer Working Set Isolation

PostgreSQL 17 pgvector HNSW Shared-Buffer Working Set Isolation defines an industrial-grade behavioral standard for backend engineering. High-dimensional HNSW index graph traversals produce non-sequential page access patterns across multi-gigabyte working sets that evict hot OLTP relation pages from Postgres shared_buffers. In hybrid vector-relational workloads, HNSW query execution must be isolated through dedicated connection pools with strict session-level memory caps, materialized pre-filtering, or separated storage partitions to prevent cache thrashing and tail-latency amplification. 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: Unconstrained In-Line HNSW Graph Traversal in Core OLTP Shared Buffers 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 DriftUnconstrained In-Line HNSW Graph Traversal in Core OLTP Shared BuffersCritical
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 isolate HNSW similarity searches from primary OLTP connection pools by using dedicated read-replica pools or discrete connection pools with scoped session parameters.

Engineers MUST ALWAYS isolate HNSW similarity searches from primary OLTP connection pools by using dedicated read-replica pools or discrete connection pools with scoped session 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.
2

Engineers MUST set session-scoped 'hnsw.ef_search' dynamically based on strict latency/recall trade-offs instead of relying on unbounded global configuration defaults.

Engineers MUST set session-scoped 'hnsw.ef_search' dynamically based on strict latency/recall trade-offs instead of relying on unbounded global configuration 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.
3

Engineers MUST NEVER execute broad un-indexed relational JOINs directly across massive HNSW search results in a single non-partitioned transaction.

Engineers MUST NEVER execute broad un-indexed relational JOINs directly across massive HNSW search results in a single non-partitioned transaction.

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

Queries executing vector searches alongside relational filters MUST use scalar pre-filtering with index intersections or CTE-fenced subqueries to prune the graph traversal candidates before distance calculations occur.

Queries executing vector searches alongside relational filters MUST use scalar pre-filtering with index intersections or CTE-fenced subqueries to prune the graph traversal candidates before distance calculations occur.

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

Database migrations MUST explicitly set HNSW storage parameters ('m' and 'ef_construction') with calculated index size budgets to ensure the index working set does not exceed available shared_buffers or OS page cache capacity.

Database migrations MUST explicitly set HNSW storage parameters ('m' and 'ef_construction') with calculated index size budgets to ensure the index working set does not exceed available shared_buffers or OS page cache capacity.

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 PostgreSQL 17 pgvector HNSW Shared-Buffer Working Set Isolation critical for AI coding assistants?

High-dimensional HNSW index graph traversals produce non-sequential page access patterns across multi-gigabyte working sets that evict hot OLTP relation pages from Postgres shared_buffers. In hybrid vector-relational workloads, HNSW query execution must be isolated through dedicated connection pools with strict session-level memory caps, materialized pre-filtering, or separated storage partitions to prevent cache thrashing and tail-latency amplification. 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 (Unconstrained In-Line HNSW Graph Traversal in Core OLTP Shared Buffers) 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

PostgreSQL