📜  mb 中的 pg_relation_size (1)

📅  最后修改于: 2023-12-03 15:02:54.952000             🧑  作者: Mango

pg_relation_size in PostgreSQL

pg_relation_size is a function in PostgreSQL that returns the size of a relation in bytes. A relation can be a table, view, or index. This function can be used to monitor the size of a relation and help identify if a table or index is growing excessively.

Syntax

The syntax for pg_relation_size is as follows:

pg_relation_size(relation regclass) RETURNS bigint

The relation parameter is the name of the relation for which the size is to be returned. It can be specified as a string or a regclass.

Example Usage
SELECT pg_relation_size('public.my_table'); -- returns size of my_table in bytes
SELECT pg_relation_size(indexname) AS index_size 
FROM pg_index 
JOIN pg_class ON pg_index.indexrelid = pg_class.oid 
WHERE tablename = 'my_table';
Additional Notes
  • The value returned by pg_relation_size includes any related indexes or toast tables.
  • The size of a table is not the same as the amount of disk space used to store it. The space used by a table includes the data, any indexes, and additional metadata. PostgreSQL uses a variety of techniques to minimize disk space usage, including compression and optimized storage.
  • pg_total_relation_size can be used to compute the total disk space used by a table, including indexes and toast tables.