From f5835b4b8d569e4d7814886f9e7b90c36d8a4fb5 Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Tue, 14 Jun 2005 21:04:42 +0000
Subject: [PATCH] Add pg_postmaster_start_time() function.

Euler Taveira de Oliveira
Matthias Schmidt
---
 doc/src/sgml/func.sgml              | 17 ++++++++++++++++-
 src/backend/postmaster/postmaster.c | 17 ++++++++++++++++-
 src/backend/tcop/postgres.c         | 17 ++++++++++++++++-
 src/backend/utils/adt/timestamp.c   |  8 +++++++-
 src/include/catalog/pg_proc.h       |  6 +++++-
 src/include/utils/timestamp.h       |  6 +++++-
 6 files changed, 65 insertions(+), 6 deletions(-)

diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 22baa22aa6d..43df7d595c9 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.254 2005/06/13 02:40:04 neilc Exp $
+$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.255 2005/06/14 21:04:38 momjian Exp $
 PostgreSQL documentation
 -->
 
@@ -8119,6 +8119,12 @@ select current_date + s.a as dates from generate_series(0,14,7) as s(a);
        <entry>session user name</entry>
       </row>
 
+      <row>
+       <entry><function>pg_postmaster_start_time()</function></entry>
+       <entry><type>timestamp with time zone</type></entry>
+       <entry><command>postmaster</> start time</entry>
+      </row>
+
       <row>
        <entry><function>user</function></entry>
        <entry><type>name</type></entry>
@@ -8216,6 +8222,15 @@ SET search_path TO <replaceable>schema</> <optional>, <replaceable>schema</>, ..
      Unix-domain socket.
    </para>
 
+   <indexterm zone="functions-info">
+    <primary>pg_postmaster_start_time</primary>
+   </indexterm>
+
+   <para>
+     <function>pg_postmaster_start_time()</function> returns the timestamp with time zone
+     when the <command>postmaster</> started.
+   </para>
+
    <indexterm zone="functions-info">
     <primary>version</primary>
    </indexterm>
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index af28bf652fb..03057dbcb21 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -37,7 +37,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.452 2005/06/09 22:01:12 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.453 2005/06/14 21:04:39 momjian Exp $
  *
  * NOTES
  *
@@ -222,6 +222,9 @@ static bool FatalError = false; /* T if recovering from backend crash */
 bool		ClientAuthInProgress = false;		/* T during new-client
 												 * authentication */
 
+/* Backend startup time */
+TimestampTz	StartTime;
+
 /*
  * State for assigning random salts and cancel keys.
  * Also, the global MyCancelKey passes the cancel key assigned to a given
@@ -330,6 +333,7 @@ typedef struct
 	InheritableSocket pgStatPipe0;
 	InheritableSocket pgStatPipe1;
 	pid_t PostmasterPid;
+	TimestampTz StartTime;
 #ifdef WIN32
 	HANDLE PostmasterHandle;
 	HANDLE initial_signal_pipe;
@@ -372,6 +376,9 @@ PostmasterMain(int argc, char *argv[])
 	char	   *userDoption = NULL;
 	int			i;
 
+	AbsoluteTime		StartTimeSec;	/* integer part */
+	int			StartTimeUSec;	/* microsecond part */
+
 	/* This will call exit() if strdup() fails. */
 	progname = get_progname(argv[0]);	
 
@@ -914,6 +921,12 @@ PostmasterMain(int argc, char *argv[])
 	 */
 	StartupPID = StartupDataBase();
 
+	/*
+	 * Get start up time
+	 */
+	StartTimeSec = GetCurrentAbsoluteTimeUsec(&StartTimeUSec);
+	StartTime = AbsoluteTimeUsecToTimestampTz(StartTimeSec, StartTimeUSec);
+
 	status = ServerLoop();
 
 	/*
@@ -3603,6 +3616,7 @@ save_backend_variables(BackendParameters *param, Port *port,
 	write_inheritable_socket(&param->pgStatPipe1, pgStatPipe[1], childPid);
 
 	param->PostmasterPid = PostmasterPid;
+	param->StartTime = StartTime;
 
 #ifdef WIN32
 	param->PostmasterHandle = PostmasterHandle;
@@ -3805,6 +3819,7 @@ restore_backend_variables(BackendParameters *param, Port *port)
 	read_inheritable_socket(&pgStatPipe[1], &param->pgStatPipe1);
 
 	PostmasterPid = param->PostmasterPid;
+	StartTime = param->StartTime;
 
 #ifdef WIN32
 	PostmasterHandle = param->PostmasterHandle;
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 58b1ffbce9a..f1c65bacc43 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.447 2005/06/03 23:05:29 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.448 2005/06/14 21:04:40 momjian Exp $
  *
  * NOTES
  *	  this is the "main" module of the postgres backend and
@@ -149,6 +149,9 @@ static int	UseNewLine = 0;		/* Use EOF as query delimiters */
 #endif   /* TCOP_DONTUSENEWLINE */
 
 
