This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: pg17-json-table-workmem-ceiling

PostgreSQL 17 JSON_TABLE Execution Memory Bounds & WorkMem Invariants

Enforces transaction-scoped work_mem ceilings and chunked ingestion during PostgreSQL 17 JSON_TABLE columnar parsing. Prevents catastrophic backend OOM kills under concurrent high-throughput SQL/JSON unpack operations by bounding plan-node allocation buffers and eliminating unbounded NESTED PATH projections.

Prohibited Anti-Pattern: Global Unbounded Ingestion and Unconstrained NESTED PATH Projections in JSON_TABLE

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 JSON_TABLE Execution Memory Bounds & WorkMem Invariants - Enforces transaction-scoped work_mem ceilings and chunked ingestion during PostgreSQL 17 JSON_TABLE columnar parsing. Prevents catastrophic backend OOM kills under concurrent high-throughput SQL/JSON unpack operations by bounding plan-node allocation buffers and eliminating unbounded NESTED PATH projections.
globs: src/**/*.ts, src/**/*.repository.ts, src/**/*.service.ts, src/db/**/*.sql
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pg17-json-table-workmem-ceiling"
---

# PostgreSQL 17 JSON_TABLE Execution Memory Bounds & WorkMem Invariants

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Global Unbounded Ingestion and Unconstrained NESTED PATH Projections in JSON_TABLE

## System Constraints
You are a Principal Software Architect enforcing database reliability and memory safety on PostgreSQL 17 JSON_TABLE operations. Mandate strict transaction-level work_mem ceilings, bounded payload chunking, and optimal column projections to prevent Linux OOM-killer invocations during concurrent vectorized JSON parsing.

## Strict Guidelines
- Engineers MUST ALWAYS scope memory-intensive JSON_TABLE executions inside transactions using explicit 'SET LOCAL work_mem = ...' to prevent query-level memory starvation or global container OOM aborts.
- Engineers MUST NOT pass monolithic JSON arrays exceeding 10,000 items to a single JSON_TABLE call; ingestion payloads MUST be batched client-side or partitioned via cursors.
- Queries utilizing JSON_TABLE with multiple NESTED PATH clauses MUST explicitly constrain intermediate projected rows using bounded path filters or child LIMIT expressions to avoid exponential Cartesian in-memory materialization.
- Services MUST prefer typed column extraction definitions (e.g., INT, TIMESTAMPTZ, VARCHAR(n)) within the JSON_TABLE COLUMNS clause rather than returning raw JSON/JSONB text for downstream parsing.
- Database access layers MUST NEVER rely on cluster-wide work_mem settings when executing parallelized JSON_TABLE queries across worker pools.

## Bad vs Good Implementations
### PostgreSQL 17 JSON_TABLE Batch Ingestion with Transactional WorkMem Capping

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Executing unconstrained Cartesian nested JSON_TABLE parsing against a massive single JSON payload uses default or elevated cluster work_mem across all concurrent workers. This frequently causes sudden query-level OOM aborts or triggers host kernel OOM killers.
```typescript
import { Pool } from 'pg';

export async function bulkIngestTelemetry(pool: Pool, largeRawPayload: object[]): Promise<void> {
  // Anti-pattern: Sending unbounded single JSON blob without local memory guardrails.
  // Can trigger severe node-level memory inflation or cgroup kill under high concurrency.
  await pool.query(`
    INSERT INTO sensor_readings (sensor_id, timestamp, metric_value)
    SELECT jt.sensor_id, jt.ts, jt.metric
    FROM JSON_TABLE(
      $1::jsonb, '$[*]'
      COLUMNS (
        sensor_id UUID PATH '$.id',
        ts TIMESTAMPTZ PATH '$.timestamp',
        nested_metrics JSONB PATH '$.metrics'
      )
    ) jt,
    JSON_TABLE(
      jt.nested_metrics, '$[*]'
      COLUMNS (
        metric DOUBLE PRECISION PATH '$.value'
      )
    );
  `, [JSON.stringify(largeRawPayload)]);
}
```

