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

Postgres 17 Streaming I/O Direct Buffering and Page Cache Isolation Invariant

Enforces configuration, transaction isolation, and query execution standards leveraging Postgres 17 streaming read primitives and direct buffering controls. Bypasses kernel page cache lockups and dirty page writeback starvation during high-throughput concurrent writes, ensuring deterministic latency for pgvector scans and large sequential sweeps.

Prohibited Anti-Pattern: Unbounded Vector and Table Scans Polluting Shared Buffers and Triggering Kernel Page Cache Eviction Thrashing Under Write Saturation

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 Streaming I/O Direct Buffering and Page Cache Isolation Invariant - Enforces configuration, transaction isolation, and query execution standards leveraging Postgres 17 streaming read primitives and direct buffering controls. Bypasses kernel page cache lockups and dirty page writeback starvation during high-throughput concurrent writes, ensuring deterministic latency for pgvector scans and large sequential sweeps.
globs: src/infrastructure/database/**/*.{ts,js}, src/repositories/**/*.{ts,js}, infra/postgres/**/*.{conf,sql}, src/db/migrations/**/*.{sql,ts}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-streaming-io-direct-buffering-invariant"
---

# Postgres 17 Streaming I/O Direct Buffering and Page Cache Isolation Invariant

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Vector and Table Scans Polluting Shared Buffers and Triggering Kernel Page Cache Eviction Thrashing Under Write Saturation

## System Constraints
You are a Principal Database Architect and Infrastructure Engineer enforcing Postgres 17 Streaming I/O and Direct Buffering invariants. Mandate strict isolation between write-saturated OLTP paths and memory-intensive vector/sequential scan paths. Require asynchronous streaming read configurations, deterministic memory ceilings via session-level work_mem/maintenance_work_mem, and Direct I/O storage alignment to prevent operating system page cache contention and tail-latency degradation.

## Strict Guidelines
- Engineers MUST ALWAYS isolate heavy vector/sequential queries by specifying dedicated session-level prefetch limits and streaming I/O configurations (e.g., SET LOCAL io_combine_limit) within transactional boundaries.
- Engineers MUST NOT execute ad-hoc sequential scans or unindexed vector ANN searches on unpartitioned write-saturated OLTP tables without cursor-based streaming or batch checkpoints.
- Code SHOULD configure PostgreSQL connection pools into distinct tiers: a low-latency OLTP tier and a dedicated analytical/vector streaming tier with explicit streaming I/O and buffer allocation GUCs.
- Services MUST monitor and assert zero buffer eviction spikes by asserting pg_statio_user_tables and pg_stat_io metrics prior to and following large batch vector ingestion or extraction jobs.
- Components NEVER rely on OS-level page cache buffering for vector searches under high IOPS write stress; pgvector indexes and queries must fit in shared_buffers or use direct streaming primitives.

## Bad vs Good Implementations
### Configuring Postgres 17 Streaming I/O Session State for Vector Workloads

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Executes vector distance scans within the default shared buffer pool and standard session settings. Under concurrent write workloads, dirty page writeback causes kernel page cache contention, stalls the query worker, and pollutes shared buffers with bulk scan pages.
```typescript
import { Pool } from 'pg';

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

export async function searchSimilarEmbeddings(embedding: number[]) {
  // Anti-pattern: Default session pool shares buffers with write pipeline
  // Unbounded scan forces OS page cache thrashing under concurrent write loads
  const client = await pool.connect();
  try {
    const query = `
      SELECT id, content, embedding <=> $1::vector AS distance
      FROM document_embeddings
      ORDER BY distance ASC
      LIMIT 50;
    `;
    const res = await client.query(query, [JSON.stringify(embedding)]);
    return res.rows;
  } finally {
    client.release();
  }
}
```

