This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: postgres17-combinebackup-lsn-fencing-standard

PostgreSQL 17 Block-Level Incremental Backup LSN Fencing and Manifest Validation Standard

Enforces strict monotonic Log Sequence Number (LSN) fencing, manifest checksum parsing, and timeline continuity before invoking PostgreSQL 17's pg_combinebackup utility. Synthesizing synthetic full backups across fractured WAL streams or mismatched backup manifests causes silent, irremediable block-level database corruption during disaster recovery.

Prohibited Anti-Pattern: Blind invocation of pg_combinebackup without pre-flight manifest LSN sequence and SHA-256 checksum boundary fencing

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: PostgreSQL 17 Block-Level Incremental Backup LSN Fencing and Manifest Validation Standard - Enforces strict monotonic Log Sequence Number (LSN) fencing, manifest checksum parsing, and timeline continuity before invoking PostgreSQL 17's pg_combinebackup utility. Synthesizing synthetic full backups across fractured WAL streams or mismatched backup manifests causes silent, irremediable block-level database corruption during disaster recovery.
globs: src/backup/**/*.{ts,js,py,go}, src/recovery/**/*.{ts,js,py,go}, src/infrastructure/**/*.{ts,js,py,go}, lib/storage/postgres/**/*.{ts,js,py,go}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres17-combinebackup-lsn-fencing-standard"
---

# PostgreSQL 17 Block-Level Incremental Backup LSN Fencing and Manifest Validation Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Blind invocation of pg_combinebackup without pre-flight manifest LSN sequence and SHA-256 checksum boundary fencing

## System Constraints
You are a Principal Infrastructure & Database Reliability Engineer enforcing the PostgreSQL 17 Block-Level Incremental Backup (pg_combinebackup) LSN Fencing Standard. Mandate that all incremental backup chains parse `backup_manifest`, verify SHA-256 file checksums, validate timeline ID parity, and rigorously verify that incremental `start-lsn` exactly matches the prior backup's `end-lsn` before invoking `pg_combinebackup`. Reject any direct shell execution or automated orchestration that combines backups without programmatic, pre-flight assertion of chain continuity.

## Strict Guidelines
- Engineers MUST ALWAYS parse each `backup_manifest` using strict schema validation and verify file checksums via `pg_verifybackup` or programmatic SHA-256 validation before running `pg_combinebackup`.
- Engineers MUST enforce strict LSN fencing: Backup `N`'s `start-lsn` MUST be less than or equal to Backup `N-1`'s `end-lsn` (and ideally identically contiguous), and Backup `N` MUST reference Backup `N-1` as its parent.
- Engineers MUST verify timeline ID continuity across the entire chain; incremental chains MUST NOT span diverging timeline boundaries without explicit recovery target timeline reconciliation.
- Services MUST NEVER invoke `pg_combinebackup` with wildcards, unverified directory paths, or unvalidated arrays of directory targets.
- Automation pipelines MUST execute `pg_combinebackup` with dry-run verification mode flags or sandbox synthesis before writing to production restore targets.
- Services MUST immediately abort orchestration and quarantine candidate directories if an LSN sequence gap, checksum mismatch, or orphaned incremental node is detected.

## Bad vs Good Implementations
### PostgreSQL 17 Incremental Chain LSN and Manifest Pre-flight Verification

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Directly running pg_combinebackup without parsing backup_manifest files fails to detect missing intermediate incrementals, timeline divergences, or corrupted manifests, leading to silent block-level corruption during synthetic full reconstruction.
```typescript
import { execSync } from "child_process";

// ANTI-PATTERN: Blindly concatenating backup directories and invoking pg_combinebackup
// without parsing backup_manifest files, checking LSN continuity, or verifying checksums.
export async function restoreIncrementalChain(backupDirs: string[], targetDir: string): Promise<void> {
  // Blindly assumes dirs are sorted, compatible, and uncorrupted
  const args = backupDirs.join(" ");
  execSync(`pg_combinebackup ${args} -o ${targetDir}`);
}
```

