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-invariants

Postgres 17 pgvector Scaled HNSW Halfvec Quantization Standards

Mandates half-precision scalar quantization (halfvec) and strict hyperparameter boundaries for HNSW indexes on high-dimensional vector embeddings in Postgres 17. Eliminates unconstrained shared buffer consumption, maintenance memory exhaustion, and connection starvation during concurrent write-heavy vector ingestion pipelines.

Prohibited Anti-Pattern: Unquantized Float32 HNSW Indexing Under High-Concurrency Vector Ingestion

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 pgvector Scaled HNSW Halfvec Quantization Standards - Mandates half-precision scalar quantization (halfvec) and strict hyperparameter boundaries for HNSW indexes on high-dimensional vector embeddings in Postgres 17. Eliminates unconstrained shared buffer consumption, maintenance memory exhaustion, and connection starvation during concurrent write-heavy vector ingestion pipelines.
globs: src/db/migrations/**/*.ts, src/db/schema/**/*.ts, src/repositories/**/*.ts, migrations/**/*.sql
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-pgvector-hnsw-quantization-invariants"
---

# Postgres 17 pgvector Scaled HNSW Halfvec Quantization Standards

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unquantized Float32 HNSW Indexing Under High-Concurrency Vector Ingestion

## System Constraints
You are a Principal Database Architect enforcing Postgres 17 and pgvector memory safety invariants. Mandate half-precision (halfvec) or scalar-quantized indexing patterns for all HNSW vector indexes exceeding 100,000 entities or 768 dimensions. Enforce explicit m and ef_construction limits, batch-bounded ingestion thresholds, and decoupled index creation strategies to prevent buffer cache pollution and OOM terminations.

## Strict Guidelines
- Engineers MUST ALWAYS cast 32-bit float embeddings to 'halfvec' (16-bit half-precision) when defining HNSW indexes on vectors with dimensions >= 768.
- Engineers MUST NOT construct HNSW indexes with default parameters during active high-concurrency ingestion; 'm' MUST be constrained between 16 and 32, and 'ef_construction' MUST be bounded between 64 and 128.
- Database migrations MUST configure 'maintenance_work_mem' explicitly within the indexing transaction or session before executing 'CREATE INDEX CONCURRENTLY' for HNSW graphs.
- Services MUST batch vector writes in chunks of 500 to 2,000 vectors per transaction to prevent wal_buffers saturation and worker lock contention during concurrent HNSW graph updates.
- Components NEVER query unindexed 'vector' columns directly in production hot paths without setting session-level 'hnsw.ef_search' to an upper bound (maximum 100).

## Bad vs Good Implementations
### Postgres 17 HNSW Index Migration and Quantized Embedding Schema

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Creating an HNSW index on a standard 32-bit vector(1536) column consumes excessive RAM (1.5KB for the vector payload plus up to 4KB of HNSW graph edge structures per row). Under concurrent ingestion, maintenance_work_mem and shared_buffers are rapidly exhausted, degrading query latency across the entire database instance.
```typescript
import { sql } from 'kysely';
import type { Kysely } from 'kysely';

export async function up(db: Kysely<unknown>): Promise<void> {
  // Anti-Pattern: Unquantized 1536-dim vector creates a full-precision float32 HNSW graph.
  // Causes severe memory ballooning (~6-8KB per node in RAM) and out-of-memory worker crashes.
  await db.schema
    .createTable('document_embeddings')
    .addColumn('id', 'uuid', (col) => col.primaryKey())
    .addColumn('embedding', sql`vector(1536)`, (col) => col.notNull())
    .execute();

  // Default HNSW index without m, ef_construction, or halfvec quantization
  await sql`
    CREATE INDEX idx_document_embeddings_vector 
    ON document_embeddings USING hnsw (embedding vector_cosine_ops);
  `.execute(db);
}
```

