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

PostgreSQL 17 JSON_TABLE Zero-Copy Projection & Plan-Cache Safe Ingestion

Mandates native ANSI SQL/JSON JSON_TABLE projection for high-throughput batch ingestion pipelines on PostgreSQL 17+, completely bypassing Node.js heap allocations and plan-cache invalidation. Eliminates multi-pass jsonb_array_elements / jsonb_to_recordset table-scan overhead by binding payloads to single parameterized JSONB arguments evaluated strictly at the storage engine level.

Prohibited Anti-Pattern: Heap-Bloating In-Memory Batch Transformations and Dynamic Multi-Row SQL Value Construction

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 JSON_TABLE Zero-Copy Projection & Plan-Cache Safe Ingestion - Mandates native ANSI SQL/JSON JSON_TABLE projection for high-throughput batch ingestion pipelines on PostgreSQL 17+, completely bypassing Node.js heap allocations and plan-cache invalidation. Eliminates multi-pass jsonb_array_elements / jsonb_to_recordset table-scan overhead by binding payloads to single parameterized JSONB arguments evaluated strictly at the storage engine level.
globs: src/**/*.repository.ts, src/**/*.dao.ts, src/**/*.service.ts, src/db/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pg17-json-table-zero-copy-ingestion"
---

# PostgreSQL 17 JSON_TABLE Zero-Copy Projection & Plan-Cache Safe Ingestion

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Heap-Bloating In-Memory Batch Transformations and Dynamic Multi-Row SQL Value Construction

## System Constraints
You are a Principal Software Architect enforcing PostgreSQL 17 JSON_TABLE zero-copy projections and plan-cache stability. You must reject application-level object destructuring loops, multi-variable parameter bloat (e.g., dynamic ($1, $2), ($3, $4) clauses), and legacy PostgreSQL JSON extraction functions (jsonb_array_elements, jsonb_to_recordset) in high-throughput paths. Mandate the use of ANSI SQL/JSON JSON_TABLE with explicit column types, PATH expressions, parameterized arguments, and ERROR ON ERROR failure modes to ensure stable execution plans and zero JavaScript memory allocation overhead during payload unnesting.

## Strict Guidelines
- Engineers MUST ALWAYS pass high-volume batch payloads as a single parameterized JSONB string or Buffer ($1::jsonb) rather than dynamically constructing multi-row VALUES statements.
- Engineers MUST ALWAYS project tabular structures from nested JSON using PostgreSQL 17 ANSI SQL/JSON JSON_TABLE instead of legacy jsonb_array_elements or jsonb_to_recordset functions.
- Engineers MUST explicitly specify column data types and PATH targets inside JSON_TABLE COLUMNS clauses (e.g., COLUMNS (id uuid PATH '$.id', metric_value double precision PATH '$.val')).
- Engineers MUST include explicit ERROR ON ERROR handling clauses within JSON_TABLE definitions to prevent silent NULL coercions on malformed stream payloads.
- Engineers MUST NOT construct dynamic SQL templates with varying numbers of bind parameters ($1..$N) for batches, as this pollutes the PostgreSQL plan cache and exhausts backend catalog memory.
- Engineers MUST NOT deserialize large JSON payloads in the Node.js/V8 runtime solely to remap fields for relational insertion when the target schema mirrors or projects subsets of the incoming stream.
- Services MUST execute batch inserts via INSERT INTO ... SELECT ... FROM JSON_TABLE(...) ON CONFLICT DO UPDATE/NOTHING to achieve end-to-end zero-copy pipeline execution.

## Bad vs Good Implementations
### Ingesting High-Throughput Batch Telemetry

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Iterating over thousands of events in Node.js creates severe V8 garbage collection churn. Building variable-length SQL strings ($1...$N) invalidates the PostgreSQL prepared statement plan cache for every distinct batch size, causing excessive CPU consumption during query re-planning.
```typescript
import { Pool } from 'pg';

interface TelemetryEvent {
  id: string;
  tenantId: string;
  reading: number;
  recordedAt: string;
}

export async function ingestTelemetryBad(pool: Pool, events: TelemetryEvent[]): Promise<void> {
  // Anti-pattern: Heap allocation via array mapping and dynamic SQL string generation.
  // Generates variable parameter counts ($1, $2, $3...) which evicts Postgres prepared statement caches.
  if (events.length === 0) return;

  const values: unknown[] = [];
  const placeholders: string[] = [];

  events.forEach((event, idx) => {
    const offset = idx * 4;
    placeholders.push(`($${offset + 1}, $${offset + 2}, $${offset + 3}, $${offset + 4})`);
    values.push(event.id, event.tenantId, event.reading, event.recordedAt);
  });

  const query = `
    INSERT INTO telemetry_readings (id, tenant_id, reading, recorded_at)
    VALUES ${placeholders.join(', ')}
    ON CONFLICT (id) DO NOTHING;
  `;

  await pool.query(query, values);
}
```

