如何获取 Greenplum 中用户最后登录时间和登录频率

这几天搞系统迁移,老板突然想知道给客户开的那么多用户当中,哪些还在用,哪些已经不用了。我们的数据库是 Greenplum,而且还是一直没有升级的老版本,Google 了一下没有发现特别好的查看用户登录情况的方法。咨询了 Greenplum 的售后同事后,对方建议我们使用 gp_toolkit.gp_log_database 通过遍历日志来获取用户登录信息。

gp_log_database 的详细信息可以在官方指南里找到。

https://gpdb.docs.pivotal.io/43130/ref_guide/gp_toolkit.html#topic16

官方关于它的描述是“This view uses an external table to read the server log files of the entire Greenplum system (master, segments, and mirrors) and lists log entries associated with the current database. Associated log entries can be identified by the session id (logsession) and command id (logcmdcount). The use of this view requires superuser permissions.”

注意这里的 entire Greenplum system (master, segments, and mirrors) ,意味着这个 view 将会尝试去加载海量的所有日志文件。一开始我还抱有幻想觉得可能有什么高大上的方式来获取日志数据,直到我看到报错信息里的 cat 才知道它就是一次性把所有日志读进 Greenplum。

所以为了让查询能进行下去,我不得不把所有 segment 和 mirror 的日志都先藏起来,然后把 master 上除了今年的日志之外的日志也都藏起来,只留下了 master 上今年至今不到5个月的日志。

 

之后,psql 进入数据库,直接查询 gp_toolkit.gp_log_database,例如查查各个用户今年以来最后一次登录时间:

select loguser, max(logtime)

from gp_toolkit.gp_log_database

where logdatabase='xxxxx'

group by loguser

order by max(logtime) desc;

 

然后再查查到目前为止每个用户都有多少天有登录过:

select loguser, count(distinct cast(logtime as date))

from gp_toolkit.gp_log_database

where logdatabase='xxxxx'

group by loguser

order by count(distinct cast(logtime as date)) desc;

posted @ 2017-05-11 23:33  兔大沛  阅读(794)  评论(0编辑  收藏  举报