Get Schema Sizes
I got this from asking GitHub CoPilot. I did need to refine it further.
CoPilot Chat
I used the following prompt: Get the total amount of space in postgresql for a given schema.
and the response was:
To get the total amount of space used by a specific schema in PostgreSQL, you can use the following SQL query. This query sums the sizes of all tables (including indexes and TOAST data) within the specified schema:
| |
Explanation:
pg_class: Contains information about tables and indexes.pg_namespace: Contains schema names.pg_total_relation_size(): Returns the total size of a table including all indexes and TOAST data.pg_size_pretty(): Converts the size into a human-readable format (e.g., MB, GB).
Would you like a version of this query that includes size breakdowns by table as well?
I then asked: Can you convert the total size to a number?
Yes, you can convert the total size to a numeric value (in bytes) by removing the
pg_size_pretty() function from the query. Here’s the modified version:
| |
This will return the total size in bytes as a numeric value, which you can then convert to kilobytes, megabytes, or gigabytes as needed.
My Changes
As usual when using LLMs or even StackOverflow, it got me close. I am sure that I could keep asking CoPilot but I wanted to get the columns, not so much have it write all the code.
| |
Basically I wanted the total size but I wanted to sort by size in descending order. This got me the following result:
| |
I could do further refinement but this is good enough.