Table Dropped or Truncated by Mistake?

Recover Deleted Tables Without Backups

Search retained disk pages for data from before DROP TABLE or TRUNCATE when no backup is available

Scenario Description

DROP TABLE or TRUNCATE was accidentally executed in production. After DROP, the table definition is no longer available through normal queries; after TRUNCATE, the table remains but its data is gone. Neither operation supports the same row-level WAL extraction as DELETE. Check available backups and surviving disk pages.

Typical scenarios include:

  • Accidental DROP TABLE deleted an important business table
  • TRUNCATE TABLE emptied a table containing critical data
  • Script errors caused table deletion
  • ORM framework automatically executed DROP TABLE
  • Database cleanup scripts accidentally removed production data

Challenges

DROP TABLE and TRUNCATE do not record individual row removals like DELETE, so row-level DELETE extraction from WAL does not apply. Without a usable backup, preserve remaining disk pages promptly and use the table structure to attempt recovery.

Why So Difficult

  • - DROP/TRUNCATE does not log row data
  • - Row-level DELETE extraction from WAL does not apply
  • - Table metadata may be inaccessible after DROP
  • - Disk space may be overwritten

Traditional Limitations

  • - Recovery normally requires a full backup
  • - PITR requires a usable base backup and continuous WAL
  • - Professional recovery is expensive
  • - Success rate not guaranteed

PDU Solution

Old data pages may remain on disk after DROP or TRUNCATE. Preserve a page image first, then use PDU to find those pages and extract readable records with confirmed table definitions. Overwritten data cannot be reconstructed unless another copy survives.

Recovery Principle

Scan raw disk
Identify data page fragments
Reconstruct data

PDU Core Capabilities

  • Fragment scanning finds table data that still exists on disk after DROP/TRUNCATE
  • Confirm table definitions from original DDL, backup schemas or other reliable evidence
  • Attempt DROP TABLE recovery from retained pages without a backup
  • Can recover partial data even if some pages have been overwritten
  • Identifies and recovers data from TOAST tables for large values
  • Supports saving disk page images for later recovery (isomode)
Quick Command Example
# Step 1: Configure DISK_PATH and required PGDATA_EXCLUDE
# pdu.ini
PGDATA=/home/pg/data/
DISK_PATH=/dev/mapper/data-lv
PGDATA_EXCLUDE=/home/pg/data,/home/pg12/data
# Step 2: Image saving is on by default; build the image index directly
PDU.public=# ds idx;
# Step 3: (Optional) Scan WAL for table structure
PDU.public=# b;
PDU.public=# use mydb;
mydb.public=# scan drop;
# Step 4: Scan the saved ISO image
PDU.public=# ds iso;
# Step 5: Repair TOAST data and generate COPY statements
restore.public=# ds repair;
restore.public=# ds copy;

Critical Factors for Success

Time is Critical

Recovery success depends on whether disk space has been overwritten. The sooner you attempt recovery, the higher the success rate.

Stop Database Writes

If possible, stop database writes immediately. Minimize the chance of data page overwrite by reducing database activity.

Table Structure Helps

Confirm the table structure used for parsing. Obtain it from original DDL, an older backup or other reliable evidence. A successful decode alone does not establish which table a page belongs to.

Required Step: ISO Mode

Image saving is enabled by default. Before ds idx, configure PGDATA_EXCLUDE with every PostgreSQL data directory on DISK_PATH, then run ds idx directly. This reduces unnecessary image and scan work and generates restore/.dsiso/idx and restore/.dsiso/pgiso for ds iso recovery.

# Image saving is on by default; run directly
PDU.public=# ds idx; # Saves page images
# Later, use ISO mode for scanning
PDU.public=# ds iso;

Prerequisites

  • DISK_PATH configuration required

    Use df -h command to find the disk path where PGDATA resides.

  • Complete PGDATA_EXCLUDE configuration required

    List every PostgreSQL data directory on the DISK_PATH disk. Separate absolute paths with commas and no spaces to reduce unnecessary image and scan work.

  • Table structure configuration

    Can be auto-retrieved via scan drop, or manually configured in restore/tab.config.

  • Disk access permission

    Requires root access or read permission to raw disk device.

PostgreSQL 场景案例