Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm in the process of reviewing every SQL statement that an application makes against the database, for performance reasons. Is there an easy way to log all statements that are executed by the PostgreSQL database server? Thanks.

share|improve this question

3 Answers 3

up vote 11 down vote accepted

The config option you're looking for is log_statement = "all" (if you just want the statements), or log_min_statement_duration = <some number> if you're just after "slow" queries (for some value of "slow"). See http://www.postgresql.org/docs/current/static/runtime-config-logging.html for more details on logging configuration.

share|improve this answer
1  
Logging all statements is a performance killer (as stated in the official docs). However, 8.4 has a nice feature of getting the explain analyze of a slow query at the tie it was executed, you might want to start testing with this as 8.4 isn't yet released but it's a nice option to know that happend at the time of execution, if the analyze explain output is OK you probably are running into issues with I/O or CPU bounds, but at least you'll know it's not the query itself –  Server Horror Jun 14 '09 at 14:05
3  
I really like the log_statement = 'mod' option. It only shows creates, updates, and deletes, and skips all the select statements. Great if you're trying to figure out which code is tweaking some field. –  Don Kirkby Aug 6 '09 at 21:48

Of course, you can detect slowest queries by yourself, but I advise you to use pgFouine — a PostgreSQL log analyzer. It`s easy to install and really useful.

Sample reports: here and here.

share|improve this answer
    
I used pgFouine on a project and identified several places where an index would help things a lot. –  Paul Tomblin Jun 4 '09 at 21:54

The auto_explain module is very useful for this. It'll not only log the statements, it'll log their execution plans and can even log statements run within PL/PgSQL functions. The performance hit is fairly low unless you enable analyze, in which case you incur a fair bit of timing overhead for all queries.

See auto_explain in the documentation.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.