#### Production Standard (Correct)
> **Standard**: Batches the input JSON into deterministic chunk sizes, encapsulates the operation in a transaction with an isolated 'SET LOCAL work_mem' cap, and utilizes Postgres 17's native ANSI standard NESTED PATH clause instead of cross-joining multiple JSON_TABLE expressions.
```typescript
import { Pool, PoolClient } from 'pg';

const CHUNK_SIZE = 5_000;
const WORK_MEM_CEILING = '32MB';

export async function bulkIngestTelemetry(pool: Pool, records: object[]): Promise<void> {
  for (let i = 0; i < records.length; i += CHUNK_SIZE) {
    const chunk = records.slice(i, i + CHUNK_SIZE);
    const client: PoolClient = await pool.connect();

    try {
      await client.query('BEGIN');
      // Constrain execution memory specifically for this unpacking transaction
      await client.query(`SET LOCAL work_mem = '${WORK_MEM_CEILING}'`);

      await client.query(`
        INSERT INTO sensor_readings (sensor_id, timestamp, metric_value)
        SELECT jt.sensor_id, jt.ts, jt.metric
        FROM JSON_TABLE(
          $1::jsonb, '$[*]'
          COLUMNS (
            sensor_id UUID PATH '$.id',
            ts TIMESTAMPTZ PATH '$.timestamp',
            NESTED PATH '$.metrics[*]'
            COLUMNS (
              metric DOUBLE PRECISION PATH '$.value'
            )
          )
        ) jt
      `, [JSON.stringify(chunk)]);

      await client.query('COMMIT');
    } 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: PostgreSQL 17 JSON_TABLE Execution Memory Bounds & WorkMem Invariants

PostgreSQL 17 JSON_TABLE Execution Memory Bounds & WorkMem Invariants defines an industrial-grade behavioral standard for backend engineering. Enforces transaction-scoped work_mem ceilings and chunked ingestion during PostgreSQL 17 JSON_TABLE columnar parsing. Prevents catastrophic backend OOM kills under concurrent high-throughput SQL/JSON unpack operations by bounding plan-node allocation buffers and eliminating unbounded NESTED PATH projections. 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 Unbounded Ingestion and Unconstrained NESTED PATH Projections in JSON_TABLE 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 Unbounded Ingestion and Unconstrained NESTED PATH Projections in JSON_TABLECritical
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 scope memory-intensive JSON_TABLE executions inside transactions using explicit 'SET LOCAL work_mem = ...' to prevent query-level memory starvation or global container OOM aborts.

Engineers MUST ALWAYS scope memory-intensive JSON_TABLE executions inside transactions using explicit 'SET LOCAL work_mem = ...' to prevent query-level memory starvation or global container OOM aborts.

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 pass monolithic JSON arrays exceeding 10,000 items to a single JSON_TABLE call

Engineers MUST NOT pass monolithic JSON arrays exceeding 10,000 items to a single JSON_TABLE call; ingestion payloads MUST be batched client-side or partitioned via cursors.

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

Queries utilizing JSON_TABLE with multiple NESTED PATH clauses MUST explicitly constrain intermediate projected rows using bounded path filters or child LIMIT expressions to avoid exponential Cartesian in-memory materialization.

Queries utilizing JSON_TABLE with multiple NESTED PATH clauses MUST explicitly constrain intermediate projected rows using bounded path filters or child LIMIT expressions to avoid exponential Cartesian in-memory materialization.

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 prefer typed column extraction definitions (e.g., INT, TIMESTAMPTZ, VARCHAR(n)) within the JSON_TABLE COLUMNS clause rather than returning raw JSON/JSONB text for downstream parsing.

Services MUST prefer typed column extraction definitions (e.g., INT, TIMESTAMPTZ, VARCHAR(n)) within the JSON_TABLE COLUMNS clause rather than returning raw JSON/JSONB text for downstream parsing.

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

Database access layers MUST NEVER rely on cluster-wide work_mem settings when executing parallelized JSON_TABLE queries across worker pools.

Database access layers MUST NEVER rely on cluster-wide work_mem settings when executing parallelized JSON_TABLE queries across worker pools.

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 JSON_TABLE Execution Memory Bounds & WorkMem Invariants critical for AI coding assistants?

Enforces transaction-scoped work_mem ceilings and chunked ingestion during PostgreSQL 17 JSON_TABLE columnar parsing. Prevents catastrophic backend OOM kills under concurrent high-throughput SQL/JSON unpack operations by bounding plan-node allocation buffers and eliminating unbounded NESTED PATH projections. 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 Unbounded Ingestion and Unconstrained NESTED PATH Projections in JSON_TABLE) 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