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

Postgres 17 JSON_TABLE Bounded Work Queue and Streaming Ingestion Quota

Enforces deterministic batch quotas, strictly bounded work_mem sessions, and optimized streaming projections using PostgreSQL 17 JSON_TABLE semantics. Prevents backend OOM death spirals during high-throughput semi-structured JSONB relational shredding by replacing unconstrained jsonb_to_recordset and in-memory array expansions with bounded column-projected streams.

Prohibited Anti-Pattern: Unbounded jsonb_to_recordset cross-joins and memory allocation spikes during bulk JSON ingestion

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 JSON_TABLE Bounded Work Queue and Streaming Ingestion Quota - Enforces deterministic batch quotas, strictly bounded work_mem sessions, and optimized streaming projections using PostgreSQL 17 JSON_TABLE semantics. Prevents backend OOM death spirals during high-throughput semi-structured JSONB relational shredding by replacing unconstrained jsonb_to_recordset and in-memory array expansions with bounded column-projected streams.
globs: src/repositories/**/*.ts, src/services/**/*.ts, src/database/**/*.ts, src/db/queries/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-json-table-streaming-projection-quota"
---

# Postgres 17 JSON_TABLE Bounded Work Queue and Streaming Ingestion Quota

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded jsonb_to_recordset cross-joins and memory allocation spikes during bulk JSON ingestion

## System Constraints
You are a Principal Database Architect enforcing strict memory bound quotas and streaming projection rules using PostgreSQL 17 JSON_TABLE. You must reject any code that consumes bulk semi-structured payloads via jsonb_to_recordset or unbounded client-side JSON materialization. Mandate explicit statement-level work_mem guardrails, scalar column projections, and strictly chunked CTE-based ingestion pipelines.

## Strict Guidelines
- Engineers MUST ALWAYS use PostgreSQL 17 SQL-standard `JSON_TABLE` with explicit typed column definitions instead of `jsonb_to_recordset()` or `jsonb_array_elements()` for multi-record shredding.
- Engineers MUST ALWAYS isolate high-volume batch ingestion inside a transaction that explicitly caps `LOCAL work_mem` to prevent parallel worker memory exhaustion and Linux OOM killer invocation.
- Code MUST NEVER hydrate multi-megabyte JSON arrays in Node.js/TypeScript memory prior to SQL execution; payloads MUST be passed as streaming JSON parameter buffers or chunked under fixed row thresholds (e.g., maximum 5,000 items per batch).
- Services MUST specify `ERROR ON ERROR` or `EMPTY ON ERROR` explicitly within `JSON_TABLE` column path mappings to prevent silent NULL fallbacks or catastrophic unhandled query aborts.
- Queries MUST NOT omit explicit data type sizing (e.g., use `VARCHAR(64)` instead of unconstrained `TEXT` inside JSON_TABLE column clauses) to allow the PostgreSQL planner to compute accurate cost models and fixed-width tuple allocations.

## Bad vs Good Implementations
### Unbounded JSON Recordset Ingestion vs. Postgres 17 Bounded JSON_TABLE Streaming Projection

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using jsonb_to_recordset with an unbounded JSON payload forces Postgres to completely deserialize the full AST into executor memory. When concurrent bulk ingest queries execute, this causes extreme memory amplification and triggers the Linux OOM killer.
```typescript
import { PoolClient } from 'pg';

interface IngestionPayload {
  events: Array<{ id: string; eventName: string; payload: Record<string, unknown> }>;
}

export async function ingestTelemetryBad(client: PoolClient, data: IngestionPayload): Promise<void> {
  // Anti-Pattern: Loading entire unbounded array, using unconstrained jsonb_to_recordset
  // This allocates massive intermediate working memory, leading to backend Postgres node OOM under load.
  const query = `
    INSERT INTO event_store (event_id, event_name, metadata)
    SELECT id, eventName, payload
    FROM jsonb_to_recordset($1::jsonb) AS x(
      id UUID,
      eventName TEXT,
      payload JSONB
    );
  `;
  
  await client.query('BEGIN');
  await client.query(query, [JSON.stringify(data.events)]);
  await client.query('COMMIT');
}
```

