Skip to content

Instantly share code, notes, and snippets.

@mr-deamon
Last active October 5, 2022 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mr-deamon/9fec7b98a07f157b13867e45629df30e to your computer and use it in GitHub Desktop.
Save mr-deamon/9fec7b98a07f157b13867e45629df30e to your computer and use it in GitHub Desktop.
postgres log to query

What

Converts the two loglines containing query and paramenters into a valid query

How

Paste the query-line into the first box, the statement-line into the second and hit the button

Why

Because sometimes you need to re-run a query and have the exact same paramenters present

Example

2022-10-04 20:01:01.778 CEST [2935194-4] xxx@kslp app=id-xxx LOG:  duration: 10860.237 ms  execute <unnamed>: select * from abc where a=$1 limit $2
2022-10-04 20:01:01.778 CEST [2935194-5] xxx@kslp app=id-xxx DETAIL:  parameters: $1 = 'DE', $2 = '10'

Result:
2022-10-04 20:01:01.778 CEST [2935194-4] xxx@kslp app=id-xxx LOG:  duration: 10860.237 ms  execute <unnamed>: select * from abc where a='DE' limit '10'

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<textarea id="query"></textarea>
<textarea id="details"></textarea>
<textarea id="res"></textarea>
<input type="button" value="ok" onclick="javascript:doIt();"></input>
<script type="text/javascript">
function replaceValues(values){
str=document.getElementById("query").value.trim()
values.forEach(function(v,k){
str=str.replace("$"+k,v)
})
return str
}
function getValues(){
res=[]
const regex = /\$(\d*).*('.*')$/;
str=document.getElementById("details").value.trim().split(", ")
str.forEach(function(a){
b=a.match(regex)
res[b[1]]=b[2]
})
return res;
}
function doIt(){
val=getValues()
str=replaceValues(val)
document.getElementById("res").value = str
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment