Logo ← PostgreSQL Blog

11 Emergency Commands Every Postgres Engineer Must Know


11 Emergency Commands Every Postgres Engineer Must Know

In the life of a PostgreSQL DBA, there are moments of calm and moments of absolute chaos. When the disk hits 99%, replication lags by terabytes, or a major upgrade is looming at 3 AM, your mental muscle memory is more important than any documentation.

Most people know SELECT and INSERT, but true expertise lies in the secondary utilities that bridge the gap between a crashed cluster and a healthy one. This guide isn’t just a list of commands; it’s a battle-tested toolkit designed to turn panic into precision.

Table of Contents

  • vacuumdb: Healing Query Performance
  • clusterdb: Optimizing Physical Data Layout
  • pg_controldata: The Database Heartbeat
  • pg_isready: Instant Connectivity Checks
  • pg_basebackup: Building Replicas at Scale
  • Globals Backup: Protecting Roles and Permissions
  • WAL Identification: Pinpointing the Current Log
  • pg_waldump: Peering Inside the Black Box
  • pg_upgrade check: Risk Free Upgrade Simulation
  • pg_checksums: Fighting Silent Data Corruption
  • pg_archivecleanup: Surgical Disk Space Recovery

1. vacuumdb: Healing Query Performance

# Running only ANALYZE across all databases
vacuumdb -U postgres --all --analyze-only --verbose

The Scenario: You just finished a bulk migration or a massive delete. Suddenly, queries that took milliseconds are now timing out. Why it matters: The PostgreSQL Optimizer is only as smart as its statistics. If stats are stale, it might choose a Sequential Scan over an Index Scan. analyze-only bypasses the heavy lifting of a full vacuum and quickly refreshes the brain of your database.

2. clusterdb: Optimizing Physical Data Layout

clusterdb -U postgres -d sales_db --table orders --index idx_orders_timestamp --echo

The Scenario: You have a massive table where you frequently query date ranges, but the data is physically scattered on the disk. The Insight: This physically reorders the data on the disk based on a specific index.

  • The Pro: Massive I/O speedup for range queries.
  • The Catch: It takes an Access Exclusive Lock. Use it only during maintenance windows.

3. pg_controldata: The Database Heartbeat

/usr/pgsql-18/bin/pg_controldata -D /var/lib/pgsql/18/data

The Scenario: The database won’t start after a power failure. You need to know the state of the system before you do anything drastic. Why it’s critical: This command reads the global/pg_control file. It tells you the Cluster state (in production, shut down, or in crash recovery) and the System Identifier. It is the ID card of your cluster.

4. pg_isready: Instant Connectivity Checks

pg_isready -h 10.0.0.5 -p 5432

The Scenario: You’re writing a shell script or configuring a Load Balancer (like HAProxy) and need a lightweight way to check if Postgres is accepting connections without actually logging in. Why use it: It’s faster and safer than trying a full psql connection. It returns clear exit codes for automation.

5. pg_basebackup: Building Replicas at Scale

pg_basebackup -D /backup/path -Fp -P -R -X stream

The Scenario: You need to spin up a new Read Replica right now. The Breakdown:

  • -R: Automatically creates the standby.signal and connection info.
  • -X stream: Includes the necessary WAL files so the backup is consistent immediately.

6. Globals Backup: Protecting Roles and Permissions

sudo -u postgres pg_dumpall --globals-only --file=global_metadata.sql

The Scenario: You’ve backed up your databases, but you forgot that users, roles, and permissions are stored globally, not inside specific databases. The Risk: Without this, you might restore data to a new server and find that none of your application users can log in.

7. WAL Identification: Pinpointing the Current Log

SELECT pg_walfile_name(pg_current_wal_lsn());
-- Result: 000000010000000000000005

The Scenario: Your disk is filling up in the pg_wal directory. You need to know which specific file is currently being written to so you can investigate replication lag or archive failures.

8. pg_waldump: Peering Inside the Black Box

pg_waldump /var/lib/pgsql/18/data/pg_wal/000000010000000000000005 | head -n 20

The Scenario: You see a massive spike in write activity, but you don’t know the source. The Power: This allows you to peek inside the Write Ahead Log. It shows you exactly which transactions were recorded, turning the WAL from a mystery into an audit trail.

9. pg_upgrade check: Risk Free Upgrade Simulation

sudo -u postgres pg_upgrade \
  -b /old/bin -B /new/bin \
  -d /old/data -D /new/data \
  --check

The Scenario: You have a tight maintenance window to upgrade a 5TB database from version 17 to 18. Why it’s a lifesaver: Running --check performs a pre-flight scan for incompatible data types without moving any data. If this passes, your 3 AM anxiety levels will drop significantly.

10. pg_checksums: Fighting Silent Data Corruption

# Must be run on a stopped cluster
pg_checksums -D /var/lib/pgsql/18/data --check

The Scenario: You suspect hardware level bit rot or disk corruption. The Reality: Modern disks can report a successful write even if the data is corrupted. pg_checksums validates the integrity of every data block by comparing stored hashes.

11. pg_archivecleanup: Surgical Disk Space Recovery

pg_archivecleanup /mnt/server_archive/ 000000010000000000000008

The Scenario: Your WAL archive is bloated, and you are about to hit a disk full outage. The Warning: This is a surgical tool. It deletes WAL files older than the one specified. Use it to manually clean up your archive if your automated retention scripts fail.

Conclusion

Being a Senior DBA isn’t about knowing every flag in the manual; it’s about knowing which tool to grab when the sirens go off. These 11 commands represent the difference between a controlled recovery and a catastrophic loss.

What’s your emergency command?