Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.