1. 程式人生 > >如何獲取 Greenplum 中用戶最後登錄時間和登錄頻率

如何獲取 Greenplum 中用戶最後登錄時間和登錄頻率

數據庫 它的 ocs 登錄 super guid .html from user

這幾天搞系統遷移,老板突然想知道給客戶開的那麽多用戶當中,哪些還在用,哪些已經不用了。我們的數據庫是 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;

如何獲取 Greenplum 中用戶最後登錄時間和登錄頻率