This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: postgres-17-incremental-backup-lsn-verification-standard

Postgres 17 Block-Level Incremental Backup LSN Verification and Continuity Guardrail

Enforces strict LSN (Log Sequence Number) continuity validation, backup manifest cryptographic verification, and timeline integrity when orchestrating Postgres 17 native block-level incremental backups. Prevents broken backup chains, silent data loss, and failed disaster recovery workflows by mathematically guaranteeing prior-manifest parentage before and after invoking pg_basebackup with incremental mode.

Prohibited Anti-Pattern: Executing Unverified Incremental Backups Against Invalid or Desynchronized Parent Manifests

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Postgres 17 Block-Level Incremental Backup LSN Verification and Continuity Guardrail - Enforces strict LSN (Log Sequence Number) continuity validation, backup manifest cryptographic verification, and timeline integrity when orchestrating Postgres 17 native block-level incremental backups. Prevents broken backup chains, silent data loss, and failed disaster recovery workflows by mathematically guaranteeing prior-manifest parentage before and after invoking pg_basebackup with incremental mode.
globs: src/backup/**/*.{ts,js,py,go}, src/infra/backup/**/*.{ts,js,py,go}, src/database/backup/**/*.{ts,js,py,go}, src/jobs/backup/**/*.{ts,js,py,go}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/postgres-17-incremental-backup-lsn-verification-standard"
---

# Postgres 17 Block-Level Incremental Backup LSN Verification and Continuity Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Executing Unverified Incremental Backups Against Invalid or Desynchronized Parent Manifests

## System Constraints
You are a Principal Infrastructure & Database Reliability Architect enforcing strict invariants for PostgreSQL 17 native block-level incremental backups (pg_basebackup --incremental). You mandate cryptographic validation of backup_manifest files, timeline consistency, strict parent-child LSN continuity, and non-destructive fallback to full synthetic or physical baselines upon any chain anomaly.

## Strict Guidelines
- Engineers MUST ALWAYS verify the integrity of the reference parent 'backup_manifest' using 'pg_verifybackup' before invoking 'pg_basebackup --incremental=backup_manifest'.
- Engineers MUST parse and assert that the parent manifest's 'End-LSN' exactly matches or precedes the starting redo LSN boundary, and that Timeline IDs match without unhandled forks.
- Engineers MUST NOT rely on local file modification timestamps or external database state to infer parent-child chain validity; only cryptographic hashes inside 'backup_manifest' are authoritative.
- Services MUST implement automatic fallback to a new full physical base backup when an LSN gap, corrupted manifest checksum, or timeline divergence is detected.
- Code MUST execute 'pg_verifybackup' on the newly completed incremental backup bundle against the entire chain before marking the backup run as successful in disaster recovery metadata stores.
- Services MUST NEVER delete, prune, or evict an ancestor base or incremental backup until all descendant incremental chains are either retired or superseded by an independent full baseline.

## Bad vs Good Implementations
### Postgres 17 Incremental Backup Chain Orchestration and Verification

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Fails to run pg_verifybackup on the parent manifest, assumes the parent manifest is valid, fails to check timeline continuity, ignores potential LSN discontinuities, and does not verify the generated incremental backup bundle post-execution, leading to unrecoverable silent corruption in the backup chain.
```typescript
import { exec } from 'node:child_process';
import { promisify } from 'node:util';
const execAsync = promisify(exec);

export async function runIncrementalBackup(targetDir: string, parentDir: string): Promise<void> {
  // Anti-pattern: Blindly passing the parent manifest without verifying checksums,
  // timeline IDs, or validating the resulting backup chain integrity.
  const cmd = `pg_basebackup -h localhost -p 5432 -D ${targetDir} -Fp -P --incremental=${parentDir}/backup_manifest`;
  await execAsync(cmd);
  console.log('Incremental backup completed successfully');
}
```

