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-postgresql-17-js-302

PostgreSQL 17 JSON_TABLE Zero-Copy Relational Projection Standard

Mandates the use of PostgreSQL 17 standard SQL/JSON JSON_TABLE projections for high-throughput semi-structured ingestion pipelines instead of client-side array deserialization or legacy jsonb_array_elements expansions. This eliminates Node.js heap exhaustion and database backend palloc bloat by transforming JSON payloads directly into typed relational tuples in-engine with zero intermediate allocations.

Prohibited Anti-Pattern: Client-Side JSON Array Unpacking and Quadratic Cartesian jsonb_array_elements Ingestion

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 JSON_TABLE Zero-Copy Relational Projection Standard - Mandates the use of PostgreSQL 17 standard SQL/JSON JSON_TABLE projections for high-throughput semi-structured ingestion pipelines instead of client-side array deserialization or legacy jsonb_array_elements expansions. This eliminates Node.js heap exhaustion and database backend palloc bloat by transforming JSON payloads directly into typed relational tuples in-engine with zero intermediate allocations.
globs: src/**/*.ts, src/**/*.sql, migrations/**/*.sql
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-json-table-zero-copy-projection-postgresql-17-js-302"
---

# PostgreSQL 17 JSON_TABLE Zero-Copy Relational Projection Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Client-Side JSON Array Unpacking and Quadratic Cartesian jsonb_array_elements Ingestion

## System Constraints
You are a Principal Backend Engineer enforcing the PostgreSQL 17 JSON_TABLE Zero-Copy Projection Invariant. Enforce inline SQL/JSON transformations within single transactional boundaries, forbidding client-side array mapping of large payloads and banning legacy jsonb_to_recordset or unnested cross-joins for ingestion.

## Strict Guidelines
- Engineers MUST ALWAYS project semi-structured ingestion batches into relational tables using PostgreSQL 17 'JSON_TABLE' expressions inside a direct 'INSERT INTO ... SELECT' query.
- Engineers MUST NOT parse, deserialize, or iterate over large JSON ingestion arrays in application memory (e.g., Node.js V8 heap) prior to database insertion.
- Queries MUST specify strict path expressions (e.g., 'strict $.events[*]') and explicit typed column clauses inside 'JSON_TABLE' to prevent runtime coercion fallbacks and silent null propagation.
- Services MUST pass raw JSON buffers directly as single bind parameters ($1::jsonb) to avoid string concatenation and intermediate client allocations.
- Components NEVER utilize legacy 'jsonb_array_elements()' or 'jsonb_to_recordset()' in batch append workflows due to unbounded memory context allocations and missing zero-copy streaming optimizations.

## Bad vs Good Implementations
### Batch Ingestion of Raw Event Telemetry

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Deserializing the JSON payload into JavaScript objects inflates memory footprint by up to 10x in Node.js V8, causing frequent GC pauses, and executing iterative inserts produces catastrophic network round-trip overhead.
```typescript
import { Pool } from 'pg';

interface RawEvent {
  eventId: string;
  userId: string;
  amount: number;
  timestamp: string;
}

export async function ingestTelemetry(pool: Pool, rawPayload: string): Promise<void> {
  // ANTI-PATTERN: Parsing payload into V8 memory causes massive heap spikes.
  // Expanding tens of thousands of objects leads to garbage collection freezes.
  const events: RawEvent[] = JSON.parse(rawPayload);

  const client = await pool.connect();
  try {
    await client.query('BEGIN');
    for (const event of events) {
      await client.query(
        'INSERT INTO event_store (id, user_id, amount, created_at) VALUES ($1, $2, $3, $4)',
        [event.eventId, event.userId, event.amount, new Date(event.timestamp)]
      );
    }
    await client.query('COMMIT');
  } catch (error) {
    await client.query('ROLLBACK');
    throw error;
  } finally {
    client.release();
  }
}
```

