@@ -338,6 +338,8 @@ pgstat_bestart(void)
338338 lbeentry .st_xact_start_timestamp = 0 ;
339339 lbeentry .st_databaseid = MyDatabaseId ;
340340
341+ MemSet (& lbeentry .st_session , 0 , sizeof (lbeentry .st_session ));
342+
341343 /* We have userid for client-backends, wal-sender and bgworker processes */
342344 if (lbeentry .st_backendType == B_BACKEND
343345 || lbeentry .st_backendType == B_WAL_SENDER
@@ -525,6 +527,9 @@ pgstat_report_activity(BackendState state, const char *cmd_str)
525527 TimestampTz current_timestamp ;
526528 int len = 0 ;
527529
530+ PgBackendSessionStatus st_session_diff ;
531+ MemSet (& st_session_diff , 0 , sizeof (st_session_diff ));
532+
528533 TRACE_POSTGRESQL_STATEMENT_STATUS (cmd_str );
529534
530535 if (!beentry )
@@ -550,6 +555,7 @@ pgstat_report_activity(BackendState state, const char *cmd_str)
550555 beentry -> st_xact_start_timestamp = 0 ;
551556 beentry -> st_query_id = UINT64CONST (0 );
552557 proc -> wait_event_info = 0 ;
558+ MemSet (& beentry -> st_session , 0 , sizeof (beentry -> st_session ));
553559 PGSTAT_END_WRITE_ACTIVITY (beentry );
554560 }
555561 return ;
@@ -572,27 +578,46 @@ pgstat_report_activity(BackendState state, const char *cmd_str)
572578 current_timestamp = GetCurrentTimestamp ();
573579
574580 /*
575- * If the state has changed from "active" or "idle in transaction",
576- * calculate the duration.
581+ * If the state has changed, update per-database and per-session counters.
577582 */
578- if ((beentry -> st_state == STATE_RUNNING ||
579- beentry -> st_state == STATE_FASTPATH ||
580- beentry -> st_state == STATE_IDLEINTRANSACTION ||
581- beentry -> st_state == STATE_IDLEINTRANSACTION_ABORTED ) &&
583+ if ((PGSTAT_IS_ACTIVE ( beentry ) ||
584+ PGSTAT_IS_IDLEINTRANSACTION ( beentry ) ||
585+ PGSTAT_IS_IDLEINTRANSACTION_ABORTED ( beentry ) ||
586+ PGSTAT_IS_IDLE ( beentry ) ) &&
582587 state != beentry -> st_state )
583588 {
584589 long secs ;
585590 int usecs ;
591+ int64 usecs_diff ;
586592
587593 TimestampDifference (beentry -> st_state_start_timestamp ,
588594 current_timestamp ,
589595 & secs , & usecs );
596+ usecs_diff = secs * 1000000 + usecs ;
590597
591- if ( beentry -> st_state == STATE_RUNNING ||
592- beentry -> st_state == STATE_FASTPATH )
598+ /* Keep statistics for pg_stat_database intact */
599+ if ( PGSTAT_IS_ACTIVE ( beentry ) )
593600 pgstat_count_conn_active_time ((PgStat_Counter ) secs * 1000000 + usecs );
594- else
601+ else if (PGSTAT_IS_IDLEINTRANSACTION (beentry ) ||
602+ PGSTAT_IS_IDLEINTRANSACTION_ABORTED (beentry ))
595603 pgstat_count_conn_txn_idle_time ((PgStat_Counter ) secs * 1000000 + usecs );
604+
605+ if (PGSTAT_IS_RUNNING (beentry )) {
606+ st_session_diff .total_running_time = usecs_diff ;
607+ st_session_diff .total_running_count += 1 ;
608+ } else if (PGSTAT_IS_IDLE (beentry )){
609+ st_session_diff .total_idle_time = usecs_diff ;
610+ st_session_diff .total_idle_count += 1 ;
611+ } else if (PGSTAT_IS_IDLEINTRANSACTION (beentry )){
612+ st_session_diff .total_transaction_idle_time = usecs_diff ;
613+ st_session_diff .total_transaction_idle_count += 1 ;
614+ } else if (PGSTAT_IS_IDLEINTRANSACTION_ABORTED (beentry )){
615+ st_session_diff .total_transaction_idle_aborted_time = usecs_diff ;
616+ st_session_diff .total_transaction_idle_aborted_count += 1 ;
617+ } else if (PGSTAT_IS_FASTPATH (beentry )){
618+ st_session_diff .total_fastpath_time = usecs_diff ;
619+ st_session_diff .total_fastpath_count += 1 ;
620+ }
596621 }
597622
598623 /*
@@ -618,6 +643,21 @@ pgstat_report_activity(BackendState state, const char *cmd_str)
618643 beentry -> st_activity_start_timestamp = start_timestamp ;
619644 }
620645
646+ beentry -> st_session .total_running_time += st_session_diff .total_running_time ;
647+ beentry -> st_session .total_running_count += st_session_diff .total_running_count ;
648+
649+ beentry -> st_session .total_idle_time += st_session_diff .total_idle_time ;
650+ beentry -> st_session .total_idle_count += st_session_diff .total_idle_count ;
651+
652+ beentry -> st_session .total_transaction_idle_time += st_session_diff .total_transaction_idle_time ;
653+ beentry -> st_session .total_transaction_idle_count += st_session_diff .total_transaction_idle_count ;
654+
655+ beentry -> st_session .total_transaction_idle_aborted_time += st_session_diff .total_transaction_idle_aborted_time ;
656+ beentry -> st_session .total_transaction_idle_aborted_count += st_session_diff .total_transaction_idle_aborted_count ;
657+
658+ beentry -> st_session .total_fastpath_time += st_session_diff .total_fastpath_time ;
659+ beentry -> st_session .total_fastpath_count += st_session_diff .total_fastpath_count ;
660+
621661 PGSTAT_END_WRITE_ACTIVITY (beentry );
622662}
623663
0 commit comments