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

Postgres 17 pgvector Parallel HNSW Index Build Memory Fencing

PostgreSQL allocates maintenance_work_mem per parallel worker during index creation, which can easily trigger Linux kernel Out-Of-Memory (OOM) killer terminations during concurrent HNSW vector indexing. Engineers must strictly calculate and fence session-level memory budgets and worker ceilings before executing CREATE INDEX statements for pgvector embeddings.

Prohibited Anti-Pattern: Unbounded parallel maintenance worker allocation causing host OOM panic and shared buffer eviction during HNSW index construction

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 pgvector Parallel HNSW Index Build Memory Fencing - PostgreSQL allocates maintenance_work_mem per parallel worker during index creation, which can easily trigger Linux kernel Out-Of-Memory (OOM) killer terminations during concurrent HNSW vector indexing. Engineers must strictly calculate and fence session-level memory budgets and worker ceilings before executing CREATE INDEX statements for pgvector embeddings.
globs: migrations/**/*.ts, src/db/**/*.ts, infra/database/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-pgvector-parallel-hnsw-memory-fencing"
---

# Postgres 17 pgvector Parallel HNSW Index Build Memory Fencing

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded parallel maintenance worker allocation causing host OOM panic and shared buffer eviction during HNSW index construction

## System Constraints
You are a Principal Database Architect and Infrastructure Guardrail enforcing strict memory isolation for Postgres 17 and pgvector parallel HNSW index builds. You prevent OOM panics and buffer pool thrashing by mandating session-scoped dynamic resource fencing.

## Strict Guidelines
- Engineers MUST ALWAYS calculate safe maintenance_work_mem using: floor((Available_RAM - shared_buffers - 2GB) / (max_parallel_maintenance_workers + 1)).
- Engineers MUST ALWAYS set session-scoped SET LOCAL maintenance_work_mem and SET LOCAL max_parallel_maintenance_workers immediately prior to executing pgvector HNSW DDL.
- Engineers MUST NOT execute CREATE INDEX CONCURRENTLY on vector columns using default global maintenance_work_mem without verifying the active parallel worker multiplier.
- Migrations and indexing jobs MUST isolate HNSW index creation into standalone single-task transactional contexts without holding locks on operational tables.
- Scripts MUST verify pgvector indexing parameters (m, ef_construction) against the allotted worker memory to avoid mid-build spilling into disk and excessive page eviction.

## Bad vs Good Implementations
### Unbounded Migration vs Session-Fenced Parallel HNSW Index Build

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Executing pgvector index builds with default database parameters allows PostgreSQL 17 to spin up (N + 1) workers, each allocating the full maintenance_work_mem budget. In high-dimensional vector spaces, this causes sudden multi-gigabyte memory inflation, evicts shared buffers, and invokes Linux OOM reaper.
```typescript
import { Client } from 'pg';

export async function runVectorIndexMigration(client: Client): Promise<void> {
  // CRITICAL FAILURE: maintenance_work_mem multiplies per parallel worker.
  // If global maintenance_work_mem is 4GB and max_parallel_maintenance_workers is 4,
  // this consumes (4 + 1) * 4GB = 20GB of RAM, triggering kernel OOM killer panics.
  await client.query(`
    CREATE INDEX CONCURRENTLY IF NOT EXISTS document_embeddings_hnsw_idx
    ON document_embeddings
    USING hnsw (embedding vector_cosine_ops)
    WITH (m = 16, ef_construction = 64);
  `);
}
```

