This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: postgres-17-vectorized-streaming-read-prefetch-invariant

Postgres 17 Vectorized Streaming Read Prefetch Queue Depth Invariant

Enforces strict upper bounds on asynchronous streaming read queue depth and effective I/O concurrency in Postgres 17 workloads. Prevents lock convoys on buffer mapping partitions and avoids kernel page cache thrashing by calibrating prefetch horizons below OS block-device readahead boundaries.

Prohibited Anti-Pattern: Unbounded Streaming Read Queue Depths Inducing Buffer Partition Lock Convoys and OS Readahead Cache Thrashing

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 Vectorized Streaming Read Prefetch Queue Depth Invariant - Enforces strict upper bounds on asynchronous streaming read queue depth and effective I/O concurrency in Postgres 17 workloads. Prevents lock convoys on buffer mapping partitions and avoids kernel page cache thrashing by calibrating prefetch horizons below OS block-device readahead boundaries.
globs: src/**/*.ts, src/db/**/*.ts, src/infrastructure/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-vectorized-streaming-read-prefetch-invariant"
---

# Postgres 17 Vectorized Streaming Read Prefetch Queue Depth Invariant

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Streaming Read Queue Depths Inducing Buffer Partition Lock Convoys and OS Readahead Cache Thrashing

## System Constraints
You are a Principal Database Infrastructure Engineer enforcing the Postgres 17 Vectorized Streaming Read Prefetch Queue Depth Invariant. Postgres 17 introduces streaming I/O algorithms that proactively prefetch heap and index pages into shared buffers. Unregulated prefetch depths cause severe lock convoys on Postgres BufferMapping locks, evict hot cachelines, and trigger redundant kernel page-cache amplification against OS readahead limits. You must strictly mandate bounded, topology-aware prefetch queuing across all query construction, session configuration, and cursor streaming pipelines.

## Strict Guidelines
- Engineers MUST ALWAYS compute session-level `effective_io_concurrency` dynamically derived from underlying storage IOPS and NVMe queue topology, strictly bounding values between 16 and 64 for virtualized storage (EBS/PD) and no higher than 128 for direct-attached NVMe.
- Engineers MUST NOT allow client-side cursors or streaming queries to request unbounded prefetch batches that cause Postgres 17 streaming read engines to saturate buffer mapping partitions (`BufferMappingPartitionLock`).
- Code SHOULD divide session `effective_io_concurrency` by `max_parallel_workers_per_gather` in parallel analytical queries to guarantee that cumulative prefetch depth across all parallel worker streams never exceeds the OS storage queue depth.
- Services MUST inspect `pg_stat_io` for streaming reads context (`context = 'normal'`, `op = 'read'`) to detect high prefetch cache evictions (`pg_stat_database.blk_read_time` inflation) and tune queue windows proactively.
- Components NEVER issue unbounded batch-cursor queries without specifying an explicit `FETCH FORWARD` batch limit clamped to less than or equal to the kernel readahead ceiling (typically 128KB to 256KB, equivalent to 16-32 Postgres 8KB pages).

## Bad vs Good Implementations
### Postgres 17 Streaming Read Session Optimization and Cursor Orchestration

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Setting effective_io_concurrency to an arbitrary large value (1024) forces Postgres 17's vectorized streaming read engine to enqueue excessive asynchronous block requests. This exceeds OS block-layer readahead thresholds, causes severe contention on buffer table partition spinlocks, and leads to CPU lock convoy stalls when multiple workers execute concurrent scans.
```typescript
import { Pool, PoolClient } from 'pg';

export async function streamLargeAuditLog(pool: Pool, tenantId: string): Promise<void> {
  const client: PoolClient = await pool.connect();
  try {
    // ANTI-PATTERN: Unconditionally cranking IO concurrency to arbitrary highs
    // triggers lock convoys on BufferMapping partitions in Postgres 17's streaming reader.
    // Bypassing OS readahead limits causes double-caching and rapid cacheline eviction.
    await client.query('SET effective_io_concurrency = 1024;');
    await client.query('BEGIN;');
    
    await client.query(
      'DECLARE audit_cursor CURSOR FOR SELECT * FROM audit_logs WHERE tenant_id = $1',
      [tenantId]
    );

    // Fetching 50,000 rows at once overrides engine batching invariants
    const res = await client.query('FETCH FORWARD 50000 FROM audit_cursor;');
    console.log(`Fetched ${res.rowCount} records`);

    await client.query('COMMIT;');
  } finally {
    client.release();
  }
}
```

