Is it possible to get a history of queries made in postgres

我的梦境 提交于 2021-02-05 12:43:10

问题


Is it possible to get a history of queries made in postgres? and is it be possible to get the time it took for each query? I'm currently trying to identify slow queries in the application I'm working on.

I'm using Postgres 8.3.5


回答1:


There's no history in the database itself, if you're using psql you can use "\s" to see your command history there.

You can get future queries or other types of operations into the log files by setting log_statement in the postgresql.conf file. What you probably want instead is log_min_duration_statement, which if you set it to 0 will log all queries and their durations in the logs. That can be helpful once your apps goes live, if you set that to a higher value you'll only see the long running queries which can be helpful for optimization (you can run EXPLAIN ANALYZE on the queries you find there to figure out why they're slow).

Another handy thing to know in this area is that if you run psql and tell it "\timing", it will show how long every statement after that takes. So if you have a sql file that looks like this:

\timing
select 1;

You can run it with the right flags and see each statement interleaved with how long it took. Here's how and what the result looks like:

$ psql -ef test.sql 
Timing is on.
select 1;
 ?column? 
----------
        1
(1 row)

Time: 1.196 ms

This is handy because you don't need to be database superuser to use it, unlike changing the config file, and it's easier to use if you're developing new code and want to test it out.




回答2:


If you want to identify slow queries, than the method is to use log_min_duration_statement setting (in postgresql.conf or set per-database with ALTER DATABASE SET).

When you logged the data, you can then use grep or some specialized tools - like pgFouine or my own analyzer - which lacks proper docs, but despite this - runs quite well.




回答3:


If The question is the see the history of queries executed in the Command line. Answer is

As per Postgresql 9.3, Try \? in your command line, you will find all possible commands, in that search for history,

  \s [FILE]              display history or save it to file

in your command line, try \s. This will list the history of queries, you have executed in the current session. you can also save to the file, as shown below.

hms=# \s /tmp/save_queries.sql
Wrote history to file ".//tmp/save_queries.sql".
hms=# 



回答4:


You can use like

\s 

it will fetch you all command history of the terminal, to export it to file using

\s filename



回答5:


pgBadger is another option - also listed here: https://github.com/dhamaniasad/awesome-postgres#utilities

Requires some additional setup in advance to capture the necessary data in the postgres logs though, see the official website.




回答6:


FYI for those using the UI Navicat:

You MUST set your preferences to utilize a file as to where to store the history.

If this is blank your Navicat will be blank.

PS: I have no affiliation with or in association to Navicat or it's affiliates. Just looking to help.




回答7:


There's no history in the database itself,but if you are using DataGrip data management tool then you can check the history thats your run in the datagrip.



来源:https://stackoverflow.com/questions/1523446/is-it-possible-to-get-a-history-of-queries-made-in-postgres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!