Postgres logical slot failover triage

From mouadja02/skills · View interactive version

Postgres logical slot failover triage is an AI agent skill from the GitHub repository mouadja02/skills. Use when PostgreSQL logical replication slots retain WAL, become orphaned or invalid after switchover, or must be proven synchronized and usable before failover.

How to use this skill

Skill file

PostgreSQL Logical Slot Failover Triage

Diagnose logical-slot ownership, WAL retention, and failover readiness without turning an uncertain slot into data loss. Start read-only. A slot that looks inactive may still belong to a stopped consumer; a synchronized standby slot may exist but still be temporary or invalidated.

When to Use

When Not to Use

Prerequisites

Quick Reference

Run on each relevant PostgreSQL 17+ node and preserve timestamps:

SELECT slot_name, slot_type, database, active, active_pid,
       restart_lsn, confirmed_flush_lsn, wal_status, safe_wal_size,
       failover, synced, temporary, inactive_since,
       conflicting, invalidation_reason
FROM pg_replication_slots
ORDER BY slot_name;

Measure retained WAL on a writable primary or demoted primary:

SELECT slot_name,
       pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)::numeric AS retained_wal_bytes
FROM pg_replication_slots
WHERE slot_type = 'logical' AND restart_lsn IS NOT NULL;

Do not infer deletion safety from active = false. Correlate slot name, database, plugin, publication/subscription, consumer deployment, and the consumer's durable offset.

For a redacted offline classification:

python3 scripts/analyze_slots.py --input observations/slot-snapshot.json

The helper never connects to PostgreSQL and always reports mutation_permitted: false.

Procedure

1. Freeze topology and evidence

Record UTC timestamp, node identity, server role, PostgreSQL version, timeline, cluster manager, recent promotion/demotion, disk free bytes, WAL generation rate, and every logical consumer. Capture the query above on all relevant nodes before changing traffic or slots.

Completion: each node and slot has a unique identity, role, timestamp, and expected consumer owner.

2. Quantify retention and deadline

Measure retained_wal_bytes, filesystem free bytes, and WAL growth at least three times. safe_wal_size estimates bytes that can be written before a slot risks becoming lost when retention is bounded; NULL is not automatically safe because it can also occur for an already-lost slot. Interpret it with wal_status and invalidation_reason.

Classify:

Completion: record a measured disk-exhaustion deadline and a separate slot-loss boundary; do not substitute one for the other.

3. Reconcile ownership before mutation

For every inactive slot, identify the subscriber, connector, job, or application that created it. Compare configured slot names and durable consumer offsets. Check whether the consumer is intentionally stopped, restarting, migrated, or pointed at the new primary.

An inactive, unowned, WAL-retaining slot is only an orphan candidate. It is not deletion authorization.

Completion: ownership is confirmed, explicitly denied by all plausible owners, or marked unresolved with escalation contacts.

4. Prove standby readiness before failover

On the subscriber, inventory the primary slot and table-synchronization slots required by that subscriber. On the standby, require every required slot to exist and satisfy:

synced = true AND temporary = false AND invalidation_reason IS NULL

Also verify the standby is ahead of the subscriber as required by the official failover procedure. Slot synchronization is asynchronous; existence alone is insufficient. A synchronized standby slot cannot be used for logical decoding or manually dropped while it remains a synced standby slot.

Completion: the complete required-slot set passes the readiness predicate and position check on the intended promotion target.

5. Classify the incident

| Evidence | Classification | Safe next step |

| --- | --- | --- |

| Demoted primary, inactive, owner unknown, retained WAL grows | Orphan candidate | Identify owner; protect disk; require approval before mutation |

| Standby slot missing, unsynced, temporary, or invalidated | Failover not ready | Stop planned promotion for that consumer; repair synchronization |

| wal_status=lost or invalidation reason set | Unusable slot | Use the consumer's approved re-snapshot/recovery path |

| Owner confirmed but consumer stopped | Inactive owned | Restore/reconcile consumer before considering slot action |

| Healthy primary slot lacks failover enablement | Not failover enabled | Plan and test failover-slot configuration before switchover |

Completion: every logical slot has one classification, supporting fields, and a named decision owner.

6. Stage recovery without destructive defaults

  1. Protect disk headroom by throttling nonessential writers or expanding storage through the platform's supported procedure.
  2. Restore a known consumer when its durable offset and required WAL remain valid.
  3. If a slot is unusable, follow that consumer's explicit re-snapshot/rebootstrap procedure.
  4. For a suspected orphan, obtain written owner confirmation, backup/recovery evidence, impact analysis, and human approval before any drop.
  5. Rehearse promotion and consumer continuation in a disposable environment before production failover.

Never automatically drop or advance a slot, alter a subscription, promote a standby, or copy slot state by filesystem manipulation.

Completion: recovery has a bounded window, rollback, owner, and evidence that consumer data continuity is preserved.

7. Verify after recovery or failover

Re-capture all slot rows and positions. Confirm the consumer resumes from the intended point, retained WAL decreases or stabilizes, no slot becomes invalidated, disk headroom recovers, and old-primary slots are reconciled by the cluster manager's supported lifecycle.

Completion: representative changes arrive exactly once at the downstream system, WAL remains within the documented reserve under normal traffic, and a repeated failover drill passes.

Offline Snapshot Schema

{
  "schema_version": 1,
  "server_role": "demoted_primary",
  "warning_threshold_bytes": 1073741824,
  "slots": [{
    "slot_name": "orders_cdc",
    "slot_type": "logical",
    "active": false,
    "failover": true,
    "synced": false,
    "temporary": false,
    "invalidation_reason": null,
    "wal_status": "extended",
    "safe_wal_size": 2147483648,
    "retained_wal_bytes": 42949672960,
    "consumer_owner_confirmed": false
  }]
}

Use only redacted metadata. Do not include connection strings, credentials, captured row data, or consumer secrets.

Unsafe Operations and Failure Recovery

Objective Verification

Pass only when all are true:

Pitfalls

Evaluation Prompts

See CODE0 for normal, difficult-edge, and should-not-activate prompts with deterministic assertions.

Sources and Provenance

Sourced facts about failover-slot synchronization, readiness, and pg_replication_slots fields come from PostgreSQL 18 documentation, accessed 2026-07-29. Factual demand evidence comes from CloudNativePG and Debezium issue reports. The classification table, evidence sequence, thresholds, snapshot schema, and helper are original operational recommendations, not PostgreSQL guarantees.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.