Re: [PATCHES] log session end - again - Mailing list pgsql-hackers

From Bruce Momjian
Subject Re: [PATCHES] log session end - again
Date
Msg-id 200402170356.i1H3u7Q03428@candle.pha.pa.us
Whole thread Raw
In response to Re: [PATCHES] log session end - again  (Andrew Dunstan <andrew@dunslane.net>)
List pgsql-hackers
Patch applied.  Thanks.

I modified the doc wording a bit --- patch attached.

---------------------------------------------------------------------------


Andrew Dunstan wrote:
>
> You can find it here.
>
> http://archives.postgresql.org/pgsql-patches/2004-02/msg00072.php
>
> I know Neil was reviewing it and had a minor doc style quibble, as well
> as the question he raised on -hackers about psql tab completion.
>
> cheers
>
> andrew
>
> Bruce Momjian wrote:
>
> >Andrew Dunstan wrote:
> >
> >
> >>Based on Larry's idea, I had in mind to provide a third escape in the
> >>log_line_info string (in addition to the %U and %D that I had previously
> >>done) of %S for sessionid, which would look something like this:
> >>402251fc.713f
> >>
> >>I will start redoing this feature when the log_disconnections patch is
> >>dealt with.
> >>
> >>
> >
> >Andrew, I can't find the log_disconnections patch.  I know I saw it, but
> >I can't find it now.  Would you resent it please?
> >
> >
> >
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
>                http://www.postgresql.org/docs/faqs/FAQ.html
>

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
<http://ads.postgresql.org/redirect.php?lvl=sponsor>

Search for:

Search:

Format:  Results/page:


    <http://ads.postgresql.org/redirect.php?lvl=free>
pgsql-announce <http://archives.postgresql.org/pgsql-announce>

*Users Lists*
pgsql-admin <http://archives.postgresql.org/pgsql-admin>
pgsql-advocacy <http://archives.postgresql.org/pgsql-advocacy>
pgsql-benchmarks <http://archives.postgresql.org/pgsql-benchmarks>
pgsql-bugs <http://archives.postgresql.org/pgsql-bugs>
pgsql-chat <http://archives.postgresql.org/pgsql-chat>
pgsql-docs <http://archives.postgresql.org/pgsql-docs>
pgsql-cygwin <http://archives.postgresql.org/pgsql-cygwin>
pgsql-general <http://archives.postgresql.org/pgsql-general>
pgsql-interfaces <http://archives.postgresql.org/pgsql-interfaces>
pgsql-jdbc <http://archives.postgresql.org/pgsql-jdbc>
pgsql-jobs <http://archives.postgresql.org/pgsql-jobs>
pgsql-novice <http://archives.postgresql.org/pgsql-novice>
pgsql-odbc <http://archives.postgresql.org/pgsql-odbc>
pgsql-performance <http://archives.postgresql.org/pgsql-performance>
pgsql-php <http://archives.postgresql.org/pgsql-php>
pgsql-ports <http://archives.postgresql.org/pgsql-ports>
pgsql-sql <http://archives.postgresql.org/pgsql-sql>
pgsql-www <http://archives.postgresql.org/pgsql-www>

*Developer Lists*
pgsql-committers <http://archives.postgresql.org/pgsql-committers>
pgsql-hackers <http://archives.postgresql.org/pgsql-hackers>
pgsql-hackers-win32 <http://archives.postgresql.org/pgsql-hackers-win32>
pgsql-patches <http://archives.postgresql.org/pgsql-patches>

*Regional Lists*
pgsql-de-allgemein <http://archives.postgresql.org/pgsql-de-allgemein>
pgsql-fr-generale <http://archives.postgresql.org/pgsql-fr-generale>
pgsql-tr-genel <http://archives.postgresql.org/pgsql-tr-genel>

*Project Lists*
pgadmin-hackers <http://archives.postgresql.org/pgadmin-hackers>
pgadmin-support <http://archives.postgresql.org/pgadmin-support>

*User Groups*
San Francisco <http://archives.postgresql.org/sfpug>




    Re: log session end - again

------------------------------------------------------------------------

    * *From*: *Andrew Dunstan <andrew ( at ) dunslane ( dot ) net
      <mailto:andrew@DOMAIN.HIDDEN>>*
    * *To*: *"Patches (PostgreSQL)" <pgsql-patches ( at ) postgresql (
      dot ) org <mailto:pgsql-patches@DOMAIN.HIDDEN>>*
    * *Subject*: *Re: log session end - again*
    * Date: Tue, 03 Feb 2004 20:03:42 -0500

------------------------------------------------------------------------


Peter Eisentraut wrote:

    Andrew Dunstan wrote:


        This patch brings up to date what I did last year (now unfortunately
        bitrotted) to allow the logging of the end of a session, enabled by
        the config setting "log_session_end - true". It produces lines like
        these:



    If we log "session" end, shouldn't we also log "session" start,
    rather than "connection" start? It seems there should be some
    symmetry here, also for the configuration parameter names.




