This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: postgres-17-pgvector-hnsw-quantization-isolation

Postgres 17 pgvector HNSW Half-Precision Quantization & Buffer Isolation Guardrail

Mandates the usage of pgvector 0.7.0+ half-precision (halfvec) scalar quantization and strict buffer isolation for HNSW vector indexes in PostgreSQL 17. Unquantized float4 vector graphs (1536+ dims) rapidly exceed shared_buffers, causing massive disk I/O thrashing and eviction of relational working sets during OLTP workloads. Enforces halfvec cast-indexing, bounded graph connectivity parameters, and iterative scan tuning to guarantee sub-10ms vector retrieval without noisy-neighbor degradation.

Prohibited Anti-Pattern: Full-Precision vector(dim) HNSW Indexing Evicting Relational Buffer Pools

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 pgvector HNSW Half-Precision Quantization & Buffer Isolation Guardrail - Mandates the usage of pgvector 0.7.0+ half-precision (halfvec) scalar quantization and strict buffer isolation for HNSW vector indexes in PostgreSQL 17. Unquantized float4 vector graphs (1536+ dims) rapidly exceed shared_buffers, causing massive disk I/O thrashing and eviction of relational working sets during OLTP workloads. Enforces halfvec cast-indexing, bounded graph connectivity parameters, and iterative scan tuning to guarantee sub-10ms vector retrieval without noisy-neighbor degradation.
globs: src/**/*.ts, migrations/**/*.sql, src/**/*.sql, src/db/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-pgvector-hnsw-quantization-isolation"
---

# Postgres 17 pgvector HNSW Half-Precision Quantization & Buffer Isolation Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Full-Precision vector(dim) HNSW Indexing Evicting Relational Buffer Pools

## System Constraints
You are a Principal Database Architect enforcing PostgreSQL 17 and pgvector performance invariants. You must prevent memory exhaustion and cache pollution caused by unquantized vector indexes. Enforce half-precision (halfvec) quantization, strict HNSW construction tuning (m <= 16, ef_construction <= 128), and iterative filtering to keep graph traversal latency strictly below 10ms while preserving relational buffer pool residency.

## Strict Guidelines
- Engineers MUST ALWAYS store or cast high-dimensional vectors (>384 dimensions) to `halfvec` (FP16) when creating HNSW indexes to reduce memory footprint by 50% without recall loss.
- Engineers MUST NOT construct HNSW indexes over unquantized `vector(1536)` columns in tables shared with high-throughput OLTP workloads.
- Database migrations SHOULD explicitly define bounded graph parameters (`m = 16`, `ef_construction = 64` or `128`) on HNSW indexes to prevent unbounded graph bloat in `shared_buffers`.
- Services MUST execute `SET LOCAL hnsw.ef_search = <bounded_value>` (typically 40-100) per transaction to bound search graph exploration depth and enforce a hard cap on p99 query latency.
- Queries filtering vectors with relational predicates MUST leverage PostgreSQL 17 `SET LOCAL hnsw.iterative_scan = 'relaxed'` to prevent vector index exhaustion during pre-filter evaluation.
- Components NEVER issue unbounded vector similarity searches without an explicit `LIMIT` clause coupled with vector distance ordering operators (`<#>` for inner product, `<=>` for cosine, `<->` for L2).

## Bad vs Good Implementations
### Vector Schema Migration and Indexing with Half-Precision Quantization

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Default vector(1536) indices use 32-bit floats, requiring over 12GB of RAM per million vectors for index nodes alone. When traversing the graph, PostgreSQL pages in random index blocks, violently evicting high-frequency relational tables and indexes from shared_buffers.
```typescript
import { sql } from 'kysely';
import type { Kysely } from 'kysely';

export async function up(db: Kysely<unknown>): Promise<void> {
  // Anti-pattern: Storing raw float32 1536-dim vectors with default unquantized HNSW index
  // This allocates ~6KB per row just for the vector + graph overhead, polluting shared_buffers
  await sql`
    CREATE TABLE document_embeddings (
      id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
      document_id UUID NOT NULL,
      embedding vector(1536) NOT NULL
    );
    
    -- Default HNSW on float4: will exhaust RAM and evict OLTP relational tables
    CREATE INDEX idx_document_embeddings_hnsw ON document_embeddings 
    USING hnsw (embedding vector_cosine_ops);
  `.execute(db);
}
```

