Skip to content

Instantly share code, notes, and snippets.

@michelmilezzi
Last active September 18, 2019 17:13
Show Gist options
  • Save michelmilezzi/daba00425e20becb488d0602f0a9a9dd to your computer and use it in GitHub Desktop.
Save michelmilezzi/daba00425e20becb488d0602f0a9a9dd to your computer and use it in GitHub Desktop.
PostgreSQL bind parameters
# Usage: ruby parameter_bind.rb mylogfile.log
# Output: every sql command parsed with its parameters
sourceFileName = ARGV[0]
targetFile = File.open("#{sourceFileName}.sql", 'w')
lines = File.read(sourceFileName, encoding: 'utf-8').scan(/(duration:.*)\sexecute.+:\s(.+)(\n.+parameters:(.+))?/)
lines.each do |line|
if line[2] != nil then
line[2].scan(/(\$\d+)\s=\s('.*?'|NULL)/).each do | param |
line[1].gsub!(Regexp.compile("#{Regexp.quote(param[0])}(?!\\d)"), param[1])
end
end
targetFile.puts "--#{line[0]}"
targetFile.puts "#{line[1]};"
targetFile.puts "\n"
end
targetFile.close
ALTER SYSTEM SET log_min_duration_statement = 0;
ALTER SYSTEM SET log_filename = 'mylogfile';
ALTER SYSTEM SET log_truncate_on_rotation = ON;
SELECT pg_reload_conf();
SELECT pg_rotate_logfile();
--Reset
--ALTER SYSTEM RESET log_min_duration_statement;
--ALTER SYSTEM RESET log_filename;
--ALTER SYSTEM RESET log_truncate_on_rotation;
--SELECT pg_reload_conf();
--SELECT pg_rotate_logfile();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment