1. Application scenarios
Scenario 1: Check the size of the database footprint
SELECT pg_size_pretty(pg_database_size('database_name'));
Scene 2: View the size of space occupied by each table
SELECT table_schema || '.' || table_name AS table, #Table data only pg_size_pretty(pg_relation_size(table_schema || '.' || table_name)) AS size #Table data + index data #pg_size_pretty(pg_total_relation_size(table_schema || '.' || table_name)) AS size FROM information_schema.tables WHERE table_schema = 'public' ORDER BY pg_relation_size(table_schema || '.' || table_name) DESC; #pg_total_relation_size(table_schema || '.' || table_name) DESC;
To view the specific table occupancy size, available:
#Table data onlySELECT pg_size_pretty(pg_relation_size('schemal_test.table_test')); #Table data + index dataSELECT pg_size_pretty(pg_total_relation_size('schemal_test.table_test'));
2. What is the size of PostgreSQL space?
Table Space
#Find postgresql tablespace sizeSELECT pg_size_pretty (pg_tablespace_size ('tablespace_name')); #Name and size of all tablespacesSELECT spcname, pg_size_pretty(pg_tablespace_size(spcname)) as size from pg_tablespace;
Database
#Find a single postgresql database sizeSELECT pg_size_pretty(pg_database_size('db_name')); #Total size of all databases, displayed in easy-to-read formatSELECT pg_size_pretty(SUM(pg_database_size(datname))) FROM pg_database; #View the list of all databases and their sizes (in GB), descending orderSELECT pg_database.datname as db_name, pg_database_size(pg_database.datname)/1024/1024/1024 as db_size FROM pg_database ORDER by db_size DESC; # or pg_size_pretty with modified sizeSELECT pg_database.datname as db_name, pg_size_pretty(pg_database_size(pg_database.datname)) as db_size FROM pg_database ORDER by pg_database_size(pg_database.datname) DESC; #View all database names, owners, and their respective sizesSELECT AS db_name, pg_catalog.pg_get_userbyid() AS owner, CASE WHEN pg_catalog.has_database_privilege(, 'CONNECT') THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size()) ELSE 'No Access' END AS size FROM pg_catalog.pg_database db ORDER BY CASE WHEN pg_catalog.has_database_privilege(, 'CONNECT') THEN pg_catalog.pg_database_size() END;
Mode (Schema)
#The size of all tables in the current schema or any schema, the size of the table-related objects, and the total table sizeSELECT as table_name, pg_size_pretty(pg_relation_size()) as table_size, pg_size_pretty(pg_total_relation_size() - pg_relation_size()) as external_size, pg_size_pretty(pg_total_relation_size()) as total_table_size, stats.n_live_tup as live_rows FROM pg_catalog.pg_statio_user_tables as statios JOIN pg_stat_user_tables as stats USING (relname) WHERE = 'schema_name' -- Replace with pattern name UNION ALL SELECT 'TOTAL' as table_name, pg_size_pretty(sum(pg_relation_size())) AS table_size, pg_size_pretty(sum(pg_total_relation_size() - pg_relation_size())) AS external_size, pg_size_pretty(sum(pg_total_relation_size())) AS total_table_size, sum(stats.n_live_tup) AS live_rows FROM pg_catalog.pg_statio_user_tables AS statios JOIN pg_stat_user_tables AS stats USING (relname) WHERE = 'schema_name' -- Replace with pattern name ORDER BY live_rows ASC;
Table-Relation
#View the single table size of the postgresql database - excluding dependency size:SELECT pg_size_pretty(pg_relation_size('schema_test.table_name')); #SELECT pg_size_pretty(pg_relation_size('table_name')); #View the single table size of the postgresql database - including the dependency size:SELECT pg_size_pretty(pg_total_relation_size('schema_test.table_name')); #SELECT pg_size_pretty(pg_total_relation_size('table_name')); #Find the size of each table in the current database, including the indexSELECT table_schema || '.' || table_name as table_name, pg_size_pretty(pg_total_relation_size('"' || table_schema || '"."' || table_name || '"')) as table_size FROM information_schema.tables ORDER BY pg_total_relation_size('"' || table_schema || '"."' || table_name || '"') DESC #Find each table and index size in the current database, including indexSELECT table_name, pg_size_pretty(pg_table_size(table_name)) as table_size, pg_size_pretty(pg_indexes_size(table_name)) as index_size, pg_size_pretty(pg_total_relation_size(table_name)) as total_size FROM ( select ('"' || table_schema || '"."' || table_name || '"') as table_name FROM information_schema.tables) as tables ORDER BY 4 DESC #View table size and dependency sizeSELECT schemaname as schema_name, relname as table_name, pg_size_pretty(pg_total_relation_size(relid)) as table_size, pg_size_pretty(pg_total_relation_size(relid) - pg_relation_size(relid)) as external_size FROM pg_catalog.pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC; #View row count of all tablesselect relname as table_name, reltuples as rows from pg_class where relkind = ‘r' order by rowCounts desc
Index (Index-Relation)
#single index size of postgresql database:SELECT pg_size_pretty(pg_indexes_size('index_name')); #List the size of each index in the databaseSELECT indexrelname, pg_size_pretty(pg_relation_size(relid)) from pg_stat_user_indexes;
Column
#PostgreSQL column value size, to find how much space is needed to store a specific value, you can use the pg_column_size() function, for example:select pg_column_size(5::smallint); select pg_column_size(5::int); select pg_column_size(5::bigint);
#Get OIDselect * from pg_class where relname='table_name'; select oid, datname from pg_database; #View file addressselect pg_relation_filepath('table_name');
Function description
Function name | Return type | describe |
pg_column_size(any) | int | Stores the number of bytes required for a specified value (maybe compressed) |
pg_database_size(oid) | bigint | Specify the disk space used by the database for the OID |
pg_database_size(name) | bigint | The disk space used by the database with the specified name |
pg_indexes_size(regclass) | bigint | Total disk space used for the table index associated with the specified table OID or table name |
pg_relation_size(relation regclass, fork text) | bigint | Specify the table or index of OID or name, by specifying the disk space used by fork('main', 'fsm' or 'vm') |
pg_relation_size(relation regclass) | bigint | abbreviation of pg_relation_size(..., 'main') |
pg_size_pretty(bigint) | text | Convert bytes to a human-readable unit |
pg_size_pretty(numeric) | text | Convert bytes to a human-readable unit |
pg_table_size(regclass) | bigint | Specify the disk space used by the table OID or table name, remove the index (but contains TOAST, free space maps, and visual maps) |
pg_tablespace_size(oid) | bigint | Specify the disk space used by the tablespace of the OID |
pg_tablespace_size(name) | bigint | The disk space used by the tablespace with the specified name |
pg_total_relation_size(regclass) | bigint | Specifies the total disk space used by the table OID or table name, including all indexes and TOAST data |
oid obtain
#Get the OID of the data tableselect oid,relname from pg_class where relname='table_name'; #Get the database OIDselect oid, datname from pg_database; #Get the file path of the data tableselect pg_relation_filepath('table_name');
Summarize
This is the article about how PostgreSQL view the size of data occupancy in databases and tables. For more related PostgreSQL to view the size of data occupancy in data, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!