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

PostgreSQL 17 Direct I/O and Memory Accounting Guardrail

Enforces strict memory budgeting and I/O engine configuration for PostgreSQL 17 workloads to prevent kernel page-cache double-buffering and Out-Of-Memory (OOM) failures under heavy analytical concurrency. Mandates explicit session-level work_mem budgeting per parallel worker and ensures database connection pools dynamically constrain concurrent analytical execution based on physical memory thresholds.

Prohibited Anti-Pattern: Unbounded Parallel Query Execution and Global work_mem Reliance Leading to OS OOM-Killer Invocations

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 Direct I/O and Memory Accounting Guardrail - Enforces strict memory budgeting and I/O engine configuration for PostgreSQL 17 workloads to prevent kernel page-cache double-buffering and Out-Of-Memory (OOM) failures under heavy analytical concurrency. Mandates explicit session-level work_mem budgeting per parallel worker and ensures database connection pools dynamically constrain concurrent analytical execution based on physical memory thresholds.
globs: src/infrastructure/database/**/*.{ts,js}, src/db/**/*.{ts,js}, src/repositories/**/*.{ts,js}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-direct-io-memory-guardrail"
---

# PostgreSQL 17 Direct I/O and Memory Accounting Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Parallel Query Execution and Global work_mem Reliance Leading to OS OOM-Killer Invocations

## System Constraints
You are a Principal Software Architect enforcing PostgreSQL 17 direct I/O and memory accounting invariants. You must prevent memory exhaustion and kernel-level cache thrashing by ensuring database interactions explicitly budget work_mem, hash_mem_multiplier, and parallel worker allocations, while prohibiting reliance on monolithic global memory settings for concurrent analytical queries.

## Strict Guidelines
- Engineers MUST ALWAYS compute session-level `work_mem` dynamically for analytical queries based on `(available_ram_budget / (active_workers * max_parallel_workers_per_gather))` rather than relying on server-wide global defaults.
- Engineers MUST ALWAYS set `SET LOCAL work_mem` and `SET LOCAL hash_mem_multiplier` within dedicated analytical transactions to constrain complex joins and sorts.
- Engineers MUST NOT issue unbounded parallel scans or heavy aggregations across primary OLTP pools without an explicit pool partition and concurrency gate.
- Services MUST monitor active connection counts and cap analytical concurrency such that `shared_buffers + (max_connections * maintenance_margin) + sum(active_work_mem * hash_mem_multiplier * parallel_workers)` does not exceed 80% of host RAM.
- Components NEVER execute raw ad-hoc analytical queries using standard ORM finders without explicitly applying worker memory and parallel query execution caps via local session parameters.

## Bad vs Good Implementations
### Dynamic Work Memory Allocation and Parallelism Guard for Analytical Queries

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Executing analytical aggregations with default session settings allows PostgreSQL to spawn parallel workers where each worker node can allocate full work_mem multiplied by hash_mem_multiplier across multiple plan nodes. Concurrent executions rapidly exceed physical host RAM, triggering kernel OOM-killer crashes without OS page-cache buffering.
```typescript
import { Pool } from 'pg';

export class ReportRepository {
  constructor(private readonly pool: Pool) {}

  // BAD: Relies on server-level work_mem and unconstrained parallel workers.
  // In PG17 with direct I/O or low OS buffer margins, concurrent invocations
  // cause hash join allocations to multiply across workers, leading to OOM.
  async runLargeAggregation(tenantId: string): Promise<any[]> {
    const client = await this.pool.connect();
    try {
      const query = `
        SELECT category, SUM(amount), COUNT(DISTINCT user_id)
        FROM transactions
        WHERE tenant_id = $1
        GROUP BY category
        ORDER BY SUM(amount) DESC;
      `;
      const result = await client.query(query, [tenantId]);
      return result.rows;
    } finally {
      client.release();
    }
  }
}
```

