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

Postgres 17 Streaming JSON_TABLE Zero-Materialization Allocator Standard

Enforces non-materializing, stream-based evaluation for Postgres 17 SQL/JSON JSON_TABLE queries during high-volume document ingestion. Prevents catastrophic backend OOM conditions caused by eager JSONB AST deserialization and unbounded memory context retention by streaming raw text buffers directly into relational columns without intermediate DOM allocation.

Prohibited Anti-Pattern: Eager JSONB Parsing and Deep AST Materialization in JSON_TABLE Query Execution

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 Streaming JSON_TABLE Zero-Materialization Allocator Standard - Enforces non-materializing, stream-based evaluation for Postgres 17 SQL/JSON JSON_TABLE queries during high-volume document ingestion. Prevents catastrophic backend OOM conditions caused by eager JSONB AST deserialization and unbounded memory context retention by streaming raw text buffers directly into relational columns without intermediate DOM allocation.
globs: src/**/*.sql, src/**/*.ts, migrations/**/*.sql, queries/**/*.sql
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-streaming-json-table-memory-allocator"
---

# Postgres 17 Streaming JSON_TABLE Zero-Materialization Allocator Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Eager JSONB Parsing and Deep AST Materialization in JSON_TABLE Query Execution

## System Constraints
You are a Principal Database Architect enforcing Postgres 17 SQL/JSON standards. You must mandate zero-materialization streaming semantics for JSON_TABLE query pipelines. Enforce that ingested multi-gigabyte JSON documents are treated as unparsed byte streams, avoiding eager jsonb conversions or unbounded AST allocations in Postgres ExprContext. Reject any query pattern that triggers full document materialization in backend memory.

## Strict Guidelines
- Engineers MUST ALWAYS pass raw text or bytea references directly to JSON_TABLE without wrapping the document in an explicit or implicit `::jsonb` cast.
- Engineers MUST define strict or lax path expressions (e.g., 'lax $.records[*]') that avoid recursive search operators ('..') to enable single-pass streaming evaluation in the Postgres 17 query engine.
- Engineers MUST NOT use legacy functions such as `jsonb_populate_recordset` or `json_to_recordset` for multi-megabyte payloads, as they force full document materialization in memory.
- Services MUST process multi-gigabyte ingestion streams using chunked pipelines or COPY streams alongside JSON_TABLE rather than accumulating documents in client-side process RAM.
- Components NEVER execute JSON_TABLE path queries containing unindexed nested JSON_TABLE sibling projections over large collections without evaluating physical EXPLAIN (ANALYZE, MEMORY) output.

## Bad vs Good Implementations
### Streaming JSON_TABLE Ingestion vs. Eager JSONB Deserialization

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Casting the incoming payload to `::jsonb` and passing it to `jsonb_to_recordset` forces the Postgres backend to construct an in-memory JSONB binary tree for the entire document, resulting in backend process memory bloat and potential OOM crashes under high concurrency or multi-gigabyte payloads.
```typescript
import { Pool } from 'pg';

export async function ingestLargePayload(pool: Pool, rawJsonString: string): Promise<void> {
  // ANTI-PATTERN: Forcing eager `::jsonb` parsing materializes the entire DOM into backend memory.
  // This causes memory blowouts (exceeding work_mem/max RAM) on multi-gigabyte payloads.
  await pool.query(`
    INSERT INTO audit_events (event_id, event_type, payload_timestamp)
    SELECT event_id, event_type, payload_timestamp
    FROM jsonb_to_recordset($1::jsonb) AS x(
      event_id uuid,
      event_type text,
      payload_timestamp timestamptz
    )
  `, [rawJsonString]);
}
```

#### Production Standard (Correct)
> **Standard**: Passing `$1::text` directly into Postgres 17's `JSON_TABLE` with a direct path (`lax $.records[*]`) allows the streaming parser to extract typed column records incrementally, maintaining constant low memory context overhead regardless of the overall payload size.
```typescript
import { Pool } from 'pg';

export async function ingestLargePayload(pool: Pool, rawJsonStream: string): Promise<void> {
  // PRODUCTION FIX: Postgres 17 JSON_TABLE accepts raw text directly.
  // The JSON engine streams through the payload using a single-pass allocator
  // without materializing intermediate JSONB AST representations.
  await pool.query(`
    INSERT INTO audit_events (event_id, event_type, payload_timestamp)
    SELECT jt.event_id, jt.event_type, jt.payload_timestamp
    FROM JSON_TABLE(
      $1::text,
      'lax $.records[*]'
      COLUMNS (
        event_id uuid PATH '$.id',
        event_type text PATH '$.type',
        payload_timestamp timestamptz PATH '$.timestamp'
      )
      EMPTY ON ERROR
    ) AS jt;
  `, [rawJsonStream]);
}
```

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 Streaming JSON_TABLE Zero-Materialization Allocator Standard

Postgres 17 Streaming JSON_TABLE Zero-Materialization Allocator Standard defines an industrial-grade behavioral standard for backend engineering. Enforces non-materializing, stream-based evaluation for Postgres 17 SQL/JSON JSON_TABLE queries during high-volume document ingestion. Prevents catastrophic backend OOM conditions caused by eager JSONB AST deserialization and unbounded memory context retention by streaming raw text buffers directly into relational columns without intermediate DOM allocation. 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: Eager JSONB Parsing and Deep AST Materialization in JSON_TABLE 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 DriftEager JSONB Parsing and Deep AST Materialization in JSON_TABLE 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 pass raw text or bytea references directly to JSON_TABLE without wrapping the document in an explicit or implicit `

Engineers MUST ALWAYS pass raw text or bytea references directly to JSON_TABLE without wrapping the document in an explicit or implicit `::jsonb` cast.

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 define strict or lax path expressions (e.g., 'lax $.records[*]') that avoid recursive search operators ('..') to enable single-pass streaming evaluation in the Postgres 17 query engine.

Engineers MUST define strict or lax path expressions (e.g., 'lax $.records[*]') that avoid recursive search operators ('..') to enable single-pass streaming evaluation in the Postgres 17 query engine.

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

Engineers MUST NOT use legacy functions such as `jsonb_populate_recordset` or `json_to_recordset` for multi-megabyte payloads, as they force full document materialization in memory.

Engineers MUST NOT use legacy functions such as `jsonb_populate_recordset` or `json_to_recordset` for multi-megabyte payloads, as they force full document materialization in memory.

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 process multi-gigabyte ingestion streams using chunked pipelines or COPY streams alongside JSON_TABLE rather than accumulating documents in client-side process RAM.

Services MUST process multi-gigabyte ingestion streams using chunked pipelines or COPY streams alongside JSON_TABLE rather than accumulating documents in client-side process RAM.

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 JSON_TABLE path queries containing unindexed nested JSON_TABLE sibling projections over large collections without evaluating physical EXPLAIN (ANALYZE, MEMORY) output.

Components NEVER execute JSON_TABLE path queries containing unindexed nested JSON_TABLE sibling projections over large collections without evaluating physical EXPLAIN (ANALYZE, MEMORY) output.

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 Streaming JSON_TABLE Zero-Materialization Allocator Standard critical for AI coding assistants?

Enforces non-materializing, stream-based evaluation for Postgres 17 SQL/JSON JSON_TABLE queries during high-volume document ingestion. Prevents catastrophic backend OOM conditions caused by eager JSONB AST deserialization and unbounded memory context retention by streaming raw text buffers directly into relational columns without intermediate DOM allocation. 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 (Eager JSONB Parsing and Deep AST Materialization in JSON_TABLE 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

Postgres