2017-02-16 13 views
0

データをpg_stat_activityで要約してjsonとして戻す必要があります。 Postgres 9.3搭載。必要な結果は次のようになめらかである:pg_stat_activity jsonで監視するための集約データ

{ 
    "web": 67, 
    "postgres": 2, 
    "totalSessions": 69, 
    "idle in transaction": 2, 
    "active": 1, 
    "idle": 66 
} 

ここでアクティブアイドル状態のアクティブなセッションの数であり、 - アイドルの、合計 - の両方の和、残りはユーザーごとのセッションの数です。データベースでグループ化する必要はありませんでしたが、原則は同じです。

いいえ - どうすればいいですか?

答えて

0

私が思いついたqryが良く、その後ノーです:

mon=# with pre as (
    select DISTINCT 
    concat('"totalSessions":',sum(count(1)) over(),'') total 
    , concat('"',usename,'":',sum(count(1)) over (partition by usename),'') u 
    , concat('"',state,'":',sum(count(1)) over (partition by state),'') s 
    from pg_stat_activity 
    group by state,usename 
) 
    select concat('{',string_agg(j,','),'}')::json from (
    select distinct u j from pre u 
    union all 
    select distinct total from pre s 
    union all 
    select distinct s from pre s 
) pg_stat_act 
; 
              concat 
------------------------------------------------------------------------------------------- 
{"web":67,"postgres":2,"totalSessions":69,"idle in transaction":2,"active":1,"idle":66} 
(1 row) 

Time: 1.807 ms 
関連する問題