mirror of
https://github.com/postgres/postgres.git
synced 2025-11-10 17:42:29 +03:00
Add start time to pg_stat_activity
Neil Conway
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/adt/nabstime.c,v 1.104 2003/02/22 05:57:45 tgl Exp $
|
||||
* $Header: /cvsroot/pgsql/src/backend/utils/adt/nabstime.c,v 1.105 2003/03/20 03:34:56 momjian Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -82,10 +82,13 @@ static int istinterval(char *i_string,
|
||||
AbsoluteTime *i_end);
|
||||
|
||||
|
||||
/* GetCurrentAbsoluteTime()
|
||||
* Get the current system time.
|
||||
/*
|
||||
* GetCurrentAbsoluteTime()
|
||||
*
|
||||
* Get the current system time. Set timezone parameters if not specified
|
||||
* elsewhere. Define HasCTZSet to allow clients to specify the default
|
||||
* timezone.
|
||||
*
|
||||
* Returns the number of seconds since epoch (January 1 1970 GMT).
|
||||
*/
|
||||
AbsoluteTime
|
||||
GetCurrentAbsoluteTime(void)
|
||||
@@ -127,9 +130,14 @@ GetCurrentDateTime(struct tm * tm)
|
||||
abstime2tm(GetCurrentTransactionStartTime(), &tz, tm, NULL);
|
||||
}
|
||||
|
||||
/* GetCurrentTimeUsec()
|
||||
* Get the transaction start time ("now()") broken down as a struct tm,
|
||||
* plus fractional-second and timezone info.
|
||||
/*
|
||||
* GetCurrentAbsoluteTimeUsec()
|
||||
*
|
||||
* Get the current system time. Set timezone parameters if not specified
|
||||
* elsewhere. Define HasCTZSet to allow clients to specify the default
|
||||
* timezone.
|
||||
*
|
||||
* Returns the number of seconds since epoch (January 1 1970 GMT)
|
||||
*/
|
||||
void
|
||||
GetCurrentTimeUsec(struct tm * tm, fsec_t *fsec, int *tzp)
|
||||
|
||||
@@ -25,6 +25,7 @@ extern Datum pg_stat_get_backend_pid(PG_FUNCTION_ARGS);
|
||||
extern Datum pg_stat_get_backend_dbid(PG_FUNCTION_ARGS);
|
||||
extern Datum pg_stat_get_backend_userid(PG_FUNCTION_ARGS);
|
||||
extern Datum pg_stat_get_backend_activity(PG_FUNCTION_ARGS);
|
||||
extern Datum pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS);
|
||||
|
||||
extern Datum pg_stat_get_db_numbackends(PG_FUNCTION_ARGS);
|
||||
extern Datum pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS);
|
||||
@@ -221,7 +222,6 @@ pg_backend_pid(PG_FUNCTION_ARGS)
|
||||
|
||||
/*
|
||||
* Built-in function for resetting the counters
|
||||
*
|
||||
*/
|
||||
Datum
|
||||
pg_stat_reset(PG_FUNCTION_ARGS)
|
||||
@@ -301,6 +301,50 @@ pg_stat_get_backend_activity(PG_FUNCTION_ARGS)
|
||||
}
|
||||
|
||||
|
||||
Datum
|
||||
pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
|
||||
{
|
||||
PgStat_StatBeEntry *beentry;
|
||||
int32 beid;
|
||||
AbsoluteTime sec;
|
||||
int usec;
|
||||
Timestamp result;
|
||||
|
||||
beid = PG_GETARG_INT32(0);
|
||||
|
||||
if (!superuser())
|
||||
PG_RETURN_NULL();
|
||||
|
||||
if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
|
||||
PG_RETURN_NULL();
|
||||
|
||||
sec = beentry->activity_start_sec;
|
||||
usec = beentry->activity_start_usec;
|
||||
|
||||
/*
|
||||
* No time recorded for start of current query -- this is the case
|
||||
* if the user hasn't enabled query-level stats collection.
|
||||
*/
|
||||
if (sec == 0 && usec == 0)
|
||||
PG_RETURN_NULL();
|
||||
|
||||
/*
|
||||
* This method of converting "Unix time" (sec/usec since epoch) to a
|
||||
* PostgreSQL timestamp is an ugly hack -- if you fix it, be sure to
|
||||
* fix the similar hackery in timestamp.c
|
||||
*/
|
||||
#ifdef HAVE_INT64_TIMESTAMP
|
||||
result = (((sec - ((date2j(2000, 1, 1) - date2j(1970, 1, 1)) * 86400))
|
||||
* INT64CONST(1000000)) + usec);
|
||||
#else
|
||||
result = (sec + (usec * 1.0e-6) - ((date2j(2000, 1, 1) -
|
||||
date2j(1970, 1, 1)) * 86400));
|
||||
#endif
|
||||
|
||||
PG_RETURN_TIMESTAMP(result);
|
||||
}
|
||||
|
||||
|
||||
Datum
|
||||
pg_stat_get_db_numbackends(PG_FUNCTION_ARGS)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user