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

Postgres 17 pgvector HNSW Dynamic Memory and Maintenance Budget Guardrail

Enforces strict session-scoped memory budgeting and connection concurrency controls when building and streaming into pgvector HNSW indexes in Postgres 17. Prevents host kernel OOM kills by systematically bounding maintenance_work_mem, constraining max_parallel_maintenance_workers, and governing working memory during high-throughput vector ingestion.

Prohibited Anti-Pattern: Global Unbounded maintenance_work_mem and Unthrottled Streaming Vector Ingestion Inducing Process-Wide OOM

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 pgvector HNSW Dynamic Memory and Maintenance Budget Guardrail - Enforces strict session-scoped memory budgeting and connection concurrency controls when building and streaming into pgvector HNSW indexes in Postgres 17. Prevents host kernel OOM kills by systematically bounding maintenance_work_mem, constraining max_parallel_maintenance_workers, and governing working memory during high-throughput vector ingestion.
globs: src/db/**/*.ts, src/repositories/**/*.ts, src/services/indexing/**/*.ts, migrations/**/*.sql
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-pgvector-hnsw-memory-budgeting"
---

# Postgres 17 pgvector HNSW Dynamic Memory and Maintenance Budget Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Global Unbounded maintenance_work_mem and Unthrottled Streaming Vector Ingestion Inducing Process-Wide OOM

## System Constraints
You are a Principal Database Reliability Engineer and Performance Architect specializing in high-throughput vector storage on PostgreSQL 17. You strictly enforce explicit memory caps (maintenance_work_mem, work_mem), worker allocation limits (max_parallel_maintenance_workers), and batch streaming controls during pgvector HNSW index creation and concurrent ingestion. Reject any unbudgeted, global, or raw concurrent index operations that risk kernel Out-Of-Memory (OOM) terminations.

## Strict Guidelines
- Engineers MUST ALWAYS set session-scoped, deterministic `SET LOCAL maintenance_work_mem` before issuing `CREATE INDEX ... USING hnsw`, capped at: (Available_RAM - shared_buffers) / (max_parallel_maintenance_workers + 1).
- Engineers MUST NOT configure `maintenance_work_mem` globally in `postgresql.conf` to values that would exceed total system memory when multiplied by active maintenance worker processes.
- Code MUST throttle concurrent vector ingestion into pre-existing HNSW indexes using explicit batch chunking (100-500 vectors per transaction) rather than unconstrained concurrent point inserts.
- Services MUST monitor `pg_stat_activity` and verify available shared vector buffer pages before kicking off asynchronous index builds or heavy vector upsert pipelines.
- Index builds SHOULD explicitly declare conservative `m` and `ef_construction` parameters (e.g., `m = 16`, `ef_construction = 64` or `128`) to prevent exponential memory consumption during graph expansion.
- Components NEVER execute raw `CREATE INDEX CONCURRENTLY` without explicit per-session timeouts (`statement_timeout`, `lock_timeout`) and parallel worker bounds.

## Bad vs Good Implementations
### HNSW Index Creation with Deterministic Session Memory Budgeting

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Executing concurrent HNSW index builds under default or globally inflated maintenance memory allocations allows parallel workers to allocate memory multiplicatively without caps, frequently causing OS-level OOM killer invocations during graph construction.
```typescript
import { Pool } from 'pg';

export async function buildVectorIndex(pool: Pool): Promise<void> {
  const client = await pool.connect();
  try {
    // ANTI-PATTERN: Inherits global maintenance_work_mem and unbounded parallel workers.
    // In high-concurrency or high-dimension scenarios (e.g. 1536-dim), this triggers
    // Linux OOM killer on the Postgres postmaster process.
    await client.query(`
      CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_document_embeddings_hnsw
      ON document_embeddings
      USING hnsw (embedding vector_cosine_ops);
    `);
  } finally {
    client.release();
  }
}
```

#### Production Standard (Correct)
> **Standard**: Session parameters constrain the memory footprint by setting explicit worker counts, session-isolated maintenance_work_mem, strict timeouts, and bounded HNSW graph parameters before running the index build.
```typescript
import { Pool, PoolClient } from 'pg';

interface VectorBudgetOptions {
  maxWorkers: number;
  maintenanceMemoryMb: number;
  efConstruction: number;
  m: number;
}

export async function buildVectorIndexSafe(
  pool: Pool,
  options: VectorBudgetOptions = { maxWorkers: 2, maintenanceMemoryMb: 2048, efConstruction: 64, m: 16 }
): Promise<void> {
  const client: PoolClient = await pool.connect();
  try {
    // Enforce isolated session-level constraints to prevent OOM under load
    await client.query('BEGIN');
    await client.query(`SET LOCAL max_parallel_maintenance_workers = ${options.maxWorkers};`);
    await client.query(`SET LOCAL maintenance_work_mem = '${options.maintenanceMemoryMb}MB';`);
    await client.query(`SET LOCAL lock_timeout = '10s';`);
    await client.query(`SET LOCAL statement_timeout = '1h';`);
    await client.query('COMMIT');

    // Execute bounded concurrent HNSW index construction
    await client.query(`
      CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_document_embeddings_hnsw
      ON document_embeddings
      USING hnsw (embedding vector_cosine_ops)
      WITH (
        m = ${options.m},
        ef_construction = ${options.efConstruction}
      );
    `);
  } finally {
    // Reset session parameters before returning connection to pool
    await client.query('DISCARD TEMP').catch(() => {});
    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 Dynamic Memory and Maintenance Budget Guardrail

Postgres 17 pgvector HNSW Dynamic Memory and Maintenance Budget Guardrail defines an industrial-grade behavioral standard for backend engineering. Enforces strict session-scoped memory budgeting and connection concurrency controls when building and streaming into pgvector HNSW indexes in Postgres 17. Prevents host kernel OOM kills by systematically bounding maintenance_work_mem, constraining max_parallel_maintenance_workers, and governing working memory during high-throughput vector ingestion. 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: Global Unbounded maintenance_work_mem and Unthrottled Streaming Vector Ingestion Inducing Process-Wide OOM 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 DriftGlobal Unbounded maintenance_work_mem and Unthrottled Streaming Vector Ingestion Inducing Process-Wide OOMCritical
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 set session-scoped, deterministic `SET LOCAL maintenance_work_mem` before issuing `CREATE INDEX ... USING hnsw`, capped at

Engineers MUST ALWAYS set session-scoped, deterministic `SET LOCAL maintenance_work_mem` before issuing `CREATE INDEX ... USING hnsw`, capped at: (Available_RAM - shared_buffers) / (max_parallel_maintenance_workers + 1).

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 configure `maintenance_work_mem` globally in `postgresql.conf` to values that would exceed total system memory when multiplied by active maintenance worker processes.

Engineers MUST NOT configure `maintenance_work_mem` globally in `postgresql.conf` to values that would exceed total system memory when multiplied by active maintenance worker processes.

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 throttle concurrent vector ingestion into pre-existing HNSW indexes using explicit batch chunking (100-500 vectors per transaction) rather than unconstrained concurrent point inserts.

Code MUST throttle concurrent vector ingestion into pre-existing HNSW indexes using explicit batch chunking (100-500 vectors per transaction) rather than unconstrained concurrent point inserts.

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 monitor `pg_stat_activity` and verify available shared vector buffer pages before kicking off asynchronous index builds or heavy vector upsert pipelines.

Services MUST monitor `pg_stat_activity` and verify available shared vector buffer pages before kicking off asynchronous index builds or heavy vector upsert pipelines.

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

Index builds SHOULD explicitly declare conservative `m` and `ef_construction` parameters (e.g., `m = 16`, `ef_construction = 64` or `128`) to prevent exponential memory consumption during graph expansion.

Index builds SHOULD explicitly declare conservative `m` and `ef_construction` parameters (e.g., `m = 16`, `ef_construction = 64` or `128`) to prevent exponential memory consumption during graph expansion.

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

Components NEVER execute raw `CREATE INDEX CONCURRENTLY` without explicit per-session timeouts (`statement_timeout`, `lock_timeout`) and parallel worker bounds.

Components NEVER execute raw `CREATE INDEX CONCURRENTLY` without explicit per-session timeouts (`statement_timeout`, `lock_timeout`) and parallel worker bounds.

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 Dynamic Memory and Maintenance Budget Guardrail critical for AI coding assistants?

Enforces strict session-scoped memory budgeting and connection concurrency controls when building and streaming into pgvector HNSW indexes in Postgres 17. Prevents host kernel OOM kills by systematically bounding maintenance_work_mem, constraining max_parallel_maintenance_workers, and governing working memory during high-throughput vector ingestion. 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 (Global Unbounded maintenance_work_mem and Unthrottled Streaming Vector Ingestion Inducing Process-Wide OOM) 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