DB and Tablespace Capacity
These are the commands to use when an alert says "the disk is full" or "the tablespace is running out", or when planning a capacity expansion. On all three DBs, the size allocated to files and the size actually used inside them are separate. If a file is set to grow automatically, the limit is the smaller of the file's maximum size and the free disk space. Temporary space used by sorts and hashes is checked separately.
Oracle
Required privileges: SELECT on DBA_* views (SELECT_CATALOG_ROLE). To see all PDBs in a CDB, use the CDB_* views from the root and look at con_id as well.
Usage per tablespace
SELECT m.tablespace_name,
ROUND(m.tablespace_size * t.block_size / 1024 / 1024) AS max_mb,
ROUND(m.used_space * t.block_size / 1024 / 1024) AS used_mb,
ROUND(m.used_percent, 1) AS used_pct
FROM dba_tablespace_usage_metrics m
JOIN dba_tablespaces t ON t.tablespace_name = m.tablespace_name
ORDER BY m.used_percent DESC;
DBA_TABLESPACE_USAGE_METRICSis in blocks, andtablespace_sizeincludes the autoextend maximum. Soused_pctis usage relative to the limit the tablespace can still grow to.- To see usage against the currently allocated size, add up files and free space directly as below.
SELECT df.tablespace_name,
ROUND(df.bytes / 1024 / 1024) AS alloc_mb,
ROUND(NVL(fs.bytes, 0) / 1024 / 1024) AS free_mb,
ROUND((df.bytes - NVL(fs.bytes, 0)) / df.bytes * 100, 1) AS used_pct_of_alloc
FROM (SELECT tablespace_name, SUM(bytes) AS bytes FROM dba_data_files GROUP BY tablespace_name) df
LEFT JOIN (SELECT tablespace_name, SUM(bytes) AS bytes FROM dba_free_space GROUP BY tablespace_name) fs
ON fs.tablespace_name = df.tablespace_name
ORDER BY used_pct_of_alloc DESC;
Data files and autoextend settings
SELECT tablespace_name, file_name, ROUND(bytes / 1024 / 1024) AS size_mb,
autoextensible, ROUND(maxbytes / 1024 / 1024) AS max_mb,
increment_by AS next_blocks, status
FROM dba_data_files
ORDER BY tablespace_name, file_name;
- If
autoextensible = 'NO',size_mbis the limit.increment_byis a number of blocks, not MB. - One file of a smallfile tablespace is at most about 32GB with an 8KB block size. When you hit the limit, add a file.
Adding space
ALTER DATABASE DATAFILE '/u01/oradata/ORCL/users01.dbf' RESIZE 20G;
ALTER DATABASE DATAFILE '/u01/oradata/ORCL/users01.dbf' AUTOEXTEND ON NEXT 1G MAXSIZE 32767M;
ALTER TABLESPACE users ADD DATAFILE '+DATA' SIZE 10G AUTOEXTEND ON NEXT 1G MAXSIZE 32767M;
WARNING Adding a file immediately increases backup size and disk usage. Unless you use ASM or OMF, write the path exactly. Shrinking with
RESIZEfails withORA-03297when there are extents at the end of the file.
Temporary tablespace usage
SELECT tablespace_name, ROUND(tablespace_size / 1024 / 1024) AS size_mb,
ROUND(allocated_space / 1024 / 1024) AS alloc_mb,
ROUND(free_space / 1024 / 1024) AS free_mb
FROM dba_temp_free_space;
-- Sessions using temporary space right now
SELECT s.sid, s.serial#, s.username, u.sql_id, u.tablespace, u.segtype,
ROUND(u.blocks * t.block_size / 1024 / 1024) AS used_mb
FROM v$tempseg_usage u
JOIN v$session s ON s.saddr = u.session_addr
JOIN dba_tablespaces t ON t.tablespace_name = u.tablespace
ORDER BY u.blocks DESC;
- If
segtypeisSORTorHASH, it is query work space; ifDATAorINDEX, it is a global temporary table. - Running out of temporary space (
ORA-01652) is usually caused by a single query doing a large sort. Before adding space, first look at thesql_idwith the query above.
PostgreSQL
Required privileges: to see the size of other databases, CONNECT on that DB or pg_read_all_stats. For the temporary file listing, superuser or pg_monitor.
PostgreSQL has no equivalent of Oracle's tablespace quotas or file autoextend. Tables and indexes grow as files in a directory as needed, so the limit is the free space on the filesystem. Free disk space cannot be seen from SQL; check it from the OS (df -h) or monitoring.
Size per database
SELECT datname, pg_size_pretty(pg_database_size(datname)) AS size
FROM pg_database
WHERE datallowconn
ORDER BY pg_database_size(datname) DESC;
pg_database_sizeadds up all files in that DB's directory, so it can take a few seconds on a large DB.- WAL (
pg_wal) does not belong to any DB and does not appear here (see 13-log-management).
Size and location per tablespace
SELECT spcname, pg_tablespace_location(oid) AS location,
pg_size_pretty(pg_tablespace_size(oid)) AS size
FROM pg_tablespace
ORDER BY pg_tablespace_size(oid) DESC;
pg_defaultandpg_globalhave an empty location and live inside the data directory.- Moving objects to a tablespace on another disk (
ALTER TABLE ... SET TABLESPACE) lets you spread space, but anACCESS EXCLUSIVElock is held while moving.
Temporary file usage
-- Cumulative: number and bytes of temporary files per DB (since stats_reset)
SELECT datname, temp_files, pg_size_pretty(temp_bytes) AS temp_bytes, stats_reset
FROM pg_stat_database
WHERE datname IS NOT NULL
ORDER BY temp_bytes DESC;
-- Temporary files on disk right now (PostgreSQL 12+)
SELECT name, pg_size_pretty(size) AS size, modification
FROM pg_ls_tmpdir()
ORDER BY size DESC;
- From the pid in the temporary file name
pgsql_tmp<pid>.<n>, find the session inpg_stat_activity. pg_ls_tmpdir()looks only at the default tablespace. For others, usepg_ls_tmpdir('<tablespace oid>').
Logging and limiting temporary files
ALTER SYSTEM SET log_temp_files = '100MB'; -- log statements that create a temporary file larger than this (0 means all)
ALTER SYSTEM SET temp_file_limit = '20GB'; -- total temporary file space one process may use
SELECT pg_reload_conf();
WARNING A query that hits
temp_file_limitends with an error. If there are jobs that legitimately do large sorts, such as batches, set it separately per role (ALTER ROLE batch SET temp_file_limit = '-1').
SQL Server
Required privileges: sys.database_files is visible to public. Volume free space and per-session tempdb usage require VIEW SERVER STATE (VIEW SERVER PERFORMANCE STATE in 2022+).
Whole-DB summary
USE sales;
EXEC sp_spaceused @oneresultset = 1; -- 2016+
database_sizeis the sum of data and log files, andunallocated spaceis the free space inside the data files.reserved=data+index_size+unused. If values look wrong, correct the counts withEXEC sp_spaceused @updateusage = N'TRUE';.
Allocated, used, and autogrow per file
SELECT name, type_desc, physical_name,
size / 128.0 AS size_mb,
FILEPROPERTY(name, 'SpaceUsed') / 128.0 AS used_mb,
(size - FILEPROPERTY(name, 'SpaceUsed')) / 128.0 AS free_mb,
CASE max_size WHEN -1 THEN 'unlimited' WHEN 0 THEN 'no growth'
ELSE CAST(max_size / 128 AS varchar(20)) + ' MB' END AS max_size,
CASE is_percent_growth WHEN 1 THEN CAST(growth AS varchar(10)) + ' %'
ELSE CAST(growth / 128 AS varchar(10)) + ' MB' END AS growth
FROM sys.database_files;
size,max_size, andgrowthare numbers of 8KB pages, so dividing by 128 gives MB.- For a log file, a
max_sizeof268435456(2TB) is effectively unlimited. - Percentage (
%) growth increases by more each time as the file gets larger, so switching to a fixed MB amount is usually better.
Instance-wide files and free disk space
SELECT DB_NAME(mf.database_id) AS db, mf.name, mf.type_desc,
mf.size / 128 AS size_mb,
vs.volume_mount_point,
vs.total_bytes / 1048576 AS volume_total_mb,
vs.available_bytes / 1048576 AS volume_free_mb
FROM sys.master_files mf
CROSS APPLY sys.dm_os_volume_stats(mf.database_id, mf.file_id) vs
ORDER BY vs.volume_mount_point, size_mb DESC;
Growing and adding files
ALTER DATABASE sales MODIFY FILE (NAME = sales_data, SIZE = 20GB, FILEGROWTH = 1GB, MAXSIZE = 100GB);
ALTER DATABASE sales ADD FILE (NAME = sales_data2, FILENAME = N'E:\Data\sales_data2.ndf', SIZE = 10GB, FILEGROWTH = 1GB);
WARNING Without the Instant File Initialization privilege, growing a data file zero-fills the new space, and writes stall in the meantime. Do not grow by a large amount in one step during business hours.
tempdb usage
USE tempdb;
SELECT SUM(unallocated_extent_page_count) / 128 AS free_mb,
SUM(user_object_reserved_page_count) / 128 AS user_objects_mb, -- #temp, table variables
SUM(internal_object_reserved_page_count) / 128 AS internal_objects_mb, -- sorts, hashes, spools
SUM(version_store_reserved_page_count) / 128 AS version_store_mb -- snapshot isolation, online indexes
FROM sys.dm_db_file_space_usage;
-- Sessions that used a lot of tempdb
SELECT TOP (20) su.session_id, s.login_name, s.program_name,
(su.user_objects_alloc_page_count - su.user_objects_dealloc_page_count) / 128 AS user_mb,
(su.internal_objects_alloc_page_count - su.internal_objects_dealloc_page_count) / 128 AS internal_mb
FROM sys.dm_db_session_space_usage su
JOIN sys.dm_exec_sessions s ON s.session_id = su.session_id
ORDER BY user_mb + internal_mb DESC;
- Session-level values include only completed work. Usage of statements running now is in
sys.dm_db_task_space_usage. - If
version_store_mbstays large, check for long-open transactions (sys.dm_tran_active_snapshot_database_transactions).