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

Postgres 17 Asynchronous Direct I/O and Pinned Memory Clamping Standard

Enforces strict upper bounds on asynchronous Direct I/O prefetch concurrency and pinned shared buffer pages during high-volume analytical queries. Prevents kernel queue exhaustion, buffer pool thrashing, and out-of-memory worker panics in Postgres 17 Direct I/O workloads. Mandates session-level GUC clamping and bounded cursor streaming for large sequential scans.

Prohibited Anti-Pattern: Unbounded Direct I/O prefetch concurrency causing buffer cache thrashing and worker OOM

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 Asynchronous Direct I/O and Pinned Memory Clamping Standard - Enforces strict upper bounds on asynchronous Direct I/O prefetch concurrency and pinned shared buffer pages during high-volume analytical queries. Prevents kernel queue exhaustion, buffer pool thrashing, and out-of-memory worker panics in Postgres 17 Direct I/O workloads. Mandates session-level GUC clamping and bounded cursor streaming for large sequential scans.
globs: src/db/**/*.ts, src/repositories/**/*.ts, src/services/**/*.ts, migrations/**/*.sql
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-async-direct-io-clamping"
---

# Postgres 17 Asynchronous Direct I/O and Pinned Memory Clamping Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Direct I/O prefetch concurrency causing buffer cache thrashing and worker OOM

## System Constraints
You are a Principal Database Architect and Backend Engineer enforcing Postgres 17 Asynchronous Direct I/O Memory Clamping. You must ensure all analytical scanning pipelines clamp asynchronous block prefetching, limit pinned buffer concurrency, and never overwhelm the direct I/O ring buffer or OS page cache bypass paths.

## Strict Guidelines
- Engineers MUST ALWAYS explicitly clamp `effective_io_concurrency` and session `work_mem` within analytical transactions utilizing direct or asynchronous I/O paths.
- Engineers MUST NOT allow unthrottled parallel table scans to monopolize shared buffer descriptors without bounding `max_parallel_workers_per_gather` and read prefetch window limits.
- Database access layers MUST configure cursor batch fetching with explicit low watermarks when streaming analytical datasets to prevent backend pinned buffer exhaustion.
- Code SHOULD verify Postgres 17 runtime settings (`io_direct`, `io_combine_limit`) before dispatching massive sequential scans across partitioned structures.
- Components NEVER issue unbounded `SELECT *` or large-scale sequential scan queries without explicit bounded session-level prefetch limits (`SET LOCAL effective_io_concurrency = ...`).

## Bad vs Good Implementations
### Analytical Stream Execution with Session Memory and Prefetch Clamping

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Executing analytical queries with default session settings in Postgres 17 Direct I/O environments allows async prefetching to flood the shared memory pool, pin large volumes of buffer pages, and cause memory exhaustion across parallel gather workers.
```typescript
import { Pool } from 'pg';

export async function streamHighVolumeAnalytics(pool: Pool, tenantId: string) {
  const client = await pool.connect();
  try {
    // ANTI-PATTERN: Default unbounded prefetching with no session clamp
    // High concurrency analytical queries will trigger aggressive async direct I/O prefetch,
    // pinning excessive shared buffer pages and causing memory thrashing.
    const cursorQuery = `
      SELECT * FROM analytical_events 
      WHERE tenant_id = $1 
      ORDER BY created_at DESC
    `;
    const result = await client.query(cursorQuery, [tenantId]);
    return result.rows;
  } finally {
    client.release();
  }
}
```

#### Production Standard (Correct)
> **Standard**: Explicitly bounds `effective_io_concurrency`, `max_parallel_workers_per_gather`, and `work_mem` within a transaction. Uses chunked cursor reads to ensure direct I/O buffer pinning remains bounded, preventing memory exhaustion.
```typescript
import { Pool, PoolClient } from 'pg';
import Cursor from 'pg-cursor';

export async function streamHighVolumeAnalytics(
  pool: Pool, 
  tenantId: string,
  processChunk: (rows: unknown[]) => Promise<void>
): Promise<void> {
  const client: PoolClient = await pool.connect();
  const BATCH_SIZE = 1000;

  try {
    await client.query('BEGIN READ ONLY');

    // CLAMP: Restrict prefetch concurrency and worker allocations for this direct-read transaction
    await client.query('SET LOCAL effective_io_concurrency = 8');
    await client.query('SET LOCAL max_parallel_workers_per_gather = 2');
    await client.query('SET LOCAL work_mem = "64MB"');

    const cursor = client.query(new Cursor(
      `SELECT id, payload, created_at 
       FROM analytical_events 
       WHERE tenant_id = $1 
       ORDER BY created_at DESC`,
      [tenantId]
    ));

    let rows = await cursor.read(BATCH_SIZE);
    while (rows.length > 0) {
      await processChunk(rows);
      rows = await cursor.read(BATCH_SIZE);
    }

    await cursor.close();
    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 Asynchronous Direct I/O and Pinned Memory Clamping Standard

Postgres 17 Asynchronous Direct I/O and Pinned Memory Clamping Standard defines an industrial-grade behavioral standard for backend engineering. Enforces strict upper bounds on asynchronous Direct I/O prefetch concurrency and pinned shared buffer pages during high-volume analytical queries. Prevents kernel queue exhaustion, buffer pool thrashing, and out-of-memory worker panics in Postgres 17 Direct I/O workloads. Mandates session-level GUC clamping and bounded cursor streaming for large sequential scans. 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 Direct I/O prefetch concurrency causing buffer cache thrashing and worker OOM 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 Direct I/O prefetch concurrency causing buffer cache thrashing and worker OOMCritical
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 explicitly clamp `effective_io_concurrency` and session `work_mem` within analytical transactions utilizing direct or asynchronous I/O paths.

Engineers MUST ALWAYS explicitly clamp `effective_io_concurrency` and session `work_mem` within analytical transactions utilizing direct or asynchronous I/O paths.

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 unthrottled parallel table scans to monopolize shared buffer descriptors without bounding `max_parallel_workers_per_gather` and read prefetch window limits.

Engineers MUST NOT allow unthrottled parallel table scans to monopolize shared buffer descriptors without bounding `max_parallel_workers_per_gather` and read prefetch window 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.
3

Database access layers MUST configure cursor batch fetching with explicit low watermarks when streaming analytical datasets to prevent backend pinned buffer exhaustion.

Database access layers MUST configure cursor batch fetching with explicit low watermarks when streaming analytical datasets to prevent backend pinned buffer exhaustion.

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 Postgres 17 runtime settings (`io_direct`, `io_combine_limit`) before dispatching massive sequential scans across partitioned structures.

Code SHOULD verify Postgres 17 runtime settings (`io_direct`, `io_combine_limit`) before dispatching massive sequential scans across partitioned structures.

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 `SELECT *` or large-scale sequential scan queries without explicit bounded session-level prefetch limits (`SET LOCAL effective_io_concurrency = ...`).

Components NEVER issue unbounded `SELECT *` or large-scale sequential scan queries without explicit bounded session-level prefetch limits (`SET LOCAL effective_io_concurrency = ...`).

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 Asynchronous Direct I/O and Pinned Memory Clamping Standard critical for AI coding assistants?

Enforces strict upper bounds on asynchronous Direct I/O prefetch concurrency and pinned shared buffer pages during high-volume analytical queries. Prevents kernel queue exhaustion, buffer pool thrashing, and out-of-memory worker panics in Postgres 17 Direct I/O workloads. Mandates session-level GUC clamping and bounded cursor streaming for large sequential scans. 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 Direct I/O prefetch concurrency causing buffer cache thrashing and worker OOM) 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