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

pgvector 0.8 HNSW Halfvec Quantization & Autovacuum Isolation Guardrail

Mandates 16-bit scalar quantization (halfvec) for high-dimensional vector embeddings and strict table-level autovacuum throttling and worker isolation in PostgreSQL with pgvector 0.8+. Prevents severe buffer pool cache thrashing, out-of-memory kernel kills, and catalog/index lock contention under high-throughput concurrent vector ingestion.

Prohibited Anti-Pattern: Unquantized FP32 HNSW indexing with default shared autovacuum worker limits during concurrent embeddings ingestion.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: pgvector 0.8 HNSW Halfvec Quantization & Autovacuum Isolation Guardrail - Mandates 16-bit scalar quantization (halfvec) for high-dimensional vector embeddings and strict table-level autovacuum throttling and worker isolation in PostgreSQL with pgvector 0.8+. Prevents severe buffer pool cache thrashing, out-of-memory kernel kills, and catalog/index lock contention under high-throughput concurrent vector ingestion.
globs: src/**/*.ts, migrations/**/*.sql, src/db/**/*.ts, src/database/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pgvector-hnsw-halfvec-vacuum-guardrail"
---

# pgvector 0.8 HNSW Halfvec Quantization & Autovacuum Isolation Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unquantized FP32 HNSW indexing with default shared autovacuum worker limits during concurrent embeddings ingestion.

## System Constraints
You are a Principal Software Architect enforcing pgvector 0.8+ production invariants. You must reject any raw 32-bit floating-point (vector) HNSW indexes on high-dimensional vectors (>512 dimensions) where 16-bit half-precision (halfvec) is viable, and require tuned autovacuum storage parameters on vector tables to eliminate lock starvation and buffer pool bloat during concurrent ingestion.

## Strict Guidelines
- Engineers MUST ALWAYS store or index high-dimensional embeddings (>= 768 dimensions) using pgvector 0.8+ `halfvec` (16-bit float) instead of unquantized 32-bit `vector` to halve working set memory and prevent buffer pool thrashing.
- Engineers MUST NOT create an HNSW index on a production embedding table without configuring explicit table-level autovacuum storage parameters (`autovacuum_vacuum_scale_factor`, `autovacuum_vacuum_cost_limit`, `autovacuum_vacuum_cost_delay`).
- Code SHOULD enforce batch inserts with deterministic chunk sizing and set a session-level `maintenance_work_mem` when executing concurrent HNSW index builds.
- Services MUST route concurrent writes into tables with tuned `fillfactor` (e.g., 90) to accommodate HOT (Heap-Only Tuples) updates and mitigate write-amplification lock contention on HNSW graph leaves.
- Components NEVER execute raw unbounded HNSW vector similarity queries without capping `hnsw.ef_search` to safe system boundaries to protect PostgreSQL worker process memory limits.

## Bad vs Good Implementations
### PostgreSQL Vector Migration: Unquantized FP32 & Default Vacuum vs. Halfvec & Tuned Vacuum Isolation

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Creating a standard 32-bit vector HNSW index over 1536-dimensional embeddings without table-level autovacuum configuration consumes excessive buffer pool space, causing severe I/O degradation and deadlocks during concurrent ingestion.
```typescript
import { Kysely, sql } from 'kysely';

export async function up(db: Kysely<unknown>): Promise<void> {
  // Flaw 1: Stores 1536-dim embeddings as full 32-bit float vector
  // Flaw 2: HNSW index consumes massive RAM (6GB+ per 1M rows), thrashing buffer pool
  // Flaw 3: Default autovacuum causes massive bloat and lock contention during bulk writes
  await sql`
    CREATE TABLE document_embeddings (
      id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
      document_id UUID NOT NULL,
      embedding vector(1536) NOT NULL
    );

    CREATE INDEX idx_embeddings_hnsw
    ON document_embeddings
    USING hnsw (embedding vector_cosine_ops);
  `.execute(db);
}
```

