Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining postgres query and log duration

Tags:

postgresql

I am aware that you can show the duration and log queries using the configuration below in postgresql.conf

------------------------------------------------------------------------------
 CUSTOMIZED OPTIONS
------------------------------------------------------------------------------

log_statement = 'all'
log_duration = on
log_line_prefix = '{"Time":[%t], Host:%h} '

And then returns logs like

{"Time":[2018-08-13 16:24:20 +08], Host:172.18.0.2} LOG:  statement: SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."id" = 1
{"Time":[2018-08-13 16:24:20 +08], Host:172.18.0.2} LOG:  duration: 7.694 ms

But can I combine the duration and statement in a single line like?

LOG: { statement: ..., duration: 7.694 ms}
like image 592
Dean Christian Armada Avatar asked Aug 14 '18 03:08

Dean Christian Armada


1 Answers

The way you are logging, the statement is logged when the server starts processing it, but the duration is only known at the end of the execution.

This is why it has to be logged as two different messages.

If you use log_min_duration_statement = 0 instead, the statement is logged at the end of execution together with the duration.

like image 127
Laurenz Albe Avatar answered Oct 04 '22 19:10

Laurenz Albe