Add pg_postmaster_start_time() function.
authorBruce Momjian
Tue, 14 Jun 2005 21:04:42 +0000 (21:04 +0000)
committerBruce Momjian
Tue, 14 Jun 2005 21:04:42 +0000 (21:04 +0000)
Euler Taveira de Oliveira
Matthias Schmidt

doc/src/sgml/func.sgml
src/backend/postmaster/postmaster.c
src/backend/tcop/postgres.c
src/backend/utils/adt/timestamp.c
src/include/catalog/pg_proc.h
src/include/utils/timestamp.h

index 22baa22aa6d81be76633d174e9c43c92f61692c1..43df7d595c9df30655da6453465ed57a3e67efa7 100644 (file)
@@ -1,5 +1,5 @@
 
 
@@ -8119,6 +8119,12 @@ select current_date + s.a as dates from generate_series(0,14,7) as s(a);
        session user name
       
 
+      
+       pg_postmaster_start_time()
+       timestamp with time zone
+       postmaster start time
+      
+
       
        user
        name
@@ -8216,6 +8222,15 @@ SET search_path TO schema schema, ..
      Unix-domain socket.
    
 
+   
+    pg_postmaster_start_time
+   
+
+   
+     pg_postmaster_start_time() returns the timestamp with time zone
+     when the postmaster started.
+   
+
    
     version
    
index af28bf652fb448d92e4d995164d8f0d6b5e32430..03057dbcb210765fbb25011ff41d0a8ef58ab358 100644 (file)
@@ -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(¶m->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], ¶m->pgStatPipe1);
 
    PostmasterPid = param->PostmasterPid;
+   StartTime = param->StartTime;
 
 #ifdef WIN32
    PostmasterHandle = param->PostmasterHandle;
index 58b1ffbce9aa4890777df5beb724b16a0f54f38d..f1c65bacc43cf4cfef5996c98c80930b739d6897 100644 (file)
@@ -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
     *
index 27e0523f49cc1884bdaea41a1f54c4fe4316fdc6..37308d7451a769316f4040e65103a77744d0c899 100644 (file)
@@ -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)
 {
index bc5d8afefaf266c349a2468e695daf14056f47c9..c06c4a7d41f1adced6503050f1dc5e9256cf7b64 100644 (file)
@@ -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
index cfd5e1458c549f1f14578848a800537e95bdbbdb..782b75ec8dfd5e01d45623e1bd9d36687ab808c5 100644 (file)
@@ -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);