PostgreSQL
 SQL >> Base de données >  >> RDS >> PostgreSQL

Calculer la taille totale d’une table partitionnée dans PostgreSQL 9.0

Dans PostgreSQL, pg_relation_size retourne la taille d’une table. Pour une table partitionnée, elle ne compte pas les tables enfants. Il faut donc additionner leurs tailles via le catalogue pg_inherits.

postgres=# select pg_size_pretty(pg_relation_size('parent'));
 pg_size_pretty
----------------
 0 bytes

Créez une fonction pour obtenir la taille réelle :

CREATE OR REPLACE FUNCTION pg_partition_table_size(text)
RETURNS numeric AS
$$
SELECT sum(
  to_number(
    pg_size_pretty(pg_relation_size(inhrelid::regclass)),
    '999999999'
  )
)
FROM pg_inherits
WHERE inhparent = $1::regclass;
$$ LANGUAGE sql;

Utilisez ensuite la fonction avec la table parent :

SELECT pg_partition_table_size('parent');

 pg_partition_table_size
-------------------------
 16384

Cette approche donne la taille cumulée des partitions enfants.