#### Production Standard (Correct)
> **Standard**: Leverages PostgreSQL 17 JSON_TABLE to project columns natively in C/storage-engine space. The query structure has exactly one static bind parameter ($1), enabling prepared statement plan caching, avoiding V8 heap allocation, and failing fast on invalid payloads via ERROR ON ERROR.
```typescript
import { Pool } from 'pg';

export async function ingestTelemetryGood(pool: Pool, rawPayloadJson: string): Promise<void> {
  // Invariant: Pass JSON string directly to a static, plan-cache safe parameterized query.
  // PostgreSQL 17 JSON_TABLE unpacks the stream directly into the relational target with zero V8 GC overhead.
  const query = `
    INSERT INTO telemetry_readings (id, tenant_id, reading, recorded_at)
    SELECT
      jt.id,
      jt.tenant_id,
      jt.reading,
      jt.recorded_at
    FROM JSON_TABLE(
      $1::jsonb,
      '$.events[*]'
      COLUMNS (
        id uuid PATH '$.id',
        tenant_id uuid PATH '$.tenantId',
        reading double precision PATH '$.reading',
        recorded_at timestamptz PATH '$.recordedAt'
      )
      ERROR ON ERROR
    ) AS jt
    ON CONFLICT (id) DO NOTHING;
  `;

  // Statically prepared statement: cached execution plan is reused across every batch execution.
  await pool.query({
    name: 'telemetry_batch_ingest_json_table',
    text: query,
    values: [rawPayloadJson]
  });
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: PostgreSQL 17 JSON_TABLE Zero-Copy Projection & Plan-Cache Safe Ingestion

PostgreSQL 17 JSON_TABLE Zero-Copy Projection & Plan-Cache Safe Ingestion defines an industrial-grade behavioral standard for backend engineering. Mandates native ANSI SQL/JSON JSON_TABLE projection for high-throughput batch ingestion pipelines on PostgreSQL 17+, completely bypassing Node.js heap allocations and plan-cache invalidation. Eliminates multi-pass jsonb_array_elements / jsonb_to_recordset table-scan overhead by binding payloads to single parameterized JSONB arguments evaluated strictly at the storage engine level. 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: Heap-Bloating In-Memory Batch Transformations and Dynamic Multi-Row SQL Value Construction 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 DriftHeap-Bloating In-Memory Batch Transformations and Dynamic Multi-Row SQL Value ConstructionCritical
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 high-volume batch payloads as a single parameterized JSONB string or Buffer ($1

Engineers MUST ALWAYS pass high-volume batch payloads as a single parameterized JSONB string or Buffer ($1::jsonb) rather than dynamically constructing multi-row VALUES statements.

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 project tabular structures from nested JSON using PostgreSQL 17 ANSI SQL/JSON JSON_TABLE instead of legacy jsonb_array_elements or jsonb_to_recordset functions.

Engineers MUST ALWAYS project tabular structures from nested JSON using PostgreSQL 17 ANSI SQL/JSON JSON_TABLE instead of legacy jsonb_array_elements or jsonb_to_recordset functions.

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 explicitly specify column data types and PATH targets inside JSON_TABLE COLUMNS clauses (e.g., COLUMNS (id uuid PATH '$.id', metric_value double precision PATH '$.val')).

Engineers MUST explicitly specify column data types and PATH targets inside JSON_TABLE COLUMNS clauses (e.g., COLUMNS (id uuid PATH '$.id', metric_value double precision PATH '$.val')).

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

Engineers MUST include explicit ERROR ON ERROR handling clauses within JSON_TABLE definitions to prevent silent NULL coercions on malformed stream payloads.

Engineers MUST include explicit ERROR ON ERROR handling clauses within JSON_TABLE definitions to prevent silent NULL coercions on malformed stream payloads.

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

Engineers MUST NOT construct dynamic SQL templates with varying numbers of bind parameters ($1..$N) for batches, as this pollutes the PostgreSQL plan cache and exhausts backend catalog memory.

Engineers MUST NOT construct dynamic SQL templates with varying numbers of bind parameters ($1..$N) for batches, as this pollutes the PostgreSQL plan cache and exhausts backend catalog 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.
6

Engineers MUST NOT deserialize large JSON payloads in the Node.js/V8 runtime solely to remap fields for relational insertion when the target schema mirrors or projects subsets of the incoming stream.

Engineers MUST NOT deserialize large JSON payloads in the Node.js/V8 runtime solely to remap fields for relational insertion when the target schema mirrors or projects subsets of the incoming stream.

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 execute batch inserts via INSERT INTO ... SELECT ... FROM JSON_TABLE(...) ON CONFLICT DO UPDATE/NOTHING to achieve end-to-end zero-copy pipeline execution.

Services MUST execute batch inserts via INSERT INTO ... SELECT ... FROM JSON_TABLE(...) ON CONFLICT DO UPDATE/NOTHING to achieve end-to-end zero-copy pipeline execution.

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 Projection & Plan-Cache Safe Ingestion critical for AI coding assistants?

Mandates native ANSI SQL/JSON JSON_TABLE projection for high-throughput batch ingestion pipelines on PostgreSQL 17+, completely bypassing Node.js heap allocations and plan-cache invalidation. Eliminates multi-pass jsonb_array_elements / jsonb_to_recordset table-scan overhead by binding payloads to single parameterized JSONB arguments evaluated strictly at the storage engine level. 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 (Heap-Bloating In-Memory Batch Transformations and Dynamic Multi-Row SQL Value Construction) 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