Skip to content

Commit 052ef98

Browse files
committed
Merge branch 'PGPRO-4755' into dev
2 parents 478c07f + b71555c commit 052ef98

File tree

3 files changed

+79
-44
lines changed

3 files changed

+79
-44
lines changed

mamonsu/plugins/pgsql/connections.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def run(self, zbx):
3636
if Pooler.is_bootstraped() and Pooler.bootstrap_version_greater('2.3.4'):
3737
result = Pooler.query(
3838
'select state, count(*) '
39-
'from mamonsu_get_connections_states() where state is not null group by state')
39+
'from public.mamonsu_get_connections_states() where state is not null group by state')
4040
else:
4141
result = Pooler.query(
4242
'select state, count(*) '

mamonsu/tools/bootstrap/sql.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
"""
66

7-
CreateSchemaSQL = """
7+
CreateSchemaDefaultSQL = """
88
CREATE TABLE IF NOT EXISTS public.mamonsu_config (
99
version text,
1010
inserted_at timestamp DEFAULT NOW()
@@ -90,17 +90,6 @@
9090
WHERE filename similar to '{2}'
9191
$$ LANGUAGE SQL SECURITY DEFINER;
9292
93-
CREATE EXTENSION IF NOT EXISTS pg_buffercache;
94-
95-
CREATE OR REPLACE FUNCTION public.mamonsu_buffer_cache()
96-
RETURNS TABLE(SIZE BIGINT, TWICE_USED BIGINT, DIRTY BIGINT) AS $$
97-
SELECT
98-
SUM(1) * (current_setting('block_size')::int8),
99-
SUM(CASE WHEN usagecount > 1 THEN 1 ELSE 0 END) * (current_setting('block_size')::int8),
100-
SUM(CASE isdirty WHEN true THEN 1 ELSE 0 END) * (current_setting('block_size')::int8)
101-
FROM public.pg_buffercache
102-
$$ LANGUAGE SQL SECURITY DEFINER;
103-
10493
CREATE OR REPLACE FUNCTION public.mamonsu_archive_command_files()
10594
RETURNS TABLE(COUNT_FILES BIGINT, SIZE_FILES NUMERIC) AS $$
10695
SELECT count(name) AS COUNT_FILES ,
@@ -133,7 +122,18 @@
133122
$$ LANGUAGE SQL SECURITY DEFINER;
134123
"""
135124

136-
GrantsOnSchemaSQL = """
125+
CreateSchemaExtensionSQL = """
126+
CREATE OR REPLACE FUNCTION public.mamonsu_buffer_cache()
127+
RETURNS TABLE(SIZE BIGINT, TWICE_USED BIGINT, DIRTY BIGINT) AS $$
128+
SELECT
129+
SUM(1) * (current_setting('block_size')::int8),
130+
SUM(CASE WHEN usagecount > 1 THEN 1 ELSE 0 END) * (current_setting('block_size')::int8),
131+
SUM(CASE isdirty WHEN true THEN 1 ELSE 0 END) * (current_setting('block_size')::int8)
132+
FROM public.pg_buffercache
133+
$$ LANGUAGE SQL SECURITY DEFINER;
134+
"""
135+
136+
GrantsOnDefaultSchemaSQL = """
137137
ALTER TABLE public.mamonsu_config OWNER TO {1};
138138
139139
ALTER TABLE public.mamonsu_timestamp_master_{0} OWNER TO {1};
@@ -150,8 +150,6 @@
150150
151151
GRANT EXECUTE ON FUNCTION public.mamonsu_count_{2}_files() TO {1};
152152
153-
GRANT EXECUTE ON FUNCTION public.mamonsu_buffer_cache() TO {1};
154-
155153
GRANT EXECUTE ON FUNCTION public.mamonsu_archive_command_files() TO {1};
156154
157155
GRANT EXECUTE ON FUNCTION public.mamonsu_archive_stat() TO {1};
@@ -164,3 +162,7 @@
164162
165163
GRANT EXECUTE ON FUNCTION public.mamonsu_count_{2}_lag_lsn() TO {1};
166164
"""
165+
166+
GrantsOnExtensionSchemaSQL = """
167+
GRANT EXECUTE ON FUNCTION public.mamonsu_buffer_cache() TO {1};
168+
"""

mamonsu/tools/bootstrap/start.py

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
from mamonsu import __version__ as mamonsu_version
1111
from mamonsu.lib.default_config import DefaultConfig
1212
from mamonsu.plugins.pgsql.pool import Pooler
13-
from mamonsu.tools.bootstrap.sql import CreateSchemaSQL, GrantsOnSchemaSQL, QuerySplit
13+
from mamonsu.tools.bootstrap.sql import CreateSchemaExtensionSQL, CreateSchemaDefaultSQL, \
14+
GrantsOnDefaultSchemaSQL, GrantsOnExtensionSchemaSQL, QuerySplit
1415

1516

1617
class Args(DefaultConfig):
@@ -145,6 +146,37 @@ def test_db(self, host_pre):
145146
return True
146147

147148

149+
def fill_query_params(queries):
150+
formatted_queries = ""
151+
for sql in queries.format(
152+
mamonsu_version,
153+
mamonsu_version.replace('.', '_'),
154+
'[0-9A-F]{24}',
155+
'wal' if Pooler.server_version_greater('10.0') else 'xlog',
156+
'wal_lsn' if Pooler.server_version_greater('10.0') else 'xlog_location',
157+
'waiting' if Pooler.server_version_less('9.6.0') else 'case when wait_event_type is null then false '
158+
' else true end as waiting',
159+
'flush_lag, replay_lag, write_lag,' if Pooler.server_version_greater('10.0') else '',
160+
'wal_lsn' if Pooler.server_version_greater('10.0') else 'xlog_location',
161+
'flush_lag INTERVAL, replay_lag INTERVAL, write_lag INTERVAL,' if Pooler.server_version_greater('10.0')
162+
else '',
163+
'lsn' if Pooler.server_version_greater('10.0') else 'location'
164+
).split(QuerySplit):
165+
formatted_queries += sql
166+
return formatted_queries
167+
168+
169+
def fill_grant_params(queries, args):
170+
formatted_grants_queries = ""
171+
for sql in queries.format(
172+
mamonsu_version.replace('.', '_'),
173+
args.args.mamonsu_username,
174+
'wal' if Pooler.server_version_greater('10.0') else 'xlog'
175+
).split(QuerySplit):
176+
formatted_grants_queries += sql
177+
return formatted_grants_queries
178+
179+
148180
def run_deploy():
149181
args = Args()
150182

@@ -159,37 +191,38 @@ def run_deploy():
159191
sys.exit(1)
160192

161193
try:
162-
for sql in CreateSchemaSQL.format(
163-
mamonsu_version,
164-
mamonsu_version.replace('.', '_'),
165-
'[0-9A-F]{24}',
166-
'wal' if Pooler.server_version_greater('10.0') else 'xlog',
167-
'wal_lsn' if Pooler.server_version_greater('10.0') else 'xlog_location',
168-
'waiting' if Pooler.server_version_less('9.6.0') else 'case when wait_event_type is null then false '
169-
' else true end as waiting',
170-
'flush_lag, replay_lag, write_lag,' if Pooler.server_version_greater('10.0') else '',
171-
'wal_lsn' if Pooler.server_version_greater('10.0') else 'xlog_location',
172-
'flush_lag INTERVAL, replay_lag INTERVAL, write_lag INTERVAL,' if Pooler.server_version_greater('10.0')
173-
else '',
174-
'lsn' if Pooler.server_version_greater('10.0') else 'location'
175-
).split(QuerySplit):
176-
if args.args.verbose:
177-
sys.stdout.write("\nExecuting query:\n{0}\n".format(sql))
178-
Pooler.query(sql)
194+
bootstrap_queries = fill_query_params(CreateSchemaDefaultSQL)
195+
Pooler.query(bootstrap_queries)
179196
except Exception as e:
180-
sys.stderr.write("Query:\n{0}\nerror: {1}\n".format(sql, e))
197+
sys.stderr.write("Bootstrap execution have exited with an error: {0}\n".format(e))
181198
sys.exit(2)
199+
200+
try:
201+
bootstrap_extension_queries = fill_query_params(CreateSchemaExtensionSQL)
202+
Pooler.query(bootstrap_extension_queries)
203+
except Exception as e:
204+
sys.stderr.write(
205+
"Bootstrap failed to create the function which required pg_buffercache extension.\n"
206+
"Error: {0}\n".format(e))
207+
sys.stderr.write("Please install pg_buffercache extension and rerun bootstrap "
208+
"if you want to get metrics from pg_buffercache view. \n")
209+
182210
try:
183-
for sql in GrantsOnSchemaSQL.format(
184-
mamonsu_version.replace('.', '_'),
185-
args.args.mamonsu_username,
186-
'wal' if Pooler.server_version_greater('10.0') else 'xlog'
187-
).split(QuerySplit):
188-
if args.args.verbose:
189-
sys.stdout.write("\nExecuting query:\n{0}\n".format(sql))
190-
Pooler.query(sql)
211+
bootstrap_grant_queries = fill_grant_params(GrantsOnDefaultSchemaSQL, args)
212+
Pooler.query(bootstrap_grant_queries)
213+
191214
except Exception as e:
192-
sys.stderr.write("Query:\n{0}\nerror: {1}\n".format(sql, e))
215+
sys.stderr.write("Error: \n {0}\n".format(e))
216+
sys.stderr.write("Please check mamonsu user permissions and rerun bootstrap.\n")
193217
sys.exit(2)
194218

219+
try:
220+
bootstrap_grant_extension_queries = fill_grant_params(GrantsOnExtensionSchemaSQL, args)
221+
Pooler.query(bootstrap_grant_extension_queries)
222+
223+
except Exception as e:
224+
sys.stderr.write("Bootstrap failed to grant execution permission to "
225+
"the function which required pg_buffercache extension.\n")
226+
sys.stderr.write("Error: \n {0}\n".format(e))
227+
195228
sys.stdout.write("Bootstrap successfully completed\n")

0 commit comments

Comments
 (0)