Skip to content

Commit c53611e

Browse files
committed
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_disconnections". Andrew Dunstan
1 parent 1f17316 commit c53611e

File tree

6 files changed

+116
-5
lines changed

6 files changed

+116
-5
lines changed

doc/src/sgml/runtime.sgml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/runtime.sgml,v 1.237 2004/02/13 12:25:09 wieck Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/runtime.sgml,v 1.238 2004/02/17 03:54:56 momjian Exp $
33
-->
44

55
<Chapter Id="runtime">
@@ -1909,6 +1909,20 @@ SET ENABLE_SEQSCAN TO OFF;
19091909
</varlistentry>
19101910

19111911

1912+
<varlistentry>
1913+
<term><varname>log_disconnections</varname> (<type>boolean</type>)</term>
1914+
<listitem>
1915+
<para>
1916+
This outputs a line in the server logs similar to LOG_CONNECTIONS
1917+
but at session termination, and includes the duration of the
1918+
session. This is off by default. This option can only be set at
1919+
server start or in the <filename>postgresql.conf</filename>
1920+
configuration file.
1921+
</para>
1922+
</listitem>
1923+
</varlistentry>
1924+
1925+
19121926
<varlistentry>
19131927
<term><varname>log_duration</varname> (<type>boolean</type>)</term>
19141928
<listitem>

src/backend/postmaster/postmaster.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.366 2004/02/11 22:25:02 tgl Exp $
40+
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.367 2004/02/17 03:54:56 momjian Exp $
4141
*
4242
* NOTES
4343
*
@@ -2428,6 +2428,13 @@ BackendInit(Port *port)
24282428
* Signal handlers setting is moved to tcop/postgres...
24292429
*/
24302430

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

@@ -2483,6 +2490,12 @@ BackendInit(Port *port)
24832490
StrNCpy(remote_host, tmphost, sizeof(remote_host));
24842491
}
24852492

2493+
/*
2494+
* save remote_host and remote_port in port stucture
2495+
*/
2496+
port->remote_host = strdup(remote_host);
2497+
port->remote_port = strdup(remote_port);
2498+
24862499
/*
24872500
* Ready to begin client interaction. We will give up and exit(0)
24882501
* after a time delay, so that a broken client can't hog a connection

src/backend/tcop/postgres.c

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.389 2004/02/06 19:36:18 wieck Exp $
11+
* $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.390 2004/02/17 03:54:57 momjian Exp $
1212
*
1313
* NOTES
1414
* this is the "main" module of the postgres backend and
@@ -84,6 +84,9 @@ sigjmp_buf Warn_restart;
8484
bool Warn_restart_ready = false;
8585
bool InError = false;
8686

87+
/* flag for logging end of session */
88+
bool Log_disconnections = false;
89+
8790
/*
8891
* Flags for expensive function optimization -- JMH 3/9/92
8992
*/
@@ -149,6 +152,7 @@ static void start_xact_command(void);
149152
static void finish_xact_command(void);
150153
static void SigHupHandler(SIGNAL_ARGS);
151154
static void FloatExceptionHandler(SIGNAL_ARGS);
155+
static void log_session_end(void);
152156

153157

154158
/* ----------------------------------------------------------------
@@ -2406,7 +2410,10 @@ PostgresMain(int argc, char *argv[], const char *username)
24062410
* other output options.
24072411
*/
24082412
if (debug_flag >= 1)
2413+
{
24092414
SetConfigOption("log_connections", "true", debug_context, gucsource);
2415+
SetConfigOption("log_disconnections", "true", debug_context, gucsource);
2416+
}
24102417
if (debug_flag >= 2)
24112418
SetConfigOption("log_statement", "true", debug_context, gucsource);
24122419
if (debug_flag >= 3)
@@ -2435,6 +2442,12 @@ PostgresMain(int argc, char *argv[], const char *username)
24352442
gucopts = lnext(gucopts);
24362443
SetConfigOption(name, value, PGC_BACKEND, PGC_S_CLIENT);
24372444
}
2445+
2446+
/*
2447+
* set up handler to log session end.
2448+
*/
2449+
if (IsUnderPostmaster && Log_disconnections)
2450+
on_proc_exit(log_session_end,0);
24382451
}
24392452

