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

PostgreSQL 17 Direct I/O Tuning & SQL:2023 JSON_TABLE Projections

Mandates the use of PostgreSQL 17 SQL:2023 standard JSON_TABLE projections for bulk event ingestion and configures asynchronous direct I/O (io_direct) for write-ahead log (WAL) and data writes. Eliminates client-tier JSON deserialization overhead, reduces Node/Go memory pressure, and prevents operating system buffer cache double-buffering during high-throughput ingest.

Prohibited Anti-Pattern: Client-Side In-Memory JSON Shredding and Unbuffered JSONB Table Unnesting Under Buffered Disk I/O

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 Direct I/O Tuning & SQL:2023 JSON_TABLE Projections - Mandates the use of PostgreSQL 17 SQL:2023 standard JSON_TABLE projections for bulk event ingestion and configures asynchronous direct I/O (io_direct) for write-ahead log (WAL) and data writes. Eliminates client-tier JSON deserialization overhead, reduces Node/Go memory pressure, and prevents operating system buffer cache double-buffering during high-throughput ingest.
globs: src/**/*.ts, src/**/*.pg.sql, migrations/**/*.sql, infra/**/*.tf
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-async-direct-io-json-table-projections"
---

# PostgreSQL 17 Direct I/O Tuning & SQL:2023 JSON_TABLE Projections

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Client-Side In-Memory JSON Shredding and Unbuffered JSONB Table Unnesting Under Buffered Disk I/O

## System Constraints
You are a Principal Database Architect enforcing PostgreSQL 17 standards. Mandate zero-copy in-engine JSON extraction using SQL:2023 JSON_TABLE projections and direct asynchronous disk I/O. Forbid application-tier JSON array iterating for bulk database inserts, avoid legacy jsonb_to_recordset functions, and enforce strict typing on projected columns with ERROR ON ERROR semantics.

## Strict Guidelines
- Engineers MUST ALWAYS offload batch JSON transformations to PostgreSQL 17's native `JSON_TABLE` syntax rather than parsing large payload arrays in application memory.
- Engineers MUST NOT use legacy `jsonb_array_elements` or `jsonb_to_recordset` for bulk ingestion paths exceeding 1,000 events per batch due to extreme planning and iteration overhead.
- Engineers MUST configure PostgreSQL 17 `io_direct` to include `wal` (and `data` where supported by kernel/filesystem) to bypass OS page cache double-buffering under sustained write loads.
- Queries MUST specify explicit column mappings and path definitions with `ERROR ON ERROR` clauses in `JSON_TABLE` to prevent silent null conversion on schema mismatches.
- Data ingestion pipelines MUST stream raw JSON strings directly into the database client parameters as single payload buffers rather than unmarshalling into native object arrays.

## Bad vs Good Implementations
### Batch Ingestion: Client-Side Mapping vs. Native PostgreSQL 17 JSON_TABLE

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Parsing large JSON arrays in the Node.js event loop blocks the single thread, generates excessive garbage collector pressure, and forces multi-roundtrip or bloated parameter arrays. It also bypasses PostgreSQL 17's optimized SQL:2023 parser.
```typescript
import { Pool } from 'pg';

interface IngestionEvent {
  eventId: string;
  tenantId: string;
  timestamp: number;
  payload: Record<string, unknown>;
}

export async function ingestEventsBad(pool: Pool, rawJsonPayload: string): Promise<void> {
  // ANTI-PATTERN: Parsing mega-payloads in client runtime causes high memory bloat
  // and uses slow legacy jsonb unnesting or massive dynamic parameterized queries
  const events: IngestionEvent[] = JSON.parse(rawJsonPayload);

  const client = await pool.connect();
  try {
    await client.query('BEGIN');
    for (const event of events) {
      await client.query(
        `INSERT INTO telemetry_events (event_id, tenant_id, occurred_at, metadata)
         VALUES ($1, $2, to_timestamp($3 / 1000.0), $4)`,
        [event.eventId, event.tenantId, event.timestamp, JSON.stringify(event.payload)]
      );
    }
    await client.query('COMMIT');
  } catch (err) {
    await client.query('ROLLBACK');
    throw err;
  } finally {
    client.release();
  }
}
```

