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

PostgreSQL 17 Memory-Adaptive Parallel Hash Join Guardrail

Enforces bounded memory allocations and worker throttling for parallel hash join queries on PostgreSQL 17 read replicas. Uncontrolled session-level work_mem overrides multiply across parallel gather workers, triggering Linux Out-Of-Memory (OOM) killer terminations under high concurrency.

Prohibited Anti-Pattern: Unbounded Session-Level work_mem Inflation with Parallel Query Execution

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 Memory-Adaptive Parallel Hash Join Guardrail - Enforces bounded memory allocations and worker throttling for parallel hash join queries on PostgreSQL 17 read replicas. Uncontrolled session-level work_mem overrides multiply across parallel gather workers, triggering Linux Out-Of-Memory (OOM) killer terminations under high concurrency.
globs: src/**/*.ts, src/database/**/*.ts, src/repositories/**/*.ts, src/services/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-memory-adaptive-parallel-hash-join"
---

# PostgreSQL 17 Memory-Adaptive Parallel Hash Join Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Session-Level work_mem Inflation with Parallel Query Execution

## System Constraints
You are a Principal Database Reliability Engineer enforcing strict memory safety invariants for PostgreSQL 17 parallel query execution. Unbounded session-level work_mem and uncontrolled parallel worker spawn count scale multiplicatively, risking OOM-killer SIGKILL on replica nodes. Mandate explicit worker-bounded work_mem calculations, transaction isolation via SET LOCAL, and enforce query timeouts.

## Strict Guidelines
- Engineers MUST ALWAYS compute dynamic work_mem as a fraction of remaining instance headroom divided by (max_parallel_workers_per_gather + 1) * expected_concurrent_joins.
- Engineers MUST NEVER execute unbounded 'SET work_mem' at the session level; always scope modifications strictly to transactions via 'SET LOCAL work_mem'.
- Code SHOULD verify and configure 'hash_mem_multiplier' when targeting PostgreSQL 17 to ensure hash table sizes do not inflate beyond physical RAM ceilings.
- Services MUST enforce strict 'SET LOCAL statement_timeout' alongside any dynamic memory elevation to avoid worker thread starvation.
- Components NEVER execute raw parallel analytical queries directly against primary OLTP connections; they must route to dedicated analytical read-pools with enforced parallel caps.

## Bad vs Good Implementations
### Unsafe Dynamic work_mem Inflation vs Memory-Adaptive Transactional Guardrail

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Executing 'SET work_mem' at the session level corrupts the pooled connection state and scales multiplicatively with worker threads. In PostgreSQL 17, a parallel hash join allocates work_mem per participant worker, resulting in rapid memory exhaustion and OOM-killer termination under high concurrency.
```typescript
import { PoolClient } from 'pg';

export async function runAnalyticalJoin(client: PoolClient, tenantId: string): Promise<unknown[]> {
  // ANTI-PATTERN: Session-level work_mem override without considering parallel workers.
  // If max_parallel_workers_per_gather = 4, 1GB work_mem can consume up to 5GB per query.
  // Under 20 concurrent replica queries, memory demands reach 100GB+, triggering kernel OOM.
  await client.query("SET work_mem = '1GB'");
  
  const result = await client.query(
    `SELECT c.id, SUM(o.total_amount)
     FROM customers c
     JOIN orders o ON c.id = o.customer_id
     WHERE c.tenant_id = $1
     GROUP BY c.id`,
    [tenantId]
  );
  
  // Leaks elevated work_mem to subsequent pooled connections
  return result.rows;
}
```

