PG_SIZE_PRETTY

Format a byte count as a human-readable size string (bytes, kB, MB, GB, TB).

Category: miscReturns: STRINGDialect: PostgreSql

Syntax

PG_SIZE_PRETTY(size)

Description

## Overview Formats a byte count as a short human-readable string using the nearest unit from bytes, kB, MB, GB, or TB. Use this function for diagnostic and monitoring output where an exact byte count would be hard to read. ## Behavior - Returns NULL if the input is NULL. - Units are base-1024: 1 kB = 1024 bytes, 1 MB = 1,048,576 bytes, and so on. - Units use the PG-compat spellings `bytes`, `kB`, `MB`, `GB`, `TB`. - Negative values are formatted with a leading minus sign. - Deterministic and side effect free. ## Compatibility - PG-compat alias with the same output format.

Parameters

NameTypeDescription
sizeSpecifies the size in bytes to format as a human-readable string. Accepts any signed 64-bit integer.

Examples

-- Format a kilobyte
SELECT PG_SIZE_PRETTY(1024);  -- '1 kB'
-- Format a megabyte
SELECT PG_SIZE_PRETTY(1048576);  -- '1 MB'
-- Format a terabyte
SELECT PG_SIZE_PRETTY(1099511627776);  -- '1 TB'
-- Use alongside PG_COLUMN_SIZE in an information_schema report
SELECT column_name, PG_SIZE_PRETTY(PG_COLUMN_SIZE(column_name)) AS readable_size
FROM information_schema.columns
WHERE table_name = 'orders';
-- NULL propagation
SELECT PG_SIZE_PRETTY(NULL);  -- NULL

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →