This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: postgres-17-direct-io-prefetch-budget-standard

PostgreSQL 17 Vectorized Direct I/O & Prefetch Budget Architecture Standard

Enforces deterministic I/O budget allocations and OS page-cache bypass thresholds in PostgreSQL 17 streaming read pipelines. Prevents kernel inode lock contention, double-buffering degradation, and OLTP buffer pool eviction under concurrent high-throughput sequential scans and vector workloads.

Prohibited Anti-Pattern: Unbounded Streaming Scans Polluting Page Cache and Exhausting Prefetch Ring Buffers

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 Vectorized Direct I/O & Prefetch Budget Architecture Standard - Enforces deterministic I/O budget allocations and OS page-cache bypass thresholds in PostgreSQL 17 streaming read pipelines. Prevents kernel inode lock contention, double-buffering degradation, and OLTP buffer pool eviction under concurrent high-throughput sequential scans and vector workloads.
globs: src/db/**/*.ts, src/repositories/**/*.ts, src/services/**/*.ts, infra/postgres/**/*.sql, migrations/**/*.sql
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-direct-io-prefetch-budget-standard"
---

# PostgreSQL 17 Vectorized Direct I/O & Prefetch Budget Architecture Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Streaming Scans Polluting Page Cache and Exhausting Prefetch Ring Buffers

## System Constraints
You are a Principal Software Architect enforcing the PostgreSQL 17 Vectorized Direct I/O Prefetch Budget Standard. Mandate explicit streaming I/O limits, direct I/O parameters, and session-level isolation for high-throughput scans. Reject unbounded sequential read operations that fail to cap prefetch queues, bypass direct I/O safeguards, or risk shared_buffers eviction.

## Strict Guidelines
- Engineers MUST ALWAYS configure session-level `SET LOCAL io_combine_limit` and `SET LOCAL effective_io_concurrency` for analytical or bulk vector-similarity queries exceeding 10,000 tuples.
- Engineers MUST NOT execute ad-hoc, full-table sequential scans on connection pools shared with low-latency OLTP transactions.
- Database configurations MUST specify `io_method = direct` (or OS-supported AIO equivalents) on NVMe storage targets to eliminate kernel page-cache double-buffering.
- Analytical workloads MUST enforce cursor-based chunking with strict `work_mem` and `maintenance_io_concurrency` boundaries to prevent worker thread lock starvation.
- Query executors NEVER rely on server default global prefetch settings during concurrent high-dimensional vector embeddings searches (e.g., pgvector HNSW/IVFFlat scans).

## Bad vs Good Implementations
### Configuring Scoped Prefetch and I/O Budget for PostgreSQL 17 Streaming Read Workload

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Executing an unbudgeted sequential scan on a generic connection pool exhausts PostgreSQL 17 streaming read prefetch queues, triggers OS page-cache thrashing, and risks evicting hot OLTP pages from shared_buffers.
```typescript
import { Pool } from 'pg';

export async function runVectorSequentialScan(
  pool: Pool,
  embedding: number[],
  threshold: number
): Promise<any[]> {
  // Anti-pattern: Executes heavy scan on shared pool with default prefetch,
  // causing OS page cache lock thrashing and eviction of OLTP working sets
  const client = await pool.connect();
  try {
    const query = `
      SELECT id, title, embedding <=> $1::vector AS distance
      FROM document_embeddings
      WHERE embedding <=> $1::vector < $2
      ORDER BY distance ASC;
    `;
    const result = await client.query(query, [JSON.stringify(embedding), threshold]);
    return result.rows;
  } finally {
    client.release();
  }
}
```