24402453
/*
@@ -3178,3 +3191,61 @@ ShowUsage(const char *title)
31783191

31793192
pfree(str.data);
31803193
}
3194+
3195+
/*
3196+
* on_proc_exit handler to log end of session
3197+
*/
3198+
static void
3199+
log_session_end(void)
3200+
{
3201+
Port * port = MyProcPort;
3202+
struct timeval end;
3203+
int hours, minutes, seconds;
3204+
3205+
char session_time[20];
3206+
char uname[6+NAMEDATALEN];
3207+
char dbname[10+NAMEDATALEN];
3208+
char remote_host[7 + NI_MAXHOST];
3209+
char remote_port[7 + NI_MAXSERV];
3210+
3211+
snprintf(uname, sizeof(uname)," user=%s",port->user_name);
3212+
snprintf(dbname, sizeof(dbname)," database=%s",port->database_name);
3213+
snprintf(remote_host,sizeof(remote_host)," host=%s",
3214+
port->remote_host);
3215+
/* prevent redundant or empty reporting of port */
3216+
if (!LogSourcePort && strlen(port->remote_port))
3217+
snprintf(remote_port,sizeof(remote_port)," port=%s",port->remote_port);
3218+
else
3219+
remote_port[0] = '\0';
3220+
3221+
3222+
gettimeofday(&end,NULL);
3223+
3224+
if (end.tv_usec < port->session_start.tv_usec)
3225+
{
3226+
end.tv_sec--;
3227+
end.tv_usec += 1000000;
3228+
}
3229+
end.tv_sec -= port->session_start.tv_sec;
3230+
end.tv_usec -= port->session_start.tv_usec;
3231+
3232+
hours = end.tv_sec / 3600;
3233+
end.tv_sec %= 3600;
3234+
minutes = end.tv_sec / 60;
3235+
seconds = end.tv_sec % 60;
3236+
3237+
/* if time has gone backwards for some reason say so, or print time */
3238+
3239+
if (end.tv_sec < 0)
3240+
snprintf(session_time,sizeof(session_time),"negative!");
3241+
else
3242+
/* for stricter accuracy here we could round - this is close enough */
3243+
snprintf(session_time, sizeof(session_time),"%d:%02d:%02d.%02ld",
3244+
hours, minutes, seconds, end.tv_usec/10000);
3245+
3246+
ereport(
3247+
LOG,
3248+
(errmsg("disconnection: session time: %s%s%s%s%s",
3249+
session_time,uname,dbname,remote_host,remote_port)));
3250+
3251+
}

src/backend/utils/misc/guc.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Written by Peter Eisentraut <peter_e@gmx.net>.
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.186 2004/02/06 19:36:18 wieck Exp $
13+
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.187 2004/02/17 03:54:57 momjian Exp $
1414
*
1515
*--------------------------------------------------------------------
1616
*/
@@ -65,6 +65,7 @@
6565

6666
/* XXX these should appear in other modules' header files */
6767
extern bool Log_connections;
68+
extern bool Log_disconnections;
6869
extern bool check_function_bodies;
6970
extern int PreAuthDelay;
7071
extern int AuthenticationTimeout;
@@ -499,6 +500,14 @@ static struct config_bool ConfigureNamesBool[] =
499500
&Log_connections,
500501
false, NULL, NULL
501502
},
503+
{
504+
{"log_disconnections", PGC_BACKEND, LOGGING_WHAT,
505+
gettext_noop("Logs end of a session, including duration"),
506+
NULL
507+
},
508+
&Log_disconnections,
509+
false, NULL, NULL
510+
},
502511
{
503512
{"log_timestamp", PGC_SIGHUP, LOGGING_WHAT,
504513
gettext_noop("Prefixes server log messages with a time stamp."),

src/backend/utils/misc/postgresql.conf.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
#debug_print_plan = false
183183
#debug_pretty_print = false
184184
#log_connections = false
185+
#log_disconnections = false
185186
#log_duration = false
186187
#log_pid = false
187188
#log_statement = false

src/include/libpq/libpq-be.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
1212
* Portions Copyright (c) 1994, Regents of the University of California
1313
*
14-
* $PostgreSQL: pgsql/src/include/libpq/libpq-be.h,v 1.39 2003/12/20 17:31:21 momjian Exp $
14+
* $PostgreSQL: pgsql/src/include/libpq/libpq-be.h,v 1.40 2004/02/17 03:54:57 momjian Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
@@ -47,6 +47,9 @@ typedef struct Port
4747
ProtocolVersion proto; /* FE/BE protocol version */
4848
SockAddr laddr; /* local addr (postmaster) */
4949
SockAddr raddr; /* remote addr (client) */
50+
char *remote_host; /* name (or ip addr) of remote host */
51+
char *remote_port; /* text rep of remote port */
52+
struct timeval session_start; /* for session duration logging */
5053
CAC_state canAcceptConnections; /* postmaster connection status */
5154

5255
/*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy