Skip to content
Ameen Abozaid
MID-018

Database operations

  • 6 Lessons
  • 3 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Backups
  • restore
  • replication basics
  • monitoring
  • maintenance

Learn first

3 h of practice (estimate)

Learning outcomes

  1. Take logical (pg_dump) and physical backups and restore them.
  2. Explain point-in-time recovery with WAL archiving.
  3. Explain streaming replication and read replicas, including replication lag.
  4. Monitor key database health indicators (connections, locks, cache hit ratio, replication lag, disk).

Lessons

  1. 01Backups: logical vs physical
  2. 02Restore drills
  3. 03WAL and point-in-time recovery
  4. 04Replication and read replicas
  5. 05What to monitor
  6. 06Demo: restoring to five minutes before a bad DELETE

Lab and exercises

Lab

MID-018-LAB1 · Restore drill

Outcomes: 1–4 · Time: ~3 h Back up Shop's database (logical and physical with WAL archiving), simulate data loss with a bad DELETE, restore with PITR to before the loss, and time every step. Set up a standby or read replica if your environment allows, and observe replication lag. Acceptance: a runbook plus a measured recovery time.

Exercises (assessment: runbook review)

IDOutcomeTask
MID-018-EX011Take a pg_dump in custom format and restore it into a new database with parallel jobs.
MID-018-EX021Compare logical and physical backups for a 2 TB database: size, restore time, portability.
MID-018-EX032List the settings and files needed to enable WAL archiving and recover to a point in time.
MID-018-EX042Given a base backup at 02:00 and a bad change at 14:05, give the recovery target and expected steps.
MID-018-EX053Explain why a read replica can return stale data after a write and propose two handling options.
MID-018-EX064Write a monitoring checklist of six indicators with a warning threshold for each.
MID-018-EX074Review a short runbook with three flaws (never tested, no timing, backups on the same disk) and correct it.
Show solutions
  • EX01 pg_dump -Fc -f shop.dump shop; createdb shop2; pg_restore -d shop2 -j 4 shop.dump.
  • EX02 Logical: smaller to store maybe, slow restore (rebuilds indexes), portable across versions. Physical: fast, same major version, enables PITR.
  • EX03 wal_level suitable for archiving, archive_mode = on, archive_command (or library), a base backup; on restore restore_command, recovery_target_time, and recovery.signal (verify version).
  • EX04 Restore the 02:00 base backup, replay WAL to a target a little before 14:05 (for example 14:04), then promote.
  • EX05 Replication is asynchronous so the replica has not yet replayed the write. Options: read your own writes from the primary, or route by session or wait until the replica reaches the write's LSN.
  • EX06 For example: connections over 80%, replication lag over 30 s, disk over 80%, cache hit ratio under 99% for OLTP, idle-in-transaction over 1 minute, failed archive command. Thresholds are examples.
  • EX07 Add a scheduled restore test, record the measured time, copy backups to separate storage, and name an owner.
  • "Backups never restored" is addressed by EX01, EX07, and the lab.

Quiz

Outcomes: O1 backups and restore · O2 WAL and PITR · O3 replication and lag · O4 monitoring.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1pg_dump produces a:O1
  2. 2The best proof that a backup works is:O1
  3. 3A physical backup can usually be restored to:O1
  4. 4PITR needs:O2
  5. 5WAL is:O2
  6. 6To undo a bad DELETE at 14:05 you set the recovery target to:O2
  7. 7Asynchronous replication means the replica:O3
  8. 8A replica protects against an accidental DROP TABLE:O3
  9. 9Reading your own write from a lagging replica may return:O3
  10. 10Which is a key health indicator?O4

Practical part

Runbook review: find the gaps in a restore runbook (untested, untimed, same-host backups, missing WAL archive check). Pass: quiz 8/10, all gaps named with fixes, and the lab runbook with a measured recovery time.

Common mistakes

Backups never restored; reading your own writes from a lagging replica.

Assessment

Runbook review.

Career relevance

Disaster recovery (SRC-013).

Type to search levels, phases, modules and lessons.