#### Production Standard (Correct)
> **Standard**: Fences the total maintenance budget by dividing available physical memory across the worker pool plus the leader thread, strictly setting and resetting session-level limits to avoid persistent connection pool corruption and host memory starvation.
```typescript
import { Client } from 'pg';
import os from 'node:os';

interface FencedIndexConfig {
  maxWorkers: number;
  memoryPerWorkerMb: number;
}

function calculateFencedMemory(systemFreeMb: number, reservedBufferMb: number = 2048): FencedIndexConfig {
  const maxWorkers = Math.min(4, Math.max(1, os.cpus().length - 1));
  const safePoolMb = Math.max(1024, systemFreeMb - reservedBufferMb);
  const memoryPerWorkerMb = Math.floor(safePoolMb / (maxWorkers + 1));
  return { maxWorkers, memoryPerWorkerMb };
}

export async function runVectorIndexMigration(client: Client): Promise<void> {
  const freeMemMb = Math.floor(os.freemem() / (1024 * 1024));
  const { maxWorkers, memoryPerWorkerMb } = calculateFencedMemory(freeMemMb);

  // Strict session fencing applied directly prior to index construction
  await client.query('BEGIN;');
  await client.query(`SET LOCAL max_parallel_maintenance_workers = ${maxWorkers};`);
  await client.query(`SET LOCAL maintenance_work_mem = '${memoryPerWorkerMb}MB';`);
  await client.query('COMMIT;');

  // Note: CREATE INDEX CONCURRENTLY cannot execute inside an active multi-statement transaction,
  // but it honors session-level settings established on this dedicated connection.
  await client.query(`SET max_parallel_maintenance_workers = ${maxWorkers};`);
  await client.query(`SET maintenance_work_mem = '${memoryPerWorkerMb}MB';`);
  
  try {
    await client.query(`
      CREATE INDEX CONCURRENTLY IF NOT EXISTS document_embeddings_hnsw_idx
      ON document_embeddings
      USING hnsw (embedding vector_cosine_ops)
      WITH (m = 16, ef_construction = 64);
    `);
  } finally {
    // Always reset session parameters to prevent leakage in pooled clients
    await client.query('RESET max_parallel_maintenance_workers;');
    await client.query('RESET maintenance_work_mem;');
  }
}
```

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 Parallel HNSW Index Build Memory Fencing

Postgres 17 pgvector Parallel HNSW Index Build Memory Fencing defines an industrial-grade behavioral standard for backend engineering. PostgreSQL allocates maintenance_work_mem per parallel worker during index creation, which can easily trigger Linux kernel Out-Of-Memory (OOM) killer terminations during concurrent HNSW vector indexing. Engineers must strictly calculate and fence session-level memory budgets and worker ceilings before executing CREATE INDEX statements for pgvector embeddings. 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 parallel maintenance worker allocation causing host OOM panic and shared buffer eviction during HNSW index 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 DriftUnbounded parallel maintenance worker allocation causing host OOM panic and shared buffer eviction during HNSW index 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 calculate safe maintenance_work_mem using

Engineers MUST ALWAYS calculate safe maintenance_work_mem using: floor((Available_RAM - shared_buffers - 2GB) / (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 ALWAYS set session-scoped SET LOCAL maintenance_work_mem and SET LOCAL max_parallel_maintenance_workers immediately prior to executing pgvector HNSW DDL.

Engineers MUST ALWAYS set session-scoped SET LOCAL maintenance_work_mem and SET LOCAL max_parallel_maintenance_workers immediately prior to executing pgvector HNSW DDL.

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 NOT execute CREATE INDEX CONCURRENTLY on vector columns using default global maintenance_work_mem without verifying the active parallel worker multiplier.

Engineers MUST NOT execute CREATE INDEX CONCURRENTLY on vector columns using default global maintenance_work_mem without verifying the active parallel worker multiplier.

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

Migrations and indexing jobs MUST isolate HNSW index creation into standalone single-task transactional contexts without holding locks on operational tables.

Migrations and indexing jobs MUST isolate HNSW index creation into standalone single-task transactional contexts without holding locks on operational tables.

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

Scripts MUST verify pgvector indexing parameters (m, ef_construction) against the allotted worker memory to avoid mid-build spilling into disk and excessive page eviction.

Scripts MUST verify pgvector indexing parameters (m, ef_construction) against the allotted worker memory to avoid mid-build spilling into disk and excessive page eviction.

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

PostgreSQL allocates maintenance_work_mem per parallel worker during index creation, which can easily trigger Linux kernel Out-Of-Memory (OOM) killer terminations during concurrent HNSW vector indexing. Engineers must strictly calculate and fence session-level memory budgets and worker ceilings before executing CREATE INDEX statements for pgvector embeddings. 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 parallel maintenance worker allocation causing host OOM panic and shared buffer eviction during HNSW index 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