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

Postgres 17 Streaming I/O and Parallel Hash Execution Budgeting

Enforces strict session-level resource admission controls and deterministic parameter scaling for PostgreSQL 17 streaming read operations and parallel hash join execution. Prevents kernel page cache eviction spirals, NVMe queue saturation, and Out-Of-Memory (OOM) terminations caused by unconstrained concurrent analytical OLAP queries.

Prohibited Anti-Pattern: Global Memory Oversubscription and Unbounded Parallel Streaming I/O Prefetching

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 Streaming I/O and Parallel Hash Execution Budgeting - Enforces strict session-level resource admission controls and deterministic parameter scaling for PostgreSQL 17 streaming read operations and parallel hash join execution. Prevents kernel page cache eviction spirals, NVMe queue saturation, and Out-Of-Memory (OOM) terminations caused by unconstrained concurrent analytical OLAP queries.
globs: src/database/**/*.ts, src/repositories/**/*.ts, src/services/analytics/**/*.ts, src/infra/db/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-streaming-io-parallel-budgeting"
---

# Postgres 17 Streaming I/O and Parallel Hash Execution Budgeting

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Global Memory Oversubscription and Unbounded Parallel Streaming I/O Prefetching

## System Constraints
You are a Principal Database Architect and Infrastructure Engineer specializing in PostgreSQL 17 internals, kernel page cache mechanics, and high-concurrency OLAP query engine optimization. Mandate strict budgeting invariants across dynamic session configurations, parallel worker allocation, and streaming I/O controls. Ensure no analytical query executes without deterministic memory ceiling calculation and transactional session-local GUC application.

## Strict Guidelines
- Engineers MUST ALWAYS calculate aggregate memory ceilings dynamically prior to running parallel OLAP queries using the formula: Total Peak Memory = (max_parallel_workers_per_gather + 1) * work_mem * hash_mem_multiplier.
- Engineers MUST NOT rely on static, high global 'work_mem' or 'maintenance_work_mem' configurations inside 'postgresql.conf' for OLAP workloads, as concurrent parallel scans will overcommit physical RAM and trigger the OS OOM killer.
- Services MUST execute analytical queries within explicit transactions that apply dynamic 'SET LOCAL' statements for 'work_mem', 'hash_mem_multiplier', and 'max_parallel_workers_per_gather', guaranteeing settings are discarded upon transaction commit or rollback.
- Engineers MUST configure Postgres 17 streaming I/O parameters ('io_combine_limit' and table-level prefetch limits) proportionate to underlying storage controller queue depths (e.g., NVMe max submission queue depth) to avoid kernel direct-I/O lock contention.
- Components NEVER execute unbounded analytical queries with 'max_parallel_workers_per_gather' exceeding physical CPU core availability divided by peak concurrent analytical tenants.
- Services MUST implement concurrency admission control (e.g., token-bucket or semaphore based on cumulative allocated memory) before dispatching parallel hash joins to PostgreSQL.

## Bad vs Good Implementations
### Dynamic Session-Local Budgeting for Parallel Analytical Queries

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Fails to enforce query-level resource budgets. Relies on cluster-wide defaults, creating an explosive memory footprint during concurrent parallel hash joins and streaming sequential scans that evict hot pages from the OS buffer cache.
```typescript
import { Pool } from 'pg';

const pool = new Pool();

export async function runOlapAggregation(tenantId: string, startDate: Date): Promise<any[]> {
  // Anti-Pattern: Executes directly against global defaults or unconstrained connection pools.
  // A high number of parallel workers combined with Hash Joins will allocate (workers + 1) * work_mem * hash_mem_multiplier,
  // causing severe memory pressure and thrashing the OS buffer cache across concurrent executions.
  const query = `
    SELECT category_id, SUM(amount), COUNT(DISTINCT customer_id)
    FROM large_fact_orders
    WHERE tenant_id = $1 AND order_date >= $2
    GROUP BY category_id;
  `;
  const result = await pool.query(query, [tenantId, startDate]);
  return result.rows;
}
```

