Skip to content

Commit 46b9e7f

Browse files
committed
feat: run boostrap queries in one transaction and queries for extension in separate transaction.
1 parent 30f08ad commit 46b9e7f

File tree

2 files changed

+82
-43
lines changed

2 files changed

+82
-43
lines changed

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: 65 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,42 @@ 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)
196+
# if args.args.verbose:
197+
# sys.stdout.write("\nExecuting query:\n{0}\n".format(sql))
179198
except Exception as e:
180-
sys.stderr.write("Query:\n{0}\nerror: {1}\n".format(sql, e))
199+
sys.stderr.write("Bootstrap execution have exited with an error: {0}\n".format(e))
181200
sys.exit(2)
201+
202+
try:
203+
bootstrap_extension_queries = fill_query_params(CreateSchemaExtensionSQL)
204+
Pooler.query(bootstrap_extension_queries)
205+
# if args.args.verbose:
206+
# sys.stdout.write("\nExecuting query:\n{0}\n".format(sql))
207+
except Exception as e:
208+
sys.stderr.write(
209+
"Bootstrap failed to create the function which required pg_buffercache extension.\n"
210+
"Error: {0}\n".format(e))
211+
sys.stderr.write("Please install pg_buffercache extension and rerun bootstrap "
212+
"if you want to get metrics from pg_buffercache view. \n")
213+
182214
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)
215+
bootstrap_grant_queries = fill_grant_params(GrantsOnDefaultSchemaSQL, args)
216+
Pooler.query(bootstrap_grant_queries)
217+
191218
except Exception as e:
192-
sys.stderr.write("Query:\n{0}\nerror: {1}\n".format(sql, e))
219+
sys.stderr.write("Error: \n {0}\n".format(e))
220+
sys.stderr.write("Please check user rights and rerun bootstrap: \n {0}\n".format(e))
193221
sys.exit(2)
194222

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

0 commit comments

Comments
 (0)