aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/pgstatfuncs.c
diff options
context:
space:
mode:
authorBruce Momjian <bruce@momjian.us>2003-03-20 03:34:57 +0000
committerBruce Momjian <bruce@momjian.us>2003-03-20 03:34:57 +0000
commita18331004a15c4e37fe88312bd882b49edb8228f (patch)
tree40ce63073108d5823958c460bc7698fb97423428 /src/backend/utils/adt/pgstatfuncs.c
parentddd50a0babd9a8ae478eee607108b7ed5675571d (diff)
downloadpostgresql-a18331004a15c4e37fe88312bd882b49edb8228f.tar.gz
postgresql-a18331004a15c4e37fe88312bd882b49edb8228f.zip
Add start time to pg_stat_activity
Neil Conway
Diffstat (limited to 'src/backend/utils/adt/pgstatfuncs.c')
-rw-r--r--src/backend/utils/adt/pgstatfuncs.c46
1 files changed, 45 insertions, 1 deletions
diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index 42b63b8739f..455da1c2aa9 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -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)
@@ -302,6 +302,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)
{
PgStat_StatDBEntry *dbentry;