#### Production Standard (Correct)
> **Standard**: Wraps the analytical scan in a bounded read-only transaction, applying `SET LOCAL` limits to `effective_io_concurrency` and `io_combine_limit`, and streams via cursor to cap memory and OS direct I/O utilization.
```typescript
import { Pool, PoolClient } from 'pg';

interface ScanOptions {
  maxIoConcurrency?: number;
  ioCombineLimitKb?: number;
  chunkSize?: number;
}

export async function runVectorSequentialScan(
  pool: Pool,
  embedding: number[],
  threshold: number,
  options: ScanOptions = {}
): Promise<any[]> {
  const { maxIoConcurrency = 8, ioCombineLimitKb = 128, chunkSize = 5000 } = options;
  const client: PoolClient = await pool.connect();

  try {
    // Invariant: Isolate analytical read stream using strict session-local prefetch budgets
    await client.query('BEGIN READ ONLY;');
    
    // Constrain Postgres 17 streaming read engine to prevent worker saturation
    await client.query(`
      SET LOCAL effective_io_concurrency = ${maxIoConcurrency};
      SET LOCAL io_combine_limit = '${ioCombineLimitKb}kB';
      SET LOCAL synchronize_seqscans = off;
    `);

    const query = `
      DECLARE scan_cursor CURSOR FOR
      SELECT id, title, embedding <=> $1::vector AS distance
      FROM document_embeddings
      WHERE embedding <=> $1::vector < $2
      ORDER BY distance ASC;
    `;
    await client.query(query, [JSON.stringify(embedding), threshold]);

    const records: any[] = [];
    while (true) {
      const fetchRes = await client.query(`FETCH FORWARD ${chunkSize} FROM scan_cursor;`);
      if (fetchRes.rows.length === 0) break;
      records.push(...fetchRes.rows);
    }

    await client.query('COMMIT;');
    return records;
  } catch (error) {
    await client.query('ROLLBACK;').catch(() => {});
    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: PostgreSQL 17 Vectorized Direct I/O & Prefetch Budget Architecture Standard

PostgreSQL 17 Vectorized Direct I/O & Prefetch Budget Architecture Standard defines an industrial-grade behavioral standard for backend engineering. Enforces deterministic I/O budget allocations and OS page-cache bypass thresholds in PostgreSQL 17 streaming read pipelines. Prevents kernel inode lock contention, double-buffering degradation, and OLTP buffer pool eviction under concurrent high-throughput sequential scans and vector workloads. 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: Unbounded Streaming Scans Polluting Page Cache and Exhausting Prefetch Ring Buffers 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 DriftUnbounded Streaming Scans Polluting Page Cache and Exhausting Prefetch Ring BuffersCritical
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 configure session-level `SET LOCAL io_combine_limit` and `SET LOCAL effective_io_concurrency` for analytical or bulk vector-similarity queries exceeding 10,000 tuples.

Engineers MUST ALWAYS configure session-level `SET LOCAL io_combine_limit` and `SET LOCAL effective_io_concurrency` for analytical or bulk vector-similarity queries exceeding 10,000 tuples.

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 execute ad-hoc, full-table sequential scans on connection pools shared with low-latency OLTP transactions.

Engineers MUST NOT execute ad-hoc, full-table sequential scans on connection pools shared with low-latency OLTP transactions.

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 configurations MUST specify `io_method = direct` (or OS-supported AIO equivalents) on NVMe storage targets to eliminate kernel page-cache double-buffering.

Database configurations MUST specify `io_method = direct` (or OS-supported AIO equivalents) on NVMe storage targets to eliminate kernel page-cache double-buffering.

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

Analytical workloads MUST enforce cursor-based chunking with strict `work_mem` and `maintenance_io_concurrency` boundaries to prevent worker thread lock starvation.

Analytical workloads MUST enforce cursor-based chunking with strict `work_mem` and `maintenance_io_concurrency` boundaries to prevent worker thread lock starvation.

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

Query executors NEVER rely on server default global prefetch settings during concurrent high-dimensional vector embeddings searches (e.g., pgvector HNSW/IVFFlat scans).

Query executors NEVER rely on server default global prefetch settings during concurrent high-dimensional vector embeddings searches (e.g., pgvector HNSW/IVFFlat scans).

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 Vectorized Direct I/O & Prefetch Budget Architecture Standard critical for AI coding assistants?

Enforces deterministic I/O budget allocations and OS page-cache bypass thresholds in PostgreSQL 17 streaming read pipelines. Prevents kernel inode lock contention, double-buffering degradation, and OLTP buffer pool eviction under concurrent high-throughput sequential scans and vector workloads. 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 (Unbounded Streaming Scans Polluting Page Cache and Exhausting Prefetch Ring Buffers) 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