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

PostgreSQL 17 Streaming I/O Prefetch & Buffer Starvation Dynamic Fence

Enforces dynamic calibration of PostgreSQL 17 streaming I/O prefetch depth and parallel worker allocation against active shared_buffers pressure and dirty page writeback ratios. Prevents parallel sequential scan workers from aggressively issuing speculative prefetch requests that displace transactional hot sets and trigger kernel-level page cache eviction storms. Mandates adaptive session-level GUC fencing and synchronized buffer-ring containment for concurrent analytical workloads.

Prohibited Anti-Pattern: Unbounded Parallel Streaming Prefetch Saturating Buffer Rings and Evicting Transactional Working Sets

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 Streaming I/O Prefetch & Buffer Starvation Dynamic Fence - Enforces dynamic calibration of PostgreSQL 17 streaming I/O prefetch depth and parallel worker allocation against active shared_buffers pressure and dirty page writeback ratios. Prevents parallel sequential scan workers from aggressively issuing speculative prefetch requests that displace transactional hot sets and trigger kernel-level page cache eviction storms. Mandates adaptive session-level GUC fencing and synchronized buffer-ring containment for concurrent analytical workloads.
globs: src/infrastructure/database/**/*.{ts,js}, src/db/**/*.{ts,js}, src/repositories/**/*.{ts,js}, src/services/analytics/**/*.{ts,js}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pg17-streaming-io-prefetch-starvation-fence"
---

# PostgreSQL 17 Streaming I/O Prefetch & Buffer Starvation Dynamic Fence

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Parallel Streaming Prefetch Saturating Buffer Rings and Evicting Transactional Working Sets

## System Constraints
You are a Principal Database Infrastructure Architect enforcing PostgreSQL 17 streaming I/O prefetch constraints. In PostgreSQL 17, sequential scans and vacuum utilize the modernized streaming I/O subsystem. Under heavy concurrent analytical loads, unthrottled worker prefetch pipelines can exhaust shared_buffers allocations, trigger kernel dirty page writebacks, and evict the OLTP hot-cache. You must enforce adaptive prefetch depth calibration, parallel worker gating, and strict session-level buffer fence controls.

## Strict Guidelines
- Engineers MUST ALWAYS enforce adaptive session-level GUC configuration (`SET LOCAL io_combine_limit`, `SET LOCAL effective_io_concurrency`, and `SET LOCAL max_parallel_workers_per_gather`) for analytical pipelines based on current buffer allocation telemetry.
- Engineers MUST NOT execute large-scale parallel sequential scans on default pool connections without dynamic calibration against `pg_stat_io` backend and shared buffer pressure.
- Code SHOULD sample `pg_stat_database` and `pg_stat_io` eviction markers (`buffers_backend`, `evictions`, `reuses`) prior to acquiring parallel scan workers to throttle prefetch sliding windows under high contention.
- Services MUST route concurrent analytical queries through dedicated, rate-limited connection pools configured with isolated buffer ring allocations and `synchronize_seqscans = on` to prevent uncoordinated disk head thrashing.
- Components NEVER execute raw ad-hoc analytical queries with unbounded parallel streaming prefetch on shared transactional (OLTP) connections.

## Bad vs Good Implementations
### Dynamic Prefetch Calibration & Buffer Fence Wrapper for PG17

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Executing analytical queries with default settings allows PostgreSQL 17's streaming I/O subsystem to prefetch up to kernel limits with multiple workers. Under concurrency, this causes shared buffer thrashing, evicts OLTP caches, and induces severe disk I/O latency spikes.
```typescript
import { Pool } from 'pg';

const pool = new Pool({ connectionString: process.env.DATABASE_URL });

export async function runAnalyticalScan(query: string, params: unknown[]): Promise<unknown[]> {
  // Anti-Pattern: Executes large analytical scans on raw connections without GUC fencing.
  // In PostgreSQL 17, streaming I/O can spawn multiple parallel workers with default
  // aggressive prefetching, overwhelming shared_buffers and triggering page eviction storms.
  const client = await pool.connect();
  try {
    const result = await client.query(query, params);
    return result.rows;
  } finally {
    client.release();
  }
}
```