#### Production Standard (Correct)
> **Standard**: Validates system identity, timeline parity, and strict monotonic LSN continuity across manifests, while invoking pg_verifybackup to ensure bit-level integrity before executing pg_combinebackup.
```typescript
import { execFile } from "child_process";
import { promisify } from "util";
import * as fs from "fs/promises";
import * as path from "path";

const execFileAsync = promisify(execFile);

interface BackupManifest {
  "PostgreSQL-Backup-Manifest-Version": number;
  Files: Record<string, { Size: number; Checksum?: string }>;
  "System-Identifier": string;
  "Start-LSN": string;
  "End-LSN": string;
  "Timeline": number;
}

function parseLsn(lsn: string): bigint {
  const [hi, lo] = lsn.split("/");
  if (!hi || !lo) throw new Error(`Invalid LSN format: ${lsn}`);
  return (BigInt(`0x${hi}`) << 32n) | BigInt(`0x${lo}`);
}

export async function combineBackupsSafely(
  backupChainDirs: string[],
  outputDir: string
): Promise<void> {
  if (backupChainDirs.length < 2) {
    throw new Error("pg_combinebackup requires at least a base backup and one incremental backup.");
  }

  let previousManifest: BackupManifest | null = null;

  for (let i = 0; i < backupChainDirs.length; i++) {
    const dir = backupChainDirs[i];
    const manifestPath = path.join(dir, "backup_manifest");

    const manifestRaw = await fs.readFile(manifestPath, "utf-8");
    const manifest: BackupManifest = JSON.parse(manifestRaw);

    if (manifest["PostgreSQL-Backup-Manifest-Version"] !== 1) {
      throw new Error(`Unsupported manifest version in ${dir}`);
    }

    // Verify integrity of the backup manifest before synthesis
    await execFileAsync("pg_verifybackup", ["-m", manifestPath, dir]);

    if (previousManifest) {
      // Invariant: System Identifiers must match exactly
      if (manifest["System-Identifier"] !== previousManifest["System-Identifier"]) {
        throw new Error(`System Identifier mismatch between backup stages at ${dir}`);
      }

      // Invariant: Timeline continuity check
      if (manifest.Timeline !== previousManifest.Timeline) {
        throw new Error(`Timeline divergence detected at ${dir}: ${manifest.Timeline} vs ${previousManifest.Timeline}`);
      }

      // Invariant: LSN Fencing - Start LSN of child must fall within bounds of parent End LSN
      const prevEndLsn = parseLsn(previousManifest["End-LSN"]);
      const currStartLsn = parseLsn(manifest["Start-LSN"]);

      if (currStartLsn > prevEndLsn) {
        throw new Error(
          `LSN Gap detected between backups! Gap: ${previousManifest["End-LSN"]} -> ${manifest["Start-LSN"]}`
        );
      }
    }

    previousManifest = manifest;
  }

  // Execute synthesis only after cryptographic and LSN invariants pass
  await execFileAsync("pg_combinebackup", [...backupChainDirs, "-o", outputDir]);
}
```

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 Block-Level Incremental Backup LSN Fencing and Manifest Validation Standard

PostgreSQL 17 Block-Level Incremental Backup LSN Fencing and Manifest Validation Standard defines an industrial-grade behavioral standard for backend engineering. Enforces strict monotonic Log Sequence Number (LSN) fencing, manifest checksum parsing, and timeline continuity before invoking PostgreSQL 17's pg_combinebackup utility. Synthesizing synthetic full backups across fractured WAL streams or mismatched backup manifests causes silent, irremediable block-level database corruption during disaster recovery. 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: Blind invocation of pg_combinebackup without pre-flight manifest LSN sequence and SHA-256 checksum boundary fencing 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 DriftBlind invocation of pg_combinebackup without pre-flight manifest LSN sequence and SHA-256 checksum boundary fencingCritical
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 parse each `backup_manifest` using strict schema validation and verify file checksums via `pg_verifybackup` or programmatic SHA-256 validation before running `pg_combinebackup`.

Engineers MUST ALWAYS parse each `backup_manifest` using strict schema validation and verify file checksums via `pg_verifybackup` or programmatic SHA-256 validation before running `pg_combinebackup`.

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 enforce strict LSN fencing

Engineers MUST enforce strict LSN fencing: Backup `N`'s `start-lsn` MUST be less than or equal to Backup `N-1`'s `end-lsn` (and ideally identically contiguous), and Backup `N` MUST reference Backup `N-1` as its parent.

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 verify timeline ID continuity across the entire chain

Engineers MUST verify timeline ID continuity across the entire chain; incremental chains MUST NOT span diverging timeline boundaries without explicit recovery target timeline reconciliation.

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 NEVER invoke `pg_combinebackup` with wildcards, unverified directory paths, or unvalidated arrays of directory targets.

Services MUST NEVER invoke `pg_combinebackup` with wildcards, unverified directory paths, or unvalidated arrays of directory targets.

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

Automation pipelines MUST execute `pg_combinebackup` with dry-run verification mode flags or sandbox synthesis before writing to production restore targets.

Automation pipelines MUST execute `pg_combinebackup` with dry-run verification mode flags or sandbox synthesis before writing to production restore targets.

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 immediately abort orchestration and quarantine candidate directories if an LSN sequence gap, checksum mismatch, or orphaned incremental node is detected.

Services MUST immediately abort orchestration and quarantine candidate directories if an LSN sequence gap, checksum mismatch, or orphaned incremental node is detected.

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 Block-Level Incremental Backup LSN Fencing and Manifest Validation Standard critical for AI coding assistants?

Enforces strict monotonic Log Sequence Number (LSN) fencing, manifest checksum parsing, and timeline continuity before invoking PostgreSQL 17's pg_combinebackup utility. Synthesizing synthetic full backups across fractured WAL streams or mismatched backup manifests causes silent, irremediable block-level database corruption during disaster recovery. 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 (Blind invocation of pg_combinebackup without pre-flight manifest LSN sequence and SHA-256 checksum boundary fencing) 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