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

Postgres 17 MemoryContext Guardrails for JSON_TABLE Parallel Scans

Enforces strict session-level memory boundaries and execution limits when utilizing Postgres 17 JSON_TABLE inside parallel query plans. Prevents runaway executor MemoryContext bloat and OOM-induced cascading worker process terminations during deep nested JSON path evaluations. Mandates explicit work_mem bounds, worker count caps, and streaming batch wrappers for high-cardinality JSON transformations.

Prohibited Anti-Pattern: Unbounded parallel worker JSON_TABLE scans exhausting OS RAM and causing backend OOM panic

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 MemoryContext Guardrails for JSON_TABLE Parallel Scans - Enforces strict session-level memory boundaries and execution limits when utilizing Postgres 17 JSON_TABLE inside parallel query plans. Prevents runaway executor MemoryContext bloat and OOM-induced cascading worker process terminations during deep nested JSON path evaluations. Mandates explicit work_mem bounds, worker count caps, and streaming batch wrappers for high-cardinality JSON transformations.
globs: src/db/**/*.ts, src/repositories/**/*.ts, src/services/**/*.ts, migrations/**/*.sql
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-memory-context-json-table-parallel-scans"
---

# Postgres 17 MemoryContext Guardrails for JSON_TABLE Parallel Scans

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded parallel worker JSON_TABLE scans exhausting OS RAM and causing backend OOM panic

## System Constraints
You are a Principal Database Infrastructure Engineer enforcing Postgres 17 MemoryContext Guardrails for JSON_TABLE Parallel Scans. In Postgres 17, parallel worker execution of nested JSON_TABLE path queries allocates intermediate nodes inside executor MemoryContexts (specifically ExprContext and MaterializedTuples) which are not automatically reaped until scan completion. You must mandate strict session-level limits, explicit materialization barriers, and guarded worker execution profiles to prevent query-level OOM panics and SIGKILL signals across database pools.

## Strict Guidelines
- Engineers MUST ALWAYS isolate heavy JSON_TABLE transformations into dedicated transaction blocks that explicitly override 'work_mem' and cap 'max_parallel_workers_per_gather' to prevent multi-worker memory multiplication.
- Engineers MUST NOT run unbounded JSON_TABLE scans across columns containing nested JSON documents without projecting only required leaf paths using selective path expressions.
- Code SHOULD enforce cursor-based chunking or keyset pagination at the query layer instead of expanding millions of JSON records in a single parallel scan phase.
- Services MUST monitor and configure 'statement_timeout' and transaction-local 'work_mem' before executing complex JSON_TABLE extraction statements.
- Queries NEVER combine nested JSON_TABLE sibling arrays without explicit NESTED PATH syntax optimizations and cardinality limits, as cartesian intermediate expansions explode the executor MemoryContext.

## Bad vs Good Implementations
### Unbounded Parallel JSON_TABLE vs. MemoryContext-Guarded Transaction Block

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Executing JSON_TABLE directly against large JSON blobs without worker caps or memory boundaries permits the parallel planner to spawn maximum workers. Each worker instantiates a full JSONPath AST inside its executor MemoryContext, causing resident memory consumption to spike multiplicatively (work_mem * parallel_workers) and triggering OOM killer termination of Postgres backend processes.
```typescript
import { Pool } from 'pg';

export async function extractAuditEvents(pool: Pool, logId: string) {
  // Anti-pattern: Global parallel workers expand entire deep JSON into unmanaged memory
  // Multiple parallel workers explode executor MemoryContext causing OOM SIGKILL
  const query = `
    SELECT jt.*
    FROM raw_ingest_logs r,
    JSON_TABLE(
      r.payload,
      '$.events[*]'
      COLUMNS (
        event_id text PATH '$.id',
        event_type text PATH '$.type',
        nested_meta jsonb PATH '$.metadata'
      )
    ) AS jt
    WHERE r.batch_id = $1;
  `;
  return pool.query(query, [logId]);
}
```

#### Production Standard (Correct)
> **Standard**: Uses a dedicated transaction scope setting LOCAL parameters that restrict parallel workers to a safe ceiling (2), cap work_mem allocations, configure strict statement timeouts, avoid unbounded JSONB field re-allocations, and set a hard upper boundary on extracted rows to prevent MemoryContext leakages.
```typescript
import { Pool, PoolClient } from 'pg';

export async function extractAuditEvents(pool: Pool, logId: string) {
  const client: PoolClient = await pool.connect();
  try {
    await client.query('BEGIN');
    // Guardrail: Constrain parallel workers and isolate work_mem to prevent OOM cascade
    await client.query('SET LOCAL max_parallel_workers_per_gather = 2;');
    await client.query("SET LOCAL work_mem = '32MB';");
    await client.query("SET LOCAL statement_timeout = '15s';");

    const query = `
      SELECT jt.event_id, jt.event_type, jt.client_timestamp
      FROM raw_ingest_logs r
      CROSS JOIN LATERAL JSON_TABLE(
        r.payload,
        '$.events[*]'
        COLUMNS (
          event_id text PATH '$.id',
          event_type text PATH '$.type',
          client_timestamp bigint PATH '$.timestamp'
        )
      ) AS jt
      WHERE r.batch_id = $1
      LIMIT 10000;
    `;
    const result = await client.query(query, [logId]);
    await client.query('COMMIT');
    return result.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.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Postgres 17 MemoryContext Guardrails for JSON_TABLE Parallel Scans

Postgres 17 MemoryContext Guardrails for JSON_TABLE Parallel Scans defines an industrial-grade behavioral standard for backend engineering. Enforces strict session-level memory boundaries and execution limits when utilizing Postgres 17 JSON_TABLE inside parallel query plans. Prevents runaway executor MemoryContext bloat and OOM-induced cascading worker process terminations during deep nested JSON path evaluations. Mandates explicit work_mem bounds, worker count caps, and streaming batch wrappers for high-cardinality JSON transformations. 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 worker JSON_TABLE scans exhausting OS RAM and causing backend OOM panic 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 worker JSON_TABLE scans exhausting OS RAM and causing backend OOM panicCritical
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 JSON_TABLE transformations into dedicated transaction blocks that explicitly override 'work_mem' and cap 'max_parallel_workers_per_gather' to prevent multi-worker memory multiplication.

Engineers MUST ALWAYS isolate heavy JSON_TABLE transformations into dedicated transaction blocks that explicitly override 'work_mem' and cap 'max_parallel_workers_per_gather' to prevent multi-worker memory multiplication.

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 run unbounded JSON_TABLE scans across columns containing nested JSON documents without projecting only required leaf paths using selective path expressions.

Engineers MUST NOT run unbounded JSON_TABLE scans across columns containing nested JSON documents without projecting only required leaf paths using selective path expressions.

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 enforce cursor-based chunking or keyset pagination at the query layer instead of expanding millions of JSON records in a single parallel scan phase.

Code SHOULD enforce cursor-based chunking or keyset pagination at the query layer instead of expanding millions of JSON records in a single parallel scan phase.

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 configure 'statement_timeout' and transaction-local 'work_mem' before executing complex JSON_TABLE extraction statements.

Services MUST monitor and configure 'statement_timeout' and transaction-local 'work_mem' before executing complex JSON_TABLE extraction statements.

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

Queries NEVER combine nested JSON_TABLE sibling arrays without explicit NESTED PATH syntax optimizations and cardinality limits, as cartesian intermediate expansions explode the executor MemoryContext.

Queries NEVER combine nested JSON_TABLE sibling arrays without explicit NESTED PATH syntax optimizations and cardinality limits, as cartesian intermediate expansions explode the executor MemoryContext.

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 MemoryContext Guardrails for JSON_TABLE Parallel Scans critical for AI coding assistants?

Enforces strict session-level memory boundaries and execution limits when utilizing Postgres 17 JSON_TABLE inside parallel query plans. Prevents runaway executor MemoryContext bloat and OOM-induced cascading worker process terminations during deep nested JSON path evaluations. Mandates explicit work_mem bounds, worker count caps, and streaming batch wrappers for high-cardinality JSON transformations. 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 worker JSON_TABLE scans exhausting OS RAM and causing backend OOM panic) 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