This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: pg17-streaming-io-parallel-scan-budget

PostgreSQL 17 Streaming I/O Engine Parallel Scan & Buffer Ring Isolation

PostgreSQL 17 introduces a modernized asynchronous streaming I/O subsystem that aggressively prefetches sequential scan blocks using unified read streams. In hybrid transactional/analytical workloads, unconstrained parallel sequential scans will monopolize storage controller queue depths and exhaust worker-specific ring buffer strategies (BAS_BULKREAD), directly inducing latency spikes across concurrently executing OLTP transactions. Application services executing large sequential analytics must enforce deterministic session-level parallel worker limits, prefetch batch caps, and replica routing invariants.

Prohibited Anti-Pattern: Unbounded Parallel Analytics Saturating PG17 Streaming I/O Prefetch Queues and Starving OLTP Shared Buffers

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 Streaming I/O Engine Parallel Scan & Buffer Ring Isolation - PostgreSQL 17 introduces a modernized asynchronous streaming I/O subsystem that aggressively prefetches sequential scan blocks using unified read streams. In hybrid transactional/analytical workloads, unconstrained parallel sequential scans will monopolize storage controller queue depths and exhaust worker-specific ring buffer strategies (BAS_BULKREAD), directly inducing latency spikes across concurrently executing OLTP transactions. Application services executing large sequential analytics must enforce deterministic session-level parallel worker limits, prefetch batch caps, and replica routing invariants.
globs: src/**/*.ts, src/**/*.service.ts, src/**/*.repository.ts, src/**/*.dao.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pg17-streaming-io-parallel-scan-budget"
---

# PostgreSQL 17 Streaming I/O Engine Parallel Scan & Buffer Ring Isolation

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Parallel Analytics Saturating PG17 Streaming I/O Prefetch Queues and Starving OLTP Shared Buffers

## System Constraints
You are a Principal Database Infrastructure Architect enforcing the PostgreSQL 17 Streaming I/O Engine Parallel Scan Budget Invariant. PostgreSQL 17 re-architects table and index scans with asynchronous streaming read-ahead pipelines. You must prevent analytical queries from exhausting disk subsystem queues, thrashing shared buffer rings, and degrading concurrent transactional throughput. Enforce strict query session boundaries, deterministic worker limits, replica isolation, and explicit buffer access strategy budgeting on all analytics-grade scan executions.

## Strict Guidelines
- Engineers MUST ALWAYS route analytical queries scanning more than 100,000 tuples or requiring multi-worker sequential execution to dedicated read-only replicas.
- Engineers MUST ALWAYS execute analytical scans inside explicit transactions with session-level guardrails: `SET LOCAL max_parallel_workers_per_gather = 2` (or lower) and bound `work_mem` to prevent memory starvation.
- Engineers MUST NOT allow unbounded `SELECT` or aggregation queries on primary transactional instances without specifying an index-backed range condition or cursor pagination.
- Code SHOULD verify and constrain PostgreSQL 17 `io_combine_limit` and streaming prefetch depth via connection pool metadata when deploying analytical workers.
- Services MUST monitor `pg_stat_io` for `context = 'bulkread'` and `io_depth` saturation to detect parallel scan starvation on active transactional rings.
- Components NEVER execute raw analytical SQL without setting `statement_timeout` to under 15,000 milliseconds when executing in the OLTP primary connection pool.

## Bad vs Good Implementations
### Unbounded Analytics Scan on Primary Pool vs. Guarded PG17 Streaming Session

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: This query executes unconstrained on the primary database pool. In PostgreSQL 17, parallel sequential workers spawn and invoke asynchronous streaming read streams (AIO prefetching). Under concurrent write loads, these bulk reads bypass or overflow the default BAS_BULKREAD ring buffer allocations, saturating disk queue depths and causing catastrophic p99 latency spikes on OLTP commits.
```typescript
import { Pool } from 'pg';

export class MetricsRepository {
  constructor(private readonly primaryPool: Pool) {}

  // ANTI-PATTERN: Runs directly against primary OLTP pool without parallel scan constraints.
  // In PG17, this triggers aggressive streaming I/O read streams across default max workers,
  // saturating physical controller queues and evicting hot OLTP buffer pages.
  async aggregateLedgerVolume(tenantId: string): Promise<{ total: string }> {
    const query = `
      SELECT SUM(amount) AS total
      FROM ledger_entries
      WHERE tenant_id = $1;
    `;
    const result = await this.primaryPool.query(query, [tenantId]);
    return result.rows[0];
  }
}
```

