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

PostgreSQL 17 Streaming I/O Optimization and Synchronized Logical Replication Failover

PostgreSQL 17 introduces vectorized streaming I/O read-ahead alongside native logical replication slot synchronization across physical standbys. Production architectures must enforce failover-enabled logical slots and streaming I/O configurations to guarantee zero data loss and prevent subscriber desynchronization during primary failovers.

Prohibited Anti-Pattern: Creating unmanaged logical replication slots without failover synchronization and ignoring PG17 streaming I/O block controls

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 Streaming I/O Optimization and Synchronized Logical Replication Failover - PostgreSQL 17 introduces vectorized streaming I/O read-ahead alongside native logical replication slot synchronization across physical standbys. Production architectures must enforce failover-enabled logical slots and streaming I/O configurations to guarantee zero data loss and prevent subscriber desynchronization during primary failovers.
globs: src/db/**/*.ts, infra/**/*.sql, migrations/**/*.sql, src/services/replication/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-streaming-io-failover-slots"
---

# PostgreSQL 17 Streaming I/O Optimization and Synchronized Logical Replication Failover

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Creating unmanaged logical replication slots without failover synchronization and ignoring PG17 streaming I/O block controls

## System Constraints
You are a Principal Software Architect enforcing PostgreSQL 17 engine invariants. You must mandate that all logical replication slots utilize PG17 failover synchronization (failover = true) and that physical standby sync slots are strictly coordinated to prevent subscriber split-brain during failover. You must also ensure query patterns and database configurations exploit PG17 streaming I/O and block read-ahead mechanics.

## Strict Guidelines
- Engineers MUST ALWAYS create logical replication slots with `failover => true` using PostgreSQL 17 `pg_create_logical_replication_slot` in HA cluster configurations.
- Engineers MUST ALWAYS register standby replication slot names in the primary's `synchronized_standby_slots` GUC to prevent WAL recycling prior to physical standby replication.
- Engineers MUST NOT allow logical replication subscribers to consume changes faster than physical standbys can persist them when HA failover slots are required.
- Database orchestration scripts MUST verify `sync_state` in `pg_replication_slots` on standbys is `ready` before promoting physical standbys to primary status.
- Code executing vector bulk scans or large analytical sequential scans SHOULD tune `io_combine_limit` and leverage PG17 vectorized streaming I/O instead of chunked application-level polling.
- Services MUST NEVER drop or re-create synchronized failover slots manually without pausing subscriber connections and checking slot LSN convergence.

## Bad vs Good Implementations
### Configuring PostgreSQL 17 Logical Replication Slot with Failover Sync

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Creating a logical replication slot without `failover => true` in PostgreSQL 17 prevents the slot from syncing to physical replicas. If the primary node fails, the subscriber cannot resume from the promoted standby, resulting in catalog desynchronization or data loss.
```typescript
import { Client } from 'pg';

export async function setupLogicalSlot(client: Client, slotName: string): Promise<void> {
  // Anti-pattern: PG16-style slot creation without failover parameter
  // This slot will be lost when a physical standby promotes, corrupting subscriber state
  await client.query(
    `SELECT pg_create_logical_replication_slot($1, 'pgoutput', false, false);`,
    [slotName]
  );
}
```

#### Production Standard (Correct)
> **Standard**: Explicitly passes `failover => true` to `pg_create_logical_replication_slot`, allowing the primary to replicate the slot state to standbys listed in `synchronized_standby_slots`, ensuring zero subscriber data loss upon cluster failover.
```typescript
import { Client } from 'pg';

export async function setupLogicalSlot(client: Client, slotName: string): Promise<void> {
  // Invariant: In PostgreSQL 17, pass failover => true to enable physical standby slot syncing
  await client.query(
    `SELECT pg_create_logical_replication_slot(
       slot_name => $1,
       plugin => 'pgoutput',
       temporary => false,
       twophase => false,
       failover => true
     );`,
    [slotName]
  );

  // Verify standby slot sync configuration
  const res = await client.query(
    `SELECT slot_name, failover, synced FROM pg_replication_slots WHERE slot_name = $1;`,
    [slotName]
  );

  if (!res.rows[0]?.failover) {
    throw new Error(`Failed to initialize failover-enabled logical slot: ${slotName}`);
  }
}
```

