Module to log queries done by specific user types
C Makefile
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


pg_log_userqueries is a PostgreSQL module that logs each query executed by a
superuser. It records each query in the standard log file.

To install pg_log_userqueries, you should untar the pg_log_userqueries tarball
anywhere you want.

You'll then need to compile it with pgxs. So the pg_config tool must be in your
path. Depending on your PostgreSQL installation, you may need to install a dev
package. Once pg_config is in your path, do "make", and then "make install".

Once it's installed, you'll need to configure PostgreSQL to make use of it:

* shared_preload_libraries='pg_log_userqueries'

There are also optional parameters. To configure them, with a PostgreSQL release
older than 9.2, you first need to add the class of pg_log_userqueries in

* custom_variable_classes = 'pg_log_userqueries'

In more recent release, custom_variable_classes is no longer available but you
can still configure pg_log_userqueries.

You'll be able to set the two following parameters:

* pg_log_userqueries.log_level: to choose the log level of pg_log_userqueries
  (WARNING by default)
* pg_log_userqueries.log_label: to give the prefix of the log line for
  pg_log_userqueries logs (defaults to the name of the extension).

Once this configuration is done, restart PostgreSQL.

You can specify exactly what you want to log:

If none of the following four variables is set, all queries issued by superusers will be logged
in all databases, and only those.

If you set any of log_db, log_user, or log_addr, superuser queries won't be systematically logged,
and you'll have to set log_superusers to on to reactivate it.

* pg_log_userqueries.log_superusers: to turn on/off logging of all superusers (off by default)
* pg_log_userqueries.log_db: to give a pipe (|) separated list of database to log.
* pg_log_userqueries.log_user: to give a pipe separated list of user to log.
* pg_log_userqueries.log_addr: to give a pipe separated list of IP addresses to log.

You can use advanced regular expression in that list. For example:

* pg_log_userqueries.log_user="^postgres$|^admin_.*|.*_adm$"

will match if the exact username is 'postgres', or if it begins with 'admin_' or
ends with '_adm'.

By default, pg_log_userqueries will write queries to PostgreSQL log destination.
A superuser can change this behavior with the pg_log_userqueries.log_destination
parameter. By default at stderr, put syslog if you prefer to use a syslog
daemon. You can also configure the syslog facility and the program


As pg_log_userqueries is a shared_preload_library, you'll have to restart PostgreSQL to completely remove it. To temporarily deactivate it, you have a workaround though:

* pg_log_userqueries.file_switchoff: path to a file that must be root-owned. If this file is present, pg_log_userqueries won't log anymore
* pg_log_userqueries.time_switchoff: frequency at which backends will check file_switchoff's presence