How calculate per tenant disk usage in a multitenant database with tenant_id columns in postgresql I am using Django and postgres for a multi-tenant webapp. We have around 100 tables and each table has a tenant_id column, identifying the tenant of that particular row. I need a solution that would calculate the total space per tenant. Is there a possibility of doing that? And can it be done dynamically?

For the background: we need that so as to limit per user disc space upto a limit of X GB. 

Thank you.
Assuming that the average size of a single row is somewhat comparable between tenants, something like the following:

select tenant_id,
       pg_total_relation_size('the_table')::decimal * (count(*)::decimal / (select count(*) from the_table)) as approx_tenant_size
from the_table
group by tenant_id;


That will calculate size per tenant based on the percentage of rows for that tenant. 

This will however return completely wrong estimates if you have large text or varchar columns and some tenants only store short values in there and some really large values.