Interview's MateSQL Learning
Interview's Mate

SQL Learning

Foundations → production SQL

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
SQL topics
Lesson 156/ 160
08. Transactions, Security & Production SQL Administration

Logical Backups (pg_dump, mysqldump) vs Physical Backups

Choose logical dumps vs physical base backups for restore speed and flexibility.

Intermediatepg_dump · Physical Backup3 quiz questions
Practice SQL
In plain wordsDiagramExamples & theoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

One kind of backup saves instructions to rebuild the data; the other saves an exact copy of the database files.

Think of it like…

A logical backup is a recipe book: you can cook any dish again anywhere, but cooking a feast takes hours. A physical backup is a photo-perfect frozen copy of the whole kitchen: restoring it is fast, but it only fits an identical kitchen.

Where you’ve seen it

A 2 TB production database is backed up nightly with pg_basebackup plus continuous WAL archiving, so it can be restored to any minute within 14 days. Separately, a weekly pg_dump of the small billing schema lets the team restore one table, or load it into a newer PostgreSQL version, without touching anything else.

Words you’ll hear

Logical backup
SQL or archive export of schema and data; portable and selective.
Physical backup
A copy of the data files; same major version and platform needed.
RPO / RTO
How much data you can lose / how long recovery may take.
Restore test
Regularly restoring a backup to prove it works.

Visual diagram

Loading interactive diagram

Theory & example queries

Logical backups

pg_dump takes a consistent snapshot of one database without blocking normal reads and writes, and writes it as plain SQL or in custom or directory format, which pg_restore can restore in parallel (-j) and selectively (one schema or table). mysqldump does the same for MySQL; use --single-transaction for a consistent InnoDB dump without locking.

Because a logical restore re-inserts data and rebuilds every index, it gets slow for large databases: hours for hundreds of gigabytes. Its strengths are portability (restore into a different major version or platform), selectivity, and readability, which make it ideal for small databases, migrations and extra protection for critical tables.

Dump and restore in parallel

pg_dump -Fd -j 4 -f /backups/shop_2026-11-20 shop then pg_restore -j 4 -d shop_restore /backups/shop_2026-11-20

Key takeaway: Logical: portable and selective, but slow to restore at scale.

Physical backups and testing

pg_basebackup copies the cluster's data files while it runs; combined with continuous WAL archiving it enables point-in-time recovery to any moment after the base backup. Restores are roughly as fast as copying files, but only to the same PostgreSQL major version and platform, and they cover the whole cluster rather than one table. Tools like pgBackRest, Barman and Percona XtraBackup (for MySQL) add compression, incremental backups and retention management. Managed services (RDS, Cloud SQL) automate snapshots and PITR.

Whatever the method, schedule restore tests: restore into a scratch instance, run sanity checks, and time it against your RTO. Keep copies off the primary server and in another location (the 3-2-1 rule: three copies, two media, one off-site), and remember that a replica isn't a backup, because a DROP TABLE replicates within seconds.

A base backup for PITR

pg_basebackup -D /backups/base_2026-11-20 -Ft -z -X stream with archive_mode = on and an archive_command (or pgBackRest) shipping WAL segments to storage.

Key takeaway: Physical + WAL for fast, point-in-time restores; test restores regularly.
Interactive walkthrough

How it works, step by step

Follow one practical SQL workflow from start to finish. Select any step or play through the flow.

Progress1/4
Step 1Requirements

Set RPO and RTO

Agree how much data loss and how much downtime are acceptable; these decide the method.

Focus at this step

RPO 5 min · RTO 1 hour

What to watch at this step
Drives
the design
Tip: use ← → to move between steps

When to use it & trade-offs

What it is

A logical backup (pg_dump, mysqldump) exports the database as SQL statements or a portable archive: schema plus data, rebuilt on restore. A physical backup (pg_basebackup, Percona XtraBackup, storage snapshots) copies the actual data files. Logical backups are flexible and portable; physical backups restore large databases much faster and, with archived WAL, allow point-in-time recovery.

Why it matters: Backups are only useful if you can restore from them within the time the business can tolerate. Choosing the type determines how long a restore takes, whether you can recover a single table, and whether you can roll back to five minutes before a mistake. It's a standard production-readiness and DBA interview topic.

When to reach for it

  • Disaster recovery plans
  • Restoring an accidentally deleted table
  • Moving data between versions or providers

When to avoid or be careful

  • Treating a read replica as a backup
  • Keeping backups only on the database server
  • Never testing a restore

Logical vs physical backups vs snapshots

Logical (pg_dump, mysqldump)
Best for: Small databases, migrations, single-table restores

Strengths: Portable across versions; selective; human-readable.

Watch out: Slow to restore large databases; no point-in-time recovery on its own.

Physical + WAL (pg_basebackup, pgBackRest)
Best for: Large production databases

Strengths: Fast restore; point-in-time recovery.

Watch out: Same major version and platform; whole cluster only.

Storage / cloud snapshots
Best for: Managed databases and quick full restores

Strengths: Fast, automated, often with PITR.

Watch out: Provider-specific; still need restore testing.

Interview signals & numbers to know

When this topic comes up

  • “How would you back up this database?”
  • “Someone dropped a table, how do we recover?”
  • RPO / RTO
  • pg_dump vs pg_basebackup
  • Disaster recovery

Benchmarks & rules of thumb

3-2-1 -
backup copies, media, off-site
-j -
parallel jobs for pg_dump / pg_restore
Same major version -
required for physical restores

Common mistakes & follow-ups

Common interview mistakes

  • Relying on nightly pg_dump for a 3 TB database and discovering a restore takes a day.
  • Keeping backups on the same disk as the database.
  • Assuming backups work because the backup job reports success, without ever restoring one.

Likely interviewer follow-ups

  • When would you choose pg_dump over pg_basebackup?
  • What's the difference between RPO and RTO?
  • Why isn't a replica a backup?
Interview tip: Lead with RPO and RTO, then pick methods to meet them, and finish with "and we test restores". That order shows you think about recovery, not just backups.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Logical Backups (pg_dump, mysqldump) vs Physical Backups with hands-on SQL practice and interview Q&A.

Open SQL Practice →SQL interview Q&A
Previous topic
Data Masking, Encryption & Protecting PII in SQL Databases
Next topic
Point-in-Time Recovery (PITR) & Write-Ahead Log (WAL) Archiving