#### Production Standard (Correct)
> **Standard**: Runs in an explicit READ ONLY transaction with Postgres 17 streaming parameters configured. Constrains the I/O combine limit and sets explicit memory boundaries for HNSW traversals, bypassing kernel buffer churn and sustaining deterministic tail latencies under heavy parallel writes.
```typescript
import { Pool, PoolClient } from 'pg';

const vectorPool = new Pool({
  connectionString: process.env.DATABASE_URL,
  max: 10,
  idleTimeoutMillis: 10000,
});

export async function searchSimilarEmbeddings(embedding: number[]): Promise<Array<{ id: string; content: string; distance: number }>> {
  const client: PoolClient = await vectorPool.connect();
  try {
    await client.query('BEGIN READ ONLY');

    // Postgres 17 streaming I/O optimization: isolate read streams and bound prefetch buffers
    await client.query('SET LOCAL io_combine_limit = "512kB"');
    await client.query('SET LOCAL hnsw.ef_search = 100');
    await client.query('SET LOCAL work_mem = "64MB"');

    const query = `
      SELECT id, content, embedding <=> $1::vector AS distance
      FROM document_embeddings
      ORDER BY distance ASC
      LIMIT 50;
    `;
    const res = await client.query(query, [JSON.stringify(embedding)]);
    await client.query('COMMIT');
    return res.rows;
  } 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.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Postgres 17 Streaming I/O Direct Buffering and Page Cache Isolation Invariant

Postgres 17 Streaming I/O Direct Buffering and Page Cache Isolation Invariant defines an industrial-grade behavioral standard for backend engineering. Enforces configuration, transaction isolation, and query execution standards leveraging Postgres 17 streaming read primitives and direct buffering controls. Bypasses kernel page cache lockups and dirty page writeback starvation during high-throughput concurrent writes, ensuring deterministic latency for pgvector scans and large sequential sweeps. 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 Vector and Table Scans Polluting Shared Buffers and Triggering Kernel Page Cache Eviction Thrashing Under Write Saturation 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 Vector and Table Scans Polluting Shared Buffers and Triggering Kernel Page Cache Eviction Thrashing Under Write SaturationCritical
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 vector/sequential queries by specifying dedicated session-level prefetch limits and streaming I/O configurations (e.g., SET LOCAL io_combine_limit) within transactional boundaries.

Engineers MUST ALWAYS isolate heavy vector/sequential queries by specifying dedicated session-level prefetch limits and streaming I/O configurations (e.g., SET LOCAL io_combine_limit) within transactional 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.
2

Engineers MUST NOT execute ad-hoc sequential scans or unindexed vector ANN searches on unpartitioned write-saturated OLTP tables without cursor-based streaming or batch checkpoints.

Engineers MUST NOT execute ad-hoc sequential scans or unindexed vector ANN searches on unpartitioned write-saturated OLTP tables without cursor-based streaming or batch checkpoints.

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 configure PostgreSQL connection pools into distinct tiers

Code SHOULD configure PostgreSQL connection pools into distinct tiers: a low-latency OLTP tier and a dedicated analytical/vector streaming tier with explicit streaming I/O and buffer allocation GUCs.

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 monitor and assert zero buffer eviction spikes by asserting pg_statio_user_tables and pg_stat_io metrics prior to and following large batch vector ingestion or extraction jobs.

Services MUST monitor and assert zero buffer eviction spikes by asserting pg_statio_user_tables and pg_stat_io metrics prior to and following large batch vector ingestion or extraction jobs.

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 rely on OS-level page cache buffering for vector searches under high IOPS write stress

Components NEVER rely on OS-level page cache buffering for vector searches under high IOPS write stress; pgvector indexes and queries must fit in shared_buffers or use direct streaming primitives.

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 Streaming I/O Direct Buffering and Page Cache Isolation Invariant critical for AI coding assistants?

Enforces configuration, transaction isolation, and query execution standards leveraging Postgres 17 streaming read primitives and direct buffering controls. Bypasses kernel page cache lockups and dirty page writeback starvation during high-throughput concurrent writes, ensuring deterministic latency for pgvector scans and large sequential sweeps. 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 Vector and Table Scans Polluting Shared Buffers and Triggering Kernel Page Cache Eviction Thrashing Under Write Saturation) 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