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

PostgreSQL 17 Streaming Direct I/O Buffer Pool Invariant

Enforces PostgreSQL 17 streaming I/O configurations and ring-buffer allocation invariants across high-throughput backend services. Prevents kernel page-cache duplication, dirty write stalls, and NVMe bus contention during concurrent high-volume sequential scans and OLTP transactions.

Prohibited Anti-Pattern: Unbounded Sequential Scans and Default Direct I/O Mismatches Inducing OS Cache Double-Buffering

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 Streaming Direct I/O Buffer Pool Invariant - Enforces PostgreSQL 17 streaming I/O configurations and ring-buffer allocation invariants across high-throughput backend services. Prevents kernel page-cache duplication, dirty write stalls, and NVMe bus contention during concurrent high-volume sequential scans and OLTP transactions.
globs: src/infrastructure/database/**/*.ts, src/repositories/**/*.ts, src/config/database.ts, migrations/**/*.sql
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pg17-streaming-direct-io-buffer-guardrail"
---

# PostgreSQL 17 Streaming Direct I/O Buffer Pool Invariant

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Sequential Scans and Default Direct I/O Mismatches Inducing OS Cache Double-Buffering

## System Constraints
You are a Principal Database Architect enforcing PostgreSQL 17 Streaming I/O and Shared Buffer Pool Invariants. You must ensure all sequential scan queries, bulk ingestion pipelines, and connection pool configurations leverage PostgreSQL 17 streaming read abstractions and ring-buffer allocation guards without double-buffering into the OS page cache or evicting OLTP hot sets.

## Strict Guidelines
- Engineers MUST ALWAYS isolate heavy sequential read operations to dedicated bulk-read pool partitions that declare bounded ring-buffer parameters (e.g., SET LOCAL io_combine_limit and cursor-based windowing) to prevent eviction of active OLTP buffer pages.
- Engineers MUST NOT execute ad-hoc, unbounded table scans in primary transactional sessions where shared_buffers are allocated for low-latency point queries and hot row updates.
- Code SHOULD verify and telemetry-sample pg_stat_io eviction vectors (specifically reads, writes, and extends per context) to assert zero memory thrashing across shared vs. direct read contexts.
- Services MUST configure PostgreSQL 17 streaming I/O connection parameters with deterministic batch sizes aligned with underlying NVMe physical block chunk limits (e.g., matching or sub-dividing kernel direct I/O boundaries).
- Components NEVER execute raw bulk export or backfill operations without explicitly invoking session-scoped temporary buffer boundaries or read-ahead limits.

## Bad vs Good Implementations
### Configuring PostgreSQL 17 Bulk Streaming Context with Session-Level I/O Bounding

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Executing full sequential reads through standard pool connections without setting PostgreSQL 17 streaming parameters or cursor buffers evicts hot transactional cache blocks in shared_buffers, induces kernel double-buffering, and stalls active OLTP write transactions.
```typescript
import { PoolClient } from 'pg';

// ANTI-PATTERN: Executing an unthrottled, unbounded bulk sequential scan directly
// on the default transactional client pool. This causes PostgreSQL shared_buffers pollution,
// kernel page-cache duplication, and evicts hot OLTP working sets under concurrent write load.
export async function exportTransactionAuditLogs(client: PoolClient, tenantId: string): Promise<any[]> {
  const query = `
    SELECT id, payload, created_at 
    FROM audit_events 
    WHERE tenant_id = $1 
    ORDER BY created_at ASC;
  `;
  const result = await client.query(query, [tenantId]);
  return result.rows;
}
```

#### Production Standard (Correct)
> **Standard**: Enforces an isolated read-only transaction utilizing a bounded cursor, tuneable PG17 streaming parameters (io_combine_limit), and bulk chunk processing. This forces PostgreSQL to leverage localized buffer rings without thrashing global shared_buffers or corrupting the OS page cache.
```typescript
import { PoolClient } from 'pg';
import Cursor from 'pg-cursor';

// PRODUCTION FIX: Configure PostgreSQL 17 streaming read parameters inside an isolated
// transaction boundary, constraining ring-buffer allocation via cursor batching to saturate NVMe Direct I/O
// without polluting shared_buffers or inducing OS page-cache thrashing.
export async function exportTransactionAuditLogs(
  client: PoolClient,
  tenantId: string,
  onBatch: (rows: any[]) => Promise<void>
): Promise<void> {
  const BATCH_SIZE = 4096; // Aligned with PG17 streaming vector read allocations

  await client.query('BEGIN READ ONLY;');
  try {
    // Tune session-level streaming I/O vector combining for PG17 NVMe saturation
    await client.query('SET LOCAL io_combine_limit = "128kB";');
    await client.query('SET LOCAL synchronous_commit = OFF;');

    const queryText = `
      DECLARE audit_stream NO SCROLL CURSOR FOR 
      SELECT id, payload, created_at 
      FROM audit_events 
      WHERE tenant_id = $1 
      ORDER BY created_at ASC;
    `;
    await client.query(queryText, [tenantId]);

    let hasMore = true;
    while (hasMore) {
      const fetchRes = await client.query(`FETCH FORWARD ${BATCH_SIZE} FROM audit_stream;`);
      if (fetchRes.rows.length === 0) {
        hasMore = false;
      } else {
        await onBatch(fetchRes.rows);
      }
    }

    await client.query('CLOSE audit_stream;');
    await client.query('COMMIT;');
  } catch (error) {
    await client.query('ROLLBACK;');
    throw error;
  }
}
```

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 Streaming Direct I/O Buffer Pool Invariant

PostgreSQL 17 Streaming Direct I/O Buffer Pool Invariant defines an industrial-grade behavioral standard for backend engineering. Enforces PostgreSQL 17 streaming I/O configurations and ring-buffer allocation invariants across high-throughput backend services. Prevents kernel page-cache duplication, dirty write stalls, and NVMe bus contention during concurrent high-volume sequential scans and OLTP transactions. 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 Sequential Scans and Default Direct I/O Mismatches Inducing OS Cache Double-Buffering 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 Sequential Scans and Default Direct I/O Mismatches Inducing OS Cache Double-BufferingCritical
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 isolate heavy sequential read operations to dedicated bulk-read pool partitions that declare bounded ring-buffer parameters (e.g., SET LOCAL io_combine_limit and cursor-based windowing) to prevent eviction of active OLTP buffer pages.

Engineers MUST ALWAYS isolate heavy sequential read operations to dedicated bulk-read pool partitions that declare bounded ring-buffer parameters (e.g., SET LOCAL io_combine_limit and cursor-based windowing) to prevent eviction of active OLTP buffer 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.
2

Engineers MUST NOT execute ad-hoc, unbounded table scans in primary transactional sessions where shared_buffers are allocated for low-latency point queries and hot row updates.

Engineers MUST NOT execute ad-hoc, unbounded table scans in primary transactional sessions where shared_buffers are allocated for low-latency point queries and hot row updates.

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 verify and telemetry-sample pg_stat_io eviction vectors (specifically reads, writes, and extends per context) to assert zero memory thrashing across shared vs. direct read contexts.

Code SHOULD verify and telemetry-sample pg_stat_io eviction vectors (specifically reads, writes, and extends per context) to assert zero memory thrashing across shared vs. direct read contexts.

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 configure PostgreSQL 17 streaming I/O connection parameters with deterministic batch sizes aligned with underlying NVMe physical block chunk limits (e.g., matching or sub-dividing kernel direct I/O boundaries).

Services MUST configure PostgreSQL 17 streaming I/O connection parameters with deterministic batch sizes aligned with underlying NVMe physical block chunk limits (e.g., matching or sub-dividing kernel direct I/O 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.
5

Components NEVER execute raw bulk export or backfill operations without explicitly invoking session-scoped temporary buffer boundaries or read-ahead limits.

Components NEVER execute raw bulk export or backfill operations without explicitly invoking session-scoped temporary buffer boundaries or read-ahead limits.

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 Direct I/O Buffer Pool Invariant critical for AI coding assistants?

Enforces PostgreSQL 17 streaming I/O configurations and ring-buffer allocation invariants across high-throughput backend services. Prevents kernel page-cache duplication, dirty write stalls, and NVMe bus contention during concurrent high-volume sequential scans and OLTP transactions. 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 Sequential Scans and Default Direct I/O Mismatches Inducing OS Cache Double-Buffering) 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