(unnamed)

text/plain

Filename: (unnamed)
Type: text/plain
Part: 1
Message: Re: Add connection active, idle time to pg_stat_activity
diff --git a/src/backend/utils/activity/backend_status.c b/src/backend/utils/activity/backend_status.c
index 8b6836a662..996f4e88d7 100644
--- a/src/backend/utils/activity/backend_status.c
+++ b/src/backend/utils/activity/backend_status.c
@@ -587,10 +587,7 @@ pgstat_report_activity(BackendState state, const char *cmd_str)
 	 * If the state has changed from "active" or "idle in transaction",
 	 * calculate the duration.
 	 */
-	if ((beentry->st_state == STATE_RUNNING ||
-		 beentry->st_state == STATE_FASTPATH ||
-		 beentry->st_state == STATE_IDLEINTRANSACTION ||
-		 beentry->st_state == STATE_IDLEINTRANSACTION_ABORTED) &&
+	if ((PGSTAT_IS_ACTIVE(beentry) || PGSTAT_IS_IDLEINTRANSACTION(beentry)) &&
 		state != beentry->st_state)
 	{
 		long		secs;
@@ -611,8 +608,7 @@ pgstat_report_activity(BackendState state, const char *cmd_str)
 		 * 2. The latter values are reset to 0 once the data has been sent
 		 * to the statistics collector.
 		 */
-		if (beentry->st_state == STATE_RUNNING ||
-			beentry->st_state == STATE_FASTPATH)
+		if (PGSTAT_IS_ACTIVE(beentry))
 			active_time_diff = usecs_diff;
 		else
 			transaction_idle_time_diff = usecs_diff;
diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index 7c2776c14c..48c0ffa33a 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -675,6 +675,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
 		{
 			SockAddr	zero_clientaddr;
 			char	   *clipped_activity;
+			int64		tmp_time;
 
 			switch (beentry->st_state)
 			{
@@ -917,9 +918,25 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
 			else
 				values[29] = UInt64GetDatum(beentry->st_query_id);
 
-			/* convert to msec for display */
-			values[30] = Float8GetDatum(beentry->st_total_active_time / 1000.0) ;
-			values[31] = Float8GetDatum(beentry->st_total_transaction_idle_time / 1000.0);
+			tmp_time = beentry->st_total_active_time;
+
+			/* add the realtime value to the counter if needed */
+			if (PGSTAT_IS_ACTIVE(beentry))
+				tmp_time +=
+					GetCurrentTimestamp() - beentry->st_state_start_timestamp;
+
+			/* convert it to msec */
+			values[30] = Float8GetDatum(tmp_time / 1000.0) ;
+
+			tmp_time = beentry->st_total_transaction_idle_time;
+
+			/* add the realtime value to the counter if needed */
+			if (PGSTAT_IS_IDLEINTRANSACTION(beentry))
+				tmp_time +=
+					GetCurrentTimestamp() - beentry->st_state_start_timestamp;
+
+			/* convert it to msec */
+			values[31] = Float8GetDatum(tmp_time);
 		}
 		else
 		{
diff --git a/src/include/utils/backend_status.h b/src/include/utils/backend_status.h
index 1791dd6842..a03225c4f0 100644
--- a/src/include/utils/backend_status.h
+++ b/src/include/utils/backend_status.h
@@ -235,6 +235,12 @@ typedef struct PgBackendStatus
 	((before_changecount) == (after_changecount) && \
 	 ((before_changecount) & 1) == 0)
 
+/* macros to identify the states for time accounting */
+#define PGSTAT_IS_ACTIVE(s)												\
+	((s)->st_state == STATE_RUNNING || (s)->st_state == STATE_FASTPATH)
+#define PGSTAT_IS_IDLEINTRANSACTION(s)			 \
+	((s)->st_state == STATE_IDLEINTRANSACTION ||		\
+	 (s)->st_state == STATE_IDLEINTRANSACTION_ABORTED)
 
 /* ----------
  * LocalPgBackendStatus