@@ -338,6 +338,8 @@ pgstat_bestart(void)
338
338
lbeentry .st_xact_start_timestamp = 0 ;
339
339
lbeentry .st_databaseid = MyDatabaseId ;
340
340
341
+ MemSet (& lbeentry .st_session , 0 , sizeof (lbeentry .st_session ));
342
+
341
343
/* We have userid for client-backends, wal-sender and bgworker processes */
342
344
if (lbeentry .st_backendType == B_BACKEND
343
345
|| lbeentry .st_backendType == B_WAL_SENDER
@@ -525,6 +527,9 @@ pgstat_report_activity(BackendState state, const char *cmd_str)
525
527
TimestampTz current_timestamp ;
526
528
int len = 0 ;
527
529
530
+ PgBackendSessionStatus st_session_diff ;
531
+ MemSet (& st_session_diff , 0 , sizeof (st_session_diff ));
532
+
528
533
TRACE_POSTGRESQL_STATEMENT_STATUS (cmd_str );
529
534
530
535
if (!beentry )
@@ -550,6 +555,7 @@ pgstat_report_activity(BackendState state, const char *cmd_str)
550
555
beentry -> st_xact_start_timestamp = 0 ;
551
556
beentry -> st_query_id = UINT64CONST (0 );
552
557
proc -> wait_event_info = 0 ;
558
+ MemSet (& beentry -> st_session , 0 , sizeof (beentry -> st_session ));
553
559
PGSTAT_END_WRITE_ACTIVITY (beentry );
554
560
}
555
561
return ;
@@ -572,27 +578,46 @@ pgstat_report_activity(BackendState state, const char *cmd_str)
572
578
current_timestamp = GetCurrentTimestamp ();
573
579
574
580
/*
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.
577
582
*/
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 ) ) &&
582
587
state != beentry -> st_state )
583
588
{
584
589
long secs ;
585
590
int usecs ;
591
+ int64 usecs_diff ;
586
592
587
593
TimestampDifference (beentry -> st_state_start_timestamp ,
588
594
current_timestamp ,
589
595
& secs , & usecs );
596
+ usecs_diff = secs * 1000000 + usecs ;
590
597
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 ) )
593
600
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 ))
595
603
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
+ }
596
621
}
597
622
598
623
/*
@@ -618,6 +643,21 @@ pgstat_report_activity(BackendState state, const char *cmd_str)
618
643
beentry -> st_activity_start_timestamp = start_timestamp ;
619
644
}
620
645
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
+
621
661
PGSTAT_END_WRITE_ACTIVITY (beentry );
622
662
}
623
663
0 commit comments