#### Production Standard (Correct)
> **Standard**: Using halfvec reduces vector storage and HNSW index size by 50% with negligible recall drop (<0.1% on standard benchmarks). Explicit m=16 and ef_construction=64 limits graph degree and build memory, keeping the entire HNSW working set isolated and resident in memory.
```typescript
import { sql } from 'kysely';
import type { Kysely } from 'kysely';

export async function up(db: Kysely<unknown>): Promise<void> {
  // Invariant fix: Use halfvec (16-bit float) for 50% index compaction,
  // explicit HNSW graph limits (m=16, ef_construction=64),
  // and functional index casting if underlying vector must remain float4.
  await sql`
    CREATE TABLE document_embeddings (
      id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
      document_id UUID NOT NULL,
      embedding halfvec(1536) NOT NULL
    );

    -- Quantized halfvec index: fits resident in working set memory
    CREATE INDEX idx_document_embeddings_hnsw_halfvec ON document_embeddings 
    USING hnsw (embedding halfvec_cosine_ops)
    WITH (m = 16, ef_construction = 64);
  `.execute(db);
}
```

### Sub-10ms Isolated Vector Search Query Execution

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Without setting session-level hnsw.ef_search, the search uses arbitrary server defaults that degrade under concurrency. Filtering by tenant_id after vector sorting forces the engine into either poor sequential scans or inefficient post-filtering index walk thrashing.
```typescript
import { Pool } from 'pg';

export async function searchSimilarDocuments(
  pool: Pool,
  queryVector: number[],
  tenantId: string,
  limit: number
) {
  const client = await pool.connect();
  try {
    // Anti-pattern: Global unconstrained search without bounded ef_search
    // or iterative scan tuning, leading to unpredictable 500ms+ latency spikes
    const query = `
      SELECT id, document_id, embedding <=> $1::vector AS distance
      FROM document_embeddings
      WHERE tenant_id = $2
      ORDER BY embedding <=> $1::vector
      LIMIT $3;
    `;
    const result = await client.query(query, [JSON.stringify(queryVector), tenantId, limit]);
    return result.rows;
  } finally {
    client.release();
  }
}
```

#### Production Standard (Correct)
> **Standard**: Scoping hnsw.ef_search to 40 guarantees deterministic sub-10ms latency bounds. Setting hnsw.iterative_scan to relaxed in Postgres 17 enables the index scanner to fetch additional candidate vectors dynamically when relational filters (tenant_id) prune candidates, preventing scan aborts without exhaustive memory allocation.
```typescript
import { Pool } from 'pg';

export async function searchSimilarDocuments(
  pool: Pool,
  queryVector: number[],
  tenantId: string,
  limit: number
) {
  const client = await pool.connect();
  try {
    await client.query('BEGIN');
    
    // Bound graph exploration to guarantee sub-10ms search
    await client.query('SET LOCAL hnsw.ef_search = 40');
    
    // Postgres 17 pgvector iterative scan for combined scalar + vector filters
    await client.query("SET LOCAL hnsw.iterative_scan = 'relaxed'");

    const query = `
      SELECT id, document_id, (embedding <=> $1::halfvec) AS distance
      FROM document_embeddings
      WHERE tenant_id = $2
      ORDER BY embedding <=> $1::halfvec
      LIMIT $3;
    `;
    
    const result = await client.query(query, [
      JSON.stringify(queryVector),
      tenantId,
      limit
    ]);
    
    await client.query('COMMIT');
    return result.rows;
  } catch (error) {
    await client.query('ROLLBACK');
    throw error;
  } finally {
    client.release();
  }
}
```

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: Postgres 17 pgvector HNSW Half-Precision Quantization & Buffer Isolation Guardrail