#### Production Standard (Correct)
> **Standard**: Casting the index expression to halfvec(1536) halves the vector storage footprint within the HNSW index structure with negligible recall loss. Constraining m to 16 and ef_construction to 64 bounds graph node connectivity, ensuring index construction stays within predictable memory boundaries without starving Postgres shared buffers.
```typescript
import { sql } from 'kysely';
import type { Kysely } from 'kysely';

export async function up(db: Kysely<unknown>): Promise<void> {
  // Invariant Fix: Store exact vector data, but index using halfvec (FP16)
  // Postgres 17 + pgvector allows direct halfvec expression indexing to cut RAM footprint by 50%.
  await db.schema
    .createTable('document_embeddings')
    .addColumn('id', 'uuid', (col) => col.primaryKey())
    .addColumn('embedding', sql`vector(1536)`, (col) => col.notNull())
    .execute();

  // Bound memory explicitly for index generation session
  await sql`SET LOCAL maintenance_work_mem = '2GB';`.execute(db);
  await sql`SET LOCAL max_parallel_maintenance_workers = 4;`.execute(db);

  // Constrained HNSW index with halfvec expression quantization and deterministic graph constraints
  await sql`
    CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_document_embeddings_halfvec
    ON document_embeddings 
    USING hnsw ((embedding::halfvec(1536)) halfvec_cosine_ops)
    WITH (m = 16, ef_construction = 64);
  `.execute(db);
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Postgres 17 pgvector Scaled HNSW Halfvec Quantization Standards

Postgres 17 pgvector Scaled HNSW Halfvec Quantization Standards defines an industrial-grade behavioral standard for backend engineering. Mandates half-precision scalar quantization (halfvec) and strict hyperparameter boundaries for HNSW indexes on high-dimensional vector embeddings in Postgres 17. Eliminates unconstrained shared buffer consumption, maintenance memory exhaustion, and connection starvation during concurrent write-heavy vector ingestion pipelines. 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 Indexing Under High-Concurrency Vector 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 Float32 HNSW Indexing Under High-Concurrency Vector IngestionCritical
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 cast 32-bit float embeddings to 'halfvec' (16-bit half-precision) when defining HNSW indexes on vectors with dimensions >= 768.

Engineers MUST ALWAYS cast 32-bit float embeddings to 'halfvec' (16-bit half-precision) when defining HNSW indexes on vectors with dimensions >= 768.

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 with default parameters during active high-concurrency ingestion

Engineers MUST NOT construct HNSW indexes with default parameters during active high-concurrency ingestion; 'm' MUST be constrained between 16 and 32, and 'ef_construction' MUST be bounded between 64 and 128.

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 configure 'maintenance_work_mem' explicitly within the indexing transaction or session before executing 'CREATE INDEX CONCURRENTLY' for HNSW graphs.

Database migrations MUST configure 'maintenance_work_mem' explicitly within the indexing transaction or session before executing 'CREATE INDEX CONCURRENTLY' for HNSW graphs.

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 batch vector writes in chunks of 500 to 2,000 vectors per transaction to prevent wal_buffers saturation and worker lock contention during concurrent HNSW graph updates.

Services MUST batch vector writes in chunks of 500 to 2,000 vectors per transaction to prevent wal_buffers saturation and worker lock contention during concurrent HNSW graph updates.

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 query unindexed 'vector' columns directly in production hot paths without setting session-level 'hnsw.ef_search' to an upper bound (maximum 100).

Components NEVER query unindexed 'vector' columns directly in production hot paths without setting session-level 'hnsw.ef_search' to an upper bound (maximum 100).

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 Scaled HNSW Halfvec Quantization Standards critical for AI coding assistants?

Mandates half-precision scalar quantization (halfvec) and strict hyperparameter boundaries for HNSW indexes on high-dimensional vector embeddings in Postgres 17. Eliminates unconstrained shared buffer consumption, maintenance memory exhaustion, and connection starvation during concurrent write-heavy vector ingestion pipelines. 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 Indexing Under High-Concurrency Vector 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

Postgres