#### Production Standard (Correct)
> **Standard**: The payload is piped directly as a raw buffer/string into PostgreSQL 17's SQL/JSON parser. `JSON_TABLE` reads the `jsonb` representation in-place and projects typed tuples straight into table storage with zero intermediate object instantiation.
```typescript
import { Pool } from 'pg';

export async function ingestTelemetry(pool: Pool, rawPayload: Buffer | string): Promise<number> {
  // PRODUCTION INVARIANT: Stream raw JSON directly to PostgreSQL 17 JSON_TABLE.
  // In-engine zero-copy projection maps binary jsonb buffers directly to relational rows.
  const query = `
    INSERT INTO event_store (id, user_id, amount, created_at)
    SELECT 
      jt.id,
      jt.user_id,
      jt.amount,
      jt.created_at
    FROM JSON_TABLE(
      $1::jsonb,
      'strict $.events[*]'
      COLUMNS (
        id UUID PATH '$.eventId',
        user_id UUID PATH '$.userId',
        amount NUMERIC(12, 4) PATH '$.amount',
        created_at TIMESTAMPTZ PATH '$.timestamp'
      )
    ) AS jt;
  `;

  const result = await pool.query(query, [rawPayload]);
  return result.rowCount ?? 0;
}
```

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: PostgreSQL 17 JSON_TABLE Zero-Copy Relational Projection Standard

PostgreSQL 17 JSON_TABLE Zero-Copy Relational Projection Standard defines an industrial-grade behavioral standard for backend engineering. Mandates the use of PostgreSQL 17 standard SQL/JSON JSON_TABLE projections for high-throughput semi-structured ingestion pipelines instead of client-side array deserialization or legacy jsonb_array_elements expansions. This eliminates Node.js heap exhaustion and database backend palloc bloat by transforming JSON payloads directly into typed relational tuples in-engine with zero intermediate allocations. 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: Client-Side JSON Array Unpacking and Quadratic Cartesian jsonb_array_elements 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 DriftClient-Side JSON Array Unpacking and Quadratic Cartesian jsonb_array_elements 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 project semi-structured ingestion batches into relational tables using PostgreSQL 17 'JSON_TABLE' expressions inside a direct 'INSERT INTO ... SELECT' query.

Engineers MUST ALWAYS project semi-structured ingestion batches into relational tables using PostgreSQL 17 'JSON_TABLE' expressions inside a direct 'INSERT INTO ... SELECT' query.

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 parse, deserialize, or iterate over large JSON ingestion arrays in application memory (e.g., Node.js V8 heap) prior to database insertion.

Engineers MUST NOT parse, deserialize, or iterate over large JSON ingestion arrays in application memory (e.g., Node.js V8 heap) prior to database insertion.

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 MUST specify strict path expressions (e.g., 'strict $.events[*]') and explicit typed column clauses inside 'JSON_TABLE' to prevent runtime coercion fallbacks and silent null propagation.

Queries MUST specify strict path expressions (e.g., 'strict $.events[*]') and explicit typed column clauses inside 'JSON_TABLE' to prevent runtime coercion fallbacks and silent null propagation.

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 pass raw JSON buffers directly as single bind parameters ($1

Services MUST pass raw JSON buffers directly as single bind parameters ($1::jsonb) to avoid string concatenation and intermediate client 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.
5

Components NEVER utilize legacy 'jsonb_array_elements()' or 'jsonb_to_recordset()' in batch append workflows due to unbounded memory context allocations and missing zero-copy streaming optimizations.

Components NEVER utilize legacy 'jsonb_array_elements()' or 'jsonb_to_recordset()' in batch append workflows due to unbounded memory context allocations and missing zero-copy streaming optimizations.

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 JSON_TABLE Zero-Copy Relational Projection Standard critical for AI coding assistants?

Mandates the use of PostgreSQL 17 standard SQL/JSON JSON_TABLE projections for high-throughput semi-structured ingestion pipelines instead of client-side array deserialization or legacy jsonb_array_elements expansions. This eliminates Node.js heap exhaustion and database backend palloc bloat by transforming JSON payloads directly into typed relational tuples in-engine with zero intermediate allocations. 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 (Client-Side JSON Array Unpacking and Quadratic Cartesian jsonb_array_elements 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

PostgreSQL