This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: pg17-logical-replication-failover-slot-sync-state-machine

Postgres 17 Logical Replication Failover Slot Synchronization State Machine

Guarantees zero data loss across cross-region failover events by constraining physical standby promotion until all logical replication slots reconcile with the physical timeline. Enforces PostgreSQL 17 failover slot synchronization primitives (sync_replication_slots, pg_sync_replication_slots) via a deterministic, finite-state promotion supervisor that asserts slot LSN convergence and prevents split-brain subscriber consumption.

Prohibited Anti-Pattern: Premature Standby Promotion without Logical Slot LSN Reconciliation

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 Logical Replication Failover Slot Synchronization State Machine - Guarantees zero data loss across cross-region failover events by constraining physical standby promotion until all logical replication slots reconcile with the physical timeline. Enforces PostgreSQL 17 failover slot synchronization primitives (sync_replication_slots, pg_sync_replication_slots) via a deterministic, finite-state promotion supervisor that asserts slot LSN convergence and prevents split-brain subscriber consumption.
globs: src/infrastructure/postgres/**/*.ts, src/database/failover/**/*.ts, src/services/replication/**/*.ts, infra/scripts/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/pg17-logical-replication-failover-slot-sync-state-machine"
---

# Postgres 17 Logical Replication Failover Slot Synchronization State Machine

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Premature Standby Promotion without Logical Slot LSN Reconciliation

## System Constraints
You are a Principal Database Reliability Engineer enforcing PostgreSQL 17 failover slot synchronization and zero-data-loss invariants. Standby instances MUST NEVER be promoted to primary until all logical replication slots marked for failover synchronization have their confirmed_flush_lsn aligned with or exceeding the standby's replay LSN, and physical replication has flushed all outstanding WAL. Reject any script or service logic that invokes pg_promote() without an explicit pre-promotion state-machine check verifying pg_replication_slots.sync_state is synchronized and synced_lsn matches pg_last_wal_replay_lsn().

## Strict Guidelines
- Engineers MUST ALWAYS configure synchronized_standby_slots on the primary and sync_replication_slots = on on physical standbys running Postgres 17.
- Engineers MUST NOT execute pg_promote() or trigger standby promotion until pg_sync_replication_slots() executes cleanly and confirms zero pending slot updates.
- Code SHOULD verify that confirmed_flush_lsn >= pg_last_wal_replay_lsn() for all failover slots before releasing subscriber traffic or issuing the promotion command.
- Services MUST implement a finite-state machine (PRE_FLIGHT -> DRAIN_PRIMARY -> SYNC_SLOTS -> VERIFY_CONVERGENCE -> PROMOTE -> POST_PROMOTE) with strict timeout boundaries and automated abort-and-fence procedures.
- Components NEVER issue consumer cutover commands to downstream logical subscribers while logical slots are still in a SYNCING or UNRECONCILED state.

## Bad vs Good Implementations
### Naive Unfenced Standby Promotion

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Directly calling pg_promote() without checking pg_replication_slots or executing pg_sync_replication_slots() introduces data loss or replay corruption on logical subscribers because the standby replay may have advanced past unsynced logical slots, or un-reconciled slots may be invalidated.
```typescript
import { Client } from 'pg';

export async function promoteStandby(standbyClient: Client): Promise<void> {
  // Anti-Pattern: Unconditional immediate promotion without checking Postgres 17 slot sync
  console.warn('Executing fast promotion to primary...');
  await standbyClient.query('SELECT pg_promote(wait => true, wait_seconds => 60);');
  // Subscribers reconnect immediately, but logical slots lagged behind WAL replay, causing data loss or duplicate reads
  console.log('Standby promoted successfully.');
}
```

