This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: pg17-bounded-cdc-replication-slots-guardrail

PostgreSQL 17 Streaming I/O and Bounded CDC Replication Slot Architecture

Enforces deterministic boundaries on logical decoding replication slots and leverages PostgreSQL 17 streaming I/O capabilities to eliminate unconstrained Write-Ahead Log (WAL) disk exhaustion. Prevents transaction catalog bloat and replica stalls by enforcing hard WAL retention ceilings, proactive slot invalidation handlers, and non-blocking asynchronous WAL consumption.

Prohibited Anti-Pattern: Unbounded Logical Replication Slots Causing WAL Disk Saturation and Engine Deadlock

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 Streaming I/O and Bounded CDC Replication Slot Architecture - Enforces deterministic boundaries on logical decoding replication slots and leverages PostgreSQL 17 streaming I/O capabilities to eliminate unconstrained Write-Ahead Log (WAL) disk exhaustion. Prevents transaction catalog bloat and replica stalls by enforcing hard WAL retention ceilings, proactive slot invalidation handlers, and non-blocking asynchronous WAL consumption.
globs: src/database/**/*.{ts,js,py,go}, src/cdc/**/*.{ts,js,py,go}, migrations/**/*.sql, infra/postgres/**/*.conf
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pg17-bounded-cdc-replication-slots-guardrail"
---

# PostgreSQL 17 Streaming I/O and Bounded CDC Replication Slot Architecture

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Logical Replication Slots Causing WAL Disk Saturation and Engine Deadlock

## System Constraints
You are a Principal Database Architect and Infrastructure Engineer enforcing PostgreSQL 17 invariants for logical decoding, Change Data Capture (CDC), and Streaming I/O. Mandate strict boundaries on max_slot_wal_keep_size, deterministic slot invalidation detection, safe LSN acknowledgement rhythms, and proper streaming I/O tuning to prevent WAL accumulation from halting production clusters.

## Strict Guidelines
- Engineers MUST ALWAYS configure an explicit 'max_slot_wal_keep_size' on logical replication slots or globally in postgresql.conf to protect disk volumes from runaway retention.
- Engineers MUST NOT create persistent logical replication slots without companion telemetry monitoring 'pg_replication_slots.wal_status' and active 'pg_wal' filesystem utilization.
- CDC consumer services MUST handle slot invalidation gracefully (when 'wal_status' transitions to 'lost') by initiating an explicit snapshot re-sync rather than throwing infinite reconnection loops.
- Engineers MUST leverage PostgreSQL 17 logical replication failover flags ('failover = true') when configuring slots intended to survive physical standby promotions.
- Services MUST acknowledge flushed LSNs in bounded batches rather than per-event or deferring until pipeline termination to avoid pinning old catalog xmins and triggering catalog bloat.
- Engineers MUST tune PostgreSQL 17 streaming I/O parameters (including 'io_combine_limit' and asynchronous prefetching) to prevent I/O bottlenecks during concurrent high-throughput CDC streaming and analytical queries.

## Bad vs Good Implementations
### Configuring Resilient CDC Logical Slot with Bounded Retention and Failover Sync

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Creating a logical slot without max_slot_wal_keep_size or failover properties pins WAL segments indefinitely if the downstream reader stalls or terminates. This guarantees 100% disk exhaustion in high-write environments.
```typescript
import { Client } from 'pg';

// ANTI-PATTERN: Unbounded slot creation with no retention limit, pinning WAL indefinitely
export async function initCdcPipeline(client: Client, slotName: string) {
  // Creates an unconstrained logical slot that can crash the DB if the consumer goes down
  await client.query(`SELECT pg_create_logical_replication_slot($1, 'pgoutput');`, [slotName]);
  
  // Processing events without committing flushed LSN back to Postgres in a timely manner
  const stream = await client.query(`START_REPLICATION SLOT ${slotName} LOGICAL 0/0;`);
  return stream;
}
```

