postgresql_16:3.6.27.7 Database Object Management Functions
PostgreSQL 16 > The SQL Language > Functions and Operators > System Administration Functions > Database Object Management Functions
The functions shown in (see functions-admin-dbsize) calculate the disk space usage of database objects, or assist in presentation or understanding of usage results. bigint results are measured in bytes. If an OID that does not represent an existing object is passed to one of these functions, NULL is returned.
| Function Description | | --- | | pg_column_size ( "any" ) integer Shows the number of bytes used to store any individual data value. If applied directly to a table column value, this reflects any compression that was done. | | pg_column_compression ( "any" ) text Shows the compression algorithm that was used to compress an individual variable-length value. Returns NULL if the value is not compressed. | | pg_database_size ( name ) bigint pg_database_size ( oid ) bigint Computes the total disk space used by the database with the specified name or OID. To use this function, you must have CONNECT privilege on the specified database (which is granted by default) or have privileges of the pg_read_all_stats role. | | pg_indexes_size ( regclass ) bigint Computes the total disk space used by indexes attached to the specified table. | | pg_relation_size ( relation regclass , fork text ) bigint Computes the disk space used by one “fork” of the specified relation. (Note that for most purposes it is more convenient to use the higher-level functions pg_total_relation_size or pg_table_size, which sum the sizes of all forks.) With one argument, this returns the size of the main data fork of the relation. The second argument can be provided to specify which fork to examine: main returns the size of the main data fork of the relation. fsm returns the size of the Free Space Map (see (see postgresql_16:8.22.3)) associated with the relation. vm returns the size of the Visibility Map (see (see postgresql_16:8.22.4)) associated with the relation. init returns the size of the initialization fork, if any, associated with the relation. | | pg_size_bytes ( text ) bigint Converts a size in human-readable format (as returned by pg_size_pretty) into bytes. Valid units are bytes, B, kB, MB, GB, TB, and PB. | | pg_size_pretty ( bigint ) text pg_size_pretty ( numeric ) text Converts a size in bytes into a more easily human-readable format with size units (bytes, kB, MB, GB, TB, or PB as appropriate). Note that the units are powers of 2 rather than powers of 10, so 1kB is 1024 bytes, 1MB is 10242 = 1048576 bytes, and so on. | | pg_table_size ( regclass ) bigint Computes the disk space used by the specified table, excluding indexes (but including its TOAST table if any, free space map, and visibility map). | | pg_tablespace_size ( name ) bigint pg_tablespace_size ( oid ) bigint Computes the total disk space used in the tablespace with the specified name or OID. To use this function, you must have CREATE privilege on the specified tablespace or have privileges of the pg_read_all_stats role, unless it is the default tablespace for the current database. | | pg_total_relation_size ( regclass ) bigint Computes the total disk space used by the specified table, including all indexes and TOAST data. The result is equivalent to pg_table_size + pg_indexes_size. |
The functions above that operate on tables or indexes accept a regclass argument, which is simply the OID of the table or index in the pg_class system catalog. You do not have to look up the OID by hand, however, since the regclass data type's input converter will do the work for you. See (see postgresql_16:3.5.19) for details.
The functions shown in (see functions-admin-dblocation) assist in identifying the specific disk files associated with database objects.
| Function Description | | --- | | pg_relation_filenode ( relation regclass ) oid Returns the “filenode” number currently assigned to the specified relation. The filenode is the base component of the file name(s) used for the relation (see (see postgresql_16:8.22.1) for more information). For most relations the result is the same as pg_class.relfilenode, but for certain system catalogs relfilenode is zero and this function must be used to get the correct value. The function returns NULL if passed a relation that does not have storage, such as a view. | | pg_relation_filepath ( relation regclass ) text Returns the entire file path name (relative to the database cluster's data directory, PGDATA) of the relation. | | pg_filenode_relation ( tablespace oid, filenode oid ) regclass Returns a relation's OID given the tablespace OID and filenode it is stored under. This is essentially the inverse mapping of pg_relation_filepath. For a relation in the database's default tablespace, the tablespace can be specified as zero. Returns NULL if no relation in the current database is associated with the given values, or if dealing with a temporary relation. |
(see functions-admin-collation) lists functions used to manage collations.
| Function Description | | --- | | pg_collation_actual_version ( oid ) text Returns the actual version of the collation object as it is currently installed in the operating system. If this is different from the value in pg_collation.collversion, then objects depending on the collation might need to be rebuilt. See also (see postgresql_16:7.1.3). | | pg_database_collation_actual_version ( oid ) text Returns the actual version of the database's collation as it is currently installed in the operating system. If this is different from the value in pg_database.datcollversion, then objects depending on the collation might need to be rebuilt. See also (see postgresql_16:7.1.5). | | pg_import_system_collations ( schema regnamespace ) integer Adds collations to the system catalog pg_collation based on all the locales it finds in the operating system. This is what initdb uses; see (see postgresql_16:4.9.2.2) for more details. If additional locales are installed into the operating system later on, this function can be run again to add collations for the new locales. Locales that match existing entries in pg_collation will be skipped. (But collation objects based on locales that are no longer present in the operating system are not removed by this function.) The schema parameter would typically be pg_catalog, but that is not a requirement; the collations could be installed into some other schema as well. The function returns the number of new collation objects it created. Use of this function is restricted to superusers. |
(see functions-info-partition) lists functions that provide information about the structure of partitioned tables.
| Function Description | | --- | | pg_partition_tree ( regclass ) setof record ( relid regclass, parentrelid regclass, isleaf boolean, level integer ) Lists the tables or indexes in the partition tree of the given partitioned table or partitioned index, with one row for each partition. Information provided includes the OID of the partition, the OID of its immediate parent, a boolean value telling if the partition is a leaf, and an integer telling its level in the hierarchy. The level value is 0 for the input table or index, 1 for its immediate child partitions, 2 for their partitions, and so on. Returns no rows if the relation does not exist or is not a partition or partitioned table. | | pg_partition_ancestors ( regclass ) setof regclass Lists the ancestor relations of the given partition, including the relation itself. Returns no rows if the relation does not exist or is not a partition or partitioned table. | | pg_partition_root ( regclass ) regclass Returns the top-most parent of the partition tree to which the given relation belongs. Returns NULL if the relation does not exist or is not a partition or partitioned table. |
For example, to check the total size of the data contained in a partitioned table measurement, one could use the following query: SELECT pg_size_pretty(sum(pg_relation_size(relid))) AS total_size FROM pg_partition_tree('measurement');