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

PostgreSQL 17 pgvector HNSW Autovacuum Fencing & Query Determinism

Enforces table-level autovacuum storage parameter fencing, non-blocking index cleanup thresholds, and isolated ingest partitioning for pgvector HNSW indexes on PostgreSQL 17. Guarantees bounded p99/p99.9 cosine distance retrieval latencies by preventing worker buffer cache thrashing and relation lock contention.

Prohibited Anti-Pattern: Unthrottled Default Autovacuum Starving Real-Time HNSW Cosine Search Queries

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 pgvector HNSW Autovacuum Fencing & Query Determinism - Enforces table-level autovacuum storage parameter fencing, non-blocking index cleanup thresholds, and isolated ingest partitioning for pgvector HNSW indexes on PostgreSQL 17. Guarantees bounded p99/p99.9 cosine distance retrieval latencies by preventing worker buffer cache thrashing and relation lock contention.
globs: src/db/**/*.ts, src/migrations/**/*.ts, migrations/**/*.sql, src/repositories/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pgvector-hnsw-autovacuum-fencing-guardrail"
---

# PostgreSQL 17 pgvector HNSW Autovacuum Fencing & Query Determinism

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unthrottled Default Autovacuum Starving Real-Time HNSW Cosine Search Queries

## System Constraints
You are a Principal Database Infrastructure Engineer enforcing strict PostgreSQL 17 pgvector concurrency guardrails. Mandate granular storage-level autovacuum fencing, controlled cost-delay budgets, and session timeouts for all tables housing HNSW vector indexes to protect high-frequency cosine distance queries from maintenance worker contention.

## Strict Guidelines
- Engineers MUST ALWAYS configure table-level storage parameters (autovacuum_vacuum_cost_delay, autovacuum_vacuum_cost_limit, and autovacuum_vacuum_scale_factor) on relations storing vector columns with HNSW indexes.
- Engineers MUST NOT allow default global autovacuum settings to trigger unbounded background index sweeps on active vector tables during operational search traffic.
- Database migrations MUST explicitly set index creation parameters (m, ef_construction) alongside maintenance fencing to prevent shared buffer lock starvation during background operations.
- Services executing cosine similarity queries MUST enforce strict statement-level lock timeouts (SET LOCAL statement_timeout and lock_timeout) to immediately shed load instead of queuing behind autovacuum metadata locks.
- High-throughput vector mutations MUST be batched or partitioned into append-only staging structures so vacuum workers can clean dead tuples without locking primary HNSW entrypoints.

## Bad vs Good Implementations
### Configuring Fenced Autovacuum and HNSW Storage on pgvector Relations

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Default table storage parameters let PostgreSQL's autovacuum fire with standard aggressive thresholds. During high write rates, autovacuum workers lock buffer pages and consume maintenance I/O, degrading concurrent p99 HNSW cosine distance search queries from 5ms to >500ms.
```typescript
import { Kysely, sql } from 'kysely';

export async function up(db: Kysely<unknown>): Promise<void> {
  // Anti-pattern: Default table parameters allow autovacuum to kick in aggressively
  // during high-throughput ingest, contending with real-time cosine distance queries.
  await db.schema
    .createTable('document_embeddings')
    .addColumn('id', 'uuid', (col) => col.primaryKey().defaultTo(sql`gen_random_uuid()`))
    .addColumn('embedding', sql`vector(1536)`, (col) => col.notNull())
    .addColumn('metadata', 'jsonb')
    .execute();

  // Unbounded default HNSW index prone to vacuum lock contention
  await sql`
    CREATE INDEX idx_embeddings_hnsw ON document_embeddings 
    USING hnsw (embedding vector_cosine_ops);
  `.execute(db);
}
```

