logger subprocess - Mailing list pgsql-patches

From Andreas Pflug
Subject logger subprocess
Date
Msg-id 41065594.7060805@pse-consulting.de
Whole thread Raw
Responses Re: logger subprocess  (Bruce Momjian <pgman@candle.pha.pa.us>)
List pgsql-patches
This is the known patch, with following changes:

- realStdErr handed over for EXEC_BACKEND, but still not tested
- Sometimes EMFILE is received in the logger's process queue, when a
backend ended after a SSL connection was interrupted. This is ignored
now (previously it forced an exit(1) and restart of the subprocess)
- log_destination needs to be PGC_POSTMASTER, because the logger process
creation depends on that.
- no functions included

Regards,
Andreas
Index: src/backend/postmaster/Makefile
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/postmaster/Makefile,v
retrieving revision 1.18
diff -u -r1.18 Makefile
--- src/backend/postmaster/Makefile    21 Jul 2004 20:34:46 -0000    1.18
+++ src/backend/postmaster/Makefile    27 Jul 2004 10:33:30 -0000
@@ -12,7 +12,7 @@
 top_builddir = ../../..
 include $(top_builddir)/src/Makefile.global

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

 all: SUBSYS.o

Index: src/backend/postmaster/postmaster.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/postmaster/postmaster.c,v
retrieving revision 1.416
diff -u -r1.416 postmaster.c
--- src/backend/postmaster/postmaster.c    27 Jul 2004 01:46:03 -0000    1.416
+++ src/backend/postmaster/postmaster.c    27 Jul 2004 10:33:36 -0000
@@ -118,7 +118,7 @@
 #include "utils/ps_status.h"
 #include "bootstrap/bootstrap.h"
 #include "pgstat.h"
-
+#include "postmaster/syslogger.h"

 /*
  * List of active backends (or child processes anyway; we don't actually
@@ -201,6 +201,7 @@
             BgWriterPID = 0,
             PgArchPID = 0,
             PgStatPID = 0;
+pid_t       SysLoggerPID = 0;

 /* Startup/shutdown state */
 #define            NoShutdown        0
@@ -852,6 +853,12 @@
 #endif

     /*
+     * start logging to file
+     */
+
+    SysLoggerPID = SysLogger_Start();
+
+    /*
      * Reset whereToSendOutput from Debug (its starting state) to None.
      * This stops ereport from sending log messages to stderr unless
      * Log_destination permits.  We don't do this until the postmaster
@@ -1231,6 +1238,11 @@
             StartupPID == 0 && !FatalError && Shutdown == NoShutdown)
             PgStatPID = pgstat_start();

+        /* If we have lost the system logger, try to start a new one */
+        if (SysLoggerPID == 0 &&
+            StartupPID == 0 && !FatalError && Shutdown == NoShutdown)
+            SysLoggerPID = SysLogger_Start();
+
         /*
          * Touch the socket and lock file at least every ten minutes, to ensure
          * that they are not removed by overzealous /tmp-cleaning tasks.
@@ -1771,6 +1783,9 @@
             kill(BgWriterPID, SIGHUP);
         if (PgArchPID != 0)
             kill(PgArchPID, SIGHUP);
+        if (SysLoggerPID != 0)
+            kill(SysLoggerPID, SIGHUP);
+
         /* PgStatPID does not currently need SIGHUP */
         load_hba();
         load_ident();
@@ -1836,7 +1851,6 @@
             if (PgStatPID != 0)
                 kill(PgStatPID, SIGQUIT);
             break;
-
         case SIGINT:
             /*
              * Fast Shutdown:
@@ -1903,6 +1917,7 @@
                 kill(PgStatPID, SIGQUIT);
             if (DLGetHead(BackendList))
                 SignalChildren(SIGQUIT);
+
             ExitPostmaster(0);
             break;
     }
@@ -2065,6 +2080,15 @@
             continue;
         }

+        /* was it the system logger, try to start a new one */
+        if (SysLoggerPID != 0 && pid == SysLoggerPID)
+        {
+            if (exitstatus != 0)
+                LogChildExit(LOG, gettext("system logger process"),
+                             pid, exitstatus);
+            SysLoggerPID = SysLogger_Start();
+            continue;
+        }
         /*
          * Else do standard backend child cleanup.
          */
@@ -2968,6 +2992,16 @@
         PgstatCollectorMain(argc, argv);
         proc_exit(0);
     }
+    if (strcmp(argv[1], "-forklog") == 0)
+    {
+        /* Close the postmaster's sockets */
+        ClosePostmasterPorts();
+
+        /* Do not want to attach to shared memory */
+
+        SysLoggerMain(argc, argv);
+        proc_exit(0);
+    }

     return 1;                    /* shouldn't get here */
 }
