This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: pgvector-quantized-in-memory-residency-invariant

PostgreSQL 17 pgvector Quantized In-Memory Index Residency Invariant

Enforces that all HNSW and IVFFlat vector indexes deploy 1-byte scalar or binary quantization with mathematical bounds verification against shared_buffers. Graph traversal during approximate nearest neighbor search requires random pointer chasing across non-contiguous 8KB pages; if the working index set exceeds shared_buffers, query latency degrades by orders of magnitude due to random I/O stall cascades. All database deployments and migrations MUST prove index memory residency prior to cutover and enforce pg_prewarm at node initialization.

Prohibited Anti-Pattern: Unquantized Float32 HNSW Vector Indexes Spilling to Disk Causing Catastrophic I/O Storms

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 pgvector Quantized In-Memory Index Residency Invariant - Enforces that all HNSW and IVFFlat vector indexes deploy 1-byte scalar or binary quantization with mathematical bounds verification against shared_buffers. Graph traversal during approximate nearest neighbor search requires random pointer chasing across non-contiguous 8KB pages; if the working index set exceeds shared_buffers, query latency degrades by orders of magnitude due to random I/O stall cascades. All database deployments and migrations MUST prove index memory residency prior to cutover and enforce pg_prewarm at node initialization.
globs: src/db/**/*.ts, src/migrations/**/*.sql, src/repositories/**/*.ts, src/services/vector/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pgvector-quantized-in-memory-residency-invariant"
---

# PostgreSQL 17 pgvector Quantized In-Memory Index Residency Invariant

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unquantized Float32 HNSW Vector Indexes Spilling to Disk Causing Catastrophic I/O Storms

## System Constraints
You are a Principal Software Architect enforcing the PostgreSQL 17 pgvector Quantized In-Memory Index Residency Invariant. Vector search graph traversals across HNSW indexes exhibit near-zero spatial locality; any page fault incurs severe NVMe/EBS random read penalties. You must strictly mandate that all production vector columns and indexes utilize 1-byte scalar quantization (int8/halfvec) or binary quantization (bit), ensure calculated index footprints remain within 40% of allocated shared_buffers, automate prewarming via pg_prewarm, and enforce two-phase retrieval (quantized coarse search with float32 re-ranking). Reject any migrations or queries violating these invariants.

## Strict Guidelines
- Engineers MUST ALWAYS use 1-byte scalar quantization (`halfvec` or int8 arrays) or binary quantization (`bit`) for high-dimensional vector indexes (>384 dimensions) at scale (>100,000 vectors).
- Engineers MUST NOT create full `vector(1536)` or unquantized float32 HNSW indexes in production environments without an automated proof script demonstrating index size <= 40% of `shared_buffers`.
- Code SHOULD enforce two-stage retrieval: filter the initial candidate set using quantized distance operators (`<#>`, `<~>`), followed by re-ranking only top-k candidates against unquantized float vectors in application memory or Postgres temporary buffers.
- Migrations MUST include an automated assertion querying `pg_relation_size` against `current_setting('shared_buffers')` to abort execution if index residency bounds are breached.
- Services MUST execute `SELECT pg_prewarm(index_relname, 'buffer', 'main')` immediately following database restart or deployment to eliminate cold-cache page fault spikes.
- Components NEVER execute concurrent parallel index builds (`maintenance_work_mem`) without bounding work memory to prevent out-of-memory kernel reaps.

## Bad vs Good Implementations
### Enforcing Quantized Vector Index Migration with Memory Residency Assertion

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Creating a full float32 (4-byte) vector index on 1536 dimensions requires ~8KB per vector in HNSW graphs. At 1M+ rows, index size rapidly breaches available shared_buffers, causing HNSW graph traversals to block on random disk I/O reads, increasing p99 latencies from 5ms to 1,200ms.
```typescript
import { Kysely, sql } from 'kysely';

export async function up(db: Kysely<unknown>): Promise<void> {
  // ANTI-PATTERN: Unquantized 1536-dim vector creates a ~12GB HNSW index on 1M rows.
  // This immediately evicts the entire buffer cache on modest instances,
  // inducing random page-fault I/O storms on every vector search.
  await sql`
    CREATE TABLE documents (
      id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
      content TEXT NOT NULL,
      embedding vector(1536) NOT NULL
    );
  `.execute(db);

  await sql`
    CREATE INDEX idx_documents_embedding_hnsw 
    ON documents USING hnsw (embedding vector_cosine_ops)
    WITH (m = 16, ef_construction = 64);
  `.execute(db);
}
```

