This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: pg17-hnsw-directio-concurrent-reindexing

Postgres 17 Concurrent Direct I/O HNSW Vector Indexing Guardrails

Governs shared memory buffer allocation, session-scoped maintenance work memory, and WAL generation thresholds during concurrent high-dimensional HNSW vector index builds. Prevents backend connection stalls and replication lag caused by unthrottled I/O and shared buffer churn during pgvector index creation or re-indexing in PostgreSQL 17.

Prohibited Anti-Pattern: Global maintenance_work_mem escalation and unthrottled CONCURRENTLY index builds exhausting shared buffers and stalling connection pools.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 Concurrent Direct I/O HNSW Vector Indexing Guardrails - Governs shared memory buffer allocation, session-scoped maintenance work memory, and WAL generation thresholds during concurrent high-dimensional HNSW vector index builds. Prevents backend connection stalls and replication lag caused by unthrottled I/O and shared buffer churn during pgvector index creation or re-indexing in PostgreSQL 17.
globs: src/database/**/*.ts, src/migrations/**/*.ts, src/services/**/*.ts, prisma/migrations/**/*.sql
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pg17-hnsw-directio-concurrent-reindexing"
---

# Postgres 17 Concurrent Direct I/O HNSW Vector Indexing Guardrails

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Global maintenance_work_mem escalation and unthrottled CONCURRENTLY index builds exhausting shared buffers and stalling connection pools.

## System Constraints
You are a Principal Database Architect enforcing strict production standards for pgvector and PostgreSQL 17 Direct I/O concurrent index generation. You must mandate localized session-level buffer limits, explicit lock timeouts, throttled parallel workers, and strict separation between transactional connection pools and migration tasks to prevent shared buffer cache eviction and WAL sender saturation.

## Strict Guidelines
- Engineers MUST ALWAYS execute `CREATE INDEX CONCURRENTLY` or `REINDEX INDEX CONCURRENTLY` in an isolated administrative session outside any active transaction block (`BEGIN ... COMMIT`).
- Engineers MUST configure session-scoped `maintenance_work_mem` and `max_parallel_maintenance_workers` prior to indexing, rather than altering global database cluster parameters.
- Engineers MUST NOT allow concurrent HNSW index builds on high-dimensional vectors (>512 dims) without configuring `vacuum_cost_limit` and `vacuum_cost_delay` or equivalent session throttles to protect the WAL pipeline.
- Services MUST acquire a dedicated standalone non-pooled administrative client with explicit `statement_timeout = 0` and aggressive `lock_timeout` (<= 5000ms) to prevent blocking the query planner during table share-lock acquisition phases.
- Components NEVER execute concurrent vector re-indexing concurrently across multiple partitions or collections simultaneously; tasks must be serialized across tables to avoid starving PostgreSQL 17 `io_direct` worker queues.

## Bad vs Good Implementations
### Unsafe global DDL execution vs. Safe Session-Scoped Throttled HNSW Concurrent Indexing

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Executing non-concurrent HNSW index creation inside a transaction block takes an ACCESS EXCLUSIVE lock on the table, blocking all production traffic. Failing to set session-level maintenance work memory and parallel worker limits forces the index build to either thrash disk or evict live cache lines in PostgreSQL 17 shared buffers, stalling active pool connections.
```typescript
import { Pool } from 'pg';

export async function reindexDocumentEmbeddings(pool: Pool): Promise<void> {
  // Anti-pattern: Executed on shared app pool inside a transaction, unconstrained memory
  const client = await pool.connect();
  try {
    await client.query('BEGIN');
    // Locks the table against reads/writes, saturates shared_buffers with vector pages
    await client.query(`
      CREATE INDEX hnsw_doc_embedding_idx 
      ON document_embeddings 
      USING hnsw (embedding vector_cosine_ops)
      WITH (m = 32, ef_construction = 200);
    `);
    await client.query('COMMIT');
  } catch (error) {
    await client.query('ROLLBACK');
    throw error;
  } finally {
    client.release();
  }
}
```

#### Production Standard (Correct)
> **Standard**: The safe pattern uses an isolated client instance without transaction wrapping, sets strict `lock_timeout` to avoid holding waiting locks on production tables, scopes `maintenance_work_mem` strictly to the session, constrains maintenance workers, and bounds I/O with `vacuum_cost_delay` to protect primary WAL streaming buffers in Postgres 17.
```typescript
import { Client } from 'pg';

interface VectorIndexConfig {
  connectionString: string;
  m?: number;
  efConstruction?: number;
  maxWorkers?: number;
  sessionMemMb?: number;
}

export async function reindexDocumentEmbeddingsSafely(config: VectorIndexConfig): Promise<void> {
  // Dedicated unpooled admin client to prevent connection pool starvation
  const client = new Client({ connectionString: config.connectionString });
  await client.connect();

  const m = config.m ?? 16;
  const efConstruction = config.efConstruction ?? 128;
  const maxWorkers = config.maxWorkers ?? 2;
  const sessionMemMb = config.sessionMemMb ?? 2048;

  try {
    // Fail fast if lock cannot be acquired during catalog acquisition phases
    await client.query('SET lock_timeout = \'5s\';');
    await client.query('SET statement_timeout = 0;');
    
    // Constrain resource utilization strictly to this maintenance backend
    await client.query(`SET maintenance_work_mem = '${sessionMemMb}MB';`);
    await client.query(`SET max_parallel_maintenance_workers = ${maxWorkers};`);
    
    // Throttle I/O generation to avoid WAL sender replication lag
    await client.query('SET vacuum_cost_delay = 2;');
    await client.query('SET vacuum_cost_limit = 1000;');

    // CONCURRENTLY cannot run inside a transaction block
    await client.query(`
      CREATE INDEX CONCURRENTLY IF NOT EXISTS hnsw_doc_embedding_idx 
      ON document_embeddings 
      USING hnsw (embedding vector_cosine_ops)
      WITH (m = ${m}, ef_construction = ${efConstruction});
    `);
  } finally {
    await client.end();
  }
}
```

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 Concurrent Direct I/O HNSW Vector Indexing Guardrails

Postgres 17 Concurrent Direct I/O HNSW Vector Indexing Guardrails defines an industrial-grade behavioral standard for backend engineering. Governs shared memory buffer allocation, session-scoped maintenance work memory, and WAL generation thresholds during concurrent high-dimensional HNSW vector index builds. Prevents backend connection stalls and replication lag caused by unthrottled I/O and shared buffer churn during pgvector index creation or re-indexing in PostgreSQL 17. 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 maintenance_work_mem escalation and unthrottled CONCURRENTLY index builds exhausting shared buffers and stalling connection pools. 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 maintenance_work_mem escalation and unthrottled CONCURRENTLY index builds exhausting shared buffers and stalling connection pools.Critical
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 execute `CREATE INDEX CONCURRENTLY` or `REINDEX INDEX CONCURRENTLY` in an isolated administrative session outside any active transaction block (`BEGIN ... COMMIT`).

Engineers MUST ALWAYS execute `CREATE INDEX CONCURRENTLY` or `REINDEX INDEX CONCURRENTLY` in an isolated administrative session outside any active transaction block (`BEGIN ... COMMIT`).

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 configure session-scoped `maintenance_work_mem` and `max_parallel_maintenance_workers` prior to indexing, rather than altering global database cluster parameters.

Engineers MUST configure session-scoped `maintenance_work_mem` and `max_parallel_maintenance_workers` prior to indexing, rather than altering global database cluster parameters.

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 concurrent HNSW index builds on high-dimensional vectors (>512 dims) without configuring `vacuum_cost_limit` and `vacuum_cost_delay` or equivalent session throttles to protect the WAL pipeline.

Engineers MUST NOT allow concurrent HNSW index builds on high-dimensional vectors (>512 dims) without configuring `vacuum_cost_limit` and `vacuum_cost_delay` or equivalent session throttles to protect the WAL pipeline.

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 acquire a dedicated standalone non-pooled administrative client with explicit `statement_timeout = 0` and aggressive `lock_timeout` (<= 5000ms) to prevent blocking the query planner during table share-lock acquisition phases.

Services MUST acquire a dedicated standalone non-pooled administrative client with explicit `statement_timeout = 0` and aggressive `lock_timeout` (<= 5000ms) to prevent blocking the query planner during table share-lock acquisition phases.

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

Components NEVER execute concurrent vector re-indexing concurrently across multiple partitions or collections simultaneously

Components NEVER execute concurrent vector re-indexing concurrently across multiple partitions or collections simultaneously; tasks must be serialized across tables to avoid starving PostgreSQL 17 `io_direct` worker queues.

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 Concurrent Direct I/O HNSW Vector Indexing Guardrails critical for AI coding assistants?

Governs shared memory buffer allocation, session-scoped maintenance work memory, and WAL generation thresholds during concurrent high-dimensional HNSW vector index builds. Prevents backend connection stalls and replication lag caused by unthrottled I/O and shared buffer churn during pgvector index creation or re-indexing in PostgreSQL 17. 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 maintenance_work_mem escalation and unthrottled CONCURRENTLY index builds exhausting shared buffers and stalling connection pools.) 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