OK, this version of the patch uses the config parameter name
"log_disconnections" for the sake of symmetry, and changes the message
wording slightly accordingly.

cheers

andrew

Index: doc/src/sgml/runtime.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql-server/doc/src/sgml/runtime.sgml,v
retrieving revision 1.235
diff -c -r1.235 runtime.sgml
*** doc/src/sgml/runtime.sgml    27 Jan 2004 16:51:43 -0000    1.235
--- doc/src/sgml/runtime.sgml    2 Feb 2004 19:08:42 -0000
***************
*** 1825,1830 ****
--- 1825,1844 ----


       <varlistentry>
+       <term><varname>log_disconnections</varname> (<type>boolean</type>)</term>
+       <listitem>
+        <para>
+        This outputs a line in the server logs similar to LOG_CONNECTIONS
+        but at session termination, and includes the duration of the
+        session.   This is off by default. This option can only be set at
+        server start or in the <filename>postgresql.conf</filename>
+        configuration file.
+        </para>
+       </listitem>
+      </varlistentry>
+
+
+      <varlistentry>
        <term><varname>log_duration</varname> (<type>boolean</type>)</term>
        <listitem>
         <para>
Index: src/backend/postmaster/postmaster.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/postmaster/postmaster.c,v
retrieving revision 1.364
diff -c -r1.364 postmaster.c
*** src/backend/postmaster/postmaster.c    28 Jan 2004 21:02:40 -0000    1.364
--- src/backend/postmaster/postmaster.c    2 Feb 2004 19:08:43 -0000
***************
*** 2438,2443 ****
--- 2438,2450 ----
       * Signal handlers setting is moved to tcop/postgres...
       */

+     /* save start time for end of session reporting */
+     gettimeofday(&(port->session_start),NULL);
+
+     /* set these to empty in case they are needed before we set them up */
+     port->remote_host = "";
+     port->remote_port = "";
+
      /* Save port etc. for ps status */
      MyProcPort = port;

***************
*** 2492,2497 ****
--- 2499,2510 ----
          snprintf(tmphost, sizeof(tmphost), "%s:%s", remote_host, remote_port);
          StrNCpy(remote_host, tmphost, sizeof(remote_host));
      }
+
+     /*
+      * save remote_host and remote_port in port stucture
+      */
+     port->remote_host = strdup(remote_host);
+     port->remote_port = strdup(remote_port);

      /*
       * Ready to begin client interaction.  We will give up and exit(0)
Index: src/backend/tcop/postgres.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/tcop/postgres.c,v
retrieving revision 1.387
diff -c -r1.387 postgres.c
*** src/backend/tcop/postgres.c    28 Jan 2004 21:02:40 -0000    1.387
--- src/backend/tcop/postgres.c    2 Feb 2004 19:08:43 -0000
***************
*** 84,89 ****
--- 84,92 ----
  bool        Warn_restart_ready = false;
  bool        InError = false;

+ /* flag for logging end of session */
+ bool        Log_disconnections = false;
+
  /*
   * Flags for expensive function optimization -- JMH 3/9/92
   */
***************
*** 149,154 ****
--- 152,158 ----
  static void finish_xact_command(void);
  static void SigHupHandler(SIGNAL_ARGS);
  static void FloatExceptionHandler(SIGNAL_ARGS);
+ static void log_session_end(void);


  /* ----------------------------------------------------------------
***************
*** 2406,2412 ****
--- 2410,2419 ----
       * other output options.
       */
      if (debug_flag >= 1)
+     {
          SetConfigOption("log_connections", "true", debug_context, gucsource);
+         SetConfigOption("log_disconnections", "true", debug_context, gucsource);
+     }
      if (debug_flag >= 2)
          SetConfigOption("log_statement", "true", debug_context, gucsource);
      if (debug_flag >= 3)
***************
*** 2435,2440 ****
--- 2442,2453 ----
              gucopts = lnext(gucopts);
              SetConfigOption(name, value, PGC_BACKEND, PGC_S_CLIENT);
          }
