Question: How do you determine the size of a PostgreSQL cluster?


In PostgreSQL, the term 'cluster' refers to a collection of databases managed by a single PostgreSQL server instance. Understanding the size of your PostgreSQL cluster can help in capacity planning and performance tuning. Here are methods to determine the size of a PostgreSQL cluster:

1. Total Size of All Databases

You can find the total size of all databases in the cluster using the following SQL query. This includes the size of the data files and excludes the WAL (Write-Ahead Logging) files.

SELECT pg_size_pretty(SUM(pg_database_size(datname))) AS total_size FROM pg_database;

2. Size of Each Database

To understand how each database contributes to the total cluster size, use this query:

SELECT datname, pg_size_pretty(pg_database_size(datname)) AS size FROM pg_database ORDER BY pg_database_size(datname) DESC;

3. Detailed Disk Usage

For a more detailed view, including individual tables within each database, connect to each database and run:

SELECT table_schema, table_name, pg_size_pretty(pg_total_relation_size('"' || table_schema || '"."' || table_name || '"')) AS size FROM information_schema.tables ORDER BY pg_total_relation_size('"' || table_schema || '"."' || table_name || '"') DESC;

This query will list the size of tables and indexes.

4. Using System Tools

Besides SQL queries, system tools like du (disk usage) can be used to measure the physical disk space used by the entire PostgreSQL data directory. This approach counts everything, including logs and temporary files.

du -sh /path/to/your/postgresql/data/directory

Replace /path/to/your/postgresql/data/directory with the actual path to your PostgreSQL data directory.

These methods provide a comprehensive view of your PostgreSQL cluster's storage utilization, helping in effective management and optimization.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book
Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.