gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 line
8.8KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>24.3. Log File Maintenance</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="routine-reindex.html" title="24.2. Routine Reindexing" /><link rel="next" href="backup.html" title="Chapter 25. Backup and Restore" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">24.3. Log File Maintenance</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="routine-reindex.html" title="24.2. Routine Reindexing">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="maintenance.html" title="Chapter 24. Routine Database Maintenance Tasks">Up</a></td><th width="60%" align="center">Chapter 24. Routine Database Maintenance Tasks</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="backup.html" title="Chapter 25. Backup and Restore">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="LOGFILE-MAINTENANCE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">24.3. Log File Maintenance</h2></div></div></div><a id="id-1.6.11.12.2" class="indexterm"></a><p>
  3. It is a good idea to save the database server's log output
  4. somewhere, rather than just discarding it via <code class="filename">/dev/null</code>.
  5. The log output is invaluable when diagnosing
  6. problems. However, the log output tends to be voluminous
  7. (especially at higher debug levels) so you won't want to save it
  8. indefinitely. You need to <span class="emphasis"><em>rotate</em></span> the log files so that
  9. new log files are started and old ones removed after a reasonable
  10. period of time.
  11. </p><p>
  12. If you simply direct the <span class="systemitem">stderr</span> of
  13. <code class="command">postgres</code> into a
  14. file, you will have log output, but
  15. the only way to truncate the log file is to stop and restart
  16. the server. This might be acceptable if you are using
  17. <span class="productname">PostgreSQL</span> in a development environment,
  18. but few production servers would find this behavior acceptable.
  19. </p><p>
  20. A better approach is to send the server's
  21. <span class="systemitem">stderr</span> output to some type of log rotation program.
  22. There is a built-in log rotation facility, which you can use by
  23. setting the configuration parameter <code class="varname">logging_collector</code> to
  24. <code class="literal">true</code> in <code class="filename">postgresql.conf</code>. The control
  25. parameters for this program are described in <a class="xref" href="runtime-config-logging.html#RUNTIME-CONFIG-LOGGING-WHERE" title="19.8.1. Where to Log">Section 19.8.1</a>. You can also use this approach
  26. to capture the log data in machine readable <acronym class="acronym">CSV</acronym>
  27. (comma-separated values) format.
  28. </p><p>
  29. Alternatively, you might prefer to use an external log rotation
  30. program if you have one that you are already using with other
  31. server software. For example, the <span class="application">rotatelogs</span>
  32. tool included in the <span class="productname">Apache</span> distribution
  33. can be used with <span class="productname">PostgreSQL</span>. One way to
  34. do this is to pipe the server's
  35. <span class="systemitem">stderr</span> output to the desired program.
  36. If you start the server with
  37. <code class="command">pg_ctl</code>, then <span class="systemitem">stderr</span>
  38. is already redirected to <span class="systemitem">stdout</span>, so you just need a
  39. pipe command, for example:
  40. </p><pre class="programlisting">
  41. pg_ctl start | rotatelogs /var/log/pgsql_log 86400
  42. </pre><p>
  43. </p><p>
  44. You can combine these approaches by setting up <span class="application">logrotate</span>
  45. to collect log files produced by <span class="productname">PostgreSQL</span> built-in
  46. logging collector. In this case, the logging collector defines the names and
  47. location of the log files, while <span class="application">logrotate</span>
  48. periodically archives these files. When initiating log rotation,
  49. <span class="application">logrotate</span> must ensure that the application
  50. sends further output to the new file. This is commonly done with a
  51. <code class="literal">postrotate</code> script that sends a <code class="literal">SIGHUP</code>
  52. signal to the application, which then reopens the log file.
  53. In <span class="productname">PostgreSQL</span>, you can run <code class="command">pg_ctl</code>
  54. with the <code class="literal">logrotate</code> option instead. When the server receives
  55. this command, the server either switches to a new log file or reopens the
  56. existing file, depending on the logging configuration
  57. (see <a class="xref" href="runtime-config-logging.html#RUNTIME-CONFIG-LOGGING-WHERE" title="19.8.1. Where to Log">Section 19.8.1</a>).
  58. </p><div class="note"><h3 class="title">Note</h3><p>
  59. When using static log file names, the server might fail to reopen the log
  60. file if the max open file limit is reached or a file table overflow occurs.
  61. In this case, log messages are sent to the old log file until a
  62. successful log rotation. If <span class="application">logrotate</span> is
  63. configured to compress the log file and delete it, the server may lose
  64. the messages logged in this timeframe. To avoid this issue, you can
  65. configure the logging collector to dynamically assign log file names
  66. and use a <code class="literal">prerotate</code> script to ignore open log files.
  67. </p></div><p>
  68. Another production-grade approach to managing log output is to
  69. send it to <span class="application">syslog</span> and let
  70. <span class="application">syslog</span> deal with file rotation. To do this, set the
  71. configuration parameter <code class="varname">log_destination</code> to <code class="literal">syslog</code>
  72. (to log to <span class="application">syslog</span> only) in
  73. <code class="filename">postgresql.conf</code>. Then you can send a <code class="literal">SIGHUP</code>
  74. signal to the <span class="application">syslog</span> daemon whenever you want to force it
  75. to start writing a new log file. If you want to automate log
  76. rotation, the <span class="application">logrotate</span> program can be
  77. configured to work with log files from
  78. <span class="application">syslog</span>.
  79. </p><p>
  80. On many systems, however, <span class="application">syslog</span> is not very reliable,
  81. particularly with large log messages; it might truncate or drop messages
  82. just when you need them the most. Also, on <span class="productname">Linux</span>,
  83. <span class="application">syslog</span> will flush each message to disk, yielding poor
  84. performance. (You can use a <span class="quote">“<span class="quote"><code class="literal">-</code></span>”</span> at the start of the file name
  85. in the <span class="application">syslog</span> configuration file to disable syncing.)
  86. </p><p>
  87. Note that all the solutions described above take care of starting new
  88. log files at configurable intervals, but they do not handle deletion
  89. of old, no-longer-useful log files. You will probably want to set
  90. up a batch job to periodically delete old log files. Another possibility
  91. is to configure the rotation program so that old log files are overwritten
  92. cyclically.
  93. </p><p>
  94. <a class="ulink" href="https://pgbadger.darold.net/" target="_top"><span class="productname">pgBadger</span></a>
  95. is an external project that does sophisticated log file analysis.
  96. <a class="ulink" href="https://bucardo.org/check_postgres/" target="_top"><span class="productname">check_postgres</span></a>
  97. provides Nagios alerts when important messages appear in the log
  98. files, as well as detection of many other extraordinary conditions.
  99. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="routine-reindex.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="maintenance.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="backup.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">24.2. Routine Reindexing </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 25. Backup and Restore</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1