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', '[email protected]'),
    ('jane_smith', '[email protected]'),
    ('alice_smith', '[email protected]'),
    ('bob_jones', '[email protected]'),
    ('susan_wilson', '[email protected]'),
    ('michael_jackson', '[email protected]'),
    ('lisa_johnson', '[email protected]'),
    ('david_smith', '[email protected]');
  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