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

Postgres 17 Direct I/O 4KB Memory-Page Alignment & Buffer Concurrency Invariant

Enforces strict 4096-byte memory page alignment, block-boundary file offsets, and strict buffer lifecycle pinning for PostgreSQL 17 direct I/O (io_direct) and asynchronous storage pipelines. Bypassing OS page cache via Direct I/O requires zero-copy kernel alignment and completion synchronization; violation causes immediate EINVAL panics, double-buffering latency regressions, and torn page corruptions under write saturation.

Prohibited Anti-Pattern: Submitting Unaligned Buffers or Releasing Concurrency Locks Prior to Direct I/O Completion

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 Direct I/O 4KB Memory-Page Alignment & Buffer Concurrency Invariant - Enforces strict 4096-byte memory page alignment, block-boundary file offsets, and strict buffer lifecycle pinning for PostgreSQL 17 direct I/O (io_direct) and asynchronous storage pipelines. Bypassing OS page cache via Direct I/O requires zero-copy kernel alignment and completion synchronization; violation causes immediate EINVAL panics, double-buffering latency regressions, and torn page corruptions under write saturation.
globs: src/**/*.ts, src/**/*.rs, src/**/*.c
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-direct-io-alignment-buffer-concurrency"
---

# Postgres 17 Direct I/O 4KB Memory-Page Alignment & Buffer Concurrency Invariant

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Submitting Unaligned Buffers or Releasing Concurrency Locks Prior to Direct I/O Completion

## System Constraints
You are a Principal Software Architect enforcing PostgreSQL 17 Direct I/O (io_direct) memory alignment and buffer concurrency invariants. You must mandate strict 4096-byte memory address alignment, transfer sizes in sector multiples, and synchronous or promise-fenced buffer pinning until storage flight completion to eliminate kernel EINVAL failures and silent data tearing.

## Strict Guidelines
- Engineers MUST ALWAYS allocate direct I/O payload memory on strict 4096-byte boundaries (using posix_memalign or aligned slice offsets).
- Engineers MUST NOT dispatch direct I/O write operations where transfer length or file offset is not an exact integer multiple of the disk block size (4096 bytes).
- Code SHOULD verify buffer pointer alignment using bitwise operations (`(address & 0xFFF) === 0`) prior to invoking direct kernel write syscalls.
- Services MUST acquire and maintain exclusive buffer pins or ownership locks through the entire duration of asynchronous I/O completion callbacks.
- Components NEVER mutate, recycle, or speculative-read from buffer memory while kernel asynchronous I/O operations are actively in flight.

## Bad vs Good Implementations
### Direct I/O Buffer Allocation and In-Flight Pin Lifecycle

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Buffer allocations using standard runtime heaps are not guaranteed to align to 4096-byte memory boundaries. Furthermore, write sizes and file offsets that do not match sector alignment cause immediate EINVAL errors under Postgres 17 Direct I/O configurations. Returning without waiting for completion lets subsequent tasks mutate or release memory during write transit, resulting in torn pages.
```typescript
import * as fs from 'node:fs';

// ANTI-PATTERN: Arbitrary heap buffer allocation without page alignment,
// unaligned write sizes, and premature buffer release prior to I/O completion.
export class DirectStorageWriter {
  private fd: number;

  constructor(fd: number) {
    this.fd = fd;
  }

  async writeBlock(offset: number, data: Uint8Array): Promise<void> {
    // Fails on O_DIRECT: Standard Buffer.from does not guarantee 4KB boundary
    const buffer = Buffer.from(data);
    
    // Unaligned length or offset causes kernel EINVAL with O_DIRECT
    fs.write(this.fd, buffer, 0, buffer.length, offset, (err) => {
      if (err) console.error('Write failed', err);
    });
    
    // Concurrency hazard: Returning immediately allows caller to mutate or recycle
    // buffer while kernel asynchronous write is still in flight
  }
}
```

