Skip to content

Commit bdf8ef6

Browse files
committed
Create a built-in log rotation program, so that we no longer have to
recommend that people go get Apache's rotatelogs program. Additional benefits are that configuration is done through GUC, rather than externally, and that the postmaster can monitor the log rotator and restart it after failure (though we certainly hope that won't happen often). Andreas Pflug, some rework by Tom Lane.
1 parent b4cd416 commit bdf8ef6

File tree

13 files changed

+1073
-69
lines changed

13 files changed

+1073
-69
lines changed

doc/src/sgml/maintenance.sgml

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/maintenance.sgml,v 1.36 2004/07/24 19:51:22 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/maintenance.sgml,v 1.37 2004/08/05 23:32:10 tgl Exp $
33
-->
44

55
<chapter id="maintenance">
@@ -445,22 +445,52 @@ VACUUM
445445
</para>
446446

447447
<para>
448-
If you simply direct the <systemitem>stderr</> of the <command>postmaster</command> into a
449-
file, the only way to truncate the log file is to stop and restart
448+
If you simply direct the <systemitem>stderr</> of the
449+
<command>postmaster</command> into a
450+
file, you will have log output, but
451+
the only way to truncate the log file is to stop and restart
450452
the <command>postmaster</command>. This may be OK if you are using
451453
<productname>PostgreSQL</productname> in a development environment,
452454
but few production servers would find this behavior acceptable.
453455
</para>
454456

455457
<para>
456-
The simplest production-grade approach to managing log output is to
458+
A better approach is to send the <command>postmaster</>'s
459+
<systemitem>stderr</> output to some type of log rotation program.
460+
There is a built-in log rotation program, which you can use by
461+
setting the configuration parameter <literal>redirect_stderr</> to
462+
<literal>true</> in <filename>postgresql.conf</>. The control
463+
parameters for this program are described in <xref
464+
linkend="runtime-config-logging-where">.
465+
</para>
466+
467+
<para>
468+
Alternatively, you might prefer to use an external log rotation
469+
program, if you have one that you are already using with other
470+
server software. For example, the <application>rotatelogs</application>
471+
tool included in the <productname>Apache</productname> distribution
472+
can be used with <productname>PostgreSQL</productname>. To do this,
473+
just pipe the <command>postmaster</>'s
474+
<systemitem>stderr</> output to the desired program.
475+
If you start the server with
476+
<command>pg_ctl</>, then <systemitem>stderr</>
477+
is already redirected to <systemitem>stdout</>, so you just need a
478+
pipe command:
479+
480+
<programlisting>
481+
pg_ctl start | rotatelogs /var/log/pgsql_log 86400
482+
</programlisting>
483+
</para>
484+
485+
<para>
486+
Another production-grade approach to managing log output is to
457487
send it all to <application>syslog</> and let
458488
<application>syslog</> deal with file rotation. To do this, set the
459-
configuration parameter <literal>log_destination</> to 'syslog' (to log to
460-
<application>syslog</> only) in <filename>postgresql.conf</>. Then
461-
you can send a <literal>SIGHUP</literal> signal to the
462-
<application>syslog</> daemon whenever you want to force it to
463-
start writing a new log file. If you want to automate log
489+
configuration parameter <literal>log_destination</> to <literal>syslog</>
490+
(to log to <application>syslog</> only) in
491+
<filename>postgresql.conf</>. Then you can send a <literal>SIGHUP</literal>
492+
signal to the <application>syslog</> daemon whenever you want to force it
493+
to start writing a new log file. If you want to automate log
464494
rotation, the <application>logrotate</application> program can be
465495
configured to work with log files from
466496
<application>syslog</application>.
@@ -471,27 +501,15 @@ VACUUM
471501
particularly with large log messages; it may truncate or drop messages
472502
just when you need them the most. Also, on <productname>linux</>,
473503
<application>syslog</> will sync each message to disk, yielding poor
474-
performance. Use a <literal>-</> at the start of the file name
475-
in the <application>syslog</> config file to disable this behavior.
504+
performance. (You can use a <literal>-</> at the start of the file name
505+
in the <application>syslog</> config file to disable this behavior.)
476506
</para>
477507

