Created
December 31, 2016 07:31
-
-
Save vasansr/4cc47f6ba3af938ff27ce48edc361924 to your computer and use it in GitHub Desktop.
Create queries from PG Logs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# | |
# Convert a log snippet to an actual executable query, by replacing | |
# all $1 $2 etc. with the parameters in the following line. | |
# TODO: handle statements spanning multiple lines (\n in the query itself). | |
# | |
import io | |
import sys | |
import re | |
def print_stmt(stmt_line, param_line): | |
# print "STMT:", stmt_line | |
# print "PARAMS:", param_line | |
params = {} | |
for param in param_line.split(', '): | |
keyval = param.split(' = ') | |
params[keyval[0]] = keyval[1] | |
print re.sub('\$\d+', lambda m: params[m.group(0)], stmt_line), ';' | |
# end def print_stmt | |
param_errors = 0 | |
stmt_errors = 0 | |
f = io.open(sys.argv[1]) | |
line = f.readline() | |
while line: | |
# remove the leading time spec | |
line = line[24:].strip() | |
if re.match('LOG: execute', line): | |
if re.search('\$', line): | |
params = f.readline() | |
params = params[24:].strip() | |
if re.match('DETAIL: parameters: ', params): | |
print_stmt(line[25:], params[21:]) | |
else: | |
param_errors += 1 | |
else: | |
print line[25:], ';' | |
else: | |
# unknown type of statement: ignore | |
stmt_errors += 1 | |
pass | |
line = f.readline() | |
# end while | |
if param_errors > 0: | |
print param_errors, "lines had param errors" | |
if stmt_errors > 0: | |
print stmt_errors, "lines had stmt errors" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment