Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL how to see which queries have run

I have a PostgreSQL DB at my computer and I have an application that runs queries on it.

How can I see which queries has run on my DB?

I use a Linux computer and pgadmin.

like image 520
kamaci Avatar asked Nov 21 '11 07:11

kamaci


People also ask

How do you check which query is running in Postgres?

A simple select * from pg_stat_activity will provide a snapshot of what is happening on your PostgreSQL database, with one line per current transaction, and the key columns: datname: The database name that the query is running on.

How do you get query history in pgAdmin 4?

The upper panel displays the SQL Editor. You can use the panel to enter, edit, or execute a query. It also shows the History tab which can be used to view the queries that have been executed in the session, and a Scratch Pad which can be used to hold text snippets during editing.


2 Answers

Turn on the server log:

log_statement = all 

This will log every call to the database server.

I would not use log_statement = all on a production server. Produces huge log files.
The manual about logging-parameters:

log_statement (enum)

Controls which SQL statements are logged. Valid values are none (off), ddl, mod, and all (all statements). [...]

Resetting the log_statement parameter requires a server reload (SIGHUP). A restart is not necessary. Read the manual on how to set parameters.

Don't confuse the server log with pgAdmin's log. Two different things!

You can also look at the server log files in pgAdmin, if you have access to the files (may not be the case with a remote server) and set it up correctly. In pgadmin III, have a look at: Tools -> Server status. That option was removed in pgadmin4.

I prefer to read the server log files with vim (or any editor / reader of your choice).

like image 75
Erwin Brandstetter Avatar answered Sep 19 '22 23:09

Erwin Brandstetter


PostgreSql is very advanced when related to logging techniques

Logs are stored in Installationfolder/data/pg_log folder. While log settings are placed in postgresql.conf file.

Log format is usually set as stderr. But CSV log format is recommended. In order to enable CSV format change in

log_destination = 'stderr,csvlog'    logging_collector = on 

In order to log all queries, very usefull for new installations, set min. execution time for a query

log_min_duration_statement = 0 

In order to view active Queries on your database, use

SELECT * FROM pg_stat_activity 

To log specific queries set query type

log_statement = 'all'           # none, ddl, mod, all 

For more information on Logging queries see PostgreSql Log.

like image 38
arvind Avatar answered Sep 18 '22 23:09

arvind