Postgres 17 pgvector HNSW Half-Precision Quantization & Buffer Isolation Guardrail defines an industrial-grade behavioral standard for backend engineering. Mandates the usage of pgvector 0.7.0+ half-precision (halfvec) scalar quantization and strict buffer isolation for HNSW vector indexes in PostgreSQL 17. Unquantized float4 vector graphs (1536+ dims) rapidly exceed shared_buffers, causing massive disk I/O thrashing and eviction of relational working sets during OLTP workloads. Enforces halfvec cast-indexing, bounded graph connectivity parameters, and iterative scan tuning to guarantee sub-10ms vector retrieval without noisy-neighbor degradation. 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: Full-Precision vector(dim) HNSW Indexing Evicting Relational Buffer 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 DriftFull-Precision vector(dim) HNSW Indexing Evicting Relational Buffer PoolsCritical
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 cast high-dimensional vectors (>384 dimensions) to `halfvec` (FP16) when creating HNSW indexes to reduce memory footprint by 50% without recall loss.

Engineers MUST ALWAYS store or cast high-dimensional vectors (>384 dimensions) to `halfvec` (FP16) when creating HNSW indexes to reduce memory footprint by 50% without recall loss.

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 construct HNSW indexes over unquantized `vector(1536)` columns in tables shared with high-throughput OLTP workloads.

Engineers MUST NOT construct HNSW indexes over unquantized `vector(1536)` columns in tables shared with high-throughput OLTP workloads.

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 SHOULD explicitly define bounded graph parameters (`m = 16`, `ef_construction = 64` or `128`) on HNSW indexes to prevent unbounded graph bloat in `shared_buffers`.

Database migrations SHOULD explicitly define bounded graph parameters (`m = 16`, `ef_construction = 64` or `128`) on HNSW indexes to prevent unbounded graph bloat in `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.
4

Services MUST execute `SET LOCAL hnsw.ef_search = <bounded_value>` (typically 40-100) per transaction to bound search graph exploration depth and enforce a hard cap on p99 query latency.

Services MUST execute `SET LOCAL hnsw.ef_search = <bounded_value>` (typically 40-100) per transaction to bound search graph exploration depth and enforce a hard cap on p99 query latency.

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

Queries filtering vectors with relational predicates MUST leverage PostgreSQL 17 `SET LOCAL hnsw.iterative_scan = 'relaxed'` to prevent vector index exhaustion during pre-filter evaluation.

Queries filtering vectors with relational predicates MUST leverage PostgreSQL 17 `SET LOCAL hnsw.iterative_scan = 'relaxed'` to prevent vector index exhaustion during pre-filter evaluation.

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 issue unbounded vector similarity searches without an explicit `LIMIT` clause coupled with vector distance ordering operators (`<#>` for inner product, `<=>` for cosine, `<->` for L2).

Components NEVER issue unbounded vector similarity searches without an explicit `LIMIT` clause coupled with vector distance ordering operators (`<#>` for inner product, `<=>` for cosine, `<->` for L2).

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 pgvector HNSW Half-Precision Quantization & Buffer Isolation Guardrail critical for AI coding assistants?

Mandates the usage of pgvector 0.7.0+ half-precision (halfvec) scalar quantization and strict buffer isolation for HNSW vector indexes in PostgreSQL 17. Unquantized float4 vector graphs (1536+ dims) rapidly exceed shared_buffers, causing massive disk I/O thrashing and eviction of relational working sets during OLTP workloads. Enforces halfvec cast-indexing, bounded graph connectivity parameters, and iterative scan tuning to guarantee sub-10ms vector retrieval without noisy-neighbor degradation. 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 (Full-Precision vector(dim) HNSW Indexing Evicting Relational Buffer 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