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

pgvector 0.8 HNSW Partitioned Indexing and Parallel Worker Configuration Standard

Enforces strict partition-aligned HNSW indexing, deterministic parallel index creation, and maintenance work memory allocations for pgvector 0.8+ on PostgreSQL 17. Unpartitioned global HNSW indexes on multi-million row datasets exhaust shared buffers and cause catastrophic tail latency spikes (p99 > 4000ms) during concurrent writes and background vacuuming.

Prohibited Anti-Pattern: Global Unpartitioned HNSW Vector Indexing with Default Worker and Memory Configuration

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: pgvector 0.8 HNSW Partitioned Indexing and Parallel Worker Configuration Standard - Enforces strict partition-aligned HNSW indexing, deterministic parallel index creation, and maintenance work memory allocations for pgvector 0.8+ on PostgreSQL 17. Unpartitioned global HNSW indexes on multi-million row datasets exhaust shared buffers and cause catastrophic tail latency spikes (p99 > 4000ms) during concurrent writes and background vacuuming.
globs: src/database/migrations/**/*.{ts,sql}, src/infrastructure/db/**/*.{ts,sql}, prisma/migrations/**/*.{sql}, src/repositories/**/*.{ts,js}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pgvector-08-hnsw-partitioned-maintenance-standard"
---

# pgvector 0.8 HNSW Partitioned Indexing and Parallel Worker Configuration Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Global Unpartitioned HNSW Vector Indexing with Default Worker and Memory Configuration

## System Constraints
You are a Principal Database Infrastructure Engineer enforcing strict pgvector 0.8 HNSW partitioning, indexing parameters, and maintenance session configurations on PostgreSQL 17. You prevent global vector index sprawl, unthrottled worker saturation, and under-allocated maintenance work memory during migration execution.

## Strict Guidelines
- Engineers MUST ALWAYS partition vector tables (RANGE or LIST) so individual HNSW graph indexes fit entirely into RAM and maintenance_work_mem.
- Engineers MUST NOT issue 'CREATE INDEX ... USING hnsw' directly on partitioned parent tables; indexes must be built concurrently on leaf partitions.
- Engineers MUST explicitly set 'SET LOCAL maintenance_work_mem' and 'SET LOCAL max_parallel_maintenance_workers' immediately before creating or rebuilding HNSW indexes.
- Migrations MUST specify 'm' and 'ef_construction' explicitly (e.g., WITH (m = 16, ef_construction = 64)) based on workload access patterns rather than relying on engine defaults.
- Vector search queries MUST ALWAYS filter by partition key to ensure partition pruning prevents fan-out across multiple unpruned HNSW indexes.

## Bad vs Good Implementations
### HNSW Index Creation on Partitioned Vector Table

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Creating an unpartitioned HNSW index on a global table without configuring maintenance_work_mem or max_parallel_maintenance_workers leads to single-worker bottlenecks, graph degradation, and degraded p99 search latencies exceeding thousands of milliseconds under concurrent writes.
```typescript
import { Client } from 'pg';

export async function createVectorIndex(db: Client) {
  // Anti-pattern: Creates a non-partitioned global index without tuning workers or memory
  // This triggers memory thrashing, exhausts shared buffers, and causes severe query tail latency.
  await db.query(`
    CREATE TABLE IF NOT EXISTS embeddings (
      id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
      tenant_id TEXT NOT NULL,
      embedding vector(1536)
    );
    CREATE INDEX ON embeddings USING hnsw (embedding vector_cosine_ops);
  `);
}
```

#### Production Standard (Correct)
> **Standard**: Applies session-level maintenance work memory and parallel worker controls before constructing the HNSW index on individual partitions, keeping graphs cache-resident and ensuring deterministic, low-latency concurrent index generation.
```typescript
import { Client } from 'pg';

export async function createPartitionedVectorIndex(db: Client, partitionName: string) {
  // Invariant: Partition-local HNSW index with isolated session tuning
  await db.query('BEGIN;');
  try {
    // Allocate deterministic memory and parallel workers for pgvector 0.8+ on PG 17
    await db.query(`
      SET LOCAL maintenance_work_mem = '4GB';
      SET LOCAL max_parallel_maintenance_workers = 4;
      SET LOCAL hnsw.ef_search = 100;
    `);

    // Create index concurrently on the specific partition
    await db.query(`
      CREATE INDEX CONCURRENTLY IF NOT EXISTS "idx_${partitionName}_hnsw"
      ON "${partitionName}"
      USING hnsw (embedding vector_cosine_ops)
      WITH (m = 16, ef_construction = 128);
    `);
    await db.query('COMMIT;');
  } catch (error) {
    await db.query('ROLLBACK;');
    throw error;
  }
}
```

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: pgvector 0.8 HNSW Partitioned Indexing and Parallel Worker Configuration Standard

pgvector 0.8 HNSW Partitioned Indexing and Parallel Worker Configuration Standard defines an industrial-grade behavioral standard for backend engineering. Enforces strict partition-aligned HNSW indexing, deterministic parallel index creation, and maintenance work memory allocations for pgvector 0.8+ on PostgreSQL 17. Unpartitioned global HNSW indexes on multi-million row datasets exhaust shared buffers and cause catastrophic tail latency spikes (p99 > 4000ms) during concurrent writes and background vacuuming. 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 Unpartitioned HNSW Vector Indexing with Default Worker and Memory Configuration 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 Unpartitioned HNSW Vector Indexing with Default Worker and Memory ConfigurationCritical
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 partition vector tables (RANGE or LIST) so individual HNSW graph indexes fit entirely into RAM and maintenance_work_mem.

Engineers MUST ALWAYS partition vector tables (RANGE or LIST) so individual HNSW graph indexes fit entirely into RAM and maintenance_work_mem.

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 issue 'CREATE INDEX ... USING hnsw' directly on partitioned parent tables

Engineers MUST NOT issue 'CREATE INDEX ... USING hnsw' directly on partitioned parent tables; indexes must be built concurrently on leaf partitions.

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 explicitly set 'SET LOCAL maintenance_work_mem' and 'SET LOCAL max_parallel_maintenance_workers' immediately before creating or rebuilding HNSW indexes.

Engineers MUST explicitly set 'SET LOCAL maintenance_work_mem' and 'SET LOCAL max_parallel_maintenance_workers' immediately before creating or rebuilding HNSW indexes.

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 MUST specify 'm' and 'ef_construction' explicitly (e.g., WITH (m = 16, ef_construction = 64)) based on workload access patterns rather than relying on engine defaults.

Migrations MUST specify 'm' and 'ef_construction' explicitly (e.g., WITH (m = 16, ef_construction = 64)) based on workload access patterns rather than relying on engine 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.
5

Vector search queries MUST ALWAYS filter by partition key to ensure partition pruning prevents fan-out across multiple unpruned HNSW indexes.

Vector search queries MUST ALWAYS filter by partition key to ensure partition pruning prevents fan-out across multiple unpruned HNSW indexes.

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 pgvector 0.8 HNSW Partitioned Indexing and Parallel Worker Configuration Standard critical for AI coding assistants?

Enforces strict partition-aligned HNSW indexing, deterministic parallel index creation, and maintenance work memory allocations for pgvector 0.8+ on PostgreSQL 17. Unpartitioned global HNSW indexes on multi-million row datasets exhaust shared buffers and cause catastrophic tail latency spikes (p99 > 4000ms) during concurrent writes and background vacuuming. 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 Unpartitioned HNSW Vector Indexing with Default Worker and Memory Configuration) 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

pgvector