Logical backup

On this page 7

A logical backup is a database backup that captures the logical content of a database, meaning its schema definitions and rows, as SQL statements or a portable archive format, rather than copying the underlying data files. Tools such as pg_dump for PostgreSQL and mysqldump for MySQL and MariaDB are the best-known examples, and the result is often called a database dump.

In backup & recovery planning, logical backups are valued for portability and selectivity: a dump can be restored on a different server version, operating system or even another database engine with some translation, and individual tables or schemas can be recovered on their own. Their weaknesses are speed on large databases and the lack of built-in point-in-time recovery.

How a logical backup is produced and restored

A logical backup tool connects to the database as a client, reads the catalog to discover tables, indexes, views, functions and permissions, and then queries every table to write out its rows. The output either contains CREATE statements followed by INSERT or COPY commands, or uses an engine-specific archive format that stores the same information more compactly.

Consistency comes from reading all tables inside one snapshot. In PostgreSQL, pg_dump uses a repeatable-read transaction, and in MySQL with InnoDB the --single-transaction option does the same, so the backup reflects one moment even while applications keep writing. Engines without transactional snapshots need table locks during the dump instead.

Restoring means replaying the dump: recreating objects, loading data and rebuilding indexes and constraints. Because indexes are rebuilt from scratch, restore time is often several times longer than dump time on large databases.

The table below lists common output formats and their properties.

FormatExample tool or optionSelective restoreParallel restore
Plain SQL scriptpg_dump plain, mysqldumpOnly by editing the fileNo
Custom archivepg_dump custom formatYes, per objectYes
Directory archivepg_dump directory formatYes, per objectYes, also parallel dump
Delimited data filesPer-table CSV or tab filesYes, per tableYes, with scripting
Common logical backup formats

Logical backup compared with physical backup

The central contrast is with a physical backup, which copies data files, pages and transaction logs at the storage level. The two methods answer different recovery questions, and mature backup strategies usually combine them.

AspectLogical backupPhysical backup
What is copiedSchema and rows as statements or archiveData files and log segments
Speed on large dataSlower dump, much slower restoreClose to disk copy speed
PortabilityAcross versions and platformsSame engine version and layout
GranularitySingle table or schema possibleUsually whole cluster or instance
Point-in-time recoveryNo, only the dump momentYes, with archived logs
Corruption detectionReading every row exposes many errorsMay copy corrupted pages silently
Logical versus physical backups

A useful side effect of logical backups is that reading every row forces the engine to touch all data, so certain kinds of page corruption surface during the dump rather than months later during an emergency restore.

Key options and parameters

The behavior of a logical backup depends heavily on tool options, and wrong defaults are a common reason why a dump looks fine but fails to restore.

SettingPurposeConsideration
Consistent snapshotOne point-in-time viewEssential for transactional engines
Routines, triggers and eventsInclude stored codeNot always included by default
Users and privilegesInclude roles and grantsOften stored separately, as with pg_dumpall
CompressionReduce archive sizeTrades CPU for disk and network
Parallel jobsDump or restore tables concurrentlyLimited by the slowest large table
Character setPreserve text encodingMismatches corrupt non-ASCII data
Important settings for logical backups

Global objects such as roles, tablespaces and server settings live outside individual databases in many engines, so a backup plan must capture them explicitly or restored databases will lack their owners and permissions.

When to use a logical backup

Logical backups are the right tool when portability or selectivity matters more than recovery speed. They are rarely sufficient on their own for large production systems, where the time to restore from a dump can exceed acceptable recovery targets.

  • Major version upgrades and migrations between servers or cloud providers.
  • Restoring a single table or schema after an accidental deletion, without rolling back the whole database.
  • Creating development or test copies, often combined with masking of personal data.
  • Long-term archives in a format that remains readable after the original engine version is retired.
  • Small and medium databases, where dump and restore complete within the agreed recovery time.

For large systems, a common pattern is physical backups with log archiving for disaster recovery and point-in-time recovery, complemented by periodic logical backups for selective restores and an independent second copy.

Limitations and common mistakes

The most serious mistake is never testing the restore. A dump file that exists is not proof of a working backup; only a successful restore into a separate environment, followed by basic checks, confirms that the backup is usable.

PitfallEffectPrevention
No consistent snapshotRelated tables out of syncUse single-transaction or snapshot options
Missing global objectsRestore fails on owners and grantsBack up roles and settings separately
Untested restoreFailure discovered during an incidentScheduled restore drills
Dump stored on same serverLost together with the serverCopy to separate storage
Restore time underestimatedRecovery target missedMeasure full restore duration
Logical backup pitfalls and how to avoid them

Long-running dumps also have a side cost on the source: a snapshot held open for hours can delay cleanup of old row versions in MVCC engines, so very large dumps are often taken from a replica instead of the primary.

In everyday language

A logical backup writes down the contents of a database as a set of instructions. These instructions say which tables to create and which rows to put into them.

It is like saving a recipe instead of freezing the finished meal. The recipe works in almost any kitchen, and you can cook just one dish from it. Cooking everything from scratch takes longer, though. The key takeaway is that logical backups are flexible and portable, but restoring a large database from them is slow.

Seen in the real world

An online booking service runs a 60 GB PostgreSQL database and protects it with nightly physical backups plus archived write-ahead logs. In addition, the team creates a weekly logical backup with pg_dump in directory format using four parallel jobs and stores it in separate object storage.

One afternoon, a faulty migration script empties the table that holds discount codes, while all other tables remain correct. Rolling the entire database back would discard hours of new bookings. Instead, the administrator uses pg_restore to load only the discount table from the latest logical backup into a staging schema, compares it with recent changes and copies the missing rows back. The outcome is a full recovery of the lost data in under an hour, with no effect on bookings made that day.