Re: uptime function to postmaster - Mailing list pgsql-patches

From Bruce Momjian
Subject Re: uptime function to postmaster
Date
Msg-id 200506142104.j5EL46Z20421@candle.pha.pa.us
Whole thread Raw
In response to Re: uptime function to postmaster  (Euler Taveira de Oliveira <eulerto@yahoo.com.br>)
List pgsql-patches
Euler Taveira de Oliveira wrote:
> Hi Bruce,
>
> > I think we should return intervals only when we can't return
> > meaningful
> > timestamp values. I don't have any logic to back up that opinion,
> > though.
> >
> Agreed.
>
> >
> > We need to preceed our function names with pg_ for cases like this
> > where
> > we are supplying pg-specific behavior.
> >
> Agreed.
>
> An updated version of the patch is attached. It is just implement
> 'pg_start_time' function that works in multi-user and stand-alone. Docs
> is attached too.

I have applied the attached patch, calling the function
pg_postmaster_start_time().  I realize a stand-alone backend doesn't
have a postmaster, but this is probably as clear as we are going to get.

Do we want this to be executed only by super-users?  I know there was
some discussion about that but I didn't see a conclusion.  The only
argument I heard was something about random seeds, but that seemed like
a weak argument.

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: doc/src/sgml/func.sgml
===================================================================
RCS file: /cvsroot/pgsql/doc/src/sgml/func.sgml,v
retrieving revision 1.254
diff -c -c -r1.254 func.sgml
*** doc/src/sgml/func.sgml    13 Jun 2005 02:40:04 -0000    1.254
--- doc/src/sgml/func.sgml    14 Jun 2005 20:59:17 -0000
***************
*** 8120,8125 ****
--- 8120,8131 ----
        </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>
         <entry>equivalent to <function>current_user</function></entry>
***************
*** 8217,8222 ****
--- 8223,8237 ----
     </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>

Index: src/backend/postmaster/postmaster.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v
retrieving revision 1.452
diff -c -c -r1.452 postmaster.c
*** src/backend/postmaster/postmaster.c    9 Jun 2005 22:01:12 -0000    1.452
--- src/backend/postmaster/postmaster.c    14 Jun 2005 20:59:21 -0000
***************
*** 222,227 ****
--- 222,230 ----
  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,335 ****
--- 333,339 ----
      InheritableSocket pgStatPipe0;
      InheritableSocket pgStatPipe1;
      pid_t PostmasterPid;
+     TimestampTz StartTime;
  #ifdef WIN32
      HANDLE PostmasterHandle;
      HANDLE initial_signal_pipe;
***************
*** 372,377 ****
--- 376,384 ----
      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,919 ****
--- 921,932 ----
       */
      StartupPID = StartupDataBase();

+     /*
+      * Get start up time
+      */
+     StartTimeSec = GetCurrentAbsoluteTimeUsec(&StartTimeUSec);
+     StartTime = AbsoluteTimeUsecToTimestampTz(StartTimeSec, StartTimeUSec);
+
      status = ServerLoop();

      /*
***************
*** 3603,3608 ****
--- 3616,3622 ----
      write_inheritable_socket(¶m->pgStatPipe1, pgStatPipe[1], childPid);

      param->PostmasterPid = PostmasterPid;
+     param->StartTime = StartTime;

  #ifdef WIN32
      param->PostmasterHandle = PostmasterHandle;
***************
*** 3805,3810 ****
--- 3819,3825 ----
      read_inheritable_socket(&pgStatPipe[1], ¶m->pgStatPipe1);

      PostmasterPid = param->PostmasterPid;
+     StartTime = param->StartTime;

  #ifdef WIN32
      PostmasterHandle = param->PostmasterHandle;
Index: src/backend/tcop/postgres.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/tcop/postgres.c,v
retrieving revision 1.447
diff -c -c -r1.447 postgres.c
*** src/backend/tcop/postgres.c    3 Jun 2005 23:05:29 -0000    1.447
--- src/backend/tcop/postgres.c    14 Jun 2005 20:59:22 -0000
***************
*** 149,154 ****
--- 149,157 ----
  #endif   /* TCOP_DONTUSENEWLINE */


+ /* Backend startup time */
+ TimestampTz    StartTime;
+
  /* ----------------------------------------------------------------
   *        decls for routines only used in this file
   * ----------------------------------------------------------------
***************
*** 2380,2385 ****
--- 2383,2391 ----
      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)))
***************
*** 2970,2975 ****
--- 2976,2990 ----
      pgstat_bestart();

      /*
+      * Get stand-alone backend startup time
+      */
+     if (!IsUnderPostmaster)
+     {
+         StartTimeSec = GetCurrentAbsoluteTimeUsec(&StartTimeUSec);
+         StartTime = AbsoluteTimeUsecToTimestampTz(StartTimeSec, StartTimeUSec);
+     }
+
+     /*
       * POSTGRES main processing loop begins here
       *
       * If an exception is encountered, processing resumes here so we abort
Index: src/backend/utils/adt/timestamp.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v
retrieving revision 1.124
diff -c -c -r1.124 timestamp.c
*** src/backend/utils/adt/timestamp.c    26 May 2005 02:04:13 -0000    1.124
--- src/backend/utils/adt/timestamp.c    14 Jun 2005 20:59:24 -0000
***************
*** 938,943 ****
--- 938,949 ----
      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: src/include/catalog/pg_proc.h
===================================================================
RCS file: /cvsroot/pgsql/src/include/catalog/pg_proc.h,v
retrieving revision 1.366
diff -c -c -r1.366 pg_proc.h
*** src/include/catalog/pg_proc.h    13 Jun 2005 02:26:50 -0000    1.366
--- src/include/catalog/pg_proc.h    14 Jun 2005 20:59:29 -0000
***************
*** 3651,3656 ****
--- 3651,3660 ----
  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: src/include/utils/timestamp.h
===================================================================
RCS file: /cvsroot/pgsql/src/include/utils/timestamp.h,v
retrieving revision 1.43
diff -c -c -r1.43 timestamp.h
*** src/include/utils/timestamp.h    25 May 2005 21:40:42 -0000    1.43
--- src/include/utils/timestamp.h    14 Jun 2005 20:59:30 -0000
***************
*** 256,261 ****
--- 256,265 ----

  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);

pgsql-patches by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: SHOW ALL with descriptions
Next
From: Bruce Momjian
Date:
Subject: Re: Tiny patch on print.c of psql