#### Production Standard (Correct)
> **Standard**: The refactored code routes execution to an isolated read replica and encloses the analytics query in a transaction that sets strict local PG17 parallel worker budgets and statement timeouts. This prevents the streaming I/O subsystem from saturating I/O queues and preserves transactional integrity on production clusters.
```typescript
import { Pool, PoolClient } from 'pg';

export class MetricsRepository {
  constructor(
    private readonly readReplicaPool: Pool,
    private readonly maxParallelWorkers: number = 2
  ) {}

  // INVARIANT: Confined to read-replica, governed by strict session-level scan and timeout budgets.
  async aggregateLedgerVolume(tenantId: string): Promise<{ total: string }> {
    const client: PoolClient = await this.readReplicaPool.connect();
    try {
      await client.query('BEGIN READ ONLY');
      
      // Constrain PG17 parallel scan workers and avoid streaming I/O queue exhaustion
      await client.query(`
        SET LOCAL max_parallel_workers_per_gather = ${this.maxParallelWorkers};
        SET LOCAL statement_timeout = '10s';
        SET LOCAL work_mem = '64MB';
      `);

      const query = `
        SELECT COALESCE(SUM(amount), 0) AS total
        FROM ledger_entries
        WHERE tenant_id = $1;
      `;
      const result = await client.query(query, [tenantId]);
      await client.query('COMMIT');
      
      return result.rows[0];
    } 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.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: PostgreSQL 17 Streaming I/O Engine Parallel Scan & Buffer Ring Isolation

PostgreSQL 17 Streaming I/O Engine Parallel Scan & Buffer Ring Isolation defines an industrial-grade behavioral standard for backend engineering. PostgreSQL 17 introduces a modernized asynchronous streaming I/O subsystem that aggressively prefetches sequential scan blocks using unified read streams. In hybrid transactional/analytical workloads, unconstrained parallel sequential scans will monopolize storage controller queue depths and exhaust worker-specific ring buffer strategies (BAS_BULKREAD), directly inducing latency spikes across concurrently executing OLTP transactions. Application services executing large sequential analytics must enforce deterministic session-level parallel worker limits, prefetch batch caps, and replica routing invariants. 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 Parallel Analytics Saturating PG17 Streaming I/O Prefetch Queues and Starving OLTP Shared 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 Parallel Analytics Saturating PG17 Streaming I/O Prefetch Queues and Starving OLTP Shared 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 route analytical queries scanning more than 100,000 tuples or requiring multi-worker sequential execution to dedicated read-only replicas.

Engineers MUST ALWAYS route analytical queries scanning more than 100,000 tuples or requiring multi-worker sequential execution to dedicated read-only replicas.

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 ALWAYS execute analytical scans inside explicit transactions with session-level guardrails

Engineers MUST ALWAYS execute analytical scans inside explicit transactions with session-level guardrails: `SET LOCAL max_parallel_workers_per_gather = 2` (or lower) and bound `work_mem` to prevent memory 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.
3

Engineers MUST NOT allow unbounded `SELECT` or aggregation queries on primary transactional instances without specifying an index-backed range condition or cursor pagination.

Engineers MUST NOT allow unbounded `SELECT` or aggregation queries on primary transactional instances without specifying an index-backed range condition or cursor pagination.

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

Code SHOULD verify and constrain PostgreSQL 17 `io_combine_limit` and streaming prefetch depth via connection pool metadata when deploying analytical workers.

Code SHOULD verify and constrain PostgreSQL 17 `io_combine_limit` and streaming prefetch depth via connection pool metadata when deploying analytical workers.

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

Services MUST monitor `pg_stat_io` for `context = 'bulkread'` and `io_depth` saturation to detect parallel scan starvation on active transactional rings.

Services MUST monitor `pg_stat_io` for `context = 'bulkread'` and `io_depth` saturation to detect parallel scan starvation on active transactional rings.

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 execute raw analytical SQL without setting `statement_timeout` to under 15,000 milliseconds when executing in the OLTP primary connection pool.

Components NEVER execute raw analytical SQL without setting `statement_timeout` to under 15,000 milliseconds when executing in the OLTP primary connection pool.

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 Streaming I/O Engine Parallel Scan & Buffer Ring Isolation critical for AI coding assistants?

PostgreSQL 17 introduces a modernized asynchronous streaming I/O subsystem that aggressively prefetches sequential scan blocks using unified read streams. In hybrid transactional/analytical workloads, unconstrained parallel sequential scans will monopolize storage controller queue depths and exhaust worker-specific ring buffer strategies (BAS_BULKREAD), directly inducing latency spikes across concurrently executing OLTP transactions. Application services executing large sequential analytics must enforce deterministic session-level parallel worker limits, prefetch batch caps, and replica routing invariants. 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 Parallel Analytics Saturating PG17 Streaming I/O Prefetch Queues and Starving OLTP Shared 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