Skip to content

Commit bb9f502

Browse files
committed
execute amcheck before doing backups
1 parent 0bf379d commit bb9f502

4 files changed

Lines changed: 94 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Checks
2+
module Candlepin
3+
class DBIndex < ForemanMaintain::Check
4+
metadata do
5+
description 'Make sure Candlepin DB indexes are OK'
6+
label :candlepin_db_index
7+
tags :db_index
8+
for_feature :candlepin_database
9+
confine do
10+
feature(:candlepin_database)&.local?
11+
end
12+
end
13+
14+
def run
15+
status, output = feature(:candlepin_database).amcheck
16+
17+
assert(status == 0, "Candlepin DB indexes have issues:\n#{output}")
18+
end
19+
end
20+
end
21+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Checks
2+
module Foreman
3+
class DBIndex < ForemanMaintain::Check
4+
metadata do
5+
description 'Make sure Foreman DB indexes are OK'
6+
label :foreman_db_index
7+
tags :db_index
8+
for_feature :foreman_database
9+
confine do
10+
feature(:foreman_database)&.local?
11+
end
12+
end
13+
14+
def run
15+
status, output = feature(:foreman_database).amcheck
16+
17+
assert(status == 0, "Foreman DB indexes have issues:\n#{output}")
18+
end
19+
end
20+
end
21+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Checks
2+
module Pulpcore
3+
class DBIndex < ForemanMaintain::Check
4+
metadata do
5+
description 'Make sure Pulpcore DB indexes are OK'
6+
label :pulpcore_db_index
7+
tags :db_index
8+
for_feature :pulpcore_database
9+
confine do
10+
feature(:pulpcore_database)&.local?
11+
end
12+
end
13+
14+
def run
15+
status, output = feature(:pulpcore_database).amcheck
16+
17+
assert(status == 0, "Pulpcore DB indexes have issues:\n#{output}")
18+
end
19+
end
20+
end
21+
end

lib/foreman_maintain/concerns/base_database.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,37 @@ def raise_psql_missing_error
140140
' Make sure system has psql utility installed.'
141141
end
142142

143+
def amcheck
144+
return unless local?
145+
146+
# creating the extension and executing the check requires superuser privileges
147+
148+
psql_cmd = "runuser - postgres -c 'psql --set=ON_ERROR_STOP=on #{configuration['database']}'"
149+
150+
amcheck_create = 'CREATE EXTENSION IF NOT EXISTS amcheck;'
151+
152+
execute!(psql_cmd, :stdin => amcheck_create)
153+
154+
amcheck_query = <<~SQL
155+
SELECT bt_index_check(index => c.oid, heapallindexed => i.indisunique),
156+
c.relname,
157+
c.relpages
158+
FROM pg_index i
159+
JOIN pg_opclass op ON i.indclass[0] = op.oid
160+
JOIN pg_am am ON op.opcmethod = am.oid
161+
JOIN pg_class c ON i.indexrelid = c.oid
162+
JOIN pg_namespace n ON c.relnamespace = n.oid
163+
WHERE am.amname = 'btree' AND n.nspname = 'public'
164+
-- Don't check temp tables, which may be from another session:
165+
AND c.relpersistence != 't'
166+
-- Function may throw an error when this is omitted:
167+
AND c.relkind = 'i' AND i.indisready AND i.indisvalid
168+
ORDER BY c.relpages DESC;
169+
SQL
170+
171+
execute_with_status(psql_cmd, :stdin => amcheck_query)
172+
end
173+
143174
private
144175

145176
def base_env

0 commit comments

Comments
 (0)