@@ -3024,7 +3058,6 @@
         if (Shutdown <= SmartShutdown)
             SignalChildren(SIGUSR1);
     }
-
     if (PgArchPID != 0 && Shutdown == NoShutdown)
     {
         if (CheckPostmasterSignal(PMSIGNAL_WAKEN_ARCHIVER))
@@ -3036,6 +3069,10 @@
             kill(PgArchPID, SIGUSR1);
         }
     }
+    if (CheckPostmasterSignal(PMSIGNAL_ROTATE_LOGFILE) && SysLoggerPID != 0)
+    {
+        kill(SysLoggerPID, SIGUSR1);
+    }

     PG_SETMASK(&UnBlockSig);

Index: src/backend/utils/error/elog.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/utils/error/elog.c,v
retrieving revision 1.142
diff -u -r1.142 elog.c
--- src/backend/utils/error/elog.c    24 Jun 2004 21:03:13 -0000    1.142
+++ src/backend/utils/error/elog.c    27 Jul 2004 10:33:39 -0000
@@ -84,6 +84,10 @@
 static void write_eventlog(int level, const char *line);
 #endif

+/* in syslogger.c */
+extern FILE *syslogFile;
+extern FILE *realStdErr;
+extern pid_t SysLoggerPID;
 /*
  * ErrorData holds the data accumulated during any one ereport() cycle.
  * Any non-NULL pointers must point to palloc'd data in ErrorContext.
@@ -1451,10 +1455,31 @@
         write_eventlog(eventlog_level, buf.data);
     }
 #endif   /* WIN32 */
-    /* Write to stderr, if enabled */
-    if ((Log_destination & LOG_DESTINATION_STDERR) || whereToSendOutput == Debug)
+
+    /*
+     * Write to stderr. If Log_destination is file or stderr
+     * if file is target, the logger process will handle this
+     */
+    if ((Log_destination & (LOG_DESTINATION_STDERR | LOG_DESTINATION_FILE))
+        || whereToSendOutput == Debug)
     {
-        fprintf(stderr, "%s", buf.data);
+        if (SysLoggerPID == MyProcPid && realStdErr != 0)
+        {
+            /*
+             * If realStdErr is not null in the SysLogger process,
+             * there's something really wrong because stderr is probably
+             * redirected to the pipe. To avoid circular writes, we
+             * write to realStdErr which is hopefully the stderr the postmaster
+             * was started with.
+             */
+            fprintf(realStdErr, "%s", buf.data);
+        }
+        else
+            fprintf(stderr, "%s", buf.data) ;
+
+        /* syslogFile is open in SysLogger only */
+        if (syslogFile != 0)
+            fprintf(syslogFile, "%s", buf.data) ;
     }

     pfree(buf.data);
Index: src/backend/utils/misc/guc.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/utils/misc/guc.c,v
retrieving revision 1.224
diff -u -r1.224 guc.c
--- src/backend/utils/misc/guc.c    24 Jul 2004 19:51:23 -0000    1.224
+++ src/backend/utils/misc/guc.c    27 Jul 2004 10:33:49 -0000
@@ -44,6 +44,7 @@
 #include "parser/parse_expr.h"
 #include "parser/parse_relation.h"
 #include "postmaster/bgwriter.h"
+#include "postmaster/syslogger.h"
 #include "postmaster/postmaster.h"
 #include "storage/bufmgr.h"
 #include "storage/fd.h"
@@ -1285,6 +1286,23 @@
         BLCKSZ, BLCKSZ, BLCKSZ, NULL, NULL
     },

