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

Postgres 17 JSON_TABLE & SIMD Ingestion Memory Bounds

Enforces bounded chunking, explicit work_mem isolation, and memory-safe projection limits when utilizing Postgres 17's SQL/JSON JSON_TABLE feature and SIMD-accelerated JSON parsers. Prevents backend worker out-of-memory (OOM) crashes and CPU starvation during high-throughput JSON batch ingestion by requiring micro-batched payloads and strict path projection constraints.

Prohibited Anti-Pattern: Ingesting Unbounded JSON Payloads into Unmetered JSON_TABLE Queries Causing PG Worker OOM

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 JSON_TABLE & SIMD Ingestion Memory Bounds - Enforces bounded chunking, explicit work_mem isolation, and memory-safe projection limits when utilizing Postgres 17's SQL/JSON JSON_TABLE feature and SIMD-accelerated JSON parsers. Prevents backend worker out-of-memory (OOM) crashes and CPU starvation during high-throughput JSON batch ingestion by requiring micro-batched payloads and strict path projection constraints.
globs: src/**/*.ts, src/**/*.repository.ts, src/**/*.dao.ts, src/**/*.service.ts, src/**/*.sql
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-json-table-simd-streaming-guardrail"
---

# Postgres 17 JSON_TABLE & SIMD Ingestion Memory Bounds

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Ingesting Unbounded JSON Payloads into Unmetered JSON_TABLE Queries Causing PG Worker OOM

## System Constraints
You are a Principal Software Architect enforcing Postgres 17 JSON_TABLE & SIMD streaming ingestion safety. Postgres 17 introduces native SQL/JSON JSON_TABLE alongside SIMD-accelerated JSON parsing, which significantly speeds up extraction but can cause catastrophic backend memory spikes (OOM crashes) if unchunked JSON arrays are projected into relational rows. You MUST mandate micro-batching, strict column sizing, explicit ERROR ON ERROR clauses, and isolated transactional work_mem thresholds.

## Strict Guidelines
- Engineers MUST ALWAYS chunk JSON batch payloads into micro-batches (maximum 2,500 elements or 5MB per chunk) prior to passing them into Postgres 17 JSON_TABLE queries.
- Engineers MUST NOT pass unbounded or arbitrarily large JSON strings directly to JSON_TABLE without explicit application-level payload size validation.
- Queries utilizing JSON_TABLE MUST explicitly define output column data types with length bounds (e.g., VARCHAR(255) rather than TEXT) and specify 'EMPTY ON ERROR' or 'ERROR ON ERROR' to prevent silent data corruption or runaway error allocations.
- Batch ingestion transactions invoking JSON_TABLE SHOULD execute 'SET LOCAL work_mem' within a dedicated transaction block to avoid unbounded memory consumption on parallel worker threads.
- Services MUST NEVER rely on client-side JSON serialization of multi-megabyte payloads when streaming; streaming pipeline abstractions MUST backpressure and batch inputs before executing JSON_TABLE extraction.

## Bad vs Good Implementations
### Unbounded Direct JSON_TABLE Ingestion vs. Micro-Batched Memory-Guarded Ingestion

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Passing an unbounded multi-megabyte JSON payload directly to JSON_TABLE causes Postgres 17 to allocate massive working memory during SIMD parsing and relational projection. If multiple concurrent requests arrive, backend workers exceed cgroup memory limits and are killed by the OS OOM killer.
```typescript
import { Pool } from 'pg';

export async function ingestAuditLogs(pool: Pool, largeRawJsonString: string): Promise<void> {
  // BAD: Ingesting an unbounded JSON string directly into JSON_TABLE without
  // chunking, without size constraints, and without transaction-level memory guardrails.
  // This can exhaust backend process memory under SIMD vector parsing of deep arrays.
  await pool.query(`
    INSERT INTO audit_logs (event_id, user_id, action, payload, created_at)
    SELECT jt.event_id, jt.user_id, jt.action, jt.payload, jt.created_at
    FROM JSON_TABLE(
      $1::jsonb, '$[*]'
      COLUMNS (
        event_id UUID PATH '$.id',
        user_id UUID PATH '$.user_id',
        action TEXT PATH '$.action',
        payload JSONB PATH '$.payload',
        created_at TIMESTAMPTZ PATH '$.timestamp'
      )
    ) AS jt;
  `, [largeRawJsonString]);
}
```

#### Production Standard (Correct)
> **Standard**: The safe implementation chunks the inbound dataset into strict batch sizes (2,000 rows / 5MB limit), isolates execution inside a transaction with localized work_mem limits and statement timeouts, and specifies bounded column types (VARCHAR(128) instead of unbounded TEXT) with explicit ERROR ON ERROR handling.
```typescript
import { Pool, PoolClient } from 'pg';

const BATCH_SIZE_LIMIT = 2000;
const MAX_PAYLOAD_BYTES = 5 * 1024 * 1024; // 5MB

interface RawAuditEntry {
  id: string;
  user_id: string;
  action: string;
  payload: Record<string, unknown>;
  timestamp: string;
}

export async function ingestAuditLogs(
  pool: Pool,
  entries: RawAuditEntry[]
): Promise<void> {
  if (entries.length === 0) return;

  // GOOD: Partition entries into bounded micro-batches to respect SIMD processing envelopes
  for (let i = 0; i < entries.length; i += BATCH_SIZE_LIMIT) {
    const chunk = entries.slice(i, i + BATCH_SIZE_LIMIT);
    const serialized = JSON.stringify(chunk);

    if (Buffer.byteLength(serialized, 'utf8') > MAX_PAYLOAD_BYTES) {
      throw new Error(`Micro-batch exceeds max byte envelope of ${MAX_PAYLOAD_BYTES} bytes`);
    }

    const client: PoolClient = await pool.connect();
    try {
      await client.query('BEGIN');
      // Constrain transaction-level memory consumption during JSON vectorization
      await client.query("SET LOCAL work_mem = '64MB'");
      await client.query("SET LOCAL statement_timeout = '15s'");

      await client.query(`
        INSERT INTO audit_logs (event_id, user_id, action, payload, created_at)
        SELECT jt.event_id, jt.user_id, jt.action, jt.payload, jt.created_at
        FROM JSON_TABLE(
          $1::jsonb, '$[*]'
          COLUMNS (
            event_id UUID PATH '$.id' ERROR ON ERROR,
            user_id UUID PATH '$.user_id' ERROR ON ERROR,
            action VARCHAR(128) PATH '$.action' ERROR ON ERROR,
            payload JSONB PATH '$.payload' ERROR ON ERROR,
            created_at TIMESTAMPTZ PATH '$.timestamp' ERROR ON ERROR
          )
        ) AS jt;
      `, [serialized]);

      await client.query('COMMIT');
    } catch (err) {
      await client.query('ROLLBACK');
      throw err;
    } finally {
      client.release();
    }
  }
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Postgres 17 JSON_TABLE & SIMD Ingestion Memory Bounds

Postgres 17 JSON_TABLE & SIMD Ingestion Memory Bounds defines an industrial-grade behavioral standard for backend engineering. Enforces bounded chunking, explicit work_mem isolation, and memory-safe projection limits when utilizing Postgres 17's SQL/JSON JSON_TABLE feature and SIMD-accelerated JSON parsers. Prevents backend worker out-of-memory (OOM) crashes and CPU starvation during high-throughput JSON batch ingestion by requiring micro-batched payloads and strict path projection constraints. 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: Ingesting Unbounded JSON Payloads into Unmetered JSON_TABLE Queries Causing PG Worker OOM 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 DriftIngesting Unbounded JSON Payloads into Unmetered JSON_TABLE Queries Causing PG Worker OOMCritical
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 chunk JSON batch payloads into micro-batches (maximum 2,500 elements or 5MB per chunk) prior to passing them into Postgres 17 JSON_TABLE queries.

Engineers MUST ALWAYS chunk JSON batch payloads into micro-batches (maximum 2,500 elements or 5MB per chunk) prior to passing them into Postgres 17 JSON_TABLE queries.

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 unbounded or arbitrarily large JSON strings directly to JSON_TABLE without explicit application-level payload size validation.

Engineers MUST NOT pass unbounded or arbitrarily large JSON strings directly to JSON_TABLE without explicit application-level payload size validation.

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 MUST explicitly define output column data types with length bounds (e.g., VARCHAR(255) rather than TEXT) and specify 'EMPTY ON ERROR' or 'ERROR ON ERROR' to prevent silent data corruption or runaway error allocations.

Queries utilizing JSON_TABLE MUST explicitly define output column data types with length bounds (e.g., VARCHAR(255) rather than TEXT) and specify 'EMPTY ON ERROR' or 'ERROR ON ERROR' to prevent silent data corruption or runaway error allocations.

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

Batch ingestion transactions invoking JSON_TABLE SHOULD execute 'SET LOCAL work_mem' within a dedicated transaction block to avoid unbounded memory consumption on parallel worker threads.

Batch ingestion transactions invoking JSON_TABLE SHOULD execute 'SET LOCAL work_mem' within a dedicated transaction block to avoid unbounded memory consumption on parallel worker threads.

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

Services MUST NEVER rely on client-side JSON serialization of multi-megabyte payloads when streaming

Services MUST NEVER rely on client-side JSON serialization of multi-megabyte payloads when streaming; streaming pipeline abstractions MUST backpressure and batch inputs before executing JSON_TABLE extraction.

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 JSON_TABLE & SIMD Ingestion Memory Bounds critical for AI coding assistants?

Enforces bounded chunking, explicit work_mem isolation, and memory-safe projection limits when utilizing Postgres 17's SQL/JSON JSON_TABLE feature and SIMD-accelerated JSON parsers. Prevents backend worker out-of-memory (OOM) crashes and CPU starvation during high-throughput JSON batch ingestion by requiring micro-batched payloads and strict path projection constraints. 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 (Ingesting Unbounded JSON Payloads into Unmetered JSON_TABLE Queries Causing PG Worker OOM) 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