Message board

Normal messages

Query to show how partitioned tables are balanced. Show count of the partitions Show less

JT wrote: 🕐 03-19-25 17:47

After partitioning a table in Postgres, it's nice to see how the data is distributed across all the partitions.  The following query will generate the count of all partitioned tables.

 

Make sure to modify the parent table name to meet your needs.

 

 

 

SELECT child.relname AS partition, n_live_tup AS count
  FROM pg_inherits AS i
  JOIN pg_class AS child ON child.oid = i.inhrelid
  JOIN pg_stat_user_tables AS stat ON stat.relid = child.oid
  WHERE i.inhparent = 'photo_licenses'::regclass;

 

    partition     | count   
------------------+--------
photo_licenses_12 | 800851
photo_licenses_13 | 799392
photo_licenses_17 | 799514
photo_licenses_14 | 800551
photo_licenses_15 | 800597
photo_licenses_16 | 798680
photo_licenses_18 | 798678
photo_licenses_19 | 800969
photo_licenses_1  | 800695
photo_licenses_2  | 801663
photo_licenses_3  | 800210
photo_licenses_4  | 800526
photo_licenses_5  | 799994
photo_licenses_6  | 799539
photo_licenses_9  | 798588
photo_licenses_7  | 799069
photo_licenses_8  | 800763
photo_licenses_10 | 800819
photo_licenses_11 | 799641
photo_licenses_20 | 800020
photo_licenses_21 | 800410
photo_licenses_22 | 798471
photo_licenses_23 | 799807
photo_licenses_24 | 800609
photo_licenses_25 | 799954
(25 rows)

Show less
Copy this message link
Sticky messages
Generate partitions on the fly
🕐 02-28-25 12:23
169 Views
Replies
this is a test
🕐 03-31-25 22:05
96 Views
Replies
PostgreSQL Bloat explained
🕐 02-24-25 21:17
127 Views
Replies