+
+         /*
+          * set up handler to log session end.
+          */
+         if (IsUnderPostmaster && Log_disconnections)
+             on_proc_exit(log_session_end,0);
      }

      /*
***************
*** 3172,3175 ****
--- 3185,3246 ----
               errdetail("%s", str.data)));

      pfree(str.data);
+ }
+
+ /*
+  * on_proc_exit handler to log end of session
+  */
+ static void
+ log_session_end(void)
+ {
+     Port * port = MyProcPort;
+     struct timeval end;
+     int  hours, minutes, seconds;
+
+     char session_time[20];
+     char uname[6+NAMEDATALEN];
+     char dbname[10+NAMEDATALEN];
+     char remote_host[7 + NI_MAXHOST];
+     char remote_port[7 + NI_MAXSERV];
+
+     snprintf(uname, sizeof(uname)," user=%s",port->user_name);
+     snprintf(dbname, sizeof(dbname)," database=%s",port->database_name);
+     snprintf(remote_host,sizeof(remote_host)," host=%s",
+              port->remote_host);
+     /* prevent redundant or empty reporting of port */
+     if (!LogSourcePort && strlen(port->remote_port))
+         snprintf(remote_port,sizeof(remote_port)," port=%s",port->remote_port);
+     else
+         remote_port[0] = '\0';
+
+
+     gettimeofday(&end,NULL);
+
+     if (end.tv_usec < port->session_start.tv_usec)
+     {
+         end.tv_sec--;
+         end.tv_usec += 1000000;
+     }
+     end.tv_sec -= port->session_start.tv_sec;
+     end.tv_usec -= port->session_start.tv_usec;
+
+     hours = end.tv_sec / 3600;
+     end.tv_sec %= 3600;
+     minutes = end.tv_sec / 60;
+     seconds = end.tv_sec % 60;
+
+     /* if time has gone backwards for some reason say so, or print time */
+
+     if (end.tv_sec < 0)
+         snprintf(session_time,sizeof(session_time),"negative!");
+     else
+         /* for stricter accuracy here we could round - this is close enough */
+         snprintf(session_time, sizeof(session_time),"%d:%02d:%02d.%02ld",
+                  hours, minutes, seconds, end.tv_usec/10000);
+
+     ereport(
+         LOG,
+         (errmsg("disconnection: session time: %s%s%s%s%s",
+                 session_time,uname,dbname,remote_host,remote_port)));
+
  }
Index: src/backend/utils/misc/guc.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/utils/misc/guc.c,v
retrieving revision 1.182
diff -c -r1.182 guc.c
*** src/backend/utils/misc/guc.c    31 Jan 2004 05:09:41 -0000    1.182
--- src/backend/utils/misc/guc.c    2 Feb 2004 19:08:44 -0000
***************
*** 65,70 ****
--- 65,71 ----

  /* XXX these should appear in other modules' header files */
  extern bool Log_connections;
+ extern bool Log_disconnections;
  extern bool check_function_bodies;
  extern int    PreAuthDelay;
  extern int    AuthenticationTimeout;
***************
*** 499,504 ****
--- 500,513 ----
              NULL
          },
          &Log_connections,
+         false, NULL, NULL
+     },
+     {
+         {"log_disconnections", PGC_BACKEND, LOGGING_WHAT,
+          gettext_noop("Logs end of a session, including duration"),
+          NULL
+         },
+         &Log_disconnections,
          false, NULL, NULL
      },
      {
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.101
diff -c -r1.101 postgresql.conf.sample
*** src/backend/utils/misc/postgresql.conf.sample    24 Jan 2004 20:00:45 -0000    1.101
--- src/backend/utils/misc/postgresql.conf.sample    2 Feb 2004 19:08:44 -0000
***************
*** 179,184 ****
--- 179,185 ----
  #debug_print_plan = false
  #debug_pretty_print = false
  #log_connections = false
+ #log_disconnections = false
  #log_duration = false
  #log_pid = false
  #log_statement = false
Index: src/include/libpq/libpq-be.h
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/include/libpq/libpq-be.h,v
retrieving revision 1.39
diff -c -r1.39 libpq-be.h
*** src/include/libpq/libpq-be.h    20 Dec 2003 17:31:21 -0000    1.39
--- src/include/libpq/libpq-be.h    2 Feb 2004 19:08:45 -0000
***************
*** 47,52 ****
--- 47,55 ----
      ProtocolVersion proto;        /* FE/BE protocol version */
      SockAddr    laddr;            /* local addr (postmaster) */
      SockAddr    raddr;            /* remote addr (client) */
+     char        *remote_host;   /* name (or ip addr) of remote host */
+     char        *remote_port;   /* text rep of remote port */
+     struct timeval  session_start;  /* for session duration logging */
      CAC_state    canAcceptConnections;    /* postmaster connection status */

      /*

------------------------------------------------------------------------

    * *References*:
          o *log session end - again <msg00047.php>*
                + /From:/ Andrew Dunstan
          o *Re: log session end - again <msg00048.php>*
                + /From:/ Peter Eisentraut

------------------------------------------------------------------------

    * Prev by Date: *New win32 signals patch (3) <msg00071.php>*
    * Next by Date: *Re: fix memcpy() overlap <msg00073.php>*
    * Previous by thread: *Re: log session end - again <msg00048.php>*
    * Next by thread: *fix memcpy() overlap <msg00049.php>*
    * Index(es):
          o *Main* <index.php#00072>
          o *Thread* <threads.php#00072>

* Home <http://archives.postgresql.org/> | Main Index <index.php> |
Thread Index <threads.php> *


pgsql-hackers by date:

Previous
From: Neil Conway
Date:
Subject: casting zero-length strings
Next
From: Bruce Momjian
Date:
Subject: Re: [PATCHES] dollar quoting