I've work in the a little patch to add into the \conninfo of psql command the connection time against a server backend
Maybe could add after, the precheck to if the connection is alive. I've take first look to the pqPacketSend, my idea is send a ECHO packet over to the database connection. If someone has a better idea it would be great to know. Regards! -- Rodrigo Ramírez Norambuena http://www.rodrigoramirez.com/
From e43fa7055af61262a95edcb7f18df1de8e4cf593 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Ram=C3=ADrez=20Norambuena?= <a...@rodrigoramirez.com> Date: Tue, 3 Sep 2019 17:15:39 -0400 Subject: [PATCH] Add to the \conninfo for time of current connection: Add extra information for \conninfo psql command to show the current time lapsed of the connection. The time will be reestablished again if the connection is reset and reconnected successfully with the backend. diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index c0a7a5566e..d1d249e5db 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -263,6 +263,32 @@ HandleSlashCmds(PsqlScanState scan_state, } +static void +printTimeConnected(void) +{ + + int hours = 0 , minutes = 0 , seconds = 0; + int secs = time(NULL) - pset.connected; + + #define SECOND (1) + #define MINUTE (SECOND * 60) + #define HOUR (MINUTE * 60) + + if (secs > HOUR) { + hours = (secs / HOUR); + secs -= (hours * HOUR); + } + if (secs > MINUTE) { + minutes = (secs / MINUTE); + secs -= (minutes * MINUTE); + } + if (secs > 0) + seconds = secs; + + printf(_("The time connection is %d hours, %d minutes and %d seconds.\n"), + hours, minutes, seconds); +} + /* * Subroutine to actually try to execute a backslash command. * @@ -624,6 +650,7 @@ exec_command_conninfo(PsqlScanState scan_state, bool active_branch) } printSSLInfo(); printGSSInfo(); + printTimeConnected(); } } @@ -3318,6 +3345,7 @@ SyncVariables(void) pset.encoding = PQclientEncoding(pset.db); pset.popt.topt.encoding = pset.encoding; pset.sversion = PQserverVersion(pset.db); + pset.connected = time(NULL); SetVariable(pset.vars, "DBNAME", PQdb(pset.db)); SetVariable(pset.vars, "USER", PQuser(pset.db)); diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 44a782478d..c9d4eaf0e7 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -408,7 +408,10 @@ CheckConnection(void) UnsyncVariables(); } else + { + pset.connected = time(NULL); fprintf(stderr, _("Succeeded.\n")); + } } return OK; diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h index 5be5091f0e..ed3ab368f5 100644 --- a/src/bin/psql/settings.h +++ b/src/bin/psql/settings.h @@ -141,6 +141,7 @@ typedef struct _psqlSettings const char *prompt3; PGVerbosity verbosity; /* current error verbosity level */ PGContextVisibility show_context; /* current context display level */ + int connected; /* unixtime for connected init time */ } PsqlSettings; extern PsqlSettings pset; -- 2.21.0