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

Postgres 17 JSON_TABLE Zero-Copy Streaming Materialization Boundary

Enforces plan-level streaming projection boundaries when querying JSONB with PostgreSQL 17 standard JSON_TABLE. Eliminates executor tuple spilling into disk-backed Tuplestore buffers and work_mem exhaustion by forbidding intermediate JSON re-serialization, untyped recordset expansions, and unbounded cartesian nested paths.

Prohibited Anti-Pattern: Unbounded Executor Spill and Tuplestore Disk Thrashing via Legacy jsonb_to_recordset and FORMAT JSON Re-Serialization

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 JSON_TABLE Zero-Copy Streaming Materialization Boundary - Enforces plan-level streaming projection boundaries when querying JSONB with PostgreSQL 17 standard JSON_TABLE. Eliminates executor tuple spilling into disk-backed Tuplestore buffers and work_mem exhaustion by forbidding intermediate JSON re-serialization, untyped recordset expansions, and unbounded cartesian nested paths.
globs: src/repositories/**/*.ts, src/database/**/*.ts, migrations/**/*.sql, src/**/*.sql
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-json-table-zero-copy-projection"
---

# Postgres 17 JSON_TABLE Zero-Copy Streaming Materialization Boundary

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Executor Spill and Tuplestore Disk Thrashing via Legacy jsonb_to_recordset and FORMAT JSON Re-Serialization

## System Constraints
You are a Principal Software Architect enforcing zero-copy streaming projections in PostgreSQL 17 using the SQL/JSON standard JSON_TABLE function. You must ensure that JSONB ingestion and relational projection operate without intermediate AST allocations, Tuplestore disk spilling, or unnecessary JSON re-serialization.

## Strict Guidelines
- Engineers MUST ALWAYS project leaf columns directly using typed SQL scalar types (e.g., INT, NUMERIC, UUID, TIMESTAMPTZ) within the JSON_TABLE COLUMNS clause rather than returning intermediate JSONB fragments.
- Engineers MUST NOT specify FORMAT JSON in JSON_TABLE COLUMNS clauses unless the target relational column is explicitly typed and stored as JSON/JSONB.
- Engineers MUST NEVER use legacy jsonb_to_recordset(), jsonb_populate_recordset(), or jsonb_array_elements() in PostgreSQL 17+ codebases when relational projections are required.
- Queries MUST NOT materialize intermediate JSON extraction inside Common Table Expressions (WITH queries without NOT MATERIALIZED) before projection.
- Nested JSON array structures MUST use explicit NESTED PATH clauses within the single JSON_TABLE declaration to allow the planner to stream hierarchical joins in a single scan instead of multiple correlated subqueries.
- Engineers MUST specify PASSING clauses to pass outer scalar parameters into jsonpath expressions rather than interpolating string literals or building dynamic SQL.
- Services MUST monitor EXPLAIN (ANALYZE, BUFFERS) outputs to ensure Table Function Scan node memory usage remains bounded and does not report Batches > 1 or Tuplestore disk spills.

## Bad vs Good Implementations
### PostgreSQL 17 JSON_TABLE Zero-Copy Projection vs. Legacy Materialization

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using jsonb_to_recordset inside a materialized CTE forces Postgres to deserialize the entire JSONB payload into memory, construct an intermediary tuple store, and materialize intermediate records before projecting, causing severe work_mem blowup.
```typescript
import { sql } from 'kysely';
import { db } from './database';

export async function getCustomerOrderMetrics(customerId: string) {
  // ANTI-PATTERN: Forces full in-memory JSON parsing, materializes intermediate records
  // using jsonb_to_recordset inside a materialized CTE, causing high work_mem allocations
  // and potential disk spills on high-volume documents.
  return await db.executeQuery(
    sql`
      WITH unnested_orders AS MATERIALIZED (
        SELECT 
          id AS batch_id,
          (jsonb_to_recordset(payload->'orders')).* AS record
        FROM customer_ingestion_batches
        WHERE customer_id = ${customerId}
      )
      SELECT 
        batch_id,
        (record).order_id::text AS order_id,
        (record).amount::numeric AS amount,
        (record).items::jsonb AS items_blob
      FROM unnested_orders;
    `
  );
}
```

