DBA Scripts: Get SQL Server Database File Details

🔧Part of the DBA-Tools Project, copy/paste SQL Server scripts and health checks.In: Storage & Capacity

A disk alert has fired, or a log file has grown again, and you need to know which file it is, where it lives and whether anything stops it growing. Get-DatabaseFilesDetail answers that in one grid: every data and log file of every user database, with its path, size, growth setting and maximum.

To check one database quickly, query sys.database_files in that database. It returns the same file details for just that database, in a few seconds:

SELECT name,
       type_desc,
       physical_name,
       size / 128 AS size_mb,
       CASE WHEN is_percent_growth = 1 THEN CAST(growth AS varchar(10)) + '%'
            ELSE CAST(growth / 128 AS varchar(10)) + ' MB' END AS auto_growth
FROM sys.database_files;

The script below is for when you want every database on the instance in one grid.


The Script

Run the following script against your SQL Server instance. It is read-only and needs VIEW ANY DEFINITION. Without it the script returns no rows, not an error.

/*
Script Name : Get-DatabaseFilesDetail
Category    : storage-capacity-management
Purpose     : Show per-file details for all user databases: path, size, max size, growth settings.
Author      : Peter Whyte (https://sqldba.blog/dba-scripts-get-sql-server-database-file-details/)
Requires    : VIEW ANY DEFINITION. Without it sys.master_files returns no rows, not an error.
HealthCheck : Yes
Notes       : Skips database snapshots (sys.master_files shows their maximum size, not disk use).
*/
-- SAFE:ReadOnly
-- IMPACT:Low
SET NOCOUNT ON;
SET QUOTED_IDENTIFIER ON;

SELECT
    d.name AS database_name,
    d.state_desc AS db_state,
    d.recovery_model_desc AS recovery_model,
    mf.file_id,
    mf.name AS logical_name,
    mf.type_desc AS file_type,
    LEFT(mf.physical_name, 1) AS drive_letter,
    mf.physical_name AS physical_path,
    CAST(mf.size * 8.0 / 1024 AS DECIMAL(12,2)) AS current_size_mb,
    -- NULL = no maximum. An unlimited log is stored as 268435456 pages (2 TB), not -1: mapped to NULL too.
    CASE
        WHEN mf.max_size = -1 THEN NULL
        WHEN mf.type = 1 AND mf.max_size = 268435456 THEN NULL
        WHEN mf.max_size = 0 THEN CAST(mf.size * 8.0 / 1024 AS DECIMAL(12,2))
        ELSE CAST(mf.max_size * 8.0 / 1024 AS DECIMAL(12,2))
    END AS max_size_mb,
    CASE mf.is_percent_growth
        WHEN 1 THEN CAST(mf.growth AS VARCHAR(10)) + '%'
        ELSE CAST(CAST(mf.growth * 8.0 / 1024 AS INT) AS VARCHAR(20)) + ' MB'
    END AS auto_growth,
    mf.is_percent_growth AS growth_is_percent,
    mf.state_desc AS file_state
FROM sys.master_files AS mf
INNER JOIN sys.databases AS d ON d.database_id = mf.database_id
WHERE d.database_id > 4
  AND d.source_database_id IS NULL   /* database snapshot */
ORDER BY d.name, mf.type, mf.file_id;

It reads sys.master_files and returns one row per data and log file of every user database. Database snapshots are left out, because sys.master_files shows their maximum size, not what they use on disk.


Example Output

SSMS running Get-DatabaseFilesDetail against the lab instance: 42 rows, one per data and log file across 21 user databases, with database state, recovery model, logical name, file type, drive letter, physical path, current size in MB, max_size_mb, autogrowth increment and file state. max_size_mb reads NULL on every row, data and log alike, meaning no file has a maximum.

Run against the lab instance: 42 files across 21 user databases, and max_size_mb is NULL on every one of them, data and log, so nothing on this instance stops a file growing until its volume is full.

Run it from the DBA Tools repo
git clone https://github.com/peterwhyte-lgtm/dba-tools
cd dba-tools
.\Initialize-Environment.ps1
.\run.ps1 Get-DatabaseFilesDetail

Add -ServerInstance SQLSERVER01 to run against another server. Script file: Get-DatabaseFilesDetail.sql


Understanding the Results

Read max_size_mb and auto_growth first: together they say whether each file can still grow, and how.

max_size_mb
The ceiling the file is allowed to grow to. NULL means no maximum, data or log, so the file grows until its volume is full. A number is a real cap.Act when current_size_mb is close to it. The next growth fails with the database still online and the application still writing.
auto_growth
growth_is_percent
How the file grows: a fixed MB step, or a percentage when growth_is_percent is 1, which makes every growth larger than the last. 0 MB means the file cannot grow at all.Act when it is a percentage on a large file, or a step of a few MB. Small steps mean frequent pauses while the file grows and, on a log, too many VLFs.
db_state
file_state
Both should read ONLINE, and a size that has stopped changing is often explained here.Act when either reads anything else: RESTORING, RECOVERY_PENDING, SUSPECT or OFFLINE. That is the headline, not a detail.
drive_letter
physical_path
Where the file lives. drive_letter is taken from the front of the path so you can sort the whole instance by volume.Act when data and log files share a drive, or a user database sits on the system drive.
current_size_mb
The allocated size of the file, not the space used inside it. A file can hold a lot of free space.
recovery_model
FULL, BULK_LOGGED or SIMPLE. A FULL database with no log backups is the usual cause of a log file that keeps growing.
database_name
logical_name
The database, and the file name you quote in ALTER DATABASE … MODIFY FILE. It is not the file name on disk.
file_id
file_type
file_id 1 is the primary data file and file_id 2 is normally the log. file_type is ROWS for data and LOG for the transaction log.
Docs: Microsoft’s reference covers sys.master_files, sys.databases and sys.database_files in full.

Frequently Asked Questions

Should SQL Server database files use percentage growth?

Generally no. Fixed MB growth settings are easier to predict and manage, especially for larger databases.

Does this work on Azure SQL Database?

No. sys.master_files is not available on Azure SQL Database, so run a sys.database_files query inside each database instead. Azure SQL Managed Instance has sys.master_files and runs the script as it is.

How do I find SQL Server database file locations?

Querying sys.master_files provides the physical paths for all database files on an instance.

How do I check SQL Server database file sizes?

Database file sizes can be reviewed using sys.master_files, sys.database_files, or monitoring scripts such as this one.


Related

You may also find these useful:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *