Skip to content

Commit

Permalink
Show space usage for partitioned tables
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Feb 20, 2024
1 parent 6d1c688 commit 546bce4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 3.4.2 (unreleased)

- Added partitioned tables to space page
- Added materialized views to space page
- Fixed error with `slow_queries` method

Expand Down
9 changes: 7 additions & 2 deletions lib/pghero/methods/space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ def relation_sizes
SELECT
n.nspname AS schema,
c.relname AS relation,
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'm' then 'matview' ELSE 'index' END AS type,
CASE c.relkind
WHEN 'r' THEN 'table'
WHEN 'p' THEN 'partitioned table'
WHEN 'm' then 'matview'
ELSE 'index'
END AS type,
pg_table_size(c.oid) AS size_bytes
FROM
pg_class c
Expand All @@ -19,7 +24,7 @@ def relation_sizes
WHERE
n.nspname NOT IN ('pg_catalog', 'information_schema')
AND n.nspname !~ '^pg_toast'
AND c.relkind IN ('r', 'm', 'i')
AND c.relkind IN ('r', 'p', 'm', 'i')
ORDER BY
pg_table_size(c.oid) DESC,
2 ASC
Expand Down
3 changes: 3 additions & 0 deletions test/space_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ def test_relation_sizes
assert relation_sizes.find { |r| r[:relation] == "users" && r[:type] == "table" }
assert relation_sizes.find { |r| r[:relation] == "users_pkey" && r[:type] == "index" }
assert relation_sizes.find { |r| r[:relation] == "all_users" && r[:type] == "matview" }
assert relation_sizes.find { |r| r[:relation] == "partitioned_users" && r[:type] == "partitioned table" }
assert relation_sizes.find { |r| r[:relation] == "partitioned_users_1" && r[:type] == "table" }
assert relation_sizes.find { |r| r[:relation] == "partitioned_users_2" && r[:type] == "table" }
end

def test_table_sizes
Expand Down
11 changes: 11 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,14 @@ class User < ActiveRecord::Base
User.insert_all!(users)
ActiveRecord::Base.connection.execute("ANALYZE users")
ActiveRecord::Base.connection.execute("CREATE MATERIALIZED VIEW all_users AS SELECT * FROM users")
ActiveRecord::Base.connection.execute(<<~SQL)
CREATE TABLE partitioned_users (
city_id integer NOT NULL
) PARTITION BY RANGE (city_id);
CREATE TABLE partitioned_users_1 PARTITION OF partitioned_users
FOR VALUES FROM (1) TO (1000);
CREATE TABLE partitioned_users_2 PARTITION OF partitioned_users
FOR VALUES FROM (1001) TO (2000);
SQL

0 comments on commit 546bce4

Please sign in to comment.