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

  1. 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');
  1. 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');
  1. This query will return the size of the users table in bytes.
 pg_total_relation_size 
------------------------
                    556