我必须在pg_stat_activity中汇总数据并将其作为json返回。 Postgres 9.3 onboard。需要的结果如下:
{
"web": 67,
"postgres": 2,
"totalSessions": 69,
"idle in transaction": 2,
"active": 1,
"idle": 66
}
其中,active是活动会话数,idle是空闲,total,两者之和,其余是每个用户的会话数。我们不需要按数据库对其进行分组,但原则是相同的。
不 - 我怎么能这样做?
答案 0 :(得分:0)
我提出的问题并非如此:
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