#### Production Standard (Correct)
> **Standard**: Table-level autovacuum parameters explicitly throttle the vacuum worker's I/O impact (autovacuum_vacuum_cost_delay = 10, cost_limit = 1000) and establish high threshold triggers, preventing vacuum sweeps from monopolizing buffer pools or starving concurrent HNSW traversal workers.
```typescript
import { Kysely, sql } from 'kysely';

export async function up(db: Kysely<unknown>): Promise<void> {
  // Invariant Fix: Apply strict autovacuum fencing parameters directly to the vector relation.
  // This throttles vacuum workers and shifts heavy cleanup away from real-time query paths.
  await sql`
    CREATE TABLE document_embeddings (
      id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
      embedding vector(1536) NOT NULL,
      metadata JSONB,
      created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp()
    ) WITH (
      autovacuum_vacuum_cost_delay = 10,
      autovacuum_vacuum_cost_limit = 1000,
      autovacuum_vacuum_scale_factor = 0.05,
      autovacuum_vacuum_threshold = 5000
    );
  `.execute(db);

  // Explicit HNSW parameters with concurrent creation and cosine distance operator class
  await sql`
    CREATE INDEX CONCURRENTLY idx_embeddings_hnsw ON document_embeddings 
    USING hnsw (embedding vector_cosine_ops)
    WITH (m = 16, ef_construction = 64);
  `.execute(db);
}
```

Recommended MCP Servers (mcp-serverlist.com)

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

Architectural Specification & Invariant Rationale

Architectural Invariant Design: PostgreSQL 17 pgvector HNSW Autovacuum Fencing & Query Determinism

PostgreSQL 17 pgvector HNSW Autovacuum Fencing & Query Determinism defines an industrial-grade behavioral standard for backend engineering. Enforces table-level autovacuum storage parameter fencing, non-blocking index cleanup thresholds, and isolated ingest partitioning for pgvector HNSW indexes on PostgreSQL 17. Guarantees bounded p99/p99.9 cosine distance retrieval latencies by preventing worker buffer cache thrashing and relation lock contention. 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: Unthrottled Default Autovacuum Starving Real-Time HNSW Cosine Search Queries 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 DriftUnthrottled Default Autovacuum Starving Real-Time HNSW Cosine Search QueriesCritical
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 configure table-level storage parameters (autovacuum_vacuum_cost_delay, autovacuum_vacuum_cost_limit, and autovacuum_vacuum_scale_factor) on relations storing vector columns with HNSW indexes.

Engineers MUST ALWAYS configure table-level storage parameters (autovacuum_vacuum_cost_delay, autovacuum_vacuum_cost_limit, and autovacuum_vacuum_scale_factor) on relations storing vector columns with 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.
2

Engineers MUST NOT allow default global autovacuum settings to trigger unbounded background index sweeps on active vector tables during operational search traffic.

Engineers MUST NOT allow default global autovacuum settings to trigger unbounded background index sweeps on active vector tables during operational search traffic.

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

Database migrations MUST explicitly set index creation parameters (m, ef_construction) alongside maintenance fencing to prevent shared buffer lock starvation during background operations.

Database migrations MUST explicitly set index creation parameters (m, ef_construction) alongside maintenance fencing to prevent shared buffer lock starvation during background operations.

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 executing cosine similarity queries MUST enforce strict statement-level lock timeouts (SET LOCAL statement_timeout and lock_timeout) to immediately shed load instead of queuing behind autovacuum metadata locks.

Services executing cosine similarity queries MUST enforce strict statement-level lock timeouts (SET LOCAL statement_timeout and lock_timeout) to immediately shed load instead of queuing behind autovacuum metadata locks.

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

High-throughput vector mutations MUST be batched or partitioned into append-only staging structures so vacuum workers can clean dead tuples without locking primary HNSW entrypoints.

High-throughput vector mutations MUST be batched or partitioned into append-only staging structures so vacuum workers can clean dead tuples without locking primary HNSW entrypoints.

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 PostgreSQL 17 pgvector HNSW Autovacuum Fencing & Query Determinism critical for AI coding assistants?

Enforces table-level autovacuum storage parameter fencing, non-blocking index cleanup thresholds, and isolated ingest partitioning for pgvector HNSW indexes on PostgreSQL 17. Guarantees bounded p99/p99.9 cosine distance retrieval latencies by preventing worker buffer cache thrashing and relation lock contention. 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 (Unthrottled Default Autovacuum Starving Real-Time HNSW Cosine Search Queries) 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

PostgreSQL