This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: postgres-17-aio-worker-saturation-standard

Postgres 17 Asynchronous Direct I/O (AIO) Worker Saturation and Direct Cache Bypassing

Regulates OS page cache bypass (O_DIRECT) and concurrent asynchronous I/O prefetch boundaries in PostgreSQL 17 environments. Prevents Linux kernel page cache lock contention and thread exhaustion by sizing io_uring queue depths, vectorized bulk scans, and maintenance worker limits relative to system NVMe queue capacities.

Prohibited Anti-Pattern: Unconstrained Asynchronous Direct I/O Prefetch Triggering Kernel io_uring Ring Overflow and NVMe Queue Starvation

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 Asynchronous Direct I/O (AIO) Worker Saturation and Direct Cache Bypassing - Regulates OS page cache bypass (O_DIRECT) and concurrent asynchronous I/O prefetch boundaries in PostgreSQL 17 environments. Prevents Linux kernel page cache lock contention and thread exhaustion by sizing io_uring queue depths, vectorized bulk scans, and maintenance worker limits relative to system NVMe queue capacities.
globs: src/database/**/*.{ts,js,sql}, infra/postgres/**/*.conf, src/repositories/**/*.{ts,js}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-aio-worker-saturation-standard"
---

# Postgres 17 Asynchronous Direct I/O (AIO) Worker Saturation and Direct Cache Bypassing

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unconstrained Asynchronous Direct I/O Prefetch Triggering Kernel io_uring Ring Overflow and NVMe Queue Starvation

## System Constraints
You are a Principal Database Architect enforcing the PostgreSQL 17 Asynchronous Direct I/O (AIO) Worker Saturation Standard. You must strictly enforce balanced AIO queue allocation, direct I/O boundary constraints, and connection pooling controls that prevent POSIX thread pool fallback, kernel submission lock contention, and storage controller saturation under vectorized analytic scans.

## Strict Guidelines
- Engineers MUST ALWAYS configure `io_method = io_uring` on supported Linux kernels (>= 6.1) and ensure `io_uring` ring depth does not exceed hardware queue depth per core.
- Engineers MUST NOT configure unconstrained `effective_io_concurrency` on sequential and parallel table scans when Direct I/O is enabled, capping concurrency based on NVMe channel count (max 256 per instance).
- Services MUST route bulk analytic scans through connection pools with strict queue throttling to prevent aggregate outstanding AIO operations from exceeding system `io_uring_entries` limits.
- Code SHOULD enforce `SET LOCAL synchronize_seqscans = on` during concurrent multi-gigabyte table scans to leverage shared storage read ring buffers.
- Engineers MUST NEVER execute unbuffered sequential scans (`io_direct = data`) concurrently with unbounded `max_parallel_workers_per_gather` without setting explicit per-session `maintenance_io_concurrency` and `work_mem` ceilings.

## Bad vs Good Implementations
### Analytic Query Executor with Controlled AIO Prefetch and Direct I/O Isolation

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Arbitrarily forcing high effective_io_concurrency and max_parallel_workers_per_gather across high connection pools exhausts kernel io_uring submission queues when Postgres 17 Direct I/O is active, leading to thread thrashing, memory bloat, and degraded system I/O latency.
```typescript
import { Pool } from 'pg';

const pool = new Pool({ max: 50 });

export async function runAnalyticalScan(tenantId: string): Promise<any[]> {
  const client = await pool.connect();
  try {
    // ANTI-PATTERN: Uncapped parallel scan with high concurrency forces AIO worker
    // saturation, kernel ring exhaustion, and bypasses memory-bounded prefetch quotas.
    await client.query('SET max_parallel_workers_per_gather = 16;');
    await client.query('SET effective_io_concurrency = 1000;');
    await client.query('SET synchronize_seqscans = off;');

    const result = await client.query(
      'SELECT id, payload FROM large_telemetry_events WHERE tenant_id = $1 ORDER BY created_at DESC',
      [tenantId]
    );
    return result.rows;
  } finally {
    client.release();
  }
}
```