#### Production Standard (Correct)
> **Standard**: Leverages PostgreSQL 17's ANSI standard JSON_TABLE with explicit schema bindings, error policies, fixed column sizes, and a deterministic LOCAL work_mem ceiling, avoiding OOM risks by streaming projections directly into the target table.
```typescript
import { PoolClient } from 'pg';

interface TelemetryEvent {
  id: string;
  eventName: string;
  payload: Record<string, unknown>;
}

const BATCH_SIZE_LIMIT = 5000;
const WORK_MEM_QUOTA = '16MB';

export async function ingestTelemetryGood(client: PoolClient, events: TelemetryEvent[]): Promise<void> {
  if (events.length > BATCH_SIZE_LIMIT) {
    throw new Error(`Ingestion batch exceeds maximum quota of ${BATCH_SIZE_LIMIT} items.`);
  }

  // Bounded transaction with deterministic work_mem and Postgres 17 JSON_TABLE
  const query = `
    BEGIN;
    SET LOCAL work_mem = '${WORK_MEM_QUOTA}';
    
    INSERT INTO event_store (event_id, event_name, metadata)
    SELECT 
      j.event_id,
      j.event_name,
      j.metadata
    FROM JSON_TABLE(
      $1::jsonb, '$[*]'
      COLUMNS (
        event_id UUID PATH '$.id' ERROR ON ERROR,
        event_name VARCHAR(128) PATH '$.eventName' ERROR ON ERROR,
        metadata JSONB PATH '$.payload'
      )
    ) AS j;
    
    COMMIT;
  `;

  await client.query(query, [JSON.stringify(events)]);
}
```

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 JSON_TABLE Bounded Work Queue and Streaming Ingestion Quota

Postgres 17 JSON_TABLE Bounded Work Queue and Streaming Ingestion Quota defines an industrial-grade behavioral standard for backend engineering. Enforces deterministic batch quotas, strictly bounded work_mem sessions, and optimized streaming projections using PostgreSQL 17 JSON_TABLE semantics. Prevents backend OOM death spirals during high-throughput semi-structured JSONB relational shredding by replacing unconstrained jsonb_to_recordset and in-memory array expansions with bounded column-projected streams. 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 jsonb_to_recordset cross-joins and memory allocation spikes during bulk JSON ingestion 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 jsonb_to_recordset cross-joins and memory allocation spikes during bulk JSON ingestionCritical
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 use PostgreSQL 17 SQL-standard `JSON_TABLE` with explicit typed column definitions instead of `jsonb_to_recordset()` or `jsonb_array_elements()` for multi-record shredding.

Engineers MUST ALWAYS use PostgreSQL 17 SQL-standard `JSON_TABLE` with explicit typed column definitions instead of `jsonb_to_recordset()` or `jsonb_array_elements()` for multi-record shredding.

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 isolate high-volume batch ingestion inside a transaction that explicitly caps `LOCAL work_mem` to prevent parallel worker memory exhaustion and Linux OOM killer invocation.

Engineers MUST ALWAYS isolate high-volume batch ingestion inside a transaction that explicitly caps `LOCAL work_mem` to prevent parallel worker memory exhaustion and Linux OOM killer invocation.

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 MUST NEVER hydrate multi-megabyte JSON arrays in Node.js/TypeScript memory prior to SQL execution

Code MUST NEVER hydrate multi-megabyte JSON arrays in Node.js/TypeScript memory prior to SQL execution; payloads MUST be passed as streaming JSON parameter buffers or chunked under fixed row thresholds (e.g., maximum 5,000 items per batch).

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 specify `ERROR ON ERROR` or `EMPTY ON ERROR` explicitly within `JSON_TABLE` column path mappings to prevent silent NULL fallbacks or catastrophic unhandled query aborts.

Services MUST specify `ERROR ON ERROR` or `EMPTY ON ERROR` explicitly within `JSON_TABLE` column path mappings to prevent silent NULL fallbacks or catastrophic unhandled query 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.
5

Queries MUST NOT omit explicit data type sizing (e.g., use `VARCHAR(64)` instead of unconstrained `TEXT` inside JSON_TABLE column clauses) to allow the PostgreSQL planner to compute accurate cost models and fixed-width tuple allocations.

Queries MUST NOT omit explicit data type sizing (e.g., use `VARCHAR(64)` instead of unconstrained `TEXT` inside JSON_TABLE column clauses) to allow the PostgreSQL planner to compute accurate cost models and fixed-width tuple 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.
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 Bounded Work Queue and Streaming Ingestion Quota critical for AI coding assistants?

Enforces deterministic batch quotas, strictly bounded work_mem sessions, and optimized streaming projections using PostgreSQL 17 JSON_TABLE semantics. Prevents backend OOM death spirals during high-throughput semi-structured JSONB relational shredding by replacing unconstrained jsonb_to_recordset and in-memory array expansions with bounded column-projected streams. 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 jsonb_to_recordset cross-joins and memory allocation spikes during bulk JSON ingestion) 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