### Validating Standby Failover Readiness Before Promotion

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Promoting a physical replica without checking that logical replication slots are synchronized will invalidate pending subscriber LSN positions, resulting in skipped or duplicate downstream messages.
```typescript
import { Client } from 'pg';

export async function promoteStandby(standbyClient: Client): Promise<void> {
  // Anti-pattern: Immediate promotion without verifying synchronized logical slots
  await standbyClient.query('SELECT pg_promote();');
}
```

#### Production Standard (Correct)
> **Standard**: Verifies that all required logical slots have `sync_state = 'ready'` and no catalog conflicts on the standby node prior to executing `pg_promote()`, preventing downstream subscriber divergence.
```typescript
import { Client } from 'pg';

export async function promoteStandby(standbyClient: Client, expectedSlots: string[]): Promise<void> {
  // Invariant: Standby logical slots must have sync_state = 'ready' before promotion
  const result = await standbyClient.query<{
    slot_name: string;
    sync_state: string;
    conflicting: boolean;
  }>(
    `SELECT slot_name, sync_state, conflicting 
     FROM pg_replication_slots 
     WHERE slot_name = ANY($1);`,
    [expectedSlots]
  );

  const unreadySlots = result.rows.filter(
    (row) => row.sync_state !== 'ready' || row.conflicting
  );

  if (unreadySlots.length > 0) {
    throw new Error(
      `Cannot promote standby: logical slots not synchronized: ${JSON.stringify(unreadySlots)}`
    );
  }

  await standbyClient.query('SELECT pg_promote();');
}
```

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 Streaming I/O Optimization and Synchronized Logical Replication Failover

PostgreSQL 17 Streaming I/O Optimization and Synchronized Logical Replication Failover defines an industrial-grade behavioral standard for backend engineering. PostgreSQL 17 introduces vectorized streaming I/O read-ahead alongside native logical replication slot synchronization across physical standbys. Production architectures must enforce failover-enabled logical slots and streaming I/O configurations to guarantee zero data loss and prevent subscriber desynchronization during primary failovers. 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: Creating unmanaged logical replication slots without failover synchronization and ignoring PG17 streaming I/O block controls 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 DriftCreating unmanaged logical replication slots without failover synchronization and ignoring PG17 streaming I/O block controlsCritical
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 create logical replication slots with `failover => true` using PostgreSQL 17 `pg_create_logical_replication_slot` in HA cluster configurations.

Engineers MUST ALWAYS create logical replication slots with `failover => true` using PostgreSQL 17 `pg_create_logical_replication_slot` in HA cluster configurations.

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 register standby replication slot names in the primary's `synchronized_standby_slots` GUC to prevent WAL recycling prior to physical standby replication.

Engineers MUST ALWAYS register standby replication slot names in the primary's `synchronized_standby_slots` GUC to prevent WAL recycling prior to physical standby replication.

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 NOT allow logical replication subscribers to consume changes faster than physical standbys can persist them when HA failover slots are required.

Engineers MUST NOT allow logical replication subscribers to consume changes faster than physical standbys can persist them when HA failover slots are required.

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

Database orchestration scripts MUST verify `sync_state` in `pg_replication_slots` on standbys is `ready` before promoting physical standbys to primary status.

Database orchestration scripts MUST verify `sync_state` in `pg_replication_slots` on standbys is `ready` before promoting physical standbys to primary status.

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

Code executing vector bulk scans or large analytical sequential scans SHOULD tune `io_combine_limit` and leverage PG17 vectorized streaming I/O instead of chunked application-level polling.

Code executing vector bulk scans or large analytical sequential scans SHOULD tune `io_combine_limit` and leverage PG17 vectorized streaming I/O instead of chunked application-level polling.

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

Services MUST NEVER drop or re-create synchronized failover slots manually without pausing subscriber connections and checking slot LSN convergence.

Services MUST NEVER drop or re-create synchronized failover slots manually without pausing subscriber connections and checking slot LSN convergence.

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 Streaming I/O Optimization and Synchronized Logical Replication Failover critical for AI coding assistants?

PostgreSQL 17 introduces vectorized streaming I/O read-ahead alongside native logical replication slot synchronization across physical standbys. Production architectures must enforce failover-enabled logical slots and streaming I/O configurations to guarantee zero data loss and prevent subscriber desynchronization during primary failovers. 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 (Creating unmanaged logical replication slots without failover synchronization and ignoring PG17 streaming I/O block controls) 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