Assess PostgreSQL Database Sizes Quickly and Easily
Quickly Assess Your PostgreSQL Database Landscape by Size
Managing multiple PostgreSQL databases requires an understanding of their storage footprint. This article introduces a simple yet effective PostgreSQL code snippet that helps you quickly determine the size of each database in your system, ordered by their storage consumption (descending).
Sample Code from Command Line
1SELECT
2 datname,
3 pg_size_pretty(pg_database_size(datname))
4FROM
5 pg_database
6ORDER
7 BY pg_database_size(datname) DESC;
Notes: PostgreSQL databases by size descending. Tested on PostgreSQL 8.4, 9.x, 10.x, 11.x, 12.x, 13.0.
Quickly Assess Your PostgreSQL Database Landscape by Size
Managing multiple PostgreSQL databases requires an understanding of their storage footprint. This article introduces a simple yet effective PostgreSQL code snippet that helps you quickly determine the size of each database in your system, ordered by their storage consumption (descending).
Code Purpose:
The provided code retrieves information about all databases within your PostgreSQL server and calculates their sizes. It then presents the results in a user-friendly format, allowing you to identify the largest databases and optimize storage allocation if necessary.
Code Breakdown:
- Data Retrieval:
pg_database: This system catalog table stores information about all databases in the server.
- Selection:
datname: This column retrieves the name of each database.pg_size_pretty(pg_database_size(datname)): This function calculates the size of each database in bytes and then converts it into a human-readable format (e.g., megabytes, gigabytes).
- Ordering:
ORDER BY pg_database_size(datname) DESC: This clause sorts the results in descending order based on the database size, with the largest database appearing first.
Key Points and Insights:
- This code provides a quick overview of your PostgreSQL database storage usage.
- Identifying the largest databases can help you prioritize optimization efforts, such as archiving old data or implementing data partitioning strategies.
- The human-readable size format (
pg_size_pretty) makes it easier to interpret the results without needing to convert bytes to larger units manually.
Explanation of Important Parts:
pg_database_size(datname): This function calculates the total size of a specific database (identified bydatname) in bytes.pg_size_pretty(size): This function takes the database size in bytes (size) and converts it into a human-readable format with units like megabytes (MB) or gigabytes (GB).
Conclusion:
By incorporating this code snippet into your PostgreSQL management routine, you can gain valuable insights into your database storage utilization. This awareness empowers you to make informed decisions about storage allocation and database optimization strategies.
References:
pg_database system catalog: https://www.postgresql.org/docs/current/catalogs.html
pg_size_pretty function: https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADMIN-DBOBJECT
Source:
https://github.com/HariSekhon/SQL-scripts/blob/master/postgres_databases_by_size.sql
Posts in this series
- How Many Connections Can Your PostgreSQL Database Handle?
- PostgreSQL Backend Connections via pg_stat_database
- Identifying Blocking PostgreSQL Queries using pg_stat_activity
- List PostgreSQL Databases by Size with Access Check
- Assess PostgreSQL Database Sizes Quickly and Easily
- Unveiling Your PostgreSQL Server - A Diagnostic Powerhouse
- Keep Your PostgreSQL Database Clean, Identify Idle Connections
- Query the PostgreSQL Configuration
- PostgreSQL Recovery Monitoring: Essential SQL Insights
- Restart All PostgreSQL Sequences with ALTER SEQUENCE
- Monitor Running Queries in PostgreSQL using pg_stat_activity
- Monitor PostgreSQL Active Sessions with pg_stat_activity
- PostgreSQL Error Handling Settings via pg_settings
- PostgreSQL File Location Settings Query via pg_settings
- PostgreSQL Lock Management Settings via pg_settings
- PostgreSQL Logging Configuration Query via pg_settings
- Monitor PostgreSQL Memory Settings with pg_settings
- PostgreSQL Table Row Count Estimates with SQL
- List PostgreSQL Tables by Size with SQL
- PostgreSQL WAL Settings Query Guide
- PostgreSQL SSL Settings Query Guide
- PostgreSQL Statistics Settings Query Guide
- PostgreSQL Resource Settings Query Guide
- PostgreSQL Replication Settings Query Guide
- PostgreSQL Query Planning Settings Query Guide
- PostgreSQL Preset Options Settings Query Guide
- PostgreSQL Miscellaneous Settings Query Guide
- Count PostgreSQL Sessions by State with SQL
- Kill Idle PostgreSQL Sessions with SQL
- Grant SELECT on All Tables in PostgreSQL
- Identify Insert-Only Tables in PostgreSQL
- Detect Soft Delete Patterns in PostgreSQL
- List PostgreSQL Object Comments with SQL
- List Foreign Key Constraints in PostgreSQL
- List PostgreSQL Enum Types and Their Values with SQL
- List All Views in a PostgreSQL Database with SQL
- Find PostgreSQL Tables Without a Primary Key
- List PostgreSQL Partitioned Tables with SQL
- List All Schemas in Your PostgreSQL Database
- PostgreSQL Database Statistics with pg_stat_database
- List PostgreSQL Roles and Their Privileges
- Scrubbing Email PII in PostgreSQL for GDPR Compliance
- List Installed Extensions in PostgreSQL
- List Collations in Your PostgreSQL Database
- PostgreSQL Replica Identity for Logical Replication