-
Notifications
You must be signed in to change notification settings - Fork 5
/
pg_explain_locks.py
executable file
·155 lines (120 loc) · 3.73 KB
/
pg_explain_locks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env python
import argparse
import os
import sys
import psycopg2
from prettytable import PrettyTable
LOCK_CHECK_QUERY = """
SELECT l.relation,
c.relname,
l.mode
FROM pg_locks l
JOIN pg_class c ON c.oid=l.relation
JOIN pg_stat_activity a on l.pid = a.pid
AND a.query NOT ILIKE '%pg_stat_activity%'
ORDER BY l.relation ASC;
"""
RELATION_ID = 'Relation ID'
RELATION_NAME = 'Relation Name'
LOCK_TYPE = 'Lock Type'
SETTINGS_FILE = f"{os.path.expanduser('~')}/.pg_explain_locks_settings"
def explain_locks_for_query(
user: str,
password: str,
host: str,
port: str,
database: str,
query: str,
):
"""Execute and rollback a query to see what locks it will take"""
# Create a DB connection that will "stage" but not commit the DB change
connection_for_schema_change = psycopg2.connect(
user=user,
password=password,
host=host,
port=port,
database=database,
)
connection_for_schema_change.autocommit = False
schema_change_cursor = connection_for_schema_change.cursor()
# Create a DB connection that will check what Locks are taken for "query"
connection_for_lock_check = psycopg2.connect(
user=user,
password=password,
host=host,
port=port,
database=database,
)
lock_check_cursor = connection_for_lock_check.cursor()
# Execute the query, but do not commit
schema_change_cursor.execute(query)
lock_check_cursor.execute(LOCK_CHECK_QUERY)
results = lock_check_cursor.fetchall()
connection_for_schema_change.rollback()
results_table = PrettyTable()
results_table.field_names = [RELATION_ID, RELATION_NAME, LOCK_TYPE]
results_table.align[RELATION_ID] = 'l'
results_table.align[RELATION_NAME] = 'l'
results_table.align[LOCK_TYPE] = 'l'
for relation_id, relation_name, lock_type in results:
results_table.add_row([relation_id, relation_name, lock_type])
print(results_table)
def parse_args_from_command_line():
parser = argparse.ArgumentParser()
parser.add_argument(
'--query',
required=True,
help='A DDL statement to explain',
)
parser.add_argument(
'--user',
required=True,
help='User for database connection',
)
parser.add_argument(
'--password',
required=True,
help='Password for database connection',
)
parser.add_argument(
'--host',
default='localhost',
help='Host for database connection',
)
parser.add_argument(
'--port',
default='5432',
help='Port for database connection',
)
parser.add_argument(
'--database',
required=True,
help='Database for database connection',
)
return vars(parser.parse_args())
def parse_args_from_settings_file():
if len(sys.argv) != 2:
print('Error : need to provide query as only arugment')
print('Example: pg_explain_locks "SELECT * FROM actors"')
exit()
args = {}
with open(SETTINGS_FILE, 'r') as settings_file:
content = settings_file.read()
lines = content.strip().split('\n')
for line in lines:
setting, value = line.split('=')
args[setting.lower()] = value
args['query'] = sys.argv[1]
return args
def main():
if os.path.exists(SETTINGS_FILE):
args = parse_args_from_settings_file()
else:
args = parse_args_from_command_line()
if 'commit' in args['query'].lower():
print(
'Just to be super safe, this tool will not work for queries that '
'include the word commit :)'
)
exit()
explain_locks_for_query(**args)