Christopher R. Bilger
March 11th, 2025
Using PostgreSQL's native functions, we can easily find our database's size when stored on disk. This includes indices and other relations which increase disk space beyond that of the data itself.
SELECT pg_size_pretty(pg_database_size('<dbname>'));
As of today, our current size on disk is ~62 GB. This includes all data, indices, and other relations.
Size Category | Range |
---|---|
Very Small | <1 GB |
Small | 1 GB - 100 GB |
Medium | 100 GB - 1 TB |
Large | 1 TB - 10 TB |
Very Large | >10 TB |
SELECT
*
FROM
users
WHERE
created_at >= '2025-01-01'
AND created_at < '2025-02-01'
AND status = 'active';
CREATE INDEX idx_users_created_at_status
ON users (created_at, status);
SELECT
*
FROM
users
WHERE
created_at >= '2025-01-01'
AND created_at < '2025-02-01'
AND status = 'active';
EXPLAIN ANALYZE are a set of two PostgreSQL options that can be used to obtain a query execution plan and the actual execution time of each node in the plan.
EXPLAIN will show the query plan, while ANALYZE will actually execute the query and show the actual execution time.
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
department VARCHAR(50),
salary INT
);
SELECT * FROM employees WHERE department = 'Engineering';
EXPLAIN ANALYZE SELECT * FROM employees WHERE department = 'Engineering';
Seq Scan on employees (cost=0.00..15.35 rows=5 width=100) (actual time=0.015..0.018 rows=3 loops=1)
Filter: (department = 'Engineering'::text)
Rows Removed by Filter: 97
Planning Time: 0.125 ms
Execution Time: 0.057 ms
Using the information returned from EXPLAIN ANALYZE, we can see that the query is performing a sequential scan on the employees table. This is not ideal for performance as it is scanning the entire table.
CREATE INDEX idx_department ON employees(department);