Skip to content

Commit f5835b4

Browse files
committed
Add pg_postmaster_start_time() function.
Euler Taveira de Oliveira Matthias Schmidt
1 parent b5e65c8 commit f5835b4

File tree

6 files changed

+65
-6
lines changed

6 files changed

+65
-6
lines changed

doc/src/sgml/func.sgml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.254 2005/06/13 02:40:04 neilc Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.255 2005/06/14 21:04:38 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -8119,6 +8119,12 @@ select current_date + s.a as dates from generate_series(0,14,7) as s(a);
81198119
<entry>session user name</entry>
81208120
</row>
81218121

8122+
<row>
8123+
<entry><function>pg_postmaster_start_time()</function></entry>
8124+
<entry><type>timestamp with time zone</type></entry>
8125+
<entry><command>postmaster</> start time</entry>
8126+
</row>
8127+
81228128
<row>
81238129
<entry><function>user</function></entry>
81248130
<entry><type>name</type></entry>
@@ -8216,6 +8222,15 @@ SET search_path TO <replaceable>schema</> <optional>, <replaceable>schema</>, ..
82168222
Unix-domain socket.
82178223
</para>
82188224

8225+
<indexterm zone="functions-info">
8226+
<primary>pg_postmaster_start_time</primary>
8227+
</indexterm>
8228+
8229+
<para>
8230+
<function>pg_postmaster_start_time()</function> returns the timestamp with time zone
8231+
when the <command>postmaster</> started.
8232+
</para>
8233+
82198234
<indexterm zone="functions-info">
82208235
<primary>version</primary>
82218236
</indexterm>

src/backend/postmaster/postmaster.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.452 2005/06/09 22:01:12 tgl Exp $
40+
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.453 2005/06/14 21:04:39 momjian Exp $
4141
*
4242
* NOTES
4343
*
@@ -222,6 +222,9 @@ static bool FatalError = false; /* T if recovering from backend crash */
222222
bool ClientAuthInProgress = false; /* T during new-client
223223
* authentication */
224224

225+
/* Backend startup time */
226+
TimestampTz StartTime;
227+
225228
/*
226229
* State for assigning random salts and cancel keys.
227230
* Also, the global MyCancelKey passes the cancel key assigned to a given
@@ -330,6 +333,7 @@ typedef struct
330333
InheritableSocket pgStatPipe0;
331334
InheritableSocket pgStatPipe1;
332335
pid_t PostmasterPid;
336+
TimestampTz StartTime;
333337
#ifdef WIN32
334338
HANDLE PostmasterHandle;
335339
HANDLE initial_signal_pipe;
@@ -372,6 +376,9 @@ PostmasterMain(int argc, char *argv[])
372376
char *userDoption = NULL;
373377
int i;
374378

379+
AbsoluteTime StartTimeSec; /* integer part */
380+
int StartTimeUSec; /* microsecond part */
381+
375382
/* This will call exit() if strdup() fails. */
376383
progname = get_progname(argv[0]);
377384

@@ -914,6 +921,12 @@ PostmasterMain(int argc, char *argv[])
914921
*/
915922
StartupPID = StartupDataBase();
916923

924+
/*
925+
* Get start up time
926+
*/
927+
StartTimeSec = GetCurrentAbsoluteTimeUsec(&StartTimeUSec);
928+
StartTime = AbsoluteTimeUsecToTimestampTz(StartTimeSec, StartTimeUSec);
929+
917930
status = ServerLoop();
918931