#### Production Standard (Correct)
> **Standard**: Uses an isolated analytical pool with strictly bounded worker pools, enables synchronize_seqscans to share buffer access, caps effective_io_concurrency to match NVMe queue capabilities, and guarantees transactional isolation for session GUCs via SET LOCAL.
```typescript
import { Pool, PoolClient } from 'pg';

// Guarded connection pool specifically reserved for direct-I/O intensive analytical workloads
const analyticPool = new Pool({
  max: 8,
  connectionTimeoutMillis: 5000,
});

export async function runAnalyticalScan(tenantId: string): Promise<any[]> {
  const client: PoolClient = await analyticPool.connect();
  try {
    await client.query('BEGIN READ ONLY;');
    // Set bounded IO concurrency calibrated to storage NVMe channels and prevent io_uring ring overflow
    await client.query('SET LOCAL max_parallel_workers_per_gather = 4;');
    await client.query('SET LOCAL effective_io_concurrency = 64;');
    await client.query('SET LOCAL synchronize_seqscans = on;');
    await client.query('SET LOCAL work_mem = "64MB";');

    const result = await client.query(
      'SELECT id, payload FROM large_telemetry_events WHERE tenant_id = $1 ORDER BY created_at DESC',
      [tenantId]
    );
    await client.query('COMMIT;');
    return result.rows;
  } catch (err) {
    await client.query('ROLLBACK;');
    throw err;
  } 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 (AIO) Worker Saturation and Direct Cache Bypassing

Postgres 17 Asynchronous Direct I/O (AIO) Worker Saturation and Direct Cache Bypassing defines an industrial-grade behavioral standard for backend engineering. Regulates OS page cache bypass (O_DIRECT) and concurrent asynchronous I/O prefetch boundaries in PostgreSQL 17 environments. Prevents Linux kernel page cache lock contention and thread exhaustion by sizing io_uring queue depths, vectorized bulk scans, and maintenance worker limits relative to system NVMe queue capacities. 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: Unconstrained Asynchronous Direct I/O Prefetch Triggering Kernel io_uring Ring Overflow and NVMe Queue Starvation 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 DriftUnconstrained Asynchronous Direct I/O Prefetch Triggering Kernel io_uring Ring Overflow and NVMe Queue StarvationCritical
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 configure `io_method = io_uring` on supported Linux kernels (>= 6.1) and ensure `io_uring` ring depth does not exceed hardware queue depth per core.

Engineers MUST ALWAYS configure `io_method = io_uring` on supported Linux kernels (>= 6.1) and ensure `io_uring` ring depth does not exceed hardware queue depth per core.

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 configure unconstrained `effective_io_concurrency` on sequential and parallel table scans when Direct I/O is enabled, capping concurrency based on NVMe channel count (max 256 per instance).

Engineers MUST NOT configure unconstrained `effective_io_concurrency` on sequential and parallel table scans when Direct I/O is enabled, capping concurrency based on NVMe channel count (max 256 per instance).

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

Services MUST route bulk analytic scans through connection pools with strict queue throttling to prevent aggregate outstanding AIO operations from exceeding system `io_uring_entries` limits.

Services MUST route bulk analytic scans through connection pools with strict queue throttling to prevent aggregate outstanding AIO operations from exceeding system `io_uring_entries` 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.
4

Code SHOULD enforce `SET LOCAL synchronize_seqscans = on` during concurrent multi-gigabyte table scans to leverage shared storage read ring buffers.

Code SHOULD enforce `SET LOCAL synchronize_seqscans = on` during concurrent multi-gigabyte table scans to leverage shared storage read ring buffers.

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

Engineers MUST NEVER execute unbuffered sequential scans (`io_direct = data`) concurrently with unbounded `max_parallel_workers_per_gather` without setting explicit per-session `maintenance_io_concurrency` and `work_mem` ceilings.

Engineers MUST NEVER execute unbuffered sequential scans (`io_direct = data`) concurrently with unbounded `max_parallel_workers_per_gather` without setting explicit per-session `maintenance_io_concurrency` and `work_mem` ceilings.

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 (AIO) Worker Saturation and Direct Cache Bypassing critical for AI coding assistants?

Regulates OS page cache bypass (O_DIRECT) and concurrent asynchronous I/O prefetch boundaries in PostgreSQL 17 environments. Prevents Linux kernel page cache lock contention and thread exhaustion by sizing io_uring queue depths, vectorized bulk scans, and maintenance worker limits relative to system NVMe queue capacities. 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 (Unconstrained Asynchronous Direct I/O Prefetch Triggering Kernel io_uring Ring Overflow and NVMe Queue Starvation) 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