#### Production Standard (Correct)
> **Standard**: Ensures the memory buffer, byte length, and file offset are strictly aligned to 4096-byte boundaries as required by Direct I/O (`io_direct`). Pin tracking enforces that concurrent writers cannot access, mutate, or release the memory buffer until the underlying kernel async operation resolves.
```typescript
import * as fs from 'node:fs';

const ALIGNMENT_BYTES = 4096;
const ALIGNMENT_MASK = ALIGNMENT_BYTES - 1;

export class DirectStorageWriter {
  private fd: number;
  private activePins: Set<number> = new Set();

  constructor(fd: number) {
    this.fd = fd;
  }

  // Allocate an aligned 4KB boundary buffer from an oversized memory segment
  public static createAlignedBuffer(sizeBytes: number): Buffer {
    if (sizeBytes % ALIGNMENT_BYTES !== 0) {
      throw new Error(`Size must be a multiple of ${ALIGNMENT_BYTES}`);
    }
    const raw = Buffer.allocUnsafe(sizeBytes + ALIGNMENT_BYTES);
    const offset = (ALIGNMENT_BYTES - ((raw.byteOffset + raw.buffer.byteLength) & ALIGNMENT_MASK)) & ALIGNMENT_MASK;
    return raw.subarray(offset, offset + sizeBytes);
  }

  async writeBlockAligned(fileOffset: number, alignedBuffer: Buffer): Promise<void> {
    // Validate 4KB boundary invariant for both offset and length
    if ((fileOffset & ALIGNMENT_MASK) !== 0) {
      throw new Error(`File offset ${fileOffset} must be 4KB-aligned for Direct I/O.`);
    }
    if ((alignedBuffer.length & ALIGNMENT_MASK) !== 0) {
      throw new Error(`Buffer length ${alignedBuffer.length} must be a multiple of 4KB.`);
    }

    const bufferAddress = alignedBuffer.byteOffset;
    if ((bufferAddress & ALIGNMENT_MASK) !== 0) {
      throw new Error(`Buffer memory pointer ${bufferAddress} is not aligned to 4KB boundary.`);
    }

    // Concurrency Invariant: Pin buffer and enforce state barrier until I/O completion
    const pinId = fileOffset;
    if (this.activePins.has(pinId)) {
      throw new Error(`Concurrency violation: Buffer offset ${pinId} is already in-flight.`);
    }
    this.activePins.add(pinId);

    return new Promise<void>((resolve, reject) => {
      fs.write(this.fd, alignedBuffer, 0, alignedBuffer.length, fileOffset, (err, written) => {
        // Release buffer lock strictly upon kernel notification
        this.activePins.delete(pinId);

        if (err) {
          return reject(new Error(`Direct I/O failure: ${err.message}`));
        }
        if (written !== alignedBuffer.length) {
          return reject(new Error(`Partial write detected: ${written}/${alignedBuffer.length}`));
        }
        resolve();
      });
    });
  }
}
```

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 Direct I/O 4KB Memory-Page Alignment & Buffer Concurrency Invariant

Postgres 17 Direct I/O 4KB Memory-Page Alignment & Buffer Concurrency Invariant defines an industrial-grade behavioral standard for backend engineering. Enforces strict 4096-byte memory page alignment, block-boundary file offsets, and strict buffer lifecycle pinning for PostgreSQL 17 direct I/O (io_direct) and asynchronous storage pipelines. Bypassing OS page cache via Direct I/O requires zero-copy kernel alignment and completion synchronization; violation causes immediate EINVAL panics, double-buffering latency regressions, and torn page corruptions under write saturation. 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: Submitting Unaligned Buffers or Releasing Concurrency Locks Prior to Direct I/O Completion 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 DriftSubmitting Unaligned Buffers or Releasing Concurrency Locks Prior to Direct I/O CompletionCritical
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 allocate direct I/O payload memory on strict 4096-byte boundaries (using posix_memalign or aligned slice offsets).

Engineers MUST ALWAYS allocate direct I/O payload memory on strict 4096-byte boundaries (using posix_memalign or aligned slice offsets).

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 dispatch direct I/O write operations where transfer length or file offset is not an exact integer multiple of the disk block size (4096 bytes).

Engineers MUST NOT dispatch direct I/O write operations where transfer length or file offset is not an exact integer multiple of the disk block size (4096 bytes).

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

Code SHOULD verify buffer pointer alignment using bitwise operations (`(address & 0xFFF) === 0`) prior to invoking direct kernel write syscalls.

Code SHOULD verify buffer pointer alignment using bitwise operations (`(address & 0xFFF) === 0`) prior to invoking direct kernel write syscalls.

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 acquire and maintain exclusive buffer pins or ownership locks through the entire duration of asynchronous I/O completion callbacks.

Services MUST acquire and maintain exclusive buffer pins or ownership locks through the entire duration of asynchronous I/O completion callbacks.

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 mutate, recycle, or speculative-read from buffer memory while kernel asynchronous I/O operations are actively in flight.

Components NEVER mutate, recycle, or speculative-read from buffer memory while kernel asynchronous I/O operations are actively in flight.

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 Direct I/O 4KB Memory-Page Alignment & Buffer Concurrency Invariant critical for AI coding assistants?

Enforces strict 4096-byte memory page alignment, block-boundary file offsets, and strict buffer lifecycle pinning for PostgreSQL 17 direct I/O (io_direct) and asynchronous storage pipelines. Bypassing OS page cache via Direct I/O requires zero-copy kernel alignment and completion synchronization; violation causes immediate EINVAL panics, double-buffering latency regressions, and torn page corruptions under write saturation. 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 (Submitting Unaligned Buffers or Releasing Concurrency Locks Prior to Direct I/O Completion) 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