#### Production Standard (Correct)
> **Standard**: Executes pg_sync_replication_slots() and verifies that every slot with failover=true has synced=true and confirmed_flush_lsn matching or exceeding pg_last_wal_replay_lsn(), preventing subscriber data skipping or split-brain consumption after standby promotion.
```typescript
import { Client } from 'pg';

interface SlotConvergenceStatus {
  slotName: string;
  synced: boolean;
  confirmedFlushLsn: string;
  replayLsn: string;
}

export enum PromotionState {
  VERIFY_REPLICATION_HEALTH = 'VERIFY_REPLICATION_HEALTH',
  SYNC_FAILOVER_SLOTS = 'SYNC_FAILOVER_SLOTS',
  ASSERT_LSN_CONVERGENCE = 'ASSERT_LSN_CONVERGENCE',
  PROMOTE = 'PROMOTE',
  POST_PROMOTION_VALIDATION = 'POST_PROMOTION_VALIDATION'
}

export async function promoteStandbySafely(
  standbyClient: Client,
  timeoutMs: number = 30000
): Promise<void> {
  const startTime = Date.now();

  // Step 1: Force manual sync of logical replication slots on PG 17 standby
  await standbyClient.query('SELECT pg_sync_replication_slots();');

  // Step 2: Poll and assert slot convergence against local replay LSN
  while (Date.now() - startTime < timeoutMs) {
    const res = await standbyClient.query<{
      slot_name: string;
      synced: boolean;
      confirmed_flush_lsn: string;
      last_replay_lsn: string;
      is_converged: boolean;
    }>(`
      SELECT 
        slot_name, 
        synced, 
        confirmed_flush_lsn,
        pg_last_wal_replay_lsn() AS last_replay_lsn,
        (confirmed_flush_lsn >= pg_last_wal_replay_lsn()) AS is_converged
      FROM pg_replication_slots
      WHERE failover = true AND active = false;
    `);

    const unconverged = res.rows.filter(r => !r.synced || !r.is_converged);
    if (unconverged.length === 0 && res.rows.length > 0) {
      // Invariant satisfied: All failover slots reconciled
      break;
    }

    await new Promise(r => setTimeout(r, 500));
  }

  // Step 3: Atomic Promotion with wait
  const promoteResult = await standbyClient.query<{ pg_promote: boolean }>(
    'SELECT pg_promote(wait => true, wait_seconds => 30);'
  );

  if (!promoteResult.rows[0]?.pg_promote) {
    throw new Error('Promotion failed or timed out; standby remains read-only.');
  }
}
```

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 Logical Replication Failover Slot Synchronization State Machine

Postgres 17 Logical Replication Failover Slot Synchronization State Machine defines an industrial-grade behavioral standard for backend engineering. Guarantees zero data loss across cross-region failover events by constraining physical standby promotion until all logical replication slots reconcile with the physical timeline. Enforces PostgreSQL 17 failover slot synchronization primitives (sync_replication_slots, pg_sync_replication_slots) via a deterministic, finite-state promotion supervisor that asserts slot LSN convergence and prevents split-brain subscriber 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: Premature Standby Promotion without Logical Slot LSN Reconciliation 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 DriftPremature Standby Promotion without Logical Slot LSN ReconciliationCritical
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 synchronized_standby_slots on the primary and sync_replication_slots = on on physical standbys running Postgres 17.

Engineers MUST ALWAYS configure synchronized_standby_slots on the primary and sync_replication_slots = on on physical standbys running Postgres 17.

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 execute pg_promote() or trigger standby promotion until pg_sync_replication_slots() executes cleanly and confirms zero pending slot updates.

Engineers MUST NOT execute pg_promote() or trigger standby promotion until pg_sync_replication_slots() executes cleanly and confirms zero pending slot updates.

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 that confirmed_flush_lsn >= pg_last_wal_replay_lsn() for all failover slots before releasing subscriber traffic or issuing the promotion command.

Code SHOULD verify that confirmed_flush_lsn >= pg_last_wal_replay_lsn() for all failover slots before releasing subscriber traffic or issuing the promotion command.

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 implement a finite-state machine (PRE_FLIGHT -> DRAIN_PRIMARY -> SYNC_SLOTS -> VERIFY_CONVERGENCE -> PROMOTE -> POST_PROMOTE) with strict timeout boundaries and automated abort-and-fence procedures.

Services MUST implement a finite-state machine (PRE_FLIGHT -> DRAIN_PRIMARY -> SYNC_SLOTS -> VERIFY_CONVERGENCE -> PROMOTE -> POST_PROMOTE) with strict timeout boundaries and automated abort-and-fence procedures.

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 issue consumer cutover commands to downstream logical subscribers while logical slots are still in a SYNCING or UNRECONCILED state.

Components NEVER issue consumer cutover commands to downstream logical subscribers while logical slots are still in a SYNCING or UNRECONCILED state.

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 Logical Replication Failover Slot Synchronization State Machine critical for AI coding assistants?

Guarantees zero data loss across cross-region failover events by constraining physical standby promotion until all logical replication slots reconcile with the physical timeline. Enforces PostgreSQL 17 failover slot synchronization primitives (sync_replication_slots, pg_sync_replication_slots) via a deterministic, finite-state promotion supervisor that asserts slot LSN convergence and prevents split-brain subscriber 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 (Premature Standby Promotion without Logical Slot LSN Reconciliation) 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