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

Postgres 17 pgvector Partition-Local HNSW Index Memory Bounding

Enforces strict partition-local memory isolation and dynamic maintenance_work_mem budgeting when generating Hierarchical Navigable Small World (HNSW) vector graph indexes on partitioned tables in Postgres 17. Prevents host kernel out-of-memory (OOM) killer invocations by disallowing global index creation across parent partitioned tables and bounding per-worker memory during parallel backfills.

Prohibited Anti-Pattern: Global Unbounded HNSW Indexing on Parent Partitioned Tables Triggering OOM Terminations

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 pgvector Partition-Local HNSW Index Memory Bounding - Enforces strict partition-local memory isolation and dynamic maintenance_work_mem budgeting when generating Hierarchical Navigable Small World (HNSW) vector graph indexes on partitioned tables in Postgres 17. Prevents host kernel out-of-memory (OOM) killer invocations by disallowing global index creation across parent partitioned tables and bounding per-worker memory during parallel backfills.
globs: src/db/**/*.ts, src/migrations/**/*.ts, src/repositories/**/*.ts, prisma/migrations/**/*.sql
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pgvector-partition-hnsw-memory-bounding-production-standard"
---

# Postgres 17 pgvector Partition-Local HNSW Index Memory Bounding

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Global Unbounded HNSW Indexing on Parent Partitioned Tables Triggering OOM Terminations

## System Constraints
You are a Principal Database Architect and Infrastructure Reliability Engineer enforcing Postgres 17 and pgvector (0.7.0+) memory-bounding invariants. You MUST strictly ensure that all vector indexes on high-throughput partitioned embedding tables are created partition-locally with explicit session-scoped maintenance_work_mem, concurrency limits, and max_parallel_maintenance_workers boundaries to guarantee zero OOM process terminates.

## Strict Guidelines
- Engineers MUST ALWAYS create HNSW indexes on individual leaf partitions rather than executing CREATE INDEX on the root partitioned table without memory bounds.
- Engineers MUST execute `SET LOCAL maintenance_work_mem` inside an isolated transaction or dedicated session specifically sized to `(available_ram * 0.5) / max_parallel_maintenance_workers` prior to running `CREATE INDEX CONCURRENTLY`.
- Engineers MUST NOT allow unindexed active ingestion partitions; index creation MUST be staged asynchronously on closed, historical, or pre-allocated partitions before traffic cutover.
- Code SHOULD verify `m` and `ef_construction` parameters in index creation scripts: high `m` (e.g., > 64) and `ef_construction` (e.g., > 200) dramatically inflate working graph memory and MUST be constrained per partition scale.
- Services MUST execute index builds sequentially across partitions or throttle concurrency using an explicit advisory lock or worker queue to prevent aggregate maintenance memory blowouts.
- Components NEVER execute DDL index migrations through standard connection pools without dropping pool connection lifetime or resetting maintenance session variables immediately after build completion.

## Bad vs Good Implementations
### Unbounded Root Partition HNSW Indexing vs Partition-Local Bounded Ingestion Indexing

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Executing CREATE INDEX directly on a parent partitioned table cascades down to all partitions simultaneously. In pgvector on Postgres, this can spawn uncontrolled graph builds whose cumulative working graph memory exceeds system limits, causing catastrophic backend termination.
```typescript
import { Pool } from 'pg';

export async function createVectorIndexes(pool: Pool): Promise<void> {
  // ANTI-PATTERN: Creates HNSW index globally on parent table.
  // This triggers concurrent graph building across all underlying partitions,
  // ignoring per-partition boundaries and multiplying maintenance_work_mem consumption,
  // causing Linux OOM killer to terminate the Postgres backend process.
  await pool.query(`
    CREATE INDEX idx_document_embeddings_hnsw
    ON document_embeddings USING hnsw (embedding vector_cosine_ops)
    WITH (m = 64, ef_construction = 256);
  `);
}
```

#### Production Standard (Correct)
> **Standard**: The invariant solution targets the leaf partition directly, restricts concurrency via `max_parallel_maintenance_workers`, explicitly budgets `maintenance_work_mem`, builds non-blockingly using `CONCURRENTLY`, and ensures session isolation before returning the client to the connection pool.
```typescript
import { Pool, PoolClient } from 'pg';

interface PartitionIndexConfig {
  partitionTableName: string;
  indexName: string;
  maxWorkers: number;
  maintenanceMemMb: number;
  m: number;
  efConstruction: number;
}

export async function createPartitionLocalHnswIndex(
  pool: Pool,
  config: PartitionIndexConfig
): Promise<void> {
  const client: PoolClient = await pool.connect();
  try {
    // Validate identifier hygiene to prevent SQL injection in DDL
    if (!/^[a-z0-9_]+$/i.test(config.partitionTableName) || !/^[a-z0-9_]+$/i.test(config.indexName)) {
      throw new Error('Invalid partition or index identifier');
    }

    // Bound per-worker memory and limit worker count strictly to this session
    await client.query(`SET max_parallel_maintenance_workers = ${config.maxWorkers};`);
    await client.query(`SET maintenance_work_mem = '${config.maintenanceMemMb}MB';`);

    // Create index CONCURRENTLY directly on the leaf partition
    const query = `
      CREATE INDEX CONCURRENTLY IF NOT EXISTS ${config.indexName}
      ON ${config.partitionTableName} USING hnsw (embedding vector_cosine_ops)
      WITH (m = ${config.m}, ef_construction = ${config.efConstruction});
    `;
    await client.query(query);
  } finally {
    // Reset session variables before returning connection to pool
    await client.query('RESET maintenance_work_mem;');
    await client.query('RESET max_parallel_maintenance_workers;');
    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 Partition-Local HNSW Index Memory Bounding

Postgres 17 pgvector Partition-Local HNSW Index Memory Bounding defines an industrial-grade behavioral standard for backend engineering. Enforces strict partition-local memory isolation and dynamic maintenance_work_mem budgeting when generating Hierarchical Navigable Small World (HNSW) vector graph indexes on partitioned tables in Postgres 17. Prevents host kernel out-of-memory (OOM) killer invocations by disallowing global index creation across parent partitioned tables and bounding per-worker memory during parallel backfills. 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 HNSW Indexing on Parent Partitioned Tables Triggering OOM Terminations 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 HNSW Indexing on Parent Partitioned Tables Triggering OOM TerminationsCritical
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 create HNSW indexes on individual leaf partitions rather than executing CREATE INDEX on the root partitioned table without memory bounds.

Engineers MUST ALWAYS create HNSW indexes on individual leaf partitions rather than executing CREATE INDEX on the root partitioned table without memory 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.
2

Engineers MUST execute `SET LOCAL maintenance_work_mem` inside an isolated transaction or dedicated session specifically sized to `(available_ram * 0.5) / max_parallel_maintenance_workers` prior to running `CREATE INDEX CONCURRENTLY`.

Engineers MUST execute `SET LOCAL maintenance_work_mem` inside an isolated transaction or dedicated session specifically sized to `(available_ram * 0.5) / max_parallel_maintenance_workers` prior to running `CREATE INDEX CONCURRENTLY`.

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 allow unindexed active ingestion partitions

Engineers MUST NOT allow unindexed active ingestion partitions; index creation MUST be staged asynchronously on closed, historical, or pre-allocated partitions before traffic cutover.

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 `m` and `ef_construction` parameters in index creation scripts

Code SHOULD verify `m` and `ef_construction` parameters in index creation scripts: high `m` (e.g., > 64) and `ef_construction` (e.g., > 200) dramatically inflate working graph memory and MUST be constrained per partition scale.

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 index builds sequentially across partitions or throttle concurrency using an explicit advisory lock or worker queue to prevent aggregate maintenance memory blowouts.

Services MUST execute index builds sequentially across partitions or throttle concurrency using an explicit advisory lock or worker queue to prevent aggregate maintenance memory blowouts.

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 DDL index migrations through standard connection pools without dropping pool connection lifetime or resetting maintenance session variables immediately after build completion.

Components NEVER execute DDL index migrations through standard connection pools without dropping pool connection lifetime or resetting maintenance session variables immediately after build completion.

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 Partition-Local HNSW Index Memory Bounding critical for AI coding assistants?

Enforces strict partition-local memory isolation and dynamic maintenance_work_mem budgeting when generating Hierarchical Navigable Small World (HNSW) vector graph indexes on partitioned tables in Postgres 17. Prevents host kernel out-of-memory (OOM) killer invocations by disallowing global index creation across parent partitioned tables and bounding per-worker memory during parallel backfills. 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 HNSW Indexing on Parent Partitioned Tables Triggering OOM Terminations) 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