#### Production Standard (Correct)
> **Standard**: Quantizing embeddings to halfvec halves memory consumption while maintaining >99% recall. Generating a stored quantized representation and enforcing an inline migration verification against shared_buffers prevents index thrashing. Automated invocation of pg_prewarm guarantees all index nodes reside in RAM upon deployment.
```typescript
import { Kysely, sql } from 'kysely';

export async function up(db: Kysely<unknown>): Promise<void> {
  // INVARIANT: Quantize vector representation using halfvec or binary quantization.
  // Store full float32 only for final re-ranking, index against halfvec (16-bit) or 8-bit scalar.
  await sql`
    CREATE EXTENSION IF NOT EXISTS vector;
    CREATE EXTENSION IF NOT EXISTS pg_prewarm;

    CREATE TABLE documents (
      id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
      content TEXT NOT NULL,
      embedding_full vector(1536) NOT NULL,
      embedding_quantized halfvec(1536) GENERATED ALWAYS AS (embedding_full::halfvec(1536)) STORED
    );
  `.execute(db);

  await sql`
    CREATE INDEX idx_documents_embedding_quantized_hnsw 
    ON documents USING hnsw (embedding_quantized halfvec_cosine_ops)
    WITH (m = 16, ef_construction = 64);
  `.execute(db);

  // INVARIANT: Verify index memory residency safety (< 40% of shared_buffers)
  const residencyCheck = await sql<{
    is_safe: boolean;
    index_size: string;
    shared_buffers: string;
  }>`
    SELECT 
      (pg_relation_size('idx_documents_embedding_quantized_hnsw') < (pg_size_bytes(current_setting('shared_buffers')) * 0.40)) AS is_safe,
      pg_size_pretty(pg_relation_size('idx_documents_embedding_quantized_hnsw')) AS index_size,
      current_setting('shared_buffers') AS shared_buffers;
  `.execute(db);

  const result = residencyCheck.rows[0];
  if (result && !result.is_safe) {
    throw new Error(
      `Index residency violation: Index size (${result.index_size}) exceeds 40% of shared_buffers (${result.shared_buffers}). Aborting migration.`
    );
  }

  // INVARIANT: Pre-populate buffer cache to guarantee zero cold-start disk reads
  await sql`SELECT pg_prewarm('idx_documents_embedding_quantized_hnsw', 'buffer', 'main');`.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 Quantized In-Memory Index Residency Invariant

PostgreSQL 17 pgvector Quantized In-Memory Index Residency Invariant defines an industrial-grade behavioral standard for backend engineering. Enforces that all HNSW and IVFFlat vector indexes deploy 1-byte scalar or binary quantization with mathematical bounds verification against shared_buffers. Graph traversal during approximate nearest neighbor search requires random pointer chasing across non-contiguous 8KB pages; if the working index set exceeds shared_buffers, query latency degrades by orders of magnitude due to random I/O stall cascades. All database deployments and migrations MUST prove index memory residency prior to cutover and enforce pg_prewarm at node initialization. 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 Float32 HNSW Vector Indexes Spilling to Disk Causing Catastrophic I/O Storms 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 Float32 HNSW Vector Indexes Spilling to Disk Causing Catastrophic I/O StormsCritical
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 use 1-byte scalar quantization (`halfvec` or int8 arrays) or binary quantization (`bit`) for high-dimensional vector indexes (>384 dimensions) at scale (>100,000 vectors).

Engineers MUST ALWAYS use 1-byte scalar quantization (`halfvec` or int8 arrays) or binary quantization (`bit`) for high-dimensional vector indexes (>384 dimensions) at scale (>100,000 vectors).

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 full `vector(1536)` or unquantized float32 HNSW indexes in production environments without an automated proof script demonstrating index size <= 40% of `shared_buffers`.

Engineers MUST NOT create full `vector(1536)` or unquantized float32 HNSW indexes in production environments without an automated proof script demonstrating index size <= 40% of `shared_buffers`.

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 two-stage retrieval

Code SHOULD enforce two-stage retrieval: filter the initial candidate set using quantized distance operators (`<#>`, `<~>`), followed by re-ranking only top-k candidates against unquantized float vectors in application memory or Postgres temporary buffers.

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 include an automated assertion querying `pg_relation_size` against `current_setting('shared_buffers')` to abort execution if index residency bounds are breached.

Migrations MUST include an automated assertion querying `pg_relation_size` against `current_setting('shared_buffers')` to abort execution if index residency bounds are breached.

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 `SELECT pg_prewarm(index_relname, 'buffer', 'main')` immediately following database restart or deployment to eliminate cold-cache page fault spikes.

Services MUST execute `SELECT pg_prewarm(index_relname, 'buffer', 'main')` immediately following database restart or deployment to eliminate cold-cache page fault spikes.

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 concurrent parallel index builds (`maintenance_work_mem`) without bounding work memory to prevent out-of-memory kernel reaps.

Components NEVER execute concurrent parallel index builds (`maintenance_work_mem`) without bounding work memory to prevent out-of-memory kernel reaps.

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 Quantized In-Memory Index Residency Invariant critical for AI coding assistants?

Enforces that all HNSW and IVFFlat vector indexes deploy 1-byte scalar or binary quantization with mathematical bounds verification against shared_buffers. Graph traversal during approximate nearest neighbor search requires random pointer chasing across non-contiguous 8KB pages; if the working index set exceeds shared_buffers, query latency degrades by orders of magnitude due to random I/O stall cascades. All database deployments and migrations MUST prove index memory residency prior to cutover and enforce pg_prewarm at node initialization. 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 Float32 HNSW Vector Indexes Spilling to Disk Causing Catastrophic I/O Storms) 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