#### Production Standard (Correct)
> **Standard**: Ensures the replication slot cannot exceed disk boundaries, verifies the WAL health status, uses PostgreSQL 17 standby synchronization options, and handles invalidated slots gracefully before a catastrophic node failure occurs.
```typescript
import { Client } from 'pg';

// PRODUCTION INVARIANT: Bounded slot retention, PG17 failover readiness, and robust status checking
export async function initBoundedCdcSlot(client: Client, slotName: string, maxWalKeepMb: number = 10240): Promise<void> {
  // 1. Verify and enforce bounded slot WAL retention globally or per session
  await client.query(`SET max_slot_wal_keep_size = '${maxWalKeepMb}MB';`);
  
  // 2. PostgreSQL 17: Create slot with failover enabled for multi-node standby continuity
  await client.query(
    `SELECT pg_create_logical_replication_slot(
       $1,
       'pgoutput',
       false,
       false,
       true /* pg17 failover flag: syncs slot to standbys */
     )
     WHERE NOT EXISTS (
       SELECT 1 FROM pg_replication_slots WHERE slot_name = $1
     );`,
    [slotName]
  );
}

export async function assertSlotHealth(client: Client, slotName: string): Promise<boolean> {
  const res = await client.query(
    `SELECT wal_status, active, pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn) AS lag_bytes
     FROM pg_replication_slots WHERE slot_name = $1;`,
    [slotName]
  );
  if (res.rows.length === 0) throw new Error(`Slot ${slotName} does not exist`);
  if (res.rows[0].wal_status === 'lost') {
    throw new Error(`Slot ${slotName} exceeded max_slot_wal_keep_size. Re-snapshot required.`);
  }
  return res.rows[0].active;
}
```

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 Streaming I/O and Bounded CDC Replication Slot Architecture

PostgreSQL 17 Streaming I/O and Bounded CDC Replication Slot Architecture defines an industrial-grade behavioral standard for backend engineering. Enforces deterministic boundaries on logical decoding replication slots and leverages PostgreSQL 17 streaming I/O capabilities to eliminate unconstrained Write-Ahead Log (WAL) disk exhaustion. Prevents transaction catalog bloat and replica stalls by enforcing hard WAL retention ceilings, proactive slot invalidation handlers, and non-blocking asynchronous WAL consumption. 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: Unbounded Logical Replication Slots Causing WAL Disk Saturation and Engine Deadlock 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 DriftUnbounded Logical Replication Slots Causing WAL Disk Saturation and Engine DeadlockCritical
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 configure an explicit 'max_slot_wal_keep_size' on logical replication slots or globally in postgresql.conf to protect disk volumes from runaway retention.

Engineers MUST ALWAYS configure an explicit 'max_slot_wal_keep_size' on logical replication slots or globally in postgresql.conf to protect disk volumes from runaway retention.

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 create persistent logical replication slots without companion telemetry monitoring 'pg_replication_slots.wal_status' and active 'pg_wal' filesystem utilization.

Engineers MUST NOT create persistent logical replication slots without companion telemetry monitoring 'pg_replication_slots.wal_status' and active 'pg_wal' filesystem utilization.

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

CDC consumer services MUST handle slot invalidation gracefully (when 'wal_status' transitions to 'lost') by initiating an explicit snapshot re-sync rather than throwing infinite reconnection loops.

CDC consumer services MUST handle slot invalidation gracefully (when 'wal_status' transitions to 'lost') by initiating an explicit snapshot re-sync rather than throwing infinite reconnection loops.

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 leverage PostgreSQL 17 logical replication failover flags ('failover = true') when configuring slots intended to survive physical standby promotions.

Engineers MUST leverage PostgreSQL 17 logical replication failover flags ('failover = true') when configuring slots intended to survive physical standby promotions.

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

Services MUST acknowledge flushed LSNs in bounded batches rather than per-event or deferring until pipeline termination to avoid pinning old catalog xmins and triggering catalog bloat.

Services MUST acknowledge flushed LSNs in bounded batches rather than per-event or deferring until pipeline termination to avoid pinning old catalog xmins and triggering catalog bloat.

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 tune PostgreSQL 17 streaming I/O parameters (including 'io_combine_limit' and asynchronous prefetching) to prevent I/O bottlenecks during concurrent high-throughput CDC streaming and analytical queries.

Engineers MUST tune PostgreSQL 17 streaming I/O parameters (including 'io_combine_limit' and asynchronous prefetching) to prevent I/O bottlenecks during concurrent high-throughput CDC streaming and analytical queries.

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 and Bounded CDC Replication Slot Architecture critical for AI coding assistants?

Enforces deterministic boundaries on logical decoding replication slots and leverages PostgreSQL 17 streaming I/O capabilities to eliminate unconstrained Write-Ahead Log (WAL) disk exhaustion. Prevents transaction catalog bloat and replica stalls by enforcing hard WAL retention ceilings, proactive slot invalidation handlers, and non-blocking asynchronous WAL consumption. 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 (Unbounded Logical Replication Slots Causing WAL Disk Saturation and Engine Deadlock) 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