|
8 | 8 | * |
9 | 9 | * |
10 | 10 | * 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 $ |
12 | 12 | * |
13 | 13 | * NOTES |
14 | 14 | * this is the "main" module of the postgres backend and |
@@ -84,6 +84,9 @@ sigjmp_buf Warn_restart; |
84 | 84 | bool Warn_restart_ready = false; |
85 | 85 | bool InError = false; |
86 | 86 |
|
| 87 | +/* flag for logging end of session */ |
| 88 | +bool Log_disconnections = false; |
| 89 | + |
87 | 90 | /* |
88 | 91 | * Flags for expensive function optimization -- JMH 3/9/92 |
89 | 92 | */ |
@@ -149,6 +152,7 @@ static void start_xact_command(void); |
149 | 152 | static void finish_xact_command(void); |
150 | 153 | static void SigHupHandler(SIGNAL_ARGS); |
151 | 154 | static void FloatExceptionHandler(SIGNAL_ARGS); |
| 155 | +static void log_session_end(void); |
152 | 156 |
|
153 | 157 |
|
154 | 158 | /* ---------------------------------------------------------------- |
@@ -2406,7 +2410,10 @@ PostgresMain(int argc, char *argv[], const char *username) |
2406 | 2410 | * other output options. |
2407 | 2411 | */ |
2408 | 2412 | if (debug_flag >= 1) |
| 2413 | + { |
2409 | 2414 | SetConfigOption("log_connections", "true", debug_context, gucsource); |
| 2415 | + SetConfigOption("log_disconnections", "true", debug_context, gucsource); |
| 2416 | + } |
2410 | 2417 | if (debug_flag >= 2) |
2411 | 2418 | SetConfigOption("log_statement", "true", debug_context, gucsource); |
2412 | 2419 | if (debug_flag >= 3) |
@@ -2435,6 +2442,12 @@ PostgresMain(int argc, char *argv[], const char *username) |
2435 | 2442 | gucopts = lnext(gucopts); |
2436 | 2443 | SetConfigOption(name, value, PGC_BACKEND, PGC_S_CLIENT); |
2437 | 2444 | } |
| 2445 | + |
| 2446 | + /* |
| 2447 | + * set up handler to log session end. |
| 2448 | + */ |
| 2449 | + if (IsUnderPostmaster && Log_disconnections) |
| 2450 | + on_proc_exit(log_session_end,0); |
2438 | 2451 | } |
2439 | 2452 |
|
2440 | 2453 | /* |
@@ -3178,3 +3191,61 @@ ShowUsage(const char *title) |
3178 | 3191 |
|
3179 | 3192 | pfree(str.data); |
3180 | 3193 | } |
| 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 | +} |
0 commit comments