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

PostgreSQL 17 SQL/JSON JSON_TABLE Optimization & Memory Streaming Guardrail

Enforces strict type-directed path expressions, explicit column projections, and memory-bounded streaming evaluation when using PostgreSQL 17 JSON_TABLE constructs. Prevents catastrophic query plan degradation, OOM crashes, and 100% CPU pinning caused by unoptimized JSONB materialization, implicit path conversions, and untyped recursive descent over high-throughput event streams.

Prohibited Anti-Pattern: Untyped Wildcard JSON_TABLE Queries Causing Intermediate Materialization & CPU Saturation

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 SQL/JSON JSON_TABLE Optimization & Memory Streaming Guardrail - Enforces strict type-directed path expressions, explicit column projections, and memory-bounded streaming evaluation when using PostgreSQL 17 JSON_TABLE constructs. Prevents catastrophic query plan degradation, OOM crashes, and 100% CPU pinning caused by unoptimized JSONB materialization, implicit path conversions, and untyped recursive descent over high-throughput event streams.
globs: src/repositories/**/*.ts, src/database/**/*.ts, src/services/**/*.ts, src/models/**/*.ts, migrations/**/*.sql
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pg17-json-table-optimization-standard"
---

# PostgreSQL 17 SQL/JSON JSON_TABLE Optimization & Memory Streaming Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Untyped Wildcard JSON_TABLE Queries Causing Intermediate Materialization & CPU Saturation

## System Constraints
You are a Principal Database Architect and Backend Systems Engineer enforcing PostgreSQL 17 SQL/JSON (JSON_TABLE) optimization invariants. Mandate explicit projection schemas, strict error-handling behaviors ('ERROR ON ERROR' vs 'EMPTY ON ERROR'), typed path navigations over unconstrained recursive lookups, and bounded batch projections to prevent backend execution thread starvation and heap exhaustion.

## Strict Guidelines
- Engineers MUST ALWAYS declare explicit column datatypes and explicit JSON path clauses for every column projected within `JSON_TABLE(...)`.
- Engineers MUST NEVER use recursive wildcards (`$.**`) or open-ended path expressions inside high-throughput `JSON_TABLE` row patterns; path patterns MUST be fully qualified to permit one-pass streaming parsing.
- Engineers MUST configure explicit error handling semantics (`ERROR ON ERROR` or `EMPTY ON ERROR`) for projected fields to prevent silent type-mismatch fallback to costly nested scans.
- Services MUST stream bulk JSON ingestion via bounded arrays using explicit `WITH ORDINALITY` indices rather than accumulating unbounded multi-megabyte payloads in a single unindexed JSONB cell.
- Database queries MUST NOT use `JSON_TABLE` as an inline filter predicate without a driving index-backed primary join; push down JSON filter predicates into `JSON_EXISTS` with deterministic JSON path predicates (`@.?`) to enable GIN/jsonb_path_ops index scans.

## Bad vs Good Implementations
### SQL/JSON Projection: Unbounded Wildcard Materialization vs. Indexed Path Streaming

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using deep wildcard recursion ('$.**') disables the PostgreSQL 17 SQL/JSON single-pass streaming parser. The engine materializes the entire nested JSONB tree into memory per tuple, pinning CPU threads and degrading execution time from milliseconds to tens of seconds under concurrent load.
```typescript
import { sql } from 'kysely';
import { db } from '../database';

export async function extractEventMetrics(rawPayloads: string[]) {
  // Anti-Pattern: Unconstrained recursive descent '$.**' inside JSON_TABLE
  // Forces PG17 engine to build complete intermediate ASTs for each row,
  // causing CPU pinning and bypass of streaming executor optimizations.
  return await sql`
    SELECT jt.metric_name, jt.metric_value
    FROM json_events je,
    JSON_TABLE(
      je.payload,
      '$.**' COLUMNS (
        metric_name text PATH '$.name',
        metric_value numeric PATH '$.value'
      )
    ) AS jt
    WHERE je.received_at >= NOW() - INTERVAL '1 hour';
  `.execute(db);
}
```

#### Production Standard (Correct)
> **Standard**: The optimized query establishes a deterministic array path ('$.metrics[*]'), leverages JSON_EXISTS for index-compatible predicate pushdown, assigns precise data types with bounds, and defines explicit error-handling behaviors ('ERROR ON ERROR' / 'EMPTY ON ERROR'), allowing the PostgreSQL 17 executor to evaluate payloads in a bounded, memory-safe stream without allocating intermediate DOM structures.
```typescript
import { sql } from 'kysely';
import { db } from '../database';

export async function extractEventMetrics(batchId: string) {
  // Production Invariant: Direct, strictly typed pathing with deterministic
  // streaming evaluation, index gating, and explicit error clauses.
  return await sql`
    SELECT jt.metric_name, jt.metric_value
    FROM json_events je,
    JSON_TABLE(
      je.payload,
      '$.metrics[*]' PASSING je.payload
      COLUMNS (
        metric_id for ordinality,
        metric_name varchar(64) PATH '$.name' ERROR ON ERROR,
        metric_value numeric(12,4) PATH '$.value' EMPTY ON ERROR
      )
    ) AS jt
    WHERE je.batch_id = ${batchId}
      AND JSON_EXISTS(je.payload, '$.metrics[*]?(@.value > 0)');
  `.execute(db);
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: PostgreSQL 17 SQL/JSON JSON_TABLE Optimization & Memory Streaming Guardrail

PostgreSQL 17 SQL/JSON JSON_TABLE Optimization & Memory Streaming Guardrail defines an industrial-grade behavioral standard for backend engineering. Enforces strict type-directed path expressions, explicit column projections, and memory-bounded streaming evaluation when using PostgreSQL 17 JSON_TABLE constructs. Prevents catastrophic query plan degradation, OOM crashes, and 100% CPU pinning caused by unoptimized JSONB materialization, implicit path conversions, and untyped recursive descent over high-throughput event 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: Untyped Wildcard JSON_TABLE Queries Causing Intermediate Materialization & CPU Saturation 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 DriftUntyped Wildcard JSON_TABLE Queries Causing Intermediate Materialization & CPU SaturationCritical
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 declare explicit column datatypes and explicit JSON path clauses for every column projected within `JSON_TABLE(...)`.

Engineers MUST ALWAYS declare explicit column datatypes and explicit JSON path clauses for every column projected within `JSON_TABLE(...)`.

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 NEVER use recursive wildcards (`$.**`) or open-ended path expressions inside high-throughput `JSON_TABLE` row patterns

Engineers MUST NEVER use recursive wildcards (`$.**`) or open-ended path expressions inside high-throughput `JSON_TABLE` row patterns; path patterns MUST be fully qualified to permit one-pass streaming 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.
3

Engineers MUST configure explicit error handling semantics (`ERROR ON ERROR` or `EMPTY ON ERROR`) for projected fields to prevent silent type-mismatch fallback to costly nested scans.

Engineers MUST configure explicit error handling semantics (`ERROR ON ERROR` or `EMPTY ON ERROR`) for projected fields to prevent silent type-mismatch fallback to costly nested scans.

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 stream bulk JSON ingestion via bounded arrays using explicit `WITH ORDINALITY` indices rather than accumulating unbounded multi-megabyte payloads in a single unindexed JSONB cell.

Services MUST stream bulk JSON ingestion via bounded arrays using explicit `WITH ORDINALITY` indices rather than accumulating unbounded multi-megabyte payloads in a single unindexed JSONB cell.

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 queries MUST NOT use `JSON_TABLE` as an inline filter predicate without a driving index-backed primary join

Database queries MUST NOT use `JSON_TABLE` as an inline filter predicate without a driving index-backed primary join; push down JSON filter predicates into `JSON_EXISTS` with deterministic JSON path predicates (`@.?`) to enable GIN/jsonb_path_ops index scans.

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 SQL/JSON JSON_TABLE Optimization & Memory Streaming Guardrail critical for AI coding assistants?

Enforces strict type-directed path expressions, explicit column projections, and memory-bounded streaming evaluation when using PostgreSQL 17 JSON_TABLE constructs. Prevents catastrophic query plan degradation, OOM crashes, and 100% CPU pinning caused by unoptimized JSONB materialization, implicit path conversions, and untyped recursive descent over high-throughput event 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 (Untyped Wildcard JSON_TABLE Queries Causing Intermediate Materialization & CPU Saturation) 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