#### Production Standard (Correct)
> **Standard**: PostgreSQL 17 JSON_TABLE streams jsonpath extractions directly into target relational tuple slots. Typed COLUMNS avoid runtime cast overhead, NESTED PATH eliminates correlated subqueries, and zero intermediate JSONB chunks are re-serialized.
```typescript
import { sql } from 'kysely';
import { db } from './database';

export async function getCustomerOrderMetrics(customerId: string) {
  // PRODUCTION INVARIANT: PostgreSQL 17 standard JSON_TABLE streams relational tuples
  // directly from the underlying JSONB column without intermediate re-serialization.
  // NESTED PATH evaluates sibling/child arrays in a single zero-copy streaming scan.
  return await db.executeQuery(
    sql`
      SELECT 
        b.id AS batch_id,
        jt.order_id,
        jt.amount,
        jt.item_sku,
        jt.quantity
      FROM customer_ingestion_batches b,
      JSON_TABLE(
        b.payload,
        '$.orders[*]'
        COLUMNS (
          order_id UUID PATH '$.id',
          amount NUMERIC(12, 2) PATH '$.total',
          NESTED PATH '$.items[*]'
            COLUMNS (
              item_sku TEXT PATH '$.sku',
              quantity INTEGER PATH '$.qty'
            )
        )
      ) AS jt
      WHERE b.customer_id = ${customerId};
    `
  );
}
```

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 Zero-Copy Streaming Materialization Boundary

Postgres 17 JSON_TABLE Zero-Copy Streaming Materialization Boundary defines an industrial-grade behavioral standard for backend engineering. Enforces plan-level streaming projection boundaries when querying JSONB with PostgreSQL 17 standard JSON_TABLE. Eliminates executor tuple spilling into disk-backed Tuplestore buffers and work_mem exhaustion by forbidding intermediate JSON re-serialization, untyped recordset expansions, and unbounded cartesian nested paths. 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 Executor Spill and Tuplestore Disk Thrashing via Legacy jsonb_to_recordset and FORMAT JSON Re-Serialization 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 Executor Spill and Tuplestore Disk Thrashing via Legacy jsonb_to_recordset and FORMAT JSON Re-SerializationCritical
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 project leaf columns directly using typed SQL scalar types (e.g., INT, NUMERIC, UUID, TIMESTAMPTZ) within the JSON_TABLE COLUMNS clause rather than returning intermediate JSONB fragments.

Engineers MUST ALWAYS project leaf columns directly using typed SQL scalar types (e.g., INT, NUMERIC, UUID, TIMESTAMPTZ) within the JSON_TABLE COLUMNS clause rather than returning intermediate JSONB fragments.

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 specify FORMAT JSON in JSON_TABLE COLUMNS clauses unless the target relational column is explicitly typed and stored as JSON/JSONB.

Engineers MUST NOT specify FORMAT JSON in JSON_TABLE COLUMNS clauses unless the target relational column is explicitly typed and stored as JSON/JSONB.

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 NEVER use legacy jsonb_to_recordset(), jsonb_populate_recordset(), or jsonb_array_elements() in PostgreSQL 17+ codebases when relational projections are required.

Engineers MUST NEVER use legacy jsonb_to_recordset(), jsonb_populate_recordset(), or jsonb_array_elements() in PostgreSQL 17+ codebases when relational projections are required.

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

Queries MUST NOT materialize intermediate JSON extraction inside Common Table Expressions (WITH queries without NOT MATERIALIZED) before projection.

Queries MUST NOT materialize intermediate JSON extraction inside Common Table Expressions (WITH queries without NOT MATERIALIZED) before projection.

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

Nested JSON array structures MUST use explicit NESTED PATH clauses within the single JSON_TABLE declaration to allow the planner to stream hierarchical joins in a single scan instead of multiple correlated subqueries.

Nested JSON array structures MUST use explicit NESTED PATH clauses within the single JSON_TABLE declaration to allow the planner to stream hierarchical joins in a single scan instead of multiple correlated subqueries.

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.
6

Engineers MUST specify PASSING clauses to pass outer scalar parameters into jsonpath expressions rather than interpolating string literals or building dynamic SQL.

Engineers MUST specify PASSING clauses to pass outer scalar parameters into jsonpath expressions rather than interpolating string literals or building dynamic SQL.

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.
7

Services MUST monitor EXPLAIN (ANALYZE, BUFFERS) outputs to ensure Table Function Scan node memory usage remains bounded and does not report Batches > 1 or Tuplestore disk spills.

Services MUST monitor EXPLAIN (ANALYZE, BUFFERS) outputs to ensure Table Function Scan node memory usage remains bounded and does not report Batches > 1 or Tuplestore disk spills.

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 Zero-Copy Streaming Materialization Boundary critical for AI coding assistants?

Enforces plan-level streaming projection boundaries when querying JSONB with PostgreSQL 17 standard JSON_TABLE. Eliminates executor tuple spilling into disk-backed Tuplestore buffers and work_mem exhaustion by forbidding intermediate JSON re-serialization, untyped recordset expansions, and unbounded cartesian nested paths. 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 Executor Spill and Tuplestore Disk Thrashing via Legacy jsonb_to_recordset and FORMAT JSON Re-Serialization) 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