Find the disk size of a PostgreSQL table and its indexes
This article will discuss how to find the disk size of a PostgreSQL table and its indexes.
Find the disk size of PostgreSQL tables and databases using PSQL
You can use \l+
to view the database size and use to \d+
show table size. But before that, you need to log in to the database to execute the query.
Here are the commands and output for displaying table and database sizes in Postgres:
postgres=# \l+
Output:
postgres-# \d+
List of relations
Schema | Name | Type | Owner | Persistence | Access method | Size | Description
--------+------------------+----------+----------+-------------+---------------+------------+-------------
public | book_lends | table | postgres | permanent | heap | 16 kB |
public | books | table | postgres | permanent | heap | 16 kB |
public | employee | table | postgres | permanent | heap | 16 kB |
public | employee_id_seq | sequence | postgres | permanent | | 8192 bytes |
public | events | table | postgres | permanent | heap | 16 kB |
public | mock_data | table | postgres | permanent | heap | 48 kB |
public | product | table | postgres | permanent | heap | 16 kB |
public | product_id_seq | sequence | postgres | permanent | | 8192 bytes |
public | products | table | postgres | permanent | heap | 16 kB |
public | products_id_seq | sequence | postgres | permanent | | 8192 bytes |
public | prroducts | table | postgres | permanent | heap | 8192 bytes |
public | prroducts_id_seq | sequence | postgres | permanent | | 8192 bytes |
public | stores | table | postgres | permanent | heap | 16 kB |
public | stores_id_seq | sequence | postgres | permanent | | 8192 bytes |
public | users | table | postgres | permanent | heap | 16 kB |
(15 rows)
Here it shows all the tables we have in the Postgres database along with their name, type, owner, size, access method, etc.
Find the size of the largest table in a database
This is a code snippet written by Postgres officials to display table sizes in descending order.
SELECT nspname || '.' || relname AS "relation",
pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size"
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname NOT IN ('pg_catalog', 'information_schema')
AND C.relkind <> 'i'
AND nspname !~ '^pg_toast'
ORDER BY pg_total_relation_size(C.oid) DESC
LIMIT 10;
Output:
relation | total_size
-------------------+------------
public.mock_data | 48 kB
public.product | 32 kB
public.products | 32 kB
public.books | 32 kB
public.book_lends | 32 kB
public.employee | 32 kB
public.stores | 32 kB
public.users | 32 kB
public.events | 16 kB
public.prroducts | 16 kB
(10 rows)
postgres
We searched for the 10 largest tables under
in the database .
You can click on the following link to know more about disk size queries in Postgres. You will find SQL queries for finding largest cluster, largest relation, partitioned table etc.
More on database size here .
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Terminate the PostgreSQL connection
Publish Date:2025/04/11 Views:199 Category:PostgreSQL
-
In this article, we will learn how to terminate a PostgreSQL session. Any open connections are run by background processes or tasks, PSQL which may no longer exist despite exiting the user interface or command line tool. Use ps -ef or grep
Single query to rename and change column type in PostgreSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
This article describes how to rename a column and change its type in PostgreSQL using only a single query. Renaming and changing column types in MySQL In MySQL , if you want to change the column type and rename it, you can use a simple stat
Joining columns using Select in PostgreSQL
Publish Date:2025/04/11 Views:176 Category:PostgreSQL
-
MySQL PostgreSQL is an object-relational database system, which means it can support more complex data types than its competitors . Today we will learn how to use SELECT the operator to join the columns of a table. Using operators to || joi
Using CASE in PostgreSQL
Publish Date:2025/04/11 Views:124 Category:PostgreSQL
-
This article shows how to use the statement in PostgreSQL CASE . CASE How to use the statement in PostgreSQL case Statements are similar to those in general-purpose programming languages if-else . But in SQL, if you want to write IF-ELSE ,
Using NOT IN with subqueries in PostgreSQL
Publish Date:2025/04/11 Views:93 Category:PostgreSQL
-
NOT IN The inverts the result of NOT simply using IN the operator. NOT IN The right side of the operator must have a subquery in which multiple columns are returned to check whether the expression matches the data. NOT IN Tends to return tr
Using variables in PostgreSQL
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article will demonstrate how we can declare and assign values to variables in PostgreSQL. In PostgreSQL, DECLARE variables are declared using Often you will need variables in your PL/SQL scripts. In DECLARE the section called , y
Connect to PostgreSQL using a password
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article shows various ways to connect to PostgreSQL using a password. It can be through the command line, pgpass a file, PGPASSWORD an environment variable or a connection string. Connecting to PostgreSQL with a password using the comm
Deleting a database in PostgreSQL via PSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
There are two ways to access PostgreSQL objects and databases on your system. One is through an interface, such as a graphical interface like PGADMIN, and the other is the basic command line tool psql. Today, we will look at DROP DATABASE t
Using the database in PostgreSQL
Publish Date:2025/04/11 Views:132 Category:PostgreSQL
-
This article demonstrates connecting to a database, creating a new database, and creating a table in PostgreSQL. Available databases in PostgreSQL You can run the following command after opening the Postgres command line to view all availab