#### Production Standard (Correct)
> **Standard**: Passes the raw JSON string straight to PostgreSQL 17's native `JSON_TABLE`, which executes columnar extraction at wire speed. Strict column typings and `ERROR ON ERROR` prevent corrupted schemas, while bypassing application-level de-serialization entirely.
```typescript
import { Pool } from 'pg';

export async function ingestEventsGood(pool: Pool, rawJsonPayload: string): Promise<void> {
  // PRODUCTION INVARIANT: Stream raw payload directly into PostgreSQL 17 JSON_TABLE
  // Offloads shredding to optimized C execution engine, preventing app GC thrashing.
  const query = `
    INSERT INTO telemetry_events (event_id, tenant_id, occurred_at, metadata)
    SELECT 
      j.event_id,
      j.tenant_id,
      to_timestamp(j.occurred_at_ms / 1000.0),
      j.metadata
    FROM JSON_TABLE(
      $1::jsonb, '$[*]'
      COLUMNS (
        event_id UUID PATH '$.eventId' ERROR ON ERROR,
        tenant_id VARCHAR(64) PATH '$.tenantId' ERROR ON ERROR,
        occurred_at_ms BIGINT PATH '$.timestamp' ERROR ON ERROR,
        metadata JSONB PATH '$.payload'
      )
    ) AS j;
  `;

  const client = await pool.connect();
  try {
    // Ensure session or cluster utilizes io_direct for WAL/Data where applicable
    await client.query(query, [rawJsonPayload]);
  } finally {
    client.release();
  }
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: PostgreSQL 17 Direct I/O Tuning & SQL:2023 JSON_TABLE Projections

PostgreSQL 17 Direct I/O Tuning & SQL:2023 JSON_TABLE Projections defines an industrial-grade behavioral standard for backend engineering. Mandates the use of PostgreSQL 17 SQL:2023 standard JSON_TABLE projections for bulk event ingestion and configures asynchronous direct I/O (io_direct) for write-ahead log (WAL) and data writes. Eliminates client-tier JSON deserialization overhead, reduces Node/Go memory pressure, and prevents operating system buffer cache double-buffering during high-throughput ingest. 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 In-Memory JSON Shredding and Unbuffered JSONB Table Unnesting Under Buffered Disk I/O 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 In-Memory JSON Shredding and Unbuffered JSONB Table Unnesting Under Buffered Disk I/OCritical
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 offload batch JSON transformations to PostgreSQL 17's native `JSON_TABLE` syntax rather than parsing large payload arrays in application memory.

Engineers MUST ALWAYS offload batch JSON transformations to PostgreSQL 17's native `JSON_TABLE` syntax rather than parsing large payload arrays in application 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.
2

Engineers MUST NOT use legacy `jsonb_array_elements` or `jsonb_to_recordset` for bulk ingestion paths exceeding 1,000 events per batch due to extreme planning and iteration overhead.

Engineers MUST NOT use legacy `jsonb_array_elements` or `jsonb_to_recordset` for bulk ingestion paths exceeding 1,000 events per batch due to extreme planning and iteration overhead.

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 PostgreSQL 17 `io_direct` to include `wal` (and `data` where supported by kernel/filesystem) to bypass OS page cache double-buffering under sustained write loads.

Engineers MUST configure PostgreSQL 17 `io_direct` to include `wal` (and `data` where supported by kernel/filesystem) to bypass OS page cache double-buffering under sustained write loads.

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 specify explicit column mappings and path definitions with `ERROR ON ERROR` clauses in `JSON_TABLE` to prevent silent null conversion on schema mismatches.

Queries MUST specify explicit column mappings and path definitions with `ERROR ON ERROR` clauses in `JSON_TABLE` to prevent silent null conversion on schema mismatches.

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

Data ingestion pipelines MUST stream raw JSON strings directly into the database client parameters as single payload buffers rather than unmarshalling into native object arrays.

Data ingestion pipelines MUST stream raw JSON strings directly into the database client parameters as single payload buffers rather than unmarshalling into native object arrays.

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 Direct I/O Tuning & SQL:2023 JSON_TABLE Projections critical for AI coding assistants?

Mandates the use of PostgreSQL 17 SQL:2023 standard JSON_TABLE projections for bulk event ingestion and configures asynchronous direct I/O (io_direct) for write-ahead log (WAL) and data writes. Eliminates client-tier JSON deserialization overhead, reduces Node/Go memory pressure, and prevents operating system buffer cache double-buffering during high-throughput ingest. 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 In-Memory JSON Shredding and Unbuffered JSONB Table Unnesting Under Buffered Disk I/O) 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