diff --git a/README.md b/README.md index 0e63093..81507eb 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,7 @@ _321 TILs and counting..._ - [List Database Users](postgres/list-database-users.md) - [Max Identifier Length Is 63 Bytes](postgres/max-identifier-length-is-63-bytes.md) - [pg Prefix Is Reserved For System Schemas](postgres/pg-prefix-is-reserved-for-system-schemas.md) +- [Pretty Print Data Sizes](postgres/pretty-print-data-sizes.md) - [Restart A Sequence](postgres/restart-a-sequence.md) - [Restarting Sequences When Truncating Tables](postgres/restarting-sequences-when-truncating-tables.md) - [Send A Command To psql](postgres/send-a-command-to-psql.md) diff --git a/postgres/pretty-print-data-sizes.md b/postgres/pretty-print-data-sizes.md new file mode 100644 index 0000000..7e2e4e4 --- /dev/null +++ b/postgres/pretty-print-data-sizes.md @@ -0,0 +1,38 @@ +# Pretty Print Data Sizes + +Use the `pg_size_pretty()` function to pretty print the sizes of data. Given +a `bigint`, it will determine the most human-readable format with which to +print the value: + +```sql +> select pg_size_pretty(1234::bigint); + pg_size_pretty +---------------- + 1234 bytes + +> select pg_size_pretty(123456::bigint); + pg_size_pretty +---------------- + 121 kB + +> select pg_size_pretty(1234567899::bigint); + pg_size_pretty +---------------- + 1177 MB + +> select pg_size_pretty(12345678999::bigint); + pg_size_pretty +---------------- + 11 GB +``` + +This function is particularly useful when used with the +[`pg_database_size()`](get-the-size-of-a-database.md) and +[`pg_relation_size()`](get-the-size-of-a-table.md) functions. + +```sql +> select pg_size_pretty(pg_database_size('hr_hotels')); + pg_size_pretty +---------------- + 12 MB +```