Version and Instance Health Check

These are the first things to check when you start responding to an incident, before and after patching, or when you take over a server for the first time. In all three databases, you look at the version and patch level, when it started and what state it is in now, and where the server log is and what the recent errors are. Finally, run integrity checks periodically to make sure no data blocks are corrupt.

Oracle

Required privileges: SELECT_CATALOG_ROLE to query V$ views. For ADRCI, the oracle OS account on the database server.

Version and patches

SELECT banner_full FROM v$version;            -- 18c+: e.g. Version 19.24.0.0.0
SELECT version_full FROM v$instance;          -- 18c+

-- Applied Release Updates and patches
SELECT patch_id, action, status, action_time, description
FROM dba_registry_sqlpatch
ORDER BY action_time DESC;
  • The second number in 19c (19.24) is the Release Update number.
  • dba_registry_sqlpatch is the record of what datapatch applied to the database. Binary-side patches are listed in the shell with $ORACLE_HOME/OPatch/opatch lspatches. If the two differ, datapatch was skipped.

Instance and database state

SELECT instance_name, host_name, status, database_status, archiver, logins,
       startup_time, ROUND(SYSDATE - startup_time, 1) AS up_days
FROM v$instance;

SELECT name, db_unique_name, cdb, log_mode, open_mode, database_role,
       flashback_on, force_logging, platform_name
FROM v$database;

SELECT con_id, name, open_mode, restricted FROM v$pdbs;   -- 12c+ CDB
  • status: STARTED (NOMOUNT), MOUNTED, OPEN. If logins = RESTRICTED, only users with the RESTRICTED SESSION privilege can connect.
  • If archiver = STOPPED or FAILED, archiving has stopped. The database will soon stop too, so check the space on the archive destination.
  • If a PDB stays MOUNTED, run ALTER PLUGGABLE DATABASE pdb1 OPEN; and then ... SAVE STATE; so that it opens after a restart as well.

alert log location and recent errors

SELECT name, value FROM v$diag_info
WHERE name IN ('ADR Home', 'Diag Trace', 'Diag Alert', 'Default Trace File');

The text alert log is alert_<SID>.log under the Diag Trace path. To read it with SQL (12.2+):

SELECT originating_timestamp, message_text
FROM v$diag_alert_ext
WHERE originating_timestamp > SYSTIMESTAMP - INTERVAL '1' DAY
  AND message_text LIKE '%ORA-%'
ORDER BY originating_timestamp DESC;

V$DIAG_ALERT_EXT reads the whole XML log file, so it is slow when the log is large. Always include a time-range condition.

ADRCI

adrci
show homes
set homepath diag/rdbms/orcl/ORCL
show alert -tail 50
show alert -p "message_text like '%ORA-%'"
show problem
show incident
  • A problem is a group of serious errors of the same kind (ORA-00600, ORA-07445, and so on), and an incident is each individual occurrence. When submitting to Oracle Support, package with ips pack incident <number> in /tmp.

Block corruption check

-- RMAN: check all blocks (physical and logical) without taking an actual backup
BACKUP VALIDATE CHECK LOGICAL DATABASE;
SELECT file#, block#, blocks, corruption_type FROM v$database_block_corruption;

Any rows returned are corrupt blocks. Use RMAN RECOVER CORRUPTION LIST; to do block-level recovery from a backup (Enterprise Edition).

PostgreSQL

Required privileges: mostly anyone. To list and read log files, superuser or pg_monitor (pg_ls_logdir) and pg_read_server_files (pg_read_file).

Version and startup time

SELECT version();
SHOW server_version;
SELECT current_setting('server_version_num')::int AS version_num;   -- e.g. 160004

SELECT pg_postmaster_start_time(),
       now() - pg_postmaster_start_time() AS uptime,
       pg_conf_load_time();                                         -- when the configuration was last read
  • Minor versions (the 4 in 16.4) are bug and security fixes. Within the same major version, you only swap the binaries and restart.

Primary or standby

SELECT pg_is_in_recovery();       -- true means a standby (or in recovery)
SHOW data_directory;
SHOW data_checksums;              -- whether initdb was run with data checksums enabled

Server log location

SHOW logging_collector;
SHOW log_directory;               -- if a relative path, relative to data_directory
SELECT pg_current_logfile();      -- PostgreSQL 10+, the log file currently being written

SELECT name, size, modification
FROM pg_ls_logdir()
ORDER BY modification DESC
LIMIT 5;
  • If logging_collector = off, pg_current_logfile() is NULL and the log goes to stderr. Package installs usually have it in the systemd journal (journalctl -u postgresql) or the distribution's log path.
  • To see only recent errors from the shell, use grep -E 'ERROR|FATAL|PANIC' <log file> | tail -50.

Database state and transaction ID exhaustion risk

SELECT datname, pg_size_pretty(pg_database_size(datname)) AS size,
       age(datfrozenxid) AS xid_age, datallowconn
FROM pg_database
ORDER BY age(datfrozenxid) DESC;

