pg_rotate_logfile()

A system function for rotating the current logfile

pg_rotate_logfile() is a system function for rotating the current logfile

pg_rotate_logfile() was added in PostgreSQL 8.1.

Usage

pg_rotate_logfile() → boolean

logging_collector must be set to on for pg_rotate_logfile() to function.

When executed, pg_rotate_logfile() will create a new logfile according to the naming pattern defined in log_filename.

Note that if log_filename is defined in such a way that the new filename is identical to the current filename, no logfile rotation will take place. For example, if log_filename is set to e.g. postgres.log or postgresql-%F.log (%F resolves to the current date), no rotation will take place.

Change history

Examples

Basic execution of pg_rotate_logfile():

postgres=# SELECT pg_current_logfile();
               pg_current_logfile
-------------------------------------------------
 /var/log/pgsql/postgresql-2020-11-24_000000.log
(1 row)

postgres=# SELECT pg_rotate_logfile();
 pg_rotate_logfile
-------------------
 t
(1 row)

postgres=# SELECT pg_current_logfile();
               pg_current_logfile
-------------------------------------------------
 /var/log/pgsql/postgresql-2020-11-24_063301.log
(1 row)

Execution of pg_rotate_logfile() with no effect:

postgres=# SHOW log_filename;
   log_filename
-------------------
 postgresql-%F.log
(1 row)

postgres=# SELECT pg_current_logfile();
           pg_current_logfile
------------------------------------------
 /var/log/pgsql/postgresql-2020-11-24.log
(1 row)

postgres=# SELECT pg_rotate_logfile();
 pg_rotate_logfile
-------------------
 t
(1 row)

postgres=# SELECT pg_current_logfile();
           pg_current_logfile
------------------------------------------
 /var/log/pgsql/postgresql-2020-11-24.log
(1 row)

Executing pg_rotate_logfile() when logging_collector is set to off (disabled):

WARNING:  rotation not possible because log collection not active
 pg_rotate_logfile 
-------------------
 f
(1 row)

Categories

Logging, System function

See also

pg_current_logfile(), log_directory, log_destination