Other Functions
PG_TOTAL_RELATION_SIZE
Overview
The pg_total_relation_size
function allows you to retrieve the size of a table. This function is useful for monitoring the storage requirements.
Syntax
The syntax for the pg_total_relation_size
function is as follows:
pg_total_relation_size('relation_name');
Here, relation_name is the name of the table for which you want to determine the size.
Output
The pg_total_relation_size
function returns the size of the specified table in bytes.
Example
- First, let’s assume we have a table named **users. **Use the below query to create the table.
CREATE TABLE users (
username TEXT,
email TEXT
);
INSERT INTO users (username, email) VALUES
('john_doe', 'john.doe@example.com'),
('jane_smith', 'jane.smith@example.com'),
('alice_smith', 'alice.smith@example.com'),
('bob_jones', 'bob.jones@example.com'),
('susan_wilson', 'susan.wilson@example.com'),
('michael_jackson', 'michael.jackson@example.com'),
('lisa_johnson', 'lisa.johnson@example.com'),
('david_smith', 'david.smith@example.com');
- In this case, we will use the
pg_total_relation_size
function to determine the size of a users table.
SELECT pg_total_relation_size('users');
- This query will return the size of the users table in bytes.
pg_total_relation_size
------------------------
556