#### Production Standard (Correct)
> **Standard**: Wraps the analytical query in an explicit read-only transaction and enforces deterministic execution boundaries using `SET LOCAL`. Limiting `max_parallel_workers_per_gather`, `work_mem`, and `hash_mem_multiplier` guarantees that query peak memory cannot exceed calculated safe thresholds, preventing OS OOM killer intervention under PG17 direct I/O memory constraints.
```typescript
import { Pool, PoolClient } from 'pg';

interface QueryBudget {
  maxParallelWorkers: number;
  workMemMegabytes: number;
  hashMemMultiplier: number;
}

export class ReportRepository {
  constructor(
    private readonly pool: Pool,
    private readonly defaultBudget: QueryBudget = {
      maxParallelWorkers: 2,
      workMemMegabytes: 64,
      hashMemMultiplier: 1.5,
    }
  ) {}

  // GOOD: Enforces session-level resource bounds via SET LOCAL within a transaction.
  // Memory consumption is bounded: max workers * work_mem * hash_mem_multiplier is deterministic.
  async runLargeAggregation(tenantId: string, budget = this.defaultBudget): Promise<any[]> {
    const client: PoolClient = await this.pool.connect();
    try {
      await client.query('BEGIN READ ONLY');

      // Strictly bound worker memory and parallel gather nodes for PG17 execution
      await client.query(`SET LOCAL max_parallel_workers_per_gather = ${budget.maxParallelWorkers}`);
      await client.query(`SET LOCAL work_mem = '${budget.workMemMegabytes}MB'`);
      await client.query(`SET LOCAL hash_mem_multiplier = ${budget.hashMemMultiplier}`);

      const query = `
        SELECT category, SUM(amount), COUNT(DISTINCT user_id)
        FROM transactions
        WHERE tenant_id = $1
        GROUP BY category
        ORDER BY SUM(amount) DESC;
      `;
      const result = await client.query(query, [tenantId]);

      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: PostgreSQL 17 Direct I/O and Memory Accounting Guardrail

PostgreSQL 17 Direct I/O and Memory Accounting Guardrail defines an industrial-grade behavioral standard for backend engineering. Enforces strict memory budgeting and I/O engine configuration for PostgreSQL 17 workloads to prevent kernel page-cache double-buffering and Out-Of-Memory (OOM) failures under heavy analytical concurrency. Mandates explicit session-level work_mem budgeting per parallel worker and ensures database connection pools dynamically constrain concurrent analytical execution based on physical memory thresholds. 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 Parallel Query Execution and Global work_mem Reliance Leading to OS OOM-Killer Invocations 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 Parallel Query Execution and Global work_mem Reliance Leading to OS OOM-Killer InvocationsCritical
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 compute session-level `work_mem` dynamically for analytical queries based on `(available_ram_budget / (active_workers * max_parallel_workers_per_gather))` rather than relying on server-wide global defaults.

Engineers MUST ALWAYS compute session-level `work_mem` dynamically for analytical queries based on `(available_ram_budget / (active_workers * max_parallel_workers_per_gather))` rather than relying on server-wide global defaults.

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 ALWAYS set `SET LOCAL work_mem` and `SET LOCAL hash_mem_multiplier` within dedicated analytical transactions to constrain complex joins and sorts.

Engineers MUST ALWAYS set `SET LOCAL work_mem` and `SET LOCAL hash_mem_multiplier` within dedicated analytical transactions to constrain complex joins and sorts.

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 NOT issue unbounded parallel scans or heavy aggregations across primary OLTP pools without an explicit pool partition and concurrency gate.

Engineers MUST NOT issue unbounded parallel scans or heavy aggregations across primary OLTP pools without an explicit pool partition and concurrency gate.

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 active connection counts and cap analytical concurrency such that `shared_buffers + (max_connections * maintenance_margin) + sum(active_work_mem * hash_mem_multiplier * parallel_workers)` does not exceed 80% of host RAM.

Services MUST monitor active connection counts and cap analytical concurrency such that `shared_buffers + (max_connections * maintenance_margin) + sum(active_work_mem * hash_mem_multiplier * parallel_workers)` does not exceed 80% of host 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.
5

Components NEVER execute raw ad-hoc analytical queries using standard ORM finders without explicitly applying worker memory and parallel query execution caps via local session parameters.

Components NEVER execute raw ad-hoc analytical queries using standard ORM finders without explicitly applying worker memory and parallel query execution caps via local session parameters.

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 Direct I/O and Memory Accounting Guardrail critical for AI coding assistants?

Enforces strict memory budgeting and I/O engine configuration for PostgreSQL 17 workloads to prevent kernel page-cache double-buffering and Out-Of-Memory (OOM) failures under heavy analytical concurrency. Mandates explicit session-level work_mem budgeting per parallel worker and ensures database connection pools dynamically constrain concurrent analytical execution based on physical memory thresholds. 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 Parallel Query Execution and Global work_mem Reliance Leading to OS OOM-Killer Invocations) 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