+/* Backend startup time */
+TimestampTz	StartTime;
+
 /* ----------------------------------------------------------------
  *		decls for routines only used in this file
  * ----------------------------------------------------------------
@@ -2380,6 +2383,9 @@ PostgresMain(int argc, char *argv[], const char *username)
 	sigjmp_buf	local_sigjmp_buf;
 	volatile bool send_rfq = true;
 
+	AbsoluteTime            StartTimeSec;   /* integer part */
+      int                     StartTimeUSec;  /* microsecond part */
+
 #define PendingConfigOption(name,val) \
 	(guc_names = lappend(guc_names, pstrdup(name)), \
 	 guc_values = lappend(guc_values, pstrdup(val)))
@@ -2969,6 +2975,15 @@ PostgresMain(int argc, char *argv[], const char *username)
 	 */
 	pgstat_bestart();
 
+	/*
+	 * Get stand-alone backend startup time
+	 */
+	if (!IsUnderPostmaster)
+	{
+		StartTimeSec = GetCurrentAbsoluteTimeUsec(&StartTimeUSec);
+		StartTime = AbsoluteTimeUsecToTimestampTz(StartTimeSec, StartTimeUSec);
+	}
+
 	/*
 	 * POSTGRES main processing loop begins here
 	 *
diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c
index 27e0523f49c..37308d7451a 100644
--- a/src/backend/utils/adt/timestamp.c
+++ b/src/backend/utils/adt/timestamp.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.124 2005/05/26 02:04:13 neilc Exp $
+ *	  $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.125 2005/06/14 21:04:40 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -938,6 +938,12 @@ now(PG_FUNCTION_ARGS)
 	PG_RETURN_TIMESTAMPTZ(result);
 }
 
+Datum
+pgsql_postmaster_start_time(PG_FUNCTION_ARGS)
+{
+	PG_RETURN_TIMESTAMPTZ(StartTime);
+}
+
 void
 dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
 {
diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h
index bc5d8afefaf..c06c4a7d41f 100644
--- a/src/include/catalog/pg_proc.h
+++ b/src/include/catalog/pg_proc.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.366 2005/06/13 02:26:50 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.367 2005/06/14 21:04:41 momjian Exp $
  *
  * NOTES
  *	  The script catalog/genbki.sh reads this file and generates .bki
@@ -3651,6 +3651,10 @@ DESCR("convert boolean to int4");
 DATA(insert OID = 2559 ( lastval			   PGNSP PGUID 12 f f t f v 0 20 "" _null_ _null_ _null_	lastval - _null_ ));
 DESCR("current value from last used sequence");
 
+/* start time function */
+DATA(insert OID = 2560 (  pg_postmaster_start_time PGNSP PGUID 12 f f t f s 0 1184 "" _null_ _null_ _null_ pgsql_postmaster_start_time - _null_ ));
+DESCR("postmaster start time");
+
 
 /*
  * Symbolic values for provolatile column: these indicate whether the result
diff --git a/src/include/utils/timestamp.h b/src/include/utils/timestamp.h
index cfd5e1458c5..782b75ec8df 100644
--- a/src/include/utils/timestamp.h
+++ b/src/include/utils/timestamp.h
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/utils/timestamp.h,v 1.43 2005/05/25 21:40:42 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/utils/timestamp.h,v 1.44 2005/06/14 21:04:42 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -256,6 +256,10 @@ extern Datum timestamptz_part(PG_FUNCTION_ARGS);
 
 extern Datum now(PG_FUNCTION_ARGS);
 
+extern Datum pgsql_postmaster_start_time(PG_FUNCTION_ARGS);
+
+extern TimestampTz StartTime;
+
 /* Internal routines (not fmgr-callable) */
 
 extern int	tm2timestamp(struct pg_tm * tm, fsec_t fsec, int *tzp, Timestamp *dt);
-- 
GitLab