+    {
+      {"log_rotation_age", PGC_SIGHUP, LOGGING_WHERE,
+       gettext_noop("Automatic logfile rotation will occur after n minutes"),
+       NULL
+      },
+      &Log_RotationAge,
+      24*60, 0, INT_MAX, NULL, NULL
+    },
+    {
+      {"log_rotation_size", PGC_SIGHUP, LOGGING_WHERE,
+       gettext_noop("Automatic logfile rotation will occur if this size is reached (in kb)"),
+       NULL
+      },
+      &Log_RotationSize,
+      10*1024, 0, INT_MAX, NULL, NULL
+    },
+
     /* End-of-list marker */
     {
         {NULL, 0, 0, NULL, NULL}, NULL, 0, 0, 0, NULL, NULL
@@ -1625,15 +1643,34 @@
     },

     {
-        {"log_destination", PGC_SIGHUP, LOGGING_WHERE,
-         gettext_noop("Sets the destination for server log output."),
-         gettext_noop("Valid values are combinations of stderr, syslog "
+        {"log_destination", PGC_POSTMASTER, LOGGING_WHERE,
+         gettext_noop("Sets the target for log output."),
+         gettext_noop("Valid values are combinations of stderr, file, syslog "
                       "and eventlog, depending on platform."),
          GUC_LIST_INPUT
         },
         &log_destination_string,
         "stderr", assign_log_destination, NULL
     },
+    {
+        {"log_directory", PGC_SIGHUP, LOGGING_WHERE,
+         gettext_noop("Sets the target directory for log output."),
+         gettext_noop("May be specified as relative to the cluster directory "
+                      "or as absolute path."),
+         GUC_LIST_INPUT | GUC_REPORT
+        },
+        &Log_directory,
+        "pg_log", NULL, NULL
+    },
+    {
+        {"log_filename_prefix", PGC_SIGHUP, LOGGING_WHERE,
+         gettext_noop("prefix for logfile names created in the log_directory."),
+         NULL,
+         GUC_LIST_INPUT | GUC_REPORT
+        },
+        &Log_filename_prefix,
+        "postgresql-", NULL, NULL
+    },

 #ifdef HAVE_SYSLOG
     {
@@ -5079,6 +5116,8 @@

         if (pg_strcasecmp(tok,"stderr") == 0)
             newlogdest |= LOG_DESTINATION_STDERR;
+        else if (pg_strcasecmp(tok,"file") == 0)
+            newlogdest |= LOG_DESTINATION_FILE;
 #ifdef HAVE_SYSLOG
         else if (pg_strcasecmp(tok,"syslog") == 0)
             newlogdest |= LOG_DESTINATION_SYSLOG;
Index: src/backend/utils/misc/postgresql.conf.sample
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/utils/misc/postgresql.conf.sample,v
retrieving revision 1.118
diff -u -r1.118 postgresql.conf.sample
--- src/backend/utils/misc/postgresql.conf.sample    21 Jul 2004 20:34:46 -0000    1.118
+++ src/backend/utils/misc/postgresql.conf.sample    27 Jul 2004 10:33:49 -0000
@@ -167,9 +167,17 @@

 # - Where to Log -

-#log_destination = 'stderr'    # Valid values are combinations of stderr,
-                                # syslog and eventlog, depending on
-                                # platform.
+#log_destination = 'stderr' # Valid values are combinations of stderr, file,
+                            # syslog and eventlog, depending on platform.
+#log_directory = 'pg_log'   # subdirectory where logfiles are written
+                            # if 'file' log_destination is used.
+                            # May be specified absolute or relative to PGDATA
+#log_filename_prefix = 'postgresql_' # prefix for logfile names
+#log_rotation_age = 1440    # Automatic rotation of logfiles will happen if
+                            # specified age in minutes is reached. 0 to disable.
+#log_rotation_size = 10240  # Automatic rotation of logfiles will happen if
+                            # specified size in kb is reached. 0 to disable.
+
 #syslog_facility = 'LOCAL0'
 #syslog_ident = 'postgres'

Index: src/include/storage/pmsignal.h
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/include/storage/pmsignal.h,v
retrieving revision 1.9
diff -u -r1.9 pmsignal.h
--- src/include/storage/pmsignal.h    19 Jul 2004 02:47:15 -0000    1.9
+++ src/include/storage/pmsignal.h    27 Jul 2004 10:33:51 -0000
@@ -25,7 +25,7 @@
     PMSIGNAL_PASSWORD_CHANGE,    /* pg_pwd file has changed */
     PMSIGNAL_WAKEN_CHILDREN,    /* send a SIGUSR1 signal to all backends */
     PMSIGNAL_WAKEN_ARCHIVER,    /* send a NOTIFY signal to xlog archiver */
-
+    PMSIGNAL_ROTATE_LOGFILE,    /* send SIGUSR1 to syslogger to rotate logfile */
     NUM_PMSIGNALS                /* Must be last value of enum! */
 } PMSignalReason;

Index: src/include/utils/elog.h
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/include/utils/elog.h,v
retrieving revision 1.70
diff -u -r1.70 elog.h
--- src/include/utils/elog.h    6 Jul 2004 19:51:59 -0000    1.70
+++ src/include/utils/elog.h    27 Jul 2004 10:33:51 -0000
@@ -185,10 +185,10 @@
 #define LOG_DESTINATION_STDERR   1
 #define LOG_DESTINATION_SYSLOG   2
 #define LOG_DESTINATION_EVENTLOG 4
+#define LOG_DESTINATION_FILE     8

 /* Other exported functions */
 extern void DebugFileOpen(void);
-
 /*
  * Write errors to stderr (or by equal means when stderr is
  * not available). Used before ereport/elog can be used

pgsql-patches by date:

Previous
From: Alvaro Herrera
Date:
Subject: [subxacts] Some docs
Next
From: Andreas Pflug
Date:
Subject: Admin functions contrib