#### Production Standard (Correct)
> **Standard**: Performs pre-flight verification of the parent manifest with pg_verifybackup, parses WAL ranges to ensure structural validity, avoids shell injection using execFile, and cryptographically verifies the newly generated incremental backup before certifying the LSN milestone.
```typescript
import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
import { readFile } from 'node:fs/promises';
import path from 'node:path';

const execFileAsync = promisify(execFile);

interface BackupManifest {
  'PostgreSQL-Backup-Manifest-Version': number;
  'Files': Array<{ 'Path': string; 'Size': number; 'Last-Modified': string; 'Checksum-Algorithm': string; 'Checksum': string }>;
  'WAL-Ranges': Array<{ 'Timeline': number; 'Start-LSN': string; 'End-LSN': string }>;
  'Manifest-Checksum': string;
}

export async function runVerifiedIncrementalBackup(
  targetDir: string,
  parentManifestPath: string,
  host = 'localhost',
  port = 5432
): Promise<{ endLsn: string; timeline: number }> {
  // 1. Cryptographically verify the integrity of the parent manifest before proceeding
  const parentDir = path.dirname(parentManifestPath);
  try {
    await execFileAsync('pg_verifybackup', ['-m', parentManifestPath, parentDir]);
  } catch (err) {
    throw new Error(`Parent backup manifest verification failed: ${(err as Error).message}. Must trigger full baseline.`);
  }

  // 2. Validate internal parent manifest metadata structure
  const rawManifest = await readFile(parentManifestPath, 'utf-8');
  const manifestData: BackupManifest = JSON.parse(rawManifest);
  if (!manifestData['WAL-Ranges'] || manifestData['WAL-Ranges'].length === 0) {
    throw new Error('Parent manifest contains no valid WAL-Ranges; cannot anchor incremental backup.');
  }

  // 3. Execute block-level incremental backup with safe argument array
  await execFileAsync('pg_basebackup', [
    '-h', host,
    '-p', port.toString(),
    '-D', targetDir,
    '-Fp',
    '-P',
    `--incremental=${parentManifestPath}`
  ]);

  // 4. Verify the newly generated incremental bundle
  const newManifestPath = path.join(targetDir, 'backup_manifest');
  await execFileAsync('pg_verifybackup', ['-m', newManifestPath, targetDir]);

  // 5. Parse new manifest to retrieve verified LSN boundaries
  const newRaw = await readFile(newManifestPath, 'utf-8');
  const newManifest: BackupManifest = JSON.parse(newRaw);
  const activeWalRange = newManifest['WAL-Ranges'][newManifest['WAL-Ranges'].length - 1];

  return {
    endLsn: activeWalRange['End-LSN'],
    timeline: activeWalRange['Timeline']
  };
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Postgres 17 Block-Level Incremental Backup LSN Verification and Continuity Guardrail

Postgres 17 Block-Level Incremental Backup LSN Verification and Continuity Guardrail defines an industrial-grade behavioral standard for backend engineering. Enforces strict LSN (Log Sequence Number) continuity validation, backup manifest cryptographic verification, and timeline integrity when orchestrating Postgres 17 native block-level incremental backups. Prevents broken backup chains, silent data loss, and failed disaster recovery workflows by mathematically guaranteeing prior-manifest parentage before and after invoking pg_basebackup with incremental mode. 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: Executing Unverified Incremental Backups Against Invalid or Desynchronized Parent Manifests 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 DriftExecuting Unverified Incremental Backups Against Invalid or Desynchronized Parent ManifestsCritical
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 verify the integrity of the reference parent 'backup_manifest' using 'pg_verifybackup' before invoking 'pg_basebackup --incremental=backup_manifest'.

Engineers MUST ALWAYS verify the integrity of the reference parent 'backup_manifest' using 'pg_verifybackup' before invoking 'pg_basebackup --incremental=backup_manifest'.

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 parse and assert that the parent manifest's 'End-LSN' exactly matches or precedes the starting redo LSN boundary, and that Timeline IDs match without unhandled forks.

Engineers MUST parse and assert that the parent manifest's 'End-LSN' exactly matches or precedes the starting redo LSN boundary, and that Timeline IDs match without unhandled forks.

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 rely on local file modification timestamps or external database state to infer parent-child chain validity

Engineers MUST NOT rely on local file modification timestamps or external database state to infer parent-child chain validity; only cryptographic hashes inside 'backup_manifest' are authoritative.

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 automatic fallback to a new full physical base backup when an LSN gap, corrupted manifest checksum, or timeline divergence is detected.

Services MUST implement automatic fallback to a new full physical base backup when an LSN gap, corrupted manifest checksum, or timeline divergence 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.
5

Code MUST execute 'pg_verifybackup' on the newly completed incremental backup bundle against the entire chain before marking the backup run as successful in disaster recovery metadata stores.

Code MUST execute 'pg_verifybackup' on the newly completed incremental backup bundle against the entire chain before marking the backup run as successful in disaster recovery metadata stores.

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 delete, prune, or evict an ancestor base or incremental backup until all descendant incremental chains are either retired or superseded by an independent full baseline.

Services MUST NEVER delete, prune, or evict an ancestor base or incremental backup until all descendant incremental chains are either retired or superseded by an independent full baseline.

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 Block-Level Incremental Backup LSN Verification and Continuity Guardrail critical for AI coding assistants?

Enforces strict LSN (Log Sequence Number) continuity validation, backup manifest cryptographic verification, and timeline integrity when orchestrating Postgres 17 native block-level incremental backups. Prevents broken backup chains, silent data loss, and failed disaster recovery workflows by mathematically guaranteeing prior-manifest parentage before and after invoking pg_basebackup with incremental mode. 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 (Executing Unverified Incremental Backups Against Invalid or Desynchronized Parent Manifests) 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