#### Production Standard (Correct)
> **Standard**: Calculates an explicit memory budget based on physical concurrency and memory capacity, applying transactional 'SET LOCAL' overrides for Postgres 17 streaming parameters, workers, and hash multipliers before execution.
```typescript
import { Pool, PoolClient } from 'pg';

interface ResourceBudget {
  maxWorkers: number;
  workMemMb: number;
  hashMemMultiplier: number;
  ioCombineLimitKb: number;
}

export class OlapExecutionService {
  constructor(
    private readonly pool: Pool,
    private readonly maxHostMemoryMb: number = 16384
  ) {}

  private calculateDynamicBudget(concurrentQueries: number): ResourceBudget {
    // Bound per-query parallel memory footprint: (workers + 1) * work_mem * hash_mem_multiplier
    const allocatableRam = this.maxHostMemoryMb * 0.6; // Dedicate 60% of host RAM to active parallel queries
    const queryShare = Math.floor(allocatableRam / Math.max(1, concurrentQueries));
    
    const maxWorkers = Math.min(4, Math.max(1, Math.floor(navigator.hardwareConcurrency ? navigator.hardwareConcurrency / 2 : 2)));
    const hashMemMultiplier = 2.0;
    const totalFactor = (maxWorkers + 1) * hashMemMultiplier;
    const workMemMb = Math.max(8, Math.floor(queryShare / totalFactor));

    return {
      maxWorkers,
      workMemMb,
      hashMemMultiplier,
      ioCombineLimitKb: 128 // Aligned with optimal Linux kernel streaming I/O vector chunks
    };
  }

  public async executeBudgetedAggregation(tenantId: string, startDate: Date, activeConcurrency: number): Promise<any[]> {
    const budget = this.calculateDynamicBudget(activeConcurrency);
    const client: PoolClient = await this.pool.connect();

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

      // Invariant: Apply deterministic GUC constraints scoped strictly to this transaction
      await client.query(`SET LOCAL max_parallel_workers_per_gather = ${budget.maxWorkers};`);
      await client.query(`SET LOCAL work_mem = '${budget.workMemMb}MB';`);
      await client.query(`SET LOCAL hash_mem_multiplier = ${budget.hashMemMultiplier};`);
      // Postgres 17 streaming read optimization
      await client.query(`SET LOCAL io_combine_limit = '${budget.ioCombineLimitKb}kB';`);

      const query = `
        SELECT category_id, SUM(amount), COUNT(DISTINCT customer_id)
        FROM large_fact_orders
        WHERE tenant_id = $1 AND order_date >= $2
        GROUP BY category_id;
      `;
      const result = await client.query(query, [tenantId, startDate]);

      await client.query('COMMIT');
      return result.rows;
    } catch (error) {
      await client.query('ROLLBACK');
      throw error;
    } finally {
      client.release();
    }
  }
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Postgres 17 Streaming I/O and Parallel Hash Execution Budgeting

Postgres 17 Streaming I/O and Parallel Hash Execution Budgeting defines an industrial-grade behavioral standard for backend engineering. Enforces strict session-level resource admission controls and deterministic parameter scaling for PostgreSQL 17 streaming read operations and parallel hash join execution. Prevents kernel page cache eviction spirals, NVMe queue saturation, and Out-Of-Memory (OOM) terminations caused by unconstrained concurrent analytical OLAP queries. 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: Global Memory Oversubscription and Unbounded Parallel Streaming I/O Prefetching 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 DriftGlobal Memory Oversubscription and Unbounded Parallel Streaming I/O PrefetchingCritical
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 calculate aggregate memory ceilings dynamically prior to running parallel OLAP queries using the formula

Engineers MUST ALWAYS calculate aggregate memory ceilings dynamically prior to running parallel OLAP queries using the formula: Total Peak Memory = (max_parallel_workers_per_gather + 1) * work_mem * hash_mem_multiplier.

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 rely on static, high global 'work_mem' or 'maintenance_work_mem' configurations inside 'postgresql.conf' for OLAP workloads, as concurrent parallel scans will overcommit physical RAM and trigger the OS OOM killer.

Engineers MUST NOT rely on static, high global 'work_mem' or 'maintenance_work_mem' configurations inside 'postgresql.conf' for OLAP workloads, as concurrent parallel scans will overcommit physical RAM and trigger the OS 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.
3

Services MUST execute analytical queries within explicit transactions that apply dynamic 'SET LOCAL' statements for 'work_mem', 'hash_mem_multiplier', and 'max_parallel_workers_per_gather', guaranteeing settings are discarded upon transaction commit or rollback.

Services MUST execute analytical queries within explicit transactions that apply dynamic 'SET LOCAL' statements for 'work_mem', 'hash_mem_multiplier', and 'max_parallel_workers_per_gather', guaranteeing settings are discarded upon transaction commit or rollback.

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 configure Postgres 17 streaming I/O parameters ('io_combine_limit' and table-level prefetch limits) proportionate to underlying storage controller queue depths (e.g., NVMe max submission queue depth) to avoid kernel direct-I/O lock contention.

Engineers MUST configure Postgres 17 streaming I/O parameters ('io_combine_limit' and table-level prefetch limits) proportionate to underlying storage controller queue depths (e.g., NVMe max submission queue depth) to avoid kernel direct-I/O lock contention.

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 unbounded analytical queries with 'max_parallel_workers_per_gather' exceeding physical CPU core availability divided by peak concurrent analytical tenants.

Components NEVER execute unbounded analytical queries with 'max_parallel_workers_per_gather' exceeding physical CPU core availability divided by peak concurrent analytical tenants.

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

Services MUST implement concurrency admission control (e.g., token-bucket or semaphore based on cumulative allocated memory) before dispatching parallel hash joins to PostgreSQL.

Services MUST implement concurrency admission control (e.g., token-bucket or semaphore based on cumulative allocated memory) before dispatching parallel hash joins to PostgreSQL.

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 and Parallel Hash Execution Budgeting critical for AI coding assistants?

Enforces strict session-level resource admission controls and deterministic parameter scaling for PostgreSQL 17 streaming read operations and parallel hash join execution. Prevents kernel page cache eviction spirals, NVMe queue saturation, and Out-Of-Memory (OOM) terminations caused by unconstrained concurrent analytical OLAP queries. 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 (Global Memory Oversubscription and Unbounded Parallel Streaming I/O Prefetching) 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