SELECT datname, xact_commit, xact_rollback, deadlocks, conflicts,
       checksum_failures, temp_bytes                                -- checksum_failures: 12+
FROM pg_stat_database
WHERE datname IS NOT NULL;
  • If xid_age is well above autovacuum_freeze_max_age (default 200 million) and keeps rising, VACUUM is blocked. As it approaches about 2 billion, the server refuses writes.
  • If checksum_failures is greater than 0, suspect disk corruption (it is counted only when data checksums are enabled).

Integrity check (amcheck)

CREATE EXTENSION IF NOT EXISTS amcheck;
SELECT bt_index_check('sales.orders_pkey'::regclass);
pg_amcheck -h db01 -U postgres -d sales --heapallindexed -j 4    # PostgreSQL 14+
  • bt_index_check checks the B-tree index structure and takes only light locks. pg_amcheck (14+) checks all tables and indexes in a database at once.
  • PostgreSQL has no full repair command like DBCC CHECKDB. If corruption turns up, restore from a backup or rebuild the index.

SQL Server

Required privileges: VIEW SERVER STATE (VIEW SERVER PERFORMANCE STATE on 2022+), securityadmin to read the error log, and sysadmin or db_owner on the database for DBCC CHECKDB.

Version and edition

SELECT @@VERSION;

SELECT SERVERPROPERTY('ProductVersion')     AS product_version,   -- e.g. 16.0.4135.4
       SERVERPROPERTY('ProductLevel')       AS product_level,     -- RTM, SPn
       SERVERPROPERTY('ProductUpdateLevel') AS cu,                -- CUnn
       SERVERPROPERTY('Edition')            AS edition,
       SERVERPROPERTY('EngineEdition')      AS engine_edition,
       SERVERPROPERTY('MachineName')        AS machine,
       SERVERPROPERTY('InstanceName')       AS instance,          -- NULL for the default instance
       SERVERPROPERTY('IsClustered')        AS is_clustered,
       SERVERPROPERTY('IsHadrEnabled')      AS is_hadr_enabled,
       SERVERPROPERTY('Collation')          AS collation;
  • Major version: 13 = 2016, 14 = 2017, 15 = 2019, 16 = 2022.
  • EngineEdition: 2 = Standard, 3 = Enterprise (including Developer), 4 = Express.

Startup time and hardware

SELECT sqlserver_start_time,
       DATEDIFF(HOUR, sqlserver_start_time, SYSDATETIME()) AS up_hours,
       cpu_count, hyperthread_ratio,
       physical_memory_kb / 1024 AS physical_memory_mb,
       committed_kb / 1024 AS committed_mb,
       committed_target_kb / 1024 AS target_mb,
       virtual_machine_type_desc
FROM sys.dm_os_sys_info;

If committed_mb is always below target_mb, memory is left over; if they are equal, it is using everything up to max server memory.

Database state

SELECT name, state_desc, user_access_desc, is_read_only, recovery_model_desc,
       compatibility_level, log_reuse_wait_desc
FROM sys.databases
ORDER BY name;

If state_desc is not ONLINE (RECOVERING, RECOVERY_PENDING, SUSPECT, EMERGENCY), look at the error log first for the cause.

Reading the error log

SELECT SERVERPROPERTY('ErrorLogFileName');          -- path of the current error log file

EXEC sp_readerrorlog 0, 1, N'error';                -- current log (0), SQL Server log (1), containing 'error'
EXEC sp_readerrorlog 1, 1, N'Login failed';         -- the previous log file
EXEC sp_readerrorlog 0, 2;                          -- SQL Server Agent log

EXEC xp_readerrorlog 0, 1, N'error', NULL,
     '2026-11-26 00:00', '2026-11-26 23:59', N'desc';   -- with a time range and sort order
  • The arguments are, in order, the log number (0 = current), the log type (1 = engine, 2 = Agent), search string 1, and search string 2. The two strings are combined with AND.
  • The error log rolls over to a new file only on restart. If it grows too large, cycle it with EXEC sp_cycle_errorlog;.

Integrity check (DBCC CHECKDB)

DBCC CHECKDB (N'sales') WITH NO_INFOMSGS, ALL_ERRORMSGS;
DBCC CHECKDB (N'sales') WITH PHYSICAL_ONLY, NO_INFOMSGS;    -- large databases, quick check

SELECT DATABASEPROPERTYEX(N'sales', 'LastGoodCheckDbTime');  -- 2016 SP2+
SELECT * FROM msdb.dbo.suspect_pages;                         -- corrupt pages found while reading
  • It runs online using an internal snapshot, but it uses a lot of I/O and tempdb. Run it during quiet hours, or restore a backup to another server and run it there.
  • If errors appear, look at the recommended repair level at the end of the message. First consider restoring from a backup (including a page restore).

WARNING DBCC CHECKDB (N'sales', REPAIR_ALLOW_DATA_LOSS) makes the structure consistent even if it has to delete the corrupt parts. Data is lost, and it is hard to tell what was deleted. Use it only as a last resort when no usable backup exists, and only in SINGLE_USER mode.