478508
<para>
479-
You may find it more useful to pipe the
480-
<systemitem>stderr</> of the <command>postmaster</> to some type of
481-
log rotation program. If you start the server with
482-
<command>pg_ctl</>, then the <systemitem>stderr</> of the <command>postmaster</command>
483-
is already redirected to <systemitem>stdout</>, so you just need a
484-
pipe command:
485-
486-
<programlisting>
487-
pg_ctl start | rotatelogs /var/log/pgsql_log 86400
488-
</programlisting>
489-
490-
The <productname>PostgreSQL</> distribution doesn't include a
491-
suitable log rotation program, but there are many available on the
492-
Internet. For example, the <application>rotatelogs</application>
493-
tool included in the <productname>Apache</productname> distribution
494-
can be used with <productname>PostgreSQL</productname>.
509+
Note that all the solutions described above take care of starting new
510+
log files at configurable intervals, but they do not handle deletion
511+
of old, no-longer-interesting log files. You will also want to set
512+
up a batch job to periodically delete old log files.
495513
</para>
496514
</sect1>
497515
</chapter>

doc/src/sgml/runtime.sgml

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/runtime.sgml,v 1.271 2004/08/03 23:42:59 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/runtime.sgml,v 1.272 2004/08/05 23:32:10 tgl Exp $
33
-->
44

55
<Chapter Id="runtime">
@@ -1800,15 +1800,91 @@ SET ENABLE_SEQSCAN TO OFF;
18001800
option to a list of desired log destinations separated by
18011801
commas. The default is to log to <systemitem>stderr</systemitem>
18021802
only.
1803+
This option can only be set at server start or in the
1804+
<filename>postgresql.conf</filename> configuration file.
18031805
</para>
18041806
</listitem>
18051807
</varlistentry>
18061808

1809+
<varlistentry id="guc-redirect-stderr" xreflabel="redirect_stderr">
1810+
<term><varname>redirect_stderr</varname> (<type>boolean</type>)</term>
1811+
<listitem>
1812+
<para>
1813+
This option allows messages sent to <application>stderr</> to be
1814+
captured and redirected into log files.
1815+
This option, in combination with logging to <application>stderr</>,
1816+
is often more useful than
1817+
logging to <application>syslog</>, since some types of messages
1818+
may not appear in <application>syslog</> output (a common example
1819+
is dynamic-linker failure messages).
1820+
This option can only be set at server start.
1821+
</para>
1822+
</listitem>
1823+
</varlistentry>
1824+
1825+
<varlistentry id="guc-log-directory" xreflabel="log_directory">
1826+
<term><varname>log_directory</varname> (<type>string</type>)</term>
1827+
<listitem>
1828+
<para>
1829+
When <varname>redirect_stderr</> is enabled, this option
1830+
determines the directory in which log files will be created.
1831+
It may be specified as an absolute path, or relative to the
1832+
cluster data directory.
1833+
This option can only be set at server start or in the
1834+
<filename>postgresql.conf</filename> configuration file.
1835+
</para>
1836+
</listitem>
1837+
</varlistentry>
1838+
1839+
<varlistentry id="guc-log-filename-prefix" xreflabel="log_filename_prefix">
1840+
<term><varname>log_filename_prefix</varname> (<type>string</type>)</term>
1841+
<listitem>
1842+
<para>
1843+
When <varname>redirect_stderr</> is enabled, this option
1844+
sets the prefix of the file names of the created log files.
1845+
The postmaster PID and the current time are appended to this
1846+
prefix to form an exact log file name.
1847+
This option can only be set at server start or in the
1848+
<filename>postgresql.conf</filename> configuration file.
1849+
</para>
1850+
</listitem>
1851+
</varlistentry>
1852+
1853+
<varlistentry id="guc-log-rotation-age" xreflabel="log_rotation_age">
1854+
<term><varname>log_rotation_age</varname> (<type>integer</type>)</term>
1855+
<listitem>
1856+
<para>
1857+
When <varname>redirect_stderr</> is enabled, this option
1858+
determines the maximum lifetime of an individual log file.
1859+
After this many minutes have elapsed, a new log file will
1860+
be created. Set to zero to disable time-based creation of
1861+
new log files.
1862+
This option can only be set at server start or in the
1863+
<filename>postgresql.conf</filename> configuration file.
1864+
</para>
1865+
</listitem>
1866+
</varlistentry>
1867+
1868+
<varlistentry id="guc-log-rotation-size" xreflabel="log_rotation_size">
1869+
<term><varname>log_rotation_size</varname> (<type>integer</type>)</term>
1870+
<listitem>
1871+
<para>
1872+
When <varname>redirect_stderr</> is enabled, this option
1873+
determines the maximum size of an individual log file.
1874+
After this many kilobytes have been emitted into a log file,
1875+
a new log file will be created. Set to zero to disable size-based
1876+
creation of new log files.
1877+
This option can only be set at server start or in the
1878+
<filename>postgresql.conf</filename> configuration file.
1879+
</para>
1880+
</listitem>
1881+
</varlistentry>
1882+
18071883
<varlistentry id="guc-syslog-facility" xreflabel="syslog_facility">
18081884
<term><varname>syslog_facility</varname> (<type>string</type>)</term>
18091885
<listitem>
18101886
<para>
1811-
If logging to <application>syslog</> is enabled, this option
1887+
When logging to <application>syslog</> is enabled, this option
18121888
determines the <application>syslog</application>
18131889
<quote>facility</quote> to be used. You may choose
18141890
from <literal>LOCAL0</>, <literal>LOCAL1</>,
@@ -1826,7 +1902,7 @@ SET ENABLE_SEQSCAN TO OFF;
18261902
<term><varname>syslog_ident</varname> (<type>string</type>)</term>
18271903
<listitem>
18281904
<para>
1829-
If logging to <application>syslog</> is enabled, this option
1905+
When logging to <application>syslog</> is enabled, this option
18301906
determines the program name used to identify
18311907
<productname>PostgreSQL</productname> messages in
18321908
<application>syslog</application> logs. The default is

src/backend/postmaster/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
# Makefile for src/backend/postmaster
55
#
66
# IDENTIFICATION
7-
# $PostgreSQL: pgsql/src/backend/postmaster/Makefile,v 1.18 2004/07/21 20:34:46 momjian Exp $
7+
# $PostgreSQL: pgsql/src/backend/postmaster/Makefile,v 1.19 2004/08/05 23:32:10 tgl Exp $
88
#
99
#-------------------------------------------------------------------------
1010

1111
subdir = src/backend/postmaster
1212
top_builddir = ../../..
1313
include $(top_builddir)/src/Makefile.global
1414

15-
OBJS = postmaster.o bgwriter.o pgstat.o pgarch.o
15+
OBJS = postmaster.o bgwriter.o pgstat.o pgarch.o syslogger.o
1616

1717
all: SUBSYS.o
1818

src/backend/postmaster/pgarch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
*
2121
* IDENTIFICATION
22-
* $PostgreSQL: pgsql/src/backend/postmaster/pgarch.c,v 1.4 2004/08/03 20:32:33 tgl Exp $
22+
* $PostgreSQL: pgsql/src/backend/postmaster/pgarch.c,v 1.5 2004/08/05 23:32:10 tgl Exp $
2323
*
2424
*-------------------------------------------------------------------------
2525
*/
@@ -172,7 +172,7 @@ pgarch_start(void)
172172
beos_backend_startup();
173173
#endif
174174
/* Close the postmaster's sockets */
175-
ClosePostmasterPorts();
175+
ClosePostmasterPorts(false);
176176

177177
/* Drop our connection to postmaster's shared memory, as well */
178178
PGSharedMemoryDetach();

src/backend/postmaster/pgstat.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* Copyright (c) 2001-2003, PostgreSQL Global Development Group
1515
*
16-
* $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.77 2004/07/01 00:50:36 tgl Exp $
16+
* $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.78 2004/08/05 23:32:10 tgl Exp $
1717
* ----------
1818
*/
1919
#include "postgres.h"
@@ -611,7 +611,7 @@ pgstat_start(void)
611611
beos_backend_startup();
612612
#endif
613613
/* Close the postmaster's sockets */
614-
ClosePostmasterPorts();
614+
ClosePostmasterPorts(false);
615615

616616
/* Drop our connection to postmaster's shared memory, as well */
617617
PGSharedMemoryDetach();

0 commit comments

Comments
 (0)