#### Production Standard (Correct)
> **Standard**: Wraps execution in a transaction using 'SET LOCAL', bounds parallel workers, sets the PostgreSQL 17 'hash_mem_multiplier' safely, clamps statement timeouts to 30s, and computes work_mem based on replica concurrency headroom.
```typescript
import { PoolClient } from 'pg';

interface MemoryBudgetConfig {
  maxAvailableMemBytes: number;
  maxParallelWorkers: number;
  activeConcurrentQueries: number;
}

function calculateSafeWorkMemKb(config: MemoryBudgetConfig): number {
  const workerMultiplier = config.maxParallelWorkers + 1;
  const safeConcurrency = Math.max(config.activeConcurrentQueries, 1);
  const perSlotBytes = config.maxAvailableMemBytes / (workerMultiplier * safeConcurrency);
  const minWorkMemKb = 4096; // 4MB floor
  const calculatedKb = Math.floor(perSlotBytes / 1024);
  return Math.max(minWorkMemKb, calculatedKb);
}

export async function runAnalyticalJoinSafe(
  client: PoolClient,
  tenantId: string,
  budget: MemoryBudgetConfig
): Promise<unknown[]> {
  const safeWorkMemKb = calculateSafeWorkMemKb(budget);
  
  await client.query('BEGIN');
  try {
    // SAFE: Strictly scoped to the transaction with worker bounds and statement timeout
    await client.query(`SET LOCAL work_mem = '${safeWorkMemKb}kB'`);
    await client.query(`SET LOCAL max_parallel_workers_per_gather = '${budget.maxParallelWorkers}'`);
    await client.query("SET LOCAL hash_mem_multiplier = '1.0'");
    await client.query("SET LOCAL statement_timeout = '30000'"); // 30s limit

    const result = await client.query(
      `SELECT c.id, SUM(o.total_amount)
       FROM customers c
       JOIN orders o ON c.id = o.customer_id
       WHERE c.tenant_id = $1
       GROUP BY c.id`,
      [tenantId]
    );

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

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 Memory-Adaptive Parallel Hash Join Guardrail

PostgreSQL 17 Memory-Adaptive Parallel Hash Join Guardrail defines an industrial-grade behavioral standard for backend engineering. Enforces bounded memory allocations and worker throttling for parallel hash join queries on PostgreSQL 17 read replicas. Uncontrolled session-level work_mem overrides multiply across parallel gather workers, triggering Linux Out-Of-Memory (OOM) killer terminations under high concurrency. 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 Session-Level work_mem Inflation with Parallel Query Execution 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 Session-Level work_mem Inflation with Parallel Query ExecutionCritical
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 dynamic work_mem as a fraction of remaining instance headroom divided by (max_parallel_workers_per_gather + 1) * expected_concurrent_joins.

Engineers MUST ALWAYS compute dynamic work_mem as a fraction of remaining instance headroom divided by (max_parallel_workers_per_gather + 1) * expected_concurrent_joins.

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 NEVER execute unbounded 'SET work_mem' at the session level

Engineers MUST NEVER execute unbounded 'SET work_mem' at the session level; always scope modifications strictly to transactions via 'SET LOCAL work_mem'.

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 verify and configure 'hash_mem_multiplier' when targeting PostgreSQL 17 to ensure hash table sizes do not inflate beyond physical RAM ceilings.

Code SHOULD verify and configure 'hash_mem_multiplier' when targeting PostgreSQL 17 to ensure hash table sizes do not inflate beyond physical RAM 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.
4

Services MUST enforce strict 'SET LOCAL statement_timeout' alongside any dynamic memory elevation to avoid worker thread starvation.

Services MUST enforce strict 'SET LOCAL statement_timeout' alongside any dynamic memory elevation to avoid worker thread starvation.

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 parallel analytical queries directly against primary OLTP connections

Components NEVER execute raw parallel analytical queries directly against primary OLTP connections; they must route to dedicated analytical read-pools with enforced parallel caps.

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 Memory-Adaptive Parallel Hash Join Guardrail critical for AI coding assistants?

Enforces bounded memory allocations and worker throttling for parallel hash join queries on PostgreSQL 17 read replicas. Uncontrolled session-level work_mem overrides multiply across parallel gather workers, triggering Linux Out-Of-Memory (OOM) killer terminations under high concurrency. 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 Session-Level work_mem Inflation with Parallel Query Execution) 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