#### Production Standard (Correct)
> **Standard**: The guarded wrapper interrogates PostgreSQL 17's `pg_stat_io` for buffer pool churn before initiating execution. It establishes session-level fences (`synchronize_seqscans`, `max_parallel_workers_per_gather`, `effective_io_concurrency`, and `io_combine_limit`), dynamically constraining prefetch depth and parallel worker spawning to shield transactional buffers from eviction storms.
```typescript
import { Pool, PoolClient } from 'pg';

interface BufferPressureMetrics {
  backendEvictionsHigh: boolean;
  recommendedPrefetch: number;
  recommendedParallelWorkers: number;
}

export class DynamicPrefetchFencePool {
  constructor(private readonly pool: Pool) {}

  private async assessBufferPressure(client: PoolClient): Promise<BufferPressureMetrics> {
    // Inspect PG17 pg_stat_io and buffer pool pressure
    const { rows } = await client.query<{
      reuses: string;
      evictions: string;
    }>(
      `SELECT reuses, evictions 
       FROM pg_stat_io 
       WHERE backend_type = 'client backend' AND context = 'normal' 
       LIMIT 1;`
    );

    const evictions = Number(rows[0]?.evictions ?? 0);
    const backendEvictionsHigh = evictions > 10_000;

    return {
      backendEvictionsHigh,
      // Throttle prefetch depth under memory pressure
      recommendedPrefetch: backendEvictionsHigh ? 4 : 32,
      recommendedParallelWorkers: backendEvictionsHigh ? 0 : 2,
    };
  }

  public async executeGuardedScan<T>(query: string, params: unknown[]): Promise<T[]> {
    const client = await this.pool.connect();
    try {
      await client.query('BEGIN READ ONLY;');
      
      const metrics = await this.assessBufferPressure(client);

      // Postgres 17 Dynamic Streaming I/O and Prefetch Fence
      await client.query(
        `SET LOCAL synchronize_seqscans = on;
         SET LOCAL max_parallel_workers_per_gather = $1;
         SET LOCAL effective_io_concurrency = $2;
         SET LOCAL io_combine_limit = $3;`,
        [
          metrics.recommendedParallelWorkers,
          metrics.recommendedPrefetch,
          metrics.backendEvictionsHigh ? '32kB' : '128kB'
        ]
      );

      const result = await client.query(query, params);
      await client.query('COMMIT;');
      return result.rows as T[];
    } 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 Prefetch & Buffer Starvation Dynamic Fence

PostgreSQL 17 Streaming I/O Prefetch & Buffer Starvation Dynamic Fence defines an industrial-grade behavioral standard for backend engineering. Enforces dynamic calibration of PostgreSQL 17 streaming I/O prefetch depth and parallel worker allocation against active shared_buffers pressure and dirty page writeback ratios. Prevents parallel sequential scan workers from aggressively issuing speculative prefetch requests that displace transactional hot sets and trigger kernel-level page cache eviction storms. Mandates adaptive session-level GUC fencing and synchronized buffer-ring containment for concurrent analytical 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 Parallel Streaming Prefetch Saturating Buffer Rings and Evicting Transactional Working Sets 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 Streaming Prefetch Saturating Buffer Rings and Evicting Transactional Working SetsCritical
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 enforce adaptive session-level GUC configuration (`SET LOCAL io_combine_limit`, `SET LOCAL effective_io_concurrency`, and `SET LOCAL max_parallel_workers_per_gather`) for analytical pipelines based on current buffer allocation telemetry.

Engineers MUST ALWAYS enforce adaptive session-level GUC configuration (`SET LOCAL io_combine_limit`, `SET LOCAL effective_io_concurrency`, and `SET LOCAL max_parallel_workers_per_gather`) for analytical pipelines based on current buffer allocation telemetry.

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 large-scale parallel sequential scans on default pool connections without dynamic calibration against `pg_stat_io` backend and shared buffer pressure.

Engineers MUST NOT execute large-scale parallel sequential scans on default pool connections without dynamic calibration against `pg_stat_io` backend and shared buffer pressure.

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 sample `pg_stat_database` and `pg_stat_io` eviction markers (`buffers_backend`, `evictions`, `reuses`) prior to acquiring parallel scan workers to throttle prefetch sliding windows under high contention.

Code SHOULD sample `pg_stat_database` and `pg_stat_io` eviction markers (`buffers_backend`, `evictions`, `reuses`) prior to acquiring parallel scan workers to throttle prefetch sliding windows under high contention.

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 route concurrent analytical queries through dedicated, rate-limited connection pools configured with isolated buffer ring allocations and `synchronize_seqscans = on` to prevent uncoordinated disk head thrashing.

Services MUST route concurrent analytical queries through dedicated, rate-limited connection pools configured with isolated buffer ring allocations and `synchronize_seqscans = on` to prevent uncoordinated disk head thrashing.

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 execute raw ad-hoc analytical queries with unbounded parallel streaming prefetch on shared transactional (OLTP) connections.

Components NEVER execute raw ad-hoc analytical queries with unbounded parallel streaming prefetch on shared transactional (OLTP) connections.

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 Prefetch & Buffer Starvation Dynamic Fence critical for AI coding assistants?

Enforces dynamic calibration of PostgreSQL 17 streaming I/O prefetch depth and parallel worker allocation against active shared_buffers pressure and dirty page writeback ratios. Prevents parallel sequential scan workers from aggressively issuing speculative prefetch requests that displace transactional hot sets and trigger kernel-level page cache eviction storms. Mandates adaptive session-level GUC fencing and synchronized buffer-ring containment for concurrent analytical 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 Parallel Streaming Prefetch Saturating Buffer Rings and Evicting Transactional Working Sets) 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