#### Production Standard (Correct)
> **Standard**: The compliant code pins `effective_io_concurrency` using `SET LOCAL` scoped strictly to the transaction, using empirically derived limits (32 for cloud block volumes). It caps parallel workers to avoid prefetch multiplication and bounds cursor retrieval increments to align with kernel readahead boundaries, eliminating buffer mapping lock convoys and OS cache thrashing.
```typescript
import { Pool, PoolClient } from 'pg';

interface StorageIoBoundary {
  maxIoConcurrency: number;
  clampedCursorFetchPages: number;
}

// OS readahead is typically 128KB (16 * 8KB pages). Bound the cursor to 32 pages max (256KB window).
const EBS_GP3_LIMITS: StorageIoBoundary = {
  maxIoConcurrency: 32,
  clampedCursorFetchPages: 32,
};

export async function streamLargeAuditLog(pool: Pool, tenantId: string): Promise<void> {
  const client: PoolClient = await pool.connect();
  try {
    await client.query('BEGIN READ ONLY;');

    // INVARIANT: Pin prefetch queue depth strictly to the storage controller topology
    // and ensure parallel worker allocations do not multiply beyond queue boundaries.
    const boundedConcurrency = EBS_GP3_LIMITS.maxIoConcurrency;
    await client.query('SET LOCAL effective_io_concurrency = $1;', [boundedConcurrency]);
    
    // Limit parallel worker aggregation to prevent multi-worker queue multiplicative overflow
    await client.query('SET LOCAL max_parallel_workers_per_gather = 2;');

    await client.query(
      'DECLARE audit_cursor CURSOR FOR SELECT * FROM audit_logs WHERE tenant_id = $1',
      [tenantId]
    );

    // Bound fetch forward to safe kernel readahead page chunks (e.g., 32 rows per batch assuming ~8KB/row)
    let rowsProcessed = 0;
    const batchSize = EBS_GP3_LIMITS.clampedCursorFetchPages;

    while (true) {
      const res = await client.query(`FETCH FORWARD ${batchSize} FROM audit_cursor;`);
      if (res.rowCount === 0) break;
      rowsProcessed += res.rowCount;
    }

    console.log(`Safely streamed ${rowsProcessed} records without queue depth thrashing`);
    await client.query('COMMIT;');
  } 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 Vectorized Streaming Read Prefetch Queue Depth Invariant

Postgres 17 Vectorized Streaming Read Prefetch Queue Depth Invariant defines an industrial-grade behavioral standard for backend engineering. Enforces strict upper bounds on asynchronous streaming read queue depth and effective I/O concurrency in Postgres 17 workloads. Prevents lock convoys on buffer mapping partitions and avoids kernel page cache thrashing by calibrating prefetch horizons below OS block-device readahead boundaries. 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 Read Queue Depths Inducing Buffer Partition Lock Convoys and OS Readahead Cache Thrashing 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 Read Queue Depths Inducing Buffer Partition Lock Convoys and OS Readahead Cache ThrashingCritical
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 compute session-level `effective_io_concurrency` dynamically derived from underlying storage IOPS and NVMe queue topology, strictly bounding values between 16 and 64 for virtualized storage (EBS/PD) and no higher than 128 for direct-attached NVMe.

Engineers MUST ALWAYS compute session-level `effective_io_concurrency` dynamically derived from underlying storage IOPS and NVMe queue topology, strictly bounding values between 16 and 64 for virtualized storage (EBS/PD) and no higher than 128 for direct-attached NVMe.

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 allow client-side cursors or streaming queries to request unbounded prefetch batches that cause Postgres 17 streaming read engines to saturate buffer mapping partitions (`BufferMappingPartitionLock`).

Engineers MUST NOT allow client-side cursors or streaming queries to request unbounded prefetch batches that cause Postgres 17 streaming read engines to saturate buffer mapping partitions (`BufferMappingPartitionLock`).

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 divide session `effective_io_concurrency` by `max_parallel_workers_per_gather` in parallel analytical queries to guarantee that cumulative prefetch depth across all parallel worker streams never exceeds the OS storage queue depth.

Code SHOULD divide session `effective_io_concurrency` by `max_parallel_workers_per_gather` in parallel analytical queries to guarantee that cumulative prefetch depth across all parallel worker streams never exceeds the OS storage queue depth.

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 inspect `pg_stat_io` for streaming reads context (`context = 'normal'`, `op = 'read'`) to detect high prefetch cache evictions (`pg_stat_database.blk_read_time` inflation) and tune queue windows proactively.

Services MUST inspect `pg_stat_io` for streaming reads context (`context = 'normal'`, `op = 'read'`) to detect high prefetch cache evictions (`pg_stat_database.blk_read_time` inflation) and tune queue windows proactively.

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 issue unbounded batch-cursor queries without specifying an explicit `FETCH FORWARD` batch limit clamped to less than or equal to the kernel readahead ceiling (typically 128KB to 256KB, equivalent to 16-32 Postgres 8KB pages).

Components NEVER issue unbounded batch-cursor queries without specifying an explicit `FETCH FORWARD` batch limit clamped to less than or equal to the kernel readahead ceiling (typically 128KB to 256KB, equivalent to 16-32 Postgres 8KB pages).

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 Vectorized Streaming Read Prefetch Queue Depth Invariant critical for AI coding assistants?

Enforces strict upper bounds on asynchronous streaming read queue depth and effective I/O concurrency in Postgres 17 workloads. Prevents lock convoys on buffer mapping partitions and avoids kernel page cache thrashing by calibrating prefetch horizons below OS block-device readahead boundaries. 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 Read Queue Depths Inducing Buffer Partition Lock Convoys and OS Readahead Cache Thrashing) 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