919932
/*
@@ -3603,6 +3616,7 @@ save_backend_variables(BackendParameters *param, Port *port,
36033616
write_inheritable_socket(&param->pgStatPipe1, pgStatPipe[1], childPid);
36043617

36053618
param->PostmasterPid = PostmasterPid;
3619+
param->StartTime = StartTime;
36063620

36073621
#ifdef WIN32
36083622
param->PostmasterHandle = PostmasterHandle;
@@ -3805,6 +3819,7 @@ restore_backend_variables(BackendParameters *param, Port *port)
38053819
read_inheritable_socket(&pgStatPipe[1], &param->pgStatPipe1);
38063820

38073821
PostmasterPid = param->PostmasterPid;
3822+
StartTime = param->StartTime;
38083823

38093824
#ifdef WIN32
38103825
PostmasterHandle = param->PostmasterHandle;

src/backend/tcop/postgres.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.447 2005/06/03 23:05:29 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.448 2005/06/14 21:04:40 momjian Exp $
1212
*
1313
* NOTES
1414
* this is the "main" module of the postgres backend and
@@ -149,6 +149,9 @@ static int UseNewLine = 0; /* Use EOF as query delimiters */
149149
#endif /* TCOP_DONTUSENEWLINE */
150150

151151

152+
/* Backend startup time */
153+
TimestampTz StartTime;
154+
152155
/* ----------------------------------------------------------------
153156
* decls for routines only used in this file
154157
* ----------------------------------------------------------------
@@ -2380,6 +2383,9 @@ PostgresMain(int argc, char *argv[], const char *username)
23802383
sigjmp_buf local_sigjmp_buf;
23812384
volatile bool send_rfq = true;
23822385

2386+
AbsoluteTime StartTimeSec; /* integer part */
2387+
int StartTimeUSec; /* microsecond part */
2388+
23832389
#define PendingConfigOption(name,val) \
23842390
(guc_names = lappend(guc_names, pstrdup(name)), \
23852391
guc_values = lappend(guc_values, pstrdup(val)))
@@ -2969,6 +2975,15 @@ PostgresMain(int argc, char *argv[], const char *username)
29692975
*/
29702976
pgstat_bestart();
29712977

2978+
/*
2979+
* Get stand-alone backend startup time
2980+
*/
2981+
if (!IsUnderPostmaster)
2982+
{
2983+
StartTimeSec = GetCurrentAbsoluteTimeUsec(&StartTimeUSec);
2984+
StartTime = AbsoluteTimeUsecToTimestampTz(StartTimeSec, StartTimeUSec);
2985+
}
2986+
29722987
/*
29732988
* POSTGRES main processing loop begins here
29742989
*

src/backend/utils/adt/timestamp.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.124 2005/05/26 02:04:13 neilc Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.125 2005/06/14 21:04:40 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -938,6 +938,12 @@ now(PG_FUNCTION_ARGS)
938938
PG_RETURN_TIMESTAMPTZ(result);
939939
}
940940

941+
Datum
942+
pgsql_postmaster_start_time(PG_FUNCTION_ARGS)
943+
{
944+
PG_RETURN_TIMESTAMPTZ(StartTime);
945+
}
946+
941947
void
942948
dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
943949
{

src/include/catalog/pg_proc.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.366 2005/06/13 02:26:50 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.367 2005/06/14 21:04:41 momjian Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.sh reads this file and generates .bki
@@ -3651,6 +3651,10 @@ DESCR("convert boolean to int4");
36513651
DATA(insert OID = 2559 ( lastval PGNSP PGUID 12 f f t f v 0 20 "" _null_ _null_ _null_ lastval - _null_ ));
36523652
DESCR("current value from last used sequence");
36533653

3654+
/* start time function */
3655+
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_ ));
3656+
DESCR("postmaster start time");
3657+
36543658

36553659
/*
36563660
* Symbolic values for provolatile column: these indicate whether the result

src/include/utils/timestamp.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/include/utils/timestamp.h,v 1.43 2005/05/25 21:40:42 momjian Exp $
9+
* $PostgreSQL: pgsql/src/include/utils/timestamp.h,v 1.44 2005/06/14 21:04:42 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -256,6 +256,10 @@ extern Datum timestamptz_part(PG_FUNCTION_ARGS);
256256

257257
extern Datum now(PG_FUNCTION_ARGS);
258258

259+
extern Datum pgsql_postmaster_start_time(PG_FUNCTION_ARGS);
260+
261+
extern TimestampTz StartTime;
262+
259263
/* Internal routines (not fmgr-callable) */
260264

261265
extern int tm2timestamp(struct pg_tm * tm, fsec_t fsec, int *tzp, Timestamp *dt);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy