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

PostgreSQL 17 Streaming I/O Direct Buffer Memory Pinning Invariant

PostgreSQL 17 introduces modernized Streaming I/O and direct I/O capabilities for sequential scans, index scans, and vector embeddings. This invariant mandates deterministic shared memory and direct I/O buffer management, preventing OS page cache double-buffering, memory exhaustion under concurrent scans, and unpinned buffer lock contention.

Prohibited Anti-Pattern: Unthrottled Concurrent Scans with Default I/O Invoking Kernel Page Cache Thrashing and OOM Kills

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 Streaming I/O Direct Buffer Memory Pinning Invariant - PostgreSQL 17 introduces modernized Streaming I/O and direct I/O capabilities for sequential scans, index scans, and vector embeddings. This invariant mandates deterministic shared memory and direct I/O buffer management, preventing OS page cache double-buffering, memory exhaustion under concurrent scans, and unpinned buffer lock contention.
globs: deployments/**/*.{yaml,yml}, infrastructure/**/*.{tf,hcl}, docker-compose*.{yaml,yml}, src/db/**/*.{ts,js,sql}, ansible/**/*.yml
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres17-streaming-io-direct-buffer-pinning-invariant"
---

# PostgreSQL 17 Streaming I/O Direct Buffer Memory Pinning Invariant

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: Unthrottled Concurrent Scans with Default I/O Invoking Kernel Page Cache Thrashing and OOM Kills

## System Constraints
You are a Principal Database Infrastructure Engineer enforcing the Postgres 17 Streaming I/O Direct Buffer Memory Pinning Invariant. Mandate deterministic shared_buffers allocation, explicit direct I/O configuration (io_method/io_combine_limit), and strict session-level work_mem/maintenance_work_mem bounding to prevent Linux page cache eviction loops and ensure predictable async vector/OLAP scans.

## Strict Guidelines
- Engineers MUST ALWAYS enable direct I/O for bulk read workflows in PostgreSQL 17 using `io_direct = 'data'` or `io_direct = 'data,wal'` where supported by underlying storage (XFS/ext4) to eliminate OS page cache double buffering.
- Engineers MUST configure `shared_buffers` to accommodate pin-heavy streaming reads deterministically without relying on host file-system readahead, setting it between 25% and 40% of dedicated RAM.
- Engineers MUST tune `io_combine_limit` alongside `streaming_read_ahead` parameters specifically to match cloud block storage striping and block sizes (typically 128kB to 512kB chunks) rather than leaving them unconstrained.
- Engineers MUST NOT allow unconstrained vector/OLAP analytics queries to scale parallel workers (`max_parallel_workers_per_gather`) without strict `work_mem` ceilings, preventing cumulative memory allocation from triggering Linux OOM killer.
- Services MUST monitor `pg_stat_io` for streaming I/O targets to ensure zero read-ahead stalls, verifying that `evictions`, `reuses`, and direct buffer pin latencies remain within sub-millisecond SLOs.
- Components NEVER execute ad-hoc high-dimensional pgvector or full-table analytic scans in the default transaction pooler; they MUST route to an isolated replica pool provisioned with dedicated direct I/O profiles.

## Bad vs Good Implementations
### Postgres 17 Direct I/O and Streaming Engine Configuration

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Omitting io_direct under high-throughput vector/OLAP reads forces PostgreSQL to double-buffer pages into Linux page cache and shared_buffers simultaneously. High work_mem combined with 8 parallel workers per gather risks node-wide OOM panics when multiple concurrent scans pin streaming read buffers.
```yaml
# Anti-Pattern: Uncalibrated PG17 configuration relying on OS cache and unbounded async readahead
apiVersion: v1
kind: ConfigMap
metadata:
  name: postgresql-config
data:
  postgresql.conf: |
    # Default / uncalibrated settings cause page-cache double buffering and OOM under concurrent vector scans
    shared_buffers = 1GB
    work_mem = 512MB
    max_parallel_workers_per_gather = 8
    # Default readahead without io_direct causes memory thrashing
    # io_direct is absent
    random_page_cost = 4.0

```

#### Production Standard (Correct)
> **Standard**: Configuring io_direct='data' enables Postgres 17's native streaming direct I/O engine, eliminating kernel page-cache replication and freeing RAM entirely for deterministic shared_buffers pinning. Bounded work_mem and io_combine_limit align memory access with modern block storage.
```yaml
# Production Invariant: Direct I/O configured with bounded parallel memory and optimized streaming combining
apiVersion: v1
kind: ConfigMap
metadata:
  name: postgresql-config
data:
  postgresql.conf: |
    # Direct I/O bypasses OS page cache, pinning streaming buffers safely inside shared_buffers
    shared_buffers = '16GB'                  # 25-40% dedicated memory for direct buffer allocation
    io_direct = 'data'                      # Postgres 17 direct I/O for bulk relations and streaming scans
    io_combine_limit = '256kB'              # Match underlying NVMe/EBS IOPS striping
    max_parallel_workers_per_gather = 4    # Bounded concurrency
    work_mem = '64MB'                       # Bounded per-worker allocation to eliminate OOM risk
    effective_cache_size = '48GB'
    track_io_timing = on                    # Required for streaming I/O diagnostic observability
    maintenance_work_mem = '2GB'

```

### TypeScript Knex/pg Driver Streaming & Stat Verification Invariant

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Inflating work_mem to 2GB across an unbounded Promise.all pool invites OOM crashes and bypasses connection-level streaming limits, saturating buffer rings without monitoring streaming I/O metrics.
```typescript
// Anti-Pattern: Firing unthrottled concurrent vector scans with ad-hoc parallel work_mem
import { Pool } from 'pg';

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

export async function runBulkVectorScans(queries: number[][]) {
  // Runs uncontrolled parallel scans across connection pool without checking pg_stat_io direct hit status
  return Promise.all(queries.map(vector => 
    pool.query('SET work_mem = "2GB"; SELECT id FROM embeddings ORDER BY vec <-> $1 LIMIT 10;', [JSON.stringify(vector)])
  ));
}
```

#### Production Standard (Correct)
> **Standard**: Routes requests through a dedicated direct-I/O enabled connection pool, enforces query concurrency via p-limit matching PostgreSQL worker configurations, and monitors pg_stat_io to guarantee direct buffer reading is operational.
```typescript
// Production Invariant: Direct buffer pinning awareness with bounded concurrency and pg_stat_io health validation
import { Pool } from 'pg';
import pLimit from 'p-limit';

const pool = new Pool({
  connectionString: process.env.DATABASE_REPLICA_DIRECT_IO_URL,
  max: 10,
});

const scanLimiter = pLimit(4); // Respect max_parallel_workers budget

export async function runControlledVectorScans(queries: number[][]): Promise<string[][]> {
  const client = await pool.connect();
  try {
    // Validate Postgres 17 streaming direct I/O state prior to batch processing
    const statIoCheck = await client.query(`
      SELECT context, io_object, reads, direct_reads 
      FROM pg_stat_io 
      WHERE context = 'normal' AND direct_reads > 0;
    `);
    
    if (statIoCheck.rowCount === 0) {
      console.warn('Postgres 17 Direct I/O streaming is inactive; fallback buffers in use.');
    }

    return await Promise.all(
      queries.map(vector =>
        scanLimiter(async () => {
          const result = await pool.query(
            'SELECT id FROM embeddings ORDER BY vec <-> $1::vector LIMIT 10;',
            [JSON.stringify(vector)]
          );
          return result.rows.map(r => r.id);
        })
      )
    );
  } 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: PostgreSQL 17 Streaming I/O Direct Buffer Memory Pinning Invariant

PostgreSQL 17 Streaming I/O Direct Buffer Memory Pinning Invariant defines an industrial-grade behavioral standard for devops engineering. PostgreSQL 17 introduces modernized Streaming I/O and direct I/O capabilities for sequential scans, index scans, and vector embeddings. This invariant mandates deterministic shared memory and direct I/O buffer management, preventing OS page cache double-buffering, memory exhaustion under concurrent scans, and unpinned buffer lock contention. 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: Unthrottled Concurrent Scans with Default I/O Invoking Kernel Page Cache Thrashing and OOM Kills 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 DriftUnthrottled Concurrent Scans with Default I/O Invoking Kernel Page Cache Thrashing and OOM KillsCritical
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 enable direct I/O for bulk read workflows in PostgreSQL 17 using `io_direct = 'data'` or `io_direct = 'data,wal'` where supported by underlying storage (XFS/ext4) to eliminate OS page cache double buffering.

Engineers MUST ALWAYS enable direct I/O for bulk read workflows in PostgreSQL 17 using `io_direct = 'data'` or `io_direct = 'data,wal'` where supported by underlying storage (XFS/ext4) to eliminate OS page cache double buffering.

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 configure `shared_buffers` to accommodate pin-heavy streaming reads deterministically without relying on host file-system readahead, setting it between 25% and 40% of dedicated RAM.

Engineers MUST configure `shared_buffers` to accommodate pin-heavy streaming reads deterministically without relying on host file-system readahead, setting it between 25% and 40% of dedicated RAM.

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 tune `io_combine_limit` alongside `streaming_read_ahead` parameters specifically to match cloud block storage striping and block sizes (typically 128kB to 512kB chunks) rather than leaving them unconstrained.

Engineers MUST tune `io_combine_limit` alongside `streaming_read_ahead` parameters specifically to match cloud block storage striping and block sizes (typically 128kB to 512kB chunks) rather than leaving them unconstrained.

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

Engineers MUST NOT allow unconstrained vector/OLAP analytics queries to scale parallel workers (`max_parallel_workers_per_gather`) without strict `work_mem` ceilings, preventing cumulative memory allocation from triggering Linux OOM killer.

Engineers MUST NOT allow unconstrained vector/OLAP analytics queries to scale parallel workers (`max_parallel_workers_per_gather`) without strict `work_mem` ceilings, preventing cumulative memory allocation from triggering Linux OOM killer.

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 streaming I/O targets to ensure zero read-ahead stalls, verifying that `evictions`, `reuses`, and direct buffer pin latencies remain within sub-millisecond SLOs.

Services MUST monitor `pg_stat_io` for streaming I/O targets to ensure zero read-ahead stalls, verifying that `evictions`, `reuses`, and direct buffer pin latencies remain within sub-millisecond SLOs.

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 ad-hoc high-dimensional pgvector or full-table analytic scans in the default transaction pooler

Components NEVER execute ad-hoc high-dimensional pgvector or full-table analytic scans in the default transaction pooler; they MUST route to an isolated replica pool provisioned with dedicated direct I/O profiles.

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 Direct Buffer Memory Pinning Invariant critical for AI coding assistants?

PostgreSQL 17 introduces modernized Streaming I/O and direct I/O capabilities for sequential scans, index scans, and vector embeddings. This invariant mandates deterministic shared memory and direct I/O buffer management, preventing OS page cache double-buffering, memory exhaustion under concurrent scans, and unpinned buffer lock contention. 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 (Unthrottled Concurrent Scans with Default I/O Invoking Kernel Page Cache Thrashing and OOM Kills) 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