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

pgvector 0.8+ AVX-512 64-Byte Memory Alignment and SIMD Invariant Guardrail

Enforces strict 64-byte memory boundary alignment and 16-element float32 dimension padding for pgvector 0.8+ embeddings in PostgreSQL 17 shared buffers. Prevents CPU alignment check faults (#AC), SIGBUS traps, and split-cache line performance regressions during AVX-512 SIMD-accelerated HNSW index distance scans.

Prohibited Anti-Pattern: Unaligned Vector Buffer Allocations and Non-Padded Embeddings Triggering AVX-512 CPU Traps and SIMD Degradation

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: pgvector 0.8+ AVX-512 64-Byte Memory Alignment and SIMD Invariant Guardrail - Enforces strict 64-byte memory boundary alignment and 16-element float32 dimension padding for pgvector 0.8+ embeddings in PostgreSQL 17 shared buffers. Prevents CPU alignment check faults (#AC), SIGBUS traps, and split-cache line performance regressions during AVX-512 SIMD-accelerated HNSW index distance scans.
globs: src/**/*.ts, src/**/*.js, db/migrations/**/*.sql, src/db/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pgvector-hnsw-avx512-memory-alignment-guardrail"
---

# pgvector 0.8+ AVX-512 64-Byte Memory Alignment and SIMD Invariant Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unaligned Vector Buffer Allocations and Non-Padded Embeddings Triggering AVX-512 CPU Traps and SIMD Degradation

## System Constraints
You are a Principal Software Architect enforcing 64-byte memory alignment and AVX-512 SIMD invariants for pgvector 0.8+ on PostgreSQL 17. Mandate 64-byte aligned buffers, dimension boundary rules, and non-trapping vector binary serialization to prevent hardware-level CPU faults and memory degradation.

## Strict Guidelines
- Engineers MUST ALWAYS allocate vector serialization buffers on 64-byte memory boundaries matching AVX-512 512-bit ZMM register alignment requirements.
- Engineers MUST ensure vector dimensions map to multiples of 16 float32 elements (64 bytes) or verify zero-padding when feeding SIMD-accelerated HNSW distance operators (L2, Cosine, Inner Product).
- Engineers MUST NOT instantiate `Float32Array` views from arbitrary byte offsets of shared stream buffers without asserting `byteOffset % 64 === 0`.
- Database migrations MUST configure vector columns with explicit dimensional bounds (e.g., `vector(1536)`) and ensure table storage strategies avoid unaligned TOAST chunk fragmentation.
- Vector binary deserialization logic MUST copy unaligned raw network buffers into aligned target memory before passing references to SIMD-enabled native addons or FFI boundaries.

## Bad vs Good Implementations
### Unaligned Vector Serialization vs. 64-Byte AVX-512 Aligned Buffer Wrapper

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Creating Float32Array instances on arbitrary buffer byte offsets violates 64-byte boundaries required by AVX-512 ZMM registers (512 bits / 8 = 64 bytes). When Postgres 17 and pgvector 0.8+ process vector operations using SIMD vector distance routines, unaligned loads produce split-cache line performance penalties or CPU alignment check (#AC) hardware traps.
```typescript
import { Pool } from 'pg';

const pool = new Pool();

export async function indexEmbedding(id: string, embedding: number[]) {
  // Anti-pattern: Allocating variable-offset buffer without alignment guarantees
  // When streaming via pg binary protocol, arbitrary offset causes unaligned reads
  // triggering AVX-512 load traps or severe split-cache line degradations
  const rawBuffer = Buffer.alloc(embedding.length * 4);
  const floatView = new Float32Array(rawBuffer.buffer, rawBuffer.byteOffset, embedding.length);
  
  for (let i = 0; i < embedding.length; i++) {
    floatView[i] = embedding[i];
  }

  // Raw array conversion incurs GC churn and unaligned binary serialization
  await pool.query(
    'INSERT INTO document_embeddings (id, embedding) VALUES ($1, $2)',
    [id, `[${embedding.join(',')}]`]
  );
}
```

#### Production Standard (Correct)
> **Standard**: Allocates a memory block with explicit 64-byte offset alignment (matching AVX-512 register widths), enforces dimension divisibility by 16 floats, and prepares a deterministic binary payload for pgvector 0.8+, avoiding unaligned memory traps and ensuring maximum SIMD throughput in PostgreSQL 17.
```typescript
import { Pool } from 'pg';

const pool = new Pool();
const AVX512_ALIGNMENT_BYTES = 64;
const FLOAT32_BYTES = 4;

/**
 * Allocates a 64-byte aligned Buffer to guarantee zero-copy AVX-512 SIMD safety.
 */
export function createAlignedVectorBuffer(dimensions: number): { buffer: Buffer; view: Float32Array } {
  if (dimensions % 16 !== 0) {
    throw new Error(`Dimension ${dimensions} must be a multiple of 16 for AVX-512 64-byte alignment`);
  }

  const byteLength = dimensions * FLOAT32_BYTES;
  const totalAllocated = byteLength + AVX512_ALIGNMENT_BYTES;
  const memory = Buffer.allocUnsafeSlow(totalAllocated);
  
  // Calculate 64-byte aligned starting offset
  const remainder = memory.byteOffset % AVX512_ALIGNMENT_BYTES;
  const offset = remainder === 0 ? 0 : AVX512_ALIGNMENT_BYTES - remainder;
  
  const alignedBuffer = memory.subarray(offset, offset + byteLength);
  const view = new Float32Array(alignedBuffer.buffer, alignedBuffer.byteOffset, dimensions);
  
  return { buffer: alignedBuffer, view };
}

export async function indexEmbedding(id: string, embedding: Float32Array) {
  const dim = embedding.length;
  const { buffer, view } = createAlignedVectorBuffer(dim);
  view.set(embedding);

  // Use pgvector binary format with 64-byte aligned payload
  // Format: uint16 dimensions, uint16 unused, float32[] vectors
  const header = Buffer.alloc(4);
  header.writeUInt16BE(dim, 0);
  header.writeUInt16BE(0, 2);
  
  const payload = Buffer.concat([header, buffer]);

  await pool.query({
    text: 'INSERT INTO document_embeddings (id, embedding) VALUES ($1, $2::vector)',
    values: [id, payload]
  });
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: pgvector 0.8+ AVX-512 64-Byte Memory Alignment and SIMD Invariant Guardrail

pgvector 0.8+ AVX-512 64-Byte Memory Alignment and SIMD Invariant Guardrail defines an industrial-grade behavioral standard for backend engineering. Enforces strict 64-byte memory boundary alignment and 16-element float32 dimension padding for pgvector 0.8+ embeddings in PostgreSQL 17 shared buffers. Prevents CPU alignment check faults (#AC), SIGBUS traps, and split-cache line performance regressions during AVX-512 SIMD-accelerated HNSW index distance scans. 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: Unaligned Vector Buffer Allocations and Non-Padded Embeddings Triggering AVX-512 CPU Traps and SIMD Degradation 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 DriftUnaligned Vector Buffer Allocations and Non-Padded Embeddings Triggering AVX-512 CPU Traps and SIMD DegradationCritical
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 allocate vector serialization buffers on 64-byte memory boundaries matching AVX-512 512-bit ZMM register alignment requirements.

Engineers MUST ALWAYS allocate vector serialization buffers on 64-byte memory boundaries matching AVX-512 512-bit ZMM register alignment requirements.

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 ensure vector dimensions map to multiples of 16 float32 elements (64 bytes) or verify zero-padding when feeding SIMD-accelerated HNSW distance operators (L2, Cosine, Inner Product).

Engineers MUST ensure vector dimensions map to multiples of 16 float32 elements (64 bytes) or verify zero-padding when feeding SIMD-accelerated HNSW distance operators (L2, Cosine, Inner Product).

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

Engineers MUST NOT instantiate `Float32Array` views from arbitrary byte offsets of shared stream buffers without asserting `byteOffset % 64 === 0`.

Engineers MUST NOT instantiate `Float32Array` views from arbitrary byte offsets of shared stream buffers without asserting `byteOffset % 64 === 0`.

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

Database migrations MUST configure vector columns with explicit dimensional bounds (e.g., `vector(1536)`) and ensure table storage strategies avoid unaligned TOAST chunk fragmentation.

Database migrations MUST configure vector columns with explicit dimensional bounds (e.g., `vector(1536)`) and ensure table storage strategies avoid unaligned TOAST chunk fragmentation.

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

Vector binary deserialization logic MUST copy unaligned raw network buffers into aligned target memory before passing references to SIMD-enabled native addons or FFI boundaries.

Vector binary deserialization logic MUST copy unaligned raw network buffers into aligned target memory before passing references to SIMD-enabled native addons or FFI boundaries.

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+ AVX-512 64-Byte Memory Alignment and SIMD Invariant Guardrail critical for AI coding assistants?

Enforces strict 64-byte memory boundary alignment and 16-element float32 dimension padding for pgvector 0.8+ embeddings in PostgreSQL 17 shared buffers. Prevents CPU alignment check faults (#AC), SIGBUS traps, and split-cache line performance regressions during AVX-512 SIMD-accelerated HNSW index distance scans. 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 (Unaligned Vector Buffer Allocations and Non-Padded Embeddings Triggering AVX-512 CPU Traps and SIMD Degradation) 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