On Thu, Sep 5, 2019 at 2:22 PM Tom Lane <t...@sss.pgh.pa.us> wrote:
>
> =?UTF-8?Q?Rodrigo_Ram=C3=ADrez_Norambuena?= <decipher...@gmail.com> writes:
> > On Tue, Sep 3, 2019 at 11:06 PM Michael Paquier <mich...@paquier.xyz> wrote:
> >> You can do basically the same thing by looking at
> >> pg_stat_activity.backend_start and compare it with the clock
> >> timestamp.  Doing it at SQL level the way you want has also the
> >> advantage to offer you a modular format output.
>
> > What are you say seams little trick to what I propose in this patch
> > because you'll need filter what is your connection, and if the
> > connection is through  the connection pooler could be more.
> > The main goal this is only getting information from the client side
> > (psql) as frontend.
>
> A couple of thoughts on this ---
> [...]

Hi Tom, about your concerns and feedback I send a new proposal of
patch related with the original idea.

Regards!
-- 
Rodrigo Ramírez Norambuena
http://www.rodrigoramirez.com/
From c71a88d67bfa7f3c7fe2d4a684057f364027e1ec 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 start time current connection:

Add extra information for \conninfo psql command to show the start time
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 93953fc8e7..1758e20ad7 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -263,6 +263,19 @@ HandleSlashCmds(PsqlScanState scan_state,
 }
 
 
+static void
+printTimeConnected(void)
+{
+
+	const char *strftime_fmt;
+	char timebuf[128];
+
+	strftime_fmt = "%c";
+	strftime(timebuf, sizeof(timebuf), strftime_fmt, localtime(&pset.connection_start));
+
+	printf(_("The connection started at %s\n"), timebuf);
+}
+
 /*
  * Subroutine to actually try to execute a backslash command.
  *
@@ -624,6 +637,7 @@ exec_command_conninfo(PsqlScanState scan_state, bool active_branch)
 			}
 			printSSLInfo();
 			printGSSInfo();
+			printTimeConnected();
 		}
 	}
 
@@ -3326,6 +3340,7 @@ SyncVariables(void)
 	pset.encoding = PQclientEncoding(pset.db);
 	pset.popt.topt.encoding = pset.encoding;
 	pset.sversion = PQserverVersion(pset.db);
+	pset.connection_start = time(NULL);
 
 	SetVariable(pset.vars, "DBNAME", PQdb(pset.db));
 	SetVariable(pset.vars, "USER", PQuser(pset.db));
diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h
index 5be5091f0e..955e696d8e 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 */
+	time_t connection_start;	/* unixtime for start time connection */
 } PsqlSettings;
 
 extern PsqlSettings pset;
-- 
2.21.0

Reply via email to