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

Postgres 17 Bounded-Memory Parallel HNSW Index Build Guardrail

Enforces strict mathematical bounds on parallel worker allocation and session-scoped maintenance_work_mem during pgvector HNSW index builds in Postgres 17. Prevents container OOM terminates by calculating max worker memory against container cgroup limits before triggering asynchronous index builds on high-cardinality embedding tables.

Prohibited Anti-Pattern: Global Unbounded maintenance_work_mem and max_parallel_maintenance_workers Leading to Container Cgroup OOM Kills During HNSW Construction

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 Bounded-Memory Parallel HNSW Index Build Guardrail - Enforces strict mathematical bounds on parallel worker allocation and session-scoped maintenance_work_mem during pgvector HNSW index builds in Postgres 17. Prevents container OOM terminates by calculating max worker memory against container cgroup limits before triggering asynchronous index builds on high-cardinality embedding tables.
globs: src/database/migrations/**/*.ts, src/database/migrations/**/*.sql, src/infrastructure/db/**/*.ts, src/jobs/index-maintenance/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-hnsw-parallel-memory-budget-standard"
---

# Postgres 17 Bounded-Memory Parallel HNSW Index Build Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Global Unbounded maintenance_work_mem and max_parallel_maintenance_workers Leading to Container Cgroup OOM Kills During HNSW Construction

## System Constraints
You are a Principal Engineer enforcing Postgres 17 Memory-Bounded Parallel Index Build for pgvector. Mandate invariant mathematical partitioning of maintenance memory across worker processes to eliminate container OOM kills while indexing high-cardinality embedding tables.

## Strict Guidelines
- Engineers MUST ALWAYS scope `maintenance_work_mem` and `max_parallel_maintenance_workers` inside an isolated transaction or session block when executing `CREATE INDEX CONCURRENTLY ... USING hnsw`.
- Engineers MUST NEVER execute HNSW indexing with global server-level `maintenance_work_mem` scaled directly to container limit, as Postgres allocates `maintenance_work_mem` per worker in parallel index builds.
- Engineers MUST enforce the invariant formula: `(maintenance_work_mem * (max_parallel_maintenance_workers + 1)) <= (container_memory_limit - postgres_shared_buffers - os_headroom)`.
- Code SHOULD verify available OS cgroup memory dynamically via runtime queries or infrastructure constraints before provisioning workers for large vector dimensions (e.g., 1536d, 3072d).
- Services MUST execute vector indexing outside of user-facing transactions, using background migration runners with automatic worker fallback when memory pressure exceeds 75% cgroup threshold.
- Components NEVER issue unbounded `CONCURRENTLY` index builds on vector tables without setting `statement_timeout = '0'` and an explicit session-level bounded memory allocation.

## Bad vs Good Implementations
### Session-Bounded Dynamic Parallel HNSW Index Build Runner

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: maintenance_work_mem is assigned per parallel maintenance worker in Postgres 17 during parallel HNSW builds. Multiplying large values by the worker count quickly breaches the physical container cgroup memory limits, triggering the Linux OOM-killer.
```typescript
import { Client } from 'pg';

export async function buildVectorIndexUnsafe(client: Client): Promise<void> {
  // FAILS: Assumes maintenance_work_mem is shared across all parallel workers.
  // In Postgres 17, parallel HNSW builds allocate up to maintenance_work_mem PER WORKER.
  // With 4 workers on an 8GB container with 4GB maintenance_work_mem,
  // memory consumption hits: 4GB * (4 + 1) = 20GB -> Instant cgroup OOM Kill.
  await client.query(`SET maintenance_work_mem = '4GB';`);
  await client.query(`SET max_parallel_maintenance_workers = 4;`);
  await client.query(`
    CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_embeddings_hnsw
    ON document_embeddings USING hnsw (embedding vector_cosine_ops)
    WITH (m = 16, ef_construction = 64);
  `);
}
```

#### Production Standard (Correct)
> **Standard**: Mathematically divides the available cgroup memory (excluding shared_buffers and safety buffers) across the parallel maintenance workers plus the leader process, enforcing strict session-scoped containment with deterministic cleanup.
```typescript
import { Client } from 'pg';

interface MemoryBudgetConfig {
  containerAvailableRamMb: number;
  sharedBuffersMb: number;
  safetyHeadroomMb: number;
  maxParallelWorkers: number;
}

export function calculateSafeWorkerMemory(config: MemoryBudgetConfig): { workerMemMb: number; workers: number } {
  const usablePoolMb = config.containerAvailableRamMb - config.sharedBuffersMb - config.safetyHeadroomMb;
  if (usablePoolMb <= 256) {
    throw new Error(`Insufficient headroom (${usablePoolMb}MB) for HNSW parallel index build.`);
  }
  // Formula: Usable Pool / (Workers + Leader Process)
  const totalProcesses = config.maxParallelWorkers + 1;
  const workerMemMb = Math.floor(usablePoolMb / totalProcesses);
  return { workerMemMb: Math.max(workerMemMb, 64), workers: config.maxParallelWorkers };
}

export async function buildVectorIndexSafely(client: Client, budget: MemoryBudgetConfig): Promise<void> {
  const { workerMemMb, workers } = calculateSafeWorkerMemory(budget);

  // Bound memory explicitly to the dedicated session
  await client.query(`SET statement_timeout = 0;`);
  await client.query(`SET max_parallel_maintenance_workers = ${workers};`);
  await client.query(`SET maintenance_work_mem = '${workerMemMb}MB';`);

  try {
    await client.query(`
      CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_embeddings_hnsw
      ON document_embeddings USING hnsw (embedding vector_cosine_ops)
      WITH (m = 16, ef_construction = 64);
    `);
  } finally {
    await client.query(`RESET maintenance_work_mem;`);
    await client.query(`RESET max_parallel_maintenance_workers;`);
    await client.query(`RESET statement_timeout;`);
  }
}
```

Recommended MCP Servers (mcp-serverlist.com)

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

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Postgres 17 Bounded-Memory Parallel HNSW Index Build Guardrail

Postgres 17 Bounded-Memory Parallel HNSW Index Build Guardrail defines an industrial-grade behavioral standard for backend engineering. Enforces strict mathematical bounds on parallel worker allocation and session-scoped maintenance_work_mem during pgvector HNSW index builds in Postgres 17. Prevents container OOM terminates by calculating max worker memory against container cgroup limits before triggering asynchronous index builds on high-cardinality embedding tables. 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 max_parallel_maintenance_workers Leading to Container Cgroup OOM Kills During HNSW Construction 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 max_parallel_maintenance_workers Leading to Container Cgroup OOM Kills During HNSW ConstructionCritical
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 scope `maintenance_work_mem` and `max_parallel_maintenance_workers` inside an isolated transaction or session block when executing `CREATE INDEX CONCURRENTLY ... USING hnsw`.

Engineers MUST ALWAYS scope `maintenance_work_mem` and `max_parallel_maintenance_workers` inside an isolated transaction or session block when executing `CREATE INDEX CONCURRENTLY ... USING hnsw`.

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 NEVER execute HNSW indexing with global server-level `maintenance_work_mem` scaled directly to container limit, as Postgres allocates `maintenance_work_mem` per worker in parallel index builds.

Engineers MUST NEVER execute HNSW indexing with global server-level `maintenance_work_mem` scaled directly to container limit, as Postgres allocates `maintenance_work_mem` per worker in parallel index builds.

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 enforce the invariant formula

Engineers MUST enforce the invariant formula: `(maintenance_work_mem * (max_parallel_maintenance_workers + 1)) <= (container_memory_limit - postgres_shared_buffers - os_headroom)`.

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 SHOULD verify available OS cgroup memory dynamically via runtime queries or infrastructure constraints before provisioning workers for large vector dimensions (e.g., 1536d, 3072d).

Code SHOULD verify available OS cgroup memory dynamically via runtime queries or infrastructure constraints before provisioning workers for large vector dimensions (e.g., 1536d, 3072d).

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

Services MUST execute vector indexing outside of user-facing transactions, using background migration runners with automatic worker fallback when memory pressure exceeds 75% cgroup threshold.

Services MUST execute vector indexing outside of user-facing transactions, using background migration runners with automatic worker fallback when memory pressure exceeds 75% cgroup threshold.

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 issue unbounded `CONCURRENTLY` index builds on vector tables without setting `statement_timeout = '0'` and an explicit session-level bounded memory allocation.

Components NEVER issue unbounded `CONCURRENTLY` index builds on vector tables without setting `statement_timeout = '0'` and an explicit session-level bounded memory allocation.

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 Bounded-Memory Parallel HNSW Index Build Guardrail critical for AI coding assistants?

Enforces strict mathematical bounds on parallel worker allocation and session-scoped maintenance_work_mem during pgvector HNSW index builds in Postgres 17. Prevents container OOM terminates by calculating max worker memory against container cgroup limits before triggering asynchronous index builds on high-cardinality embedding tables. 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 max_parallel_maintenance_workers Leading to Container Cgroup OOM Kills During HNSW Construction) 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