Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show space usage for partitioned tables #490

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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