#### Production Standard (Correct)
> **Standard**: Using halfvec(1536) halves RAM usage while maintaining ~99% recall. Explicit table-level autovacuum parameters prevent deadlocks and graph fragmentation during concurrent vector ingestion.
```typescript
import { Kysely, sql } from 'kysely';

export async function up(db: Kysely<unknown>): Promise<void> {
  // Invariant 1: Use halfvec (16-bit float) to cut index RAM by 50% without meaningful recall loss
  // Invariant 2: Configure aggressive, isolated autovacuum to reclaim dead tuples under heavy writes
  // Invariant 3: Set fillfactor < 100 to enable HOT updates and reduce leaf lock contention
  await sql`
    CREATE TABLE document_embeddings (
      id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
      document_id UUID NOT NULL,
      embedding halfvec(1536) NOT NULL
    ) WITH (
      fillfactor = 90,
      autovacuum_vacuum_scale_factor = 0.05,
      autovacuum_vacuum_cost_limit = 2000,
      autovacuum_vacuum_cost_delay = 2
    );

    CREATE INDEX idx_embeddings_hnsw_halfvec
    ON document_embeddings
    USING hnsw (embedding halfvec_cosine_ops)
    WITH (m = 16, ef_construction = 128);
  `.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: pgvector 0.8 HNSW Halfvec Quantization & Autovacuum Isolation Guardrail

pgvector 0.8 HNSW Halfvec Quantization & Autovacuum Isolation Guardrail defines an industrial-grade behavioral standard for backend engineering. Mandates 16-bit scalar quantization (halfvec) for high-dimensional vector embeddings and strict table-level autovacuum throttling and worker isolation in PostgreSQL with pgvector 0.8+. Prevents severe buffer pool cache thrashing, out-of-memory kernel kills, and catalog/index lock contention under high-throughput concurrent vector ingestion. 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: Unquantized FP32 HNSW indexing with default shared autovacuum worker limits during concurrent embeddings ingestion. 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 DriftUnquantized FP32 HNSW indexing with default shared autovacuum worker limits during concurrent embeddings ingestion.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 store or index high-dimensional embeddings (>= 768 dimensions) using pgvector 0.8+ `halfvec` (16-bit float) instead of unquantized 32-bit `vector` to halve working set memory and prevent buffer pool thrashing.

Engineers MUST ALWAYS store or index high-dimensional embeddings (>= 768 dimensions) using pgvector 0.8+ `halfvec` (16-bit float) instead of unquantized 32-bit `vector` to halve working set memory and prevent buffer pool thrashing.

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 create an HNSW index on a production embedding table without configuring explicit table-level autovacuum storage parameters (`autovacuum_vacuum_scale_factor`, `autovacuum_vacuum_cost_limit`, `autovacuum_vacuum_cost_delay`).

Engineers MUST NOT create an HNSW index on a production embedding table without configuring explicit table-level autovacuum storage parameters (`autovacuum_vacuum_scale_factor`, `autovacuum_vacuum_cost_limit`, `autovacuum_vacuum_cost_delay`).

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

Code SHOULD enforce batch inserts with deterministic chunk sizing and set a session-level `maintenance_work_mem` when executing concurrent HNSW index builds.

Code SHOULD enforce batch inserts with deterministic chunk sizing and set a session-level `maintenance_work_mem` when executing concurrent HNSW index builds.

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 route concurrent writes into tables with tuned `fillfactor` (e.g., 90) to accommodate HOT (Heap-Only Tuples) updates and mitigate write-amplification lock contention on HNSW graph leaves.

Services MUST route concurrent writes into tables with tuned `fillfactor` (e.g., 90) to accommodate HOT (Heap-Only Tuples) updates and mitigate write-amplification lock contention on HNSW graph leaves.

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 raw unbounded HNSW vector similarity queries without capping `hnsw.ef_search` to safe system boundaries to protect PostgreSQL worker process memory limits.

Components NEVER execute raw unbounded HNSW vector similarity queries without capping `hnsw.ef_search` to safe system boundaries to protect PostgreSQL worker process memory limits.

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 Halfvec Quantization & Autovacuum Isolation Guardrail critical for AI coding assistants?

Mandates 16-bit scalar quantization (halfvec) for high-dimensional vector embeddings and strict table-level autovacuum throttling and worker isolation in PostgreSQL with pgvector 0.8+. Prevents severe buffer pool cache thrashing, out-of-memory kernel kills, and catalog/index lock contention under high-throughput concurrent vector ingestion. 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 (Unquantized FP32 HNSW indexing with default shared autovacuum worker limits during concurrent embeddings ingestion.) 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