背景
Greenplum的日常監控點、評判標准,日常維護任務。
展示圖層
由於一台主機可能跑多個實例,建議分層展示。
另外,即使是ON ECS虛擬機(一個虛擬機一個實例一對一的形態)的產品形態,實際上也建議分層展示,以示通用性。
主機級圖層
1、全局
2、以集群分組
展示圖形
1、餅圖(正常、警告、嚴重錯誤、不可用,占比,數量)
2、熱力圖(每台主機一個點,顏色描繪正常、警告、嚴重錯誤、不可用)
3、列表(正常、警告、嚴重錯誤、不可用倒排,TOP 主機)
實例級圖層
1、全局
2、以集群分組
展示圖形
1、餅圖(正常、警告、嚴重錯誤、不可用,占比,數量)
2、熱力圖(每實例一個點,顏色描繪正常、警告、嚴重錯誤、不可用)
3、列表(正常、警告、嚴重錯誤、不可用倒排,TOP 實例)
明細圖層
全局、分組 -> 主機、實例 -> 主機、數據庫實例明細監控指標
全局、分組 -> 主機 -> 實例 -> 數據庫實例明細監控指標
數據庫狀態監控
監控集群的健康狀態。
1、列出當前down的segment節點。
檢查方法:
連接到postgres數據庫,執行:
SELECT * FROM gp_segment_configuration
WHERE status <> 'u';
判斷方法:
有返回,表示該segment已經down了。
事件級別:
warning。
如果有mirror節點,不影響使用。
重要程度:
重要。
監控頻率:
5-10分鍾。
處理方法:
1、檢查DOWN segment主機是否正常。
2、檢查DOWN segment的primary, mirror節點的pg_log日志。找出原因。
3、如果沒有異常,使用gprecoverseg把DOWN的節點拉起來。
2、列出當前處於change tracking的segment節點。
檢查方法:
連接到postgres數據庫,執行:
SELECT * FROM gp_segment_configuration
WHERE mode = 'c';
判斷方法:
如果有記錄返回,表示有處於change tracking的segment。
事件級別:
warning。
重要程度:
重要
監控頻率:
5-10分鍾。
處理方法:
1、檢查DOWN segment主機是否正常。
2、檢查DOWN segment的primary, mirror節點的pg_log日志。找出原因。
3、如果沒有異常,使用gprecoverseg把DOWN的節點拉起來。
3、列出當前處於re-syncing狀態的segment節點。
檢查方法:
連接到postgres數據庫,執行:
SELECT * FROM gp_segment_configuration
WHERE mode = 'r';
需要加入時間條件。判斷是否長時間處於r狀態。
判斷方法:
如果有記錄返回,表示有處於re-syncing的segment。
事件級別:
warning。
重要程度:
重要
監控頻率:
5-10分鍾。
處理方法:
如果節點很長時間處於re-synched狀態。檢查SEGMENT的primary, mirror節點的pg_log,排查錯誤。
4、列出當前角色未處於優先角色的節點的segment節點。
檢查方法:
連接到postgres數據庫,執行:
SELECT * FROM gp_segment_configuration
WHERE preferred_role <> role;
判斷方法:
如果有記錄返回,表示當前集群可能處於not balanced狀態。
事件級別:
warning。
重要程度:
重要
監控頻率:
5-10分鍾。
處理方法:
如果當前集群處於not balanced狀態,某個主機的primary節點可能更多,負擔較重,影響性能。
建議找到維護窗口,重啟數據庫集群。
5、檢測所有節點是否可達,確保QD(query dispatching)正常。
檢查方法:
連接到postgres數據庫,執行:
SELECT gp_segment_id, count(*)
FROM gp_dist_random('pg_class')
GROUP BY 1;
判斷方法:
正常情況下,每個節點返回一條記錄,如果執行失敗,表示有不可達的segment,執行SQL是QD階段會失敗。
事件級別:
critical。
重要程度:
嚴重
監控頻率:
5-10分鍾。
處理方法:
如果查詢失敗,表示某些segment節點的QD異常,這是一個罕見錯誤。需要檢查異常節點(不能觸及的segments)的硬件、網絡是否正常。
6、列出當前異常的master standby節點。
檢查方法:
連接到postgres數據庫,執行:
SELECT summary_state
FROM gp_master_mirroring;
判斷方法:
返回Not Synchronized時,表示master standby異常。
事件級別:
warning。
重要程度:
重要。
監控頻率:
5-10分鍾。
處理方法:
檢查master, standby的pg_log,是否有錯誤日志,針對性修復。
如果沒有unexpected錯誤,並且機器正常。那么使用gpinitstandby修復standby。
GPDB 4.2以及以前的版本,需要重啟GPDB集群。
7、列出當前down的segment節點。
檢查方法:
連接到postgres數據庫,執行:
SELECT procpid, state FROM pg_stat_replication;
判斷方法:
如果state不是'STREAMING',或者沒有記錄返回,那么說明master standby節點異常。
事件級別:
warning。
重要程度:
重要
監控頻率:
5-10分鍾。
處理方法:
檢查master, master standby節點的pg_log是否有異常日志。
如果沒有unexpected錯誤,並且機器正常。那么使用gpinitstandby修復standby。
GPDB 4.2以及以前的版本,需要重啟GPDB集群。
8、檢查master節點是否up並正常提供服務。
檢查方法:
連接到postgres數據庫,執行:
SELECT count(*) FROM gp_segment_configuration;
判斷方法:
QUERY正常返回,表示master節點正常。
事件級別:
critical。
重要程度:
嚴重
監控頻率:
5-10分鍾。
處理方法:
如果這個QUERY不能正常執行,說明active master節點可能DOWN了。
重試若干次,如果都異常,關閉active master(一定要確保關閉無誤),切換到standby master。
列出master, segment, standby, mirror狀態的其他方法
使用命令查詢master, segment, standby, mirror狀態
1、master和segment狀態
gpstate
或
gpstate -s
2、segment mirror狀態
gpstate -m
3、primary和mirror mapping狀態
gpstate -c
4、master standby狀態
gpstate -f
數據庫告警日志監控
1、列出FATAL and ERROR級別的錯誤日志。
檢查方法:
方法1,在安裝了gpperfmon組件的情況下
連接到gpperfmon數據庫,執行:
SELECT * FROM log_alert_history
WHERE logseverity in ('FATAL', 'ERROR')
AND logtime > (now() - interval '15 minutes');
方法2,查看所有節點(master, standby master, primary, mirror segments)的pg_log。過濾FATAL and ERROR級別的錯誤日志。
方法3,查看這些系統視圖
List of relations
Schema | Name | Type | Owner | Storage
------------+------------------------+------+----------+---------
gp_toolkit | gp_log_command_timings | view | digoal | none -- 統計
gp_toolkit | gp_log_database | view | digoal | none -- 這個包含當前數據庫日志
gp_toolkit | gp_log_master_concise | view | digoal | none -- 統計
gp_toolkit | gp_log_system | view | digoal | none -- 這個包含所有日志
(4 rows)
實際上gp_log_system是一個command外部表,列出了所有segment, master的csvlog的內容。
View definition:
SELECT __gp_log_segment_ext.logtime, __gp_log_segment_ext.loguser, __gp_log_segment_ext.logdatabase, __gp_log_segment_ext.logpid, __gp_log_segment_ext.logthread, __gp_log_segment_ext.loghost, __gp_log_segment_ext.logport, __gp_log_segme
nt_ext.logsessiontime, __gp_log_segment_ext.logtransaction, __gp_log_segment_ext.logsession, __gp_log_segment_ext.logcmdcount, __gp_log_segment_ext.logsegment, __gp_log_segment_ext.logslice, __gp_log_segment_ext.logdistxact, __gp_log_seg
ment_ext.loglocalxact, __gp_log_segment_ext.logsubxact, __gp_log_segment_ext.logseverity, __gp_log_segment_ext.logstate, __gp_log_segment_ext.logmessage, __gp_log_segment_ext.logdetail, __gp_log_segment_ext.loghint, __gp_log_segment_ext.
logquery, __gp_log_segment_ext.logquerypos, __gp_log_segment_ext.logcontext, __gp_log_segment_ext.logdebug, __gp_log_segment_ext.logcursorpos, __gp_log_segment_ext.logfunction, __gp_log_segment_ext.logfile, __gp_log_segment_ext.logline,
__gp_log_segment_ext.logstack
FROM ONLY gp_toolkit.__gp_log_segment_ext
UNION ALL
SELECT __gp_log_master_ext.logtime, __gp_log_master_ext.loguser, __gp_log_master_ext.logdatabase, __gp_log_master_ext.logpid, __gp_log_master_ext.logthread, __gp_log_master_ext.loghost, __gp_log_master_ext.logport, __gp_log_master_ext.l
ogsessiontime, __gp_log_master_ext.logtransaction, __gp_log_master_ext.logsession, __gp_log_master_ext.logcmdcount, __gp_log_master_ext.logsegment, __gp_log_master_ext.logslice, __gp_log_master_ext.logdistxact, __gp_log_master_ext.logloc
alxact, __gp_log_master_ext.logsubxact, __gp_log_master_ext.logseverity, __gp_log_master_ext.logstate, __gp_log_master_ext.logmessage, __gp_log_master_ext.logdetail, __gp_log_master_ext.loghint, __gp_log_master_ext.logquery, __gp_log_mas
ter_ext.logquerypos, __gp_log_master_ext.logcontext, __gp_log_master_ext.logdebug, __gp_log_master_ext.logcursorpos, __gp_log_master_ext.logfunction, __gp_log_master_ext.logfile, __gp_log_master_ext.logline, __gp_log_master_ext.logstack
FROM ONLY gp_toolkit.__gp_log_master_ext
ORDER BY 1;
postgres=# \d+ gp_toolkit.__gp_log_segment_ext
External table "gp_toolkit.__gp_log_segment_ext"
Column | Type | Modifiers | Storage | Description
----------------+--------------------------+-----------+----------+-------------
logtime | timestamp with time zone | | plain |
loguser | text | | extended |
logdatabase | text | | extended |
logpid | text | | extended |
logthread | text | | extended |
loghost | text | | extended |
logport | text | | extended |
logsessiontime | timestamp with time zone | | plain |
logtransaction | integer | | plain |
logsession | text | | extended |
logcmdcount | text | | extended |
logsegment | text | | extended |
logslice | text | | extended |
logdistxact | text | | extended |
loglocalxact | text | | extended |
logsubxact | text | | extended |
logseverity | text | | extended |
logstate | text | | extended |
logmessage | text | | extended |
logdetail | text | | extended |
loghint | text | | extended |
logquery | text | | extended |
logquerypos | integer | | plain |
logcontext | text | | extended |
logdebug | text | | extended |
logcursorpos | integer | | plain |
logfunction | text | | extended |
logfile | text | | extended |
logline | integer | | plain |
logstack | text | | extended |
Type: readable
Encoding: UTF8
Format type: csv
Format options: delimiter ',' null '' escape '"' quote '"'
Command: cat $GP_SEG_DATADIR/pg_log/*.csv
Execute on: all segments
postgres=# \d+ gp_toolkit.__gp_log_master_ext
External table "gp_toolkit.__gp_log_master_ext"
Column | Type | Modifiers | Storage | Description
----------------+--------------------------+-----------+----------+-------------
logtime | timestamp with time zone | | plain |
loguser | text | | extended |
logdatabase | text | | extended |
logpid | text | | extended |
logthread | text | | extended |
loghost | text | | extended |
logport | text | | extended |
logsessiontime | timestamp with time zone | | plain |
logtransaction | integer | | plain |
logsession | text | | extended |
logcmdcount | text | | extended |
logsegment | text | | extended |
logslice | text | | extended |
logdistxact | text | | extended |
loglocalxact | text | | extended |
logsubxact | text | | extended |
logseverity | text | | extended |
logstate | text | | extended |
logmessage | text | | extended |
logdetail | text | | extended |
loghint | text | | extended |
logquery | text | | extended |
logquerypos | integer | | plain |
logcontext | text | | extended |
logdebug | text | | extended |
logcursorpos | integer | | plain |
logfunction | text | | extended |
logfile | text | | extended |
logline | integer | | plain |
logstack | text | | extended |
Type: readable
Encoding: UTF8
Format type: csv
Format options: delimiter ',' null '' escape '"' quote '"'
Command: cat $GP_SEG_DATADIR/pg_log/*.csv
Execute on: master segment
字段解釋
Field Name | Data Type | Description |
---|---|---|
event_time | timestamp with time zone | Time that the log entry was written to the log |
user_name | varchar(100) | The database user name |
database_name | varchar(100) | The database name |
process_id | varchar(10) | The system process ID (prefixed with "p") |
thread_id | varchar(50) | The thread count (prefixed with "th") |
remote_host | varchar(100) | On the master, the hostname/address of the client machine. On the segment, the hostname/address of the master. |
remote_port | varchar(10) | The segment or master port number |
session_start_time | timestamp with time zone | Time session connection was opened |
transaction_id | int | Top-level transaction ID on the master. This ID is the parent of any subtransactions. |
gp_session_id | text | Session identifier number (prefixed with "con") |
gp_command_count | text | The command number within a session (prefixed with "cmd") |
gp_segment | text | The segment content identifier (prefixed with "seg" for primaries or "mir" for mirrors). The master always has a content ID of -1. |
slice_id | text | The slice ID (portion of the query plan being executed) |
distr_tranx_id | text | Distributed transaction ID |
local_tranx_id | text | Local transaction ID |
sub_tranx_id | text | Subtransaction ID |
event_severity | varchar(10) | Values include: LOG, ERROR, FATAL, PANIC, DEBUG1, DEBUG2 |
sql_state_code | varchar(10) | SQL state code associated with the log message |
event_message | text | Log or error message text |
event_detail | text | Detail message text associated with an error or warning message |
event_hint | text | Hint message text associated with an error or warning message |
internal_query | text | The internally-generated query text |
internal_query_pos | int | The cursor index into the internally-generated query text |
event_context | text | The context in which this message gets generated |
debug_query_string | text | User-supplied query string with full detail for debugging. This string can be modified for internal use. |
error_cursor_pos | int | The cursor index into the query string |
func_name | text | The function in which this message is generated |
file_name | text | The internal code file where the message originated |
file_line | int | The line of the code file where the message originated |
stack_trace | text | Stack trace text associated with this message |
判斷方法:
1、排查業務邏輯錯誤、資源限制錯誤、數據庫內核層面的嚴重錯誤。
2、對於業務邏輯錯誤,建議在QUERY時過濾,避免日志過多。
3、關注資源限制、內核錯誤。
4、錯誤代碼和解釋參考
http://gpdb.docs.pivotal.io/43160/admin_guide/managing/monitor.html
SQL Standard Error Codes
Table 4. SQL Codes
事件級別:
warning。
重要程度:
重要
監控頻率:
15分鍾。
處理方法:
建立每種嚴重錯誤的處理方法。
2、SNMP設置,事件自動通知設置。
相關參數
gp_email_smtp_server
gp_email_smtp_userid
gp_email_smtp_password or gp_snmp_monitor_address
gp_snmp_community
gp_snmp_use_inform_or_trap
硬件和操作系統監控
1、檢查異常的主機、操作系統。
檢查方法:
Set up SNMP or other system check for hardware and OS errors.
監控硬件錯誤:
檢查/var/log/mcelog日志文件的內容,如果有信息,說明該機器出現過硬件錯誤。
監控dmesg異常,例如Out of memory。
檢查/var/log/dmesg日志文件的內容.
判斷方法:
判斷被檢查文件的內容,是否出現過異常。
/var/log/mcelog
/var/log/dmesg
事件級別:
critical
重要程度:
嚴重
監控頻率:
15分鍾。
處理方法:
如果有硬件錯誤或者不可修復的軟件錯誤,建議更換機器。
添加新機器到集群,使用gprecoverseg重建segment,或者使用gpinitstandby修復standby master。
2、列出磁盤使用率。
檢查方法:
du -sh $dir
或
SELECT * FROM gp_toolkit.gp_disk_free ;
判斷方法:
數據盤:建議達到80%時warning,90%時critical。
日志、臨時文件盤:建議達到60%時warning,80%時critical。
事件級別:
critical
重要程度:
嚴重
監控頻率:
5-30分鍾
處理方法:
擴容、清數據、清WAL或臨時文件。
3、網絡監測。
檢查方法:
ifconfig
判斷方法:
ethx: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST> mtu 1500
inet xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.xxx broadcast xxx.xxx.xxx.xxx
ether xxxxxxxxxxx txqueuelen 0 (Ethernet)
RX packets 611100787 bytes 184118991357 (171.4 GiB)
RX errors 0 dropped 489309 overruns 0 frame 0
TX packets 580090906 bytes 71626153522 (66.7 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
檢查droped, errors的數量,如果除以packets比例超過某個閾值,說明丟包或者錯誤嚴重。告警。
事件級別:
warning。
重要程度:
重要
監控頻率:
小時
處理方法:
Work with network and OS teams to resolve errors.
4、存儲硬件錯誤。
檢查方法:
根據RAID卡廠商、SSD廠商提供的檢測工具。
smartclt命令。
如果是btrfs, zfs, lvm, md類管理的存儲,這些工具也可以檢測軟raid的健康狀態。
檢測是否異常。
注意,某些檢測可能會導致IO堵塞(雖然很短暫),但是也許會比較嚴重。建議和廠商確認監測命令的堵塞性。
判斷方法:
根據廠商提供的方法。
事件級別:
critical。
重要程度:
嚴重
監控頻率:
5分鍾。
處理方法:
對於RAID存儲,替換壞盤。
對於非RAID或R10存儲,替換主機。
5、列出硬件、操作系統內核的不正確配置。
檢查方法:
gpcheck
判斷方法:
根據gpcheck的輸出進行判斷。
事件級別:
critical
重要程度:
嚴重
監控頻率:
安裝集群時測試一次即可。
處理方法:
根據gpdb的推薦,設置正確的配置。
6、檢測集群的硬件性能極限。
檢查方法:
gpcheckperf
判斷方法:
檢查集群的io, 網絡的性能,判斷是否有性能傾斜。
事件級別:
critical
重要程度:
嚴重
監控頻率:
安裝集群時測試一次即可。
處理方法:
建議單機的磁盤讀寫總帶寬、網絡帶寬匹配。
例如有8塊盤,每塊盤125MB/s的讀寫帶寬,網卡為10GiB。
磁盤總帶寬約1 GB/s,與網卡帶寬1.25GB/s匹配。
The cluster may be under-specified if data transfer rates are not similar to the following:
2 GB per second disk read
1 GB per second disk write
10 Gigabit per second network read and write
If transfer rates are lower than expected, consult with your data architect regarding performance expectations.
If the machines on the cluster display an uneven performance profile, work with the system administration team to fix faulty machines.
系統表監控
1、檢查master, segment的catalog一致性。
檢查方法:
對每一個數據庫執行:
gpcheckcat -O
判斷方法:
如果有輸出,說明有不一致的catalog。
事件級別:
warning
重要程度:
重要
監控頻率:
周
處理方法:
如果gpcheckcat產生異常,會將修復腳本寫入對應的文件,例如
repair scripts generated in directory gpcheckcat.repair.YYYY-MM-DD.hh.mm.ss
執行對應的腳本,連接到對應數據庫,修復catalog異常。
2、檢查持久化表的catalog一致性。
檢查方法:
斷開所有用戶的連接,對每一個數據庫執行:
gpcheckcat -R persistent
判斷方法:
如果有輸出,說明有不一致的持久化表的catalog。
事件級別:
critical
重要程度:
嚴重
監控頻率:
月
處理方法:
如果gpcheckcat產生異常,會將修復腳本寫入對應的文件,例如
repair scripts generated in directory gpcheckcat.repair.YYYY-MM-DD.hh.mm.ss
執行對應的腳本,連接到對應數據庫,修復catalog異常。
3、檢查pg_class與pg_attribute是否不一致。
檢查方法:
斷開所有用戶的連接,對每一個數據庫執行:
gpcheckcat -R pgclass
判斷方法:
如果有輸出,說明pg_class與pg_attribute不一致。
事件級別:
warning。
重要程度:
重要
監控頻率:
月
處理方法:
如果gpcheckcat產生異常,會將修復腳本寫入對應的文件,例如
repair scripts generated in directory gpcheckcat.repair.YYYY-MM-DD.hh.mm.ss
執行對應的腳本,連接到對應數據庫,修復catalog異常。
4、檢查是否有類似"內存泄露"的臨時schema,或者missing的schema定義。
檢查方法:
斷開所有用戶的連接,對每一個數據庫執行:
gpcheckcat -R namespace
判斷方法:
如果有輸出,說明有類似"內存泄露"的臨時schema,或者missing的schema定義。
事件級別:
warning。
重要程度:
重要
監控頻率:
月
處理方法:
如果gpcheckcat產生異常,會將修復腳本寫入對應的文件,例如
repair scripts generated in directory gpcheckcat.repair.YYYY-MM-DD.hh.mm.ss
執行對應的腳本,連接到對應數據庫,修復catalog異常。
5、檢查隨機分布策略的表的約束是否正常。
檢查方法:
斷開所有用戶的連接,對每一個數據庫執行:
gpcheckcat -R distribution_policy
判斷方法:
如果有輸出,說明有隨機分布策略的表的約束是否正常。
事件級別:
warning。
重要程度:
重要
監控頻率:
月
處理方法:
如果gpcheckcat產生異常,會將修復腳本寫入對應的文件,例如
repair scripts generated in directory gpcheckcat.repair.YYYY-MM-DD.hh.mm.ss
執行對應的腳本,連接到對應數據庫,修復。
6、檢查是否有對象依賴不存在對象。
檢查方法:
斷開所有用戶的連接,對每一個數據庫執行:
gpcheckcat -R dependency
判斷方法:
如果有輸出,說明有對象依賴不存在對象。
事件級別:
warning。
重要程度:
重要
監控頻率:
月
處理方法:
如果gpcheckcat產生異常,會將修復腳本寫入對應的文件,例如
repair scripts generated in directory gpcheckcat.repair.YYYY-MM-DD.hh.mm.ss
執行對應的腳本,連接到對應數據庫,修復。
數據維護任務
1、檢查丟失統計信息的表。
檢查方法:
連接到每個數據庫,執行
SELECT * FROM gp_toolkit.gp_stats_missing;
判斷方法:
如果有返回,表示對應的表沒有收集統計信息。
事件級別:
warning
重要程度:
重要
監控頻率:
天
處理方法:
analyze table;
2、檢查表、索引膨脹、deadtuple比例。
檢查方法:
連接到每個數據庫,執行
analyze;
然后執行
SELECT * FROM gp_toolkit.gp_bloat_diag;
以上操作查詢的是膨脹比較厲害的表,下面這個查詢是膨脹明細
select * from gp_toolkit.gp_bloat_expected_pages ;
詳見
《如何檢測、清理Greenplum垃圾 - 阿里雲HybridDB for PG最佳實踐》
判斷方法:
如果gp_bloat_diag有返回,表示某些表膨脹很嚴重。
事件級別:
warning
重要程度:
重要
監控頻率:
周或月
處理方法:
在DOWNTIME(維護窗口)執行以下SQL之一,回收空間。
以下SQL會申請排它鎖,堵塞增刪改查。謹慎選擇時間操作。
-- 不重建索引
vacuum full table;
或使用原分布方法redistribute table
-- 會重建索引
alter table <table_name> set with (reorganize=true) distributed randomly;
或
alter table <table_name> set with (reorganize=true) distributed by (<column_names1>,<column_names2>....)
為了防止膨脹,應該定期執行VACUUM(VACUUM不申請排他鎖),不影響DML和查詢。
業務層監測
1、最新狀態,列表、餅圖、柱狀圖。
2、歷史狀態,列表、趨勢圖。
3、圖例
3.1、餅圖,空間占比
3.2、趨勢圖,IOPS
3.3、列表,明細
3.4、柱狀圖,表大小、IO、命中率等占比分布。
3.5、熱力圖,表大小、IO、命中率等占比分布。
1、數據傾斜
檢查方法:
1、主機級/per segment,輸出每台主機的已使用空間(所有空間、數據空間、臨時空間、REDO空間 分別展示),剩余空間(所有空間、數據空間、臨時空間、REDO空間 分別展示)。
通過操作系統命令獲得,或通過Greenplum plpythonu UDF獲得。
(Greenplum通過 gp_dist_random('gp_id') 可以在所有節點調用某個函數,並返回所有SEGMENT調用的結果。)
2、庫級/per segment,以庫為單位,輸出每個庫在每個segment的使用空間。
集群傾斜,返回采樣標准方差(單位為MB)。
create or replace function f1() returns setof text as $$
select pg_size_pretty((sum(pg_database_size(datname)))::int8) from pg_database ;
$$ language sql strict;
select gp_execution_dbid(),f1() from gp_dist_random('gp_id') order by 2 desc,1;
create or replace function ff1() returns int8 as $$ -- 返回MB
select sum(pg_database_size(datname))::int8/1024/1024 from pg_database ;
$$ language sql strict;
create or replace function fff1() returns setof int8 as $$ -- 返回MB
select ff1() from gp_dist_random('gp_id') ;
$$ language sql strict;
-- 求采樣標准方差 , MB
select stddev_pop(fff1) from fff1() ;
單庫傾斜,返回采樣標准方差(單位為MB)。
create or replace function f() returns setof text as $$
select datname||': '||pg_size_pretty(pg_database_size(datname)) from pg_database ;
$$ language sql strict;
select gp_execution_dbid(),f() from gp_dist_random('gp_id') order by 1,2;
-- 單位 MB
create or replace function ff(name) returns int8 as $$ -- 返回MB
select pg_database_size($1)/1024/1024 ;
$$ language sql strict;
create or replace function fff(name) returns setof int8 as $$ -- 返回MB
select ff($1) from gp_dist_random('gp_id') ;
$$ language sql strict;
-- 求采樣標准方差 , MB
select stddev_pop(fff) from fff('postgres');
postgres=# select stddev_pop(fff) from fff('postgres');
stddev_pop
------------------
262.642897613217
(1 row)
3、表級/per segment,以表為單位,返回采樣標准方差(單位為MB)。
create or replace function f2(oid) returns setof int8 as $$
select pg_relation_size($1)/1024/1024 from gp_dist_random('gp_id');
$$ language sql strict;
-- 表中方差:MB 單位
postgres=# select stddev_pop(f2) from f2('tbl'::text::regclass);
stddev_pop
------------------
261.800310052815
(1 row)
postgres=# select stddev_pop(f2) from f2('t_ao_column_2m_0'::text::regclass);
stddev_pop
------------------------
0.19982631347136331424
(1 row)
判斷方法:
同上
事件級別:
警告
重要程度:
重要
監控頻率:
天
處理方法:
修改分布鍵,或者使用隨機分布。
《分布式DB(Greenplum)中數據傾斜的原因和解法 - 阿里雲HybridDB for PostgreSQL最佳實踐》
查詢(QUERY)傾斜
除了數據分布傾斜,另一種傾斜是查詢傾斜,通常更加查詢計划可以發現查詢傾斜,例如" 窗口查詢 over (partition by) " 會強制重分布,如果這個分組有傾斜,那么就可能出現查詢傾斜。
通過SQL本身,以及通過觀察segment節點大小,可以發現查詢傾斜。
2、鎖等待事件
gp_toolkit.gp_locks_on_relation gp_toolkit.gp_locks_on_resqueue
檢查方法:
select usename, datname, query_start, current_query from pg_stat_activity where waiting;
判斷方法:
是否有等待時間超過N分鍾的QUERY。
事件級別:
警告
重要程度:
重要
監控頻率:
10分鍾
處理方法:
《PostgreSQL 鎖等待監控 珍藏級SQL - 誰堵塞了誰》
與業務方溝通解決,殺死或繼續等待。
3、業務層資源負載
1、資源隊列
postgres=# select * from pg_resqueue;
rsqname | rsqcountlimit | rsqcostlimit | rsqovercommit | rsqignorecostlimit
------------+---------------+--------------+---------------+--------------------
pg_default | 20 | -1 | f | 0
(1 row)
postgres=# select * from pg_resqueue_status ;
rsqname | rsqcountlimit | rsqcountvalue | rsqcostlimit | rsqcostvalue | rsqwaiters | rsqholders
------------+---------------+---------------+--------------+--------------+------------+------------
pg_default | 20 | 0 | -1 | | 0 | 0
(1 row)
gp_toolkit.gp_resq_activity
gp_toolkit.gp_resq_activity_by_queue
gp_toolkit.gp_resq_priority_backend
gp_toolkit.gp_resq_priority_statement
gp_toolkit.gp_resq_role
gp_toolkit.gp_resqueue_status
檢查方法:
select * from pg_resqueue_status where rsqwaiters > N;
判斷方法:
有記錄返回,說明有等待的QUERY。
事件級別:
警告
重要程度:
重要
監控頻率:
10分鍾
處理方法:
修改限制,或擴容。
《Greenplum 內存與負載管理(resource queue)最佳實踐》
2、會話內存開銷
psql -d testdb -f $GPHOME/share/postgresql/contrib/gp_session_state.sql
psql -d testdb -f $GPHOME/share/postgresql/contrib/uninstall_gp_session_state.sql
查詢 session_level_memory_consumption
檢查方法:
查詢 session_level_memory_consumption
判斷方法:
會話內存超過N(根據segment內存規格 百分比來確定。)
事件級別:
警告
重要程度:
重要
監控頻率:
30分鍾
處理方法:
考慮是否需要加資源、擴容。或者調優SQL。
4、節點資源負載
檢查方法:
單機單租戶、ECS單租戶型
從虛擬機或主機檢查。
dstat
iotop
iostat
sar
指標
cpu :建議正常值低於核數
iops : 建議正常值低於硬件能力
bw : 建議正常值低於硬件能力
network : 建議正常值低於帶寬
memory :建議rss低於內存大小-系統留存-os內核配置保留段
單機多租戶型
通過 cgroup stats 觀察
cpu :建議正常值低於 分配數
iops : 建議正常值低於 分配數
bw : 建議正常值低於 分配數
network :建議正常值低於 分配數
memory :建議rss低於 分配數
判斷方法:
超過閾值。
事件級別:
警告
重要程度:
重要
監控頻率:
10分鍾
處理方法:
體現在實例狀態報告,建議擴容。
5、workfile使用監控
gp_toolkit.gp_workfile_usage_per_query
gp_toolkit.gp_workfile_usage_per_segment
gp_toolkit.gp_workfile_entries
檢查方法:
select * from gp_toolkit.gp_workfile_usage_per_segment where size > N;
判斷方法:
檢查使用WORK FILE SIZE大於N的segment。
事件級別:
警告
重要程度:
重要
監控頻率:
30分鍾
處理方法:
查詢是哪個QUERY使用了大量WORK FILE,優化QUERY,或增加資源,擴大max_statement_mem。
selece * from gp_toolkit.gp_workfile_usage_per_query ;
繪圖
趨勢圖(全局、SEGMENT級、QUERY級)。
6、連接數
檢查方法:
select count(*) from pg_stat_activity;
判斷方法:
超過閾值:
N% * (max_connections - superuser_reserved_connections)
事件級別:
警告
重要程度:
重要
監控頻率:
10分鍾
處理方法:
與業務方確認:客戶端使用連接池、使用資源隊列、限制用戶、庫級連接數、擴容、加連接。
7、活躍連接數
檢查方法:
select count(*) from pg_stat_activity where current_query !~ 'IDLE';
判斷方法:
超過閾值
事件級別:
警告
重要程度:
重要
監控頻率:
10分鍾
處理方法:
如果資源也遇到瓶頸,則需要擴容、使用連接池、使用資源隊列。
繪圖
趨勢圖
8、剩余連接
檢查方法:
max_connections - superuser_reserved_connections - select count(*) from pg_stat_activity;
判斷方法:
低於閾值
事件級別:
警告
重要程度:
重要
監控頻率:
10分鍾
處理方法:
如果資源也遇到瓶頸,則需要擴容、使用連接池、使用資源隊列。
繪圖
趨勢圖
9、用戶級連接和剩余連接
檢查方法:
select rolname, rolconnlimit from pg_stat_activity;
select usename, count(*) from pg_stat_activity group by 1;
判斷方法:
超過閾值
事件級別:
警告
重要程度:
重要
監控頻率:
10分鍾
處理方法:
如果資源也遇到瓶頸,則需要擴容、使用連接池、使用資源隊列。
如果沒有瓶頸,修改用戶級連接限制。
繪圖
趨勢圖
10、庫級連接和剩余連接
檢查方法:
select datname, datconnlimit from pg_database;
select datname, count(*) from pg_stat_activity group by 1;
判斷方法:
超過閾值
事件級別:
警告
重要程度:
重要
監控頻率:
10分鍾
處理方法:
如果資源也遇到瓶頸,則需要擴容、使用連接池、使用資源隊列。
如果沒有瓶頸,修改庫級連接限制。
繪圖
趨勢圖
11、等待中會話
檢查方法:
select count(*) from pg_stat_activity where waiting;
判斷方法:
超過閾值
事件級別:
警告
重要程度:
重要
監控頻率:
10分鍾
處理方法:
排查等待原因。
繪圖
趨勢圖
12、運行中長事務
檢查方法:
select * from pg_stat_activity where now()-xact_start > interval 'N second';
判斷方法:
超過閾值
事件級別:
警告
重要程度:
重要
監控頻率:
10分鍾
處理方法:
檢查長事務原因,體現在健康報告中。
繪圖
趨勢圖
13、IDLE in transaction 長事務
檢查方法:
select * from pg_stat_activity where current_query ~ 'IDLE in transaction' and now()-xact_start > interval 'N second';
判斷方法:
超過閾值
事件級別:
警告
重要程度:
重要
監控頻率:
10分鍾
處理方法:
檢查業務,為什么事務會處於空閑狀態。
繪圖
趨勢圖
14、2PC事務
檢查方法:
select * from pg_prepared_xacts where now()-prepared > interval 'N second';
判斷方法:
超過閾值
事件級別:
警告
重要程度:
重要
監控頻率:
10分鍾
處理方法:
檢查業務,為什么會長時間不提交2PC事務。
繪圖
趨勢圖
15、慢查詢
檢查方法:
select * from pg_stat_activity where query_start > interval 'N second';
判斷方法:
超過閾值
事件級別:
警告
重要程度:
重要
監控頻率:
10分鍾
處理方法:
優化、擴容。
繪圖
趨勢圖
16、泄露大對象(PG)
檢查方法:
vacuumlo -n $db -w
判斷方法:
如果大對象沒有被引用時, 建議刪除, 否則就類似於內存泄露, 使用vacuumlo可以刪除未被引用的大對象, 例如: vacuumlo -l 1000 $db -w .
應用開發時, 注意及時刪除不需要使用的大對象, 使用lo_unlink 或 驅動對應的API .
參考 http://www.postgresql.org/docs/9.4/static/largeobjects.html
事件級別:
警告
重要程度:
重要
監控頻率:
天
處理方法:
如果大對象沒有被引用時, 建議刪除, 否則就類似於內存泄露, 使用vacuumlo可以刪除未被引用的大對象, 例如: vacuumlo -l 1000 $db -w .
應用開發時, 注意及時刪除不需要使用的大對象, 使用lo_unlink 或 驅動對應的API .
參考 http://www.postgresql.org/docs/9.4/static/largeobjects.html
繪圖
趨勢圖
17、數據庫空間分布柱狀圖
1、柱狀圖
select current_database(), buk this_buk_no, cnt rels_in_this_buk, pg_size_pretty(min) buk_min, pg_size_pretty(max) buk_max
from
(
select row_number() over (partition by buk order by tsize),tsize,buk,min(tsize) over (partition by buk),max(tsize) over (partition by buk),count(*) over (partition by buk) cnt
from
(
select pg_relation_size(a.oid) tsize, width_bucket(pg_relation_size(a.oid),tmin-1,tmax+1,10) buk
from
(
select min(pg_relation_size(a.oid)) tmin,max(pg_relation_size(a.oid)) tmax
from pg_class a,pg_namespace c where a.relnamespace=c.oid and nspname !~ $$^pg_$$ and nspname<>$$information_schema$$
) t,
pg_class a,
pg_namespace c
where a.relnamespace=c.oid and nspname !~ $$^pg_$$ and nspname<>$$information_schema$$
) t
)t where row_number=1;
區分:
庫級
表空間級
schema級
用戶級
表級
索引級
2、列表(SIZE TOP N)
select datname, pg_size_pretty(pg_database_size(datname)) from pg_database order by pg_database_size(datname) desc limit N;
select datname, pg_size_pretty(pg_relation_size(oid)) from pg_class where relkind='r' order by pg_relation_size(oid) desc limit N;
select datname, pg_size_pretty(pg_relation_size(oid)) from pg_class where relkind='i' order by pg_relation_size(oid) desc limit N;
區分
庫級
表空間級
schema級
用戶級
表級
索引級
18、耗盡序列
剩余可使用次數不足1000萬次的序列檢查:
for db in `psql --pset=pager=off -t -A -q -c 'select datname from pg_database where datname not in ($$template0$$, $$template1$$)'`
do
psql -d $db --pset=pager=off <<EOF
create or replace function f(OUT v_datname name, OUT v_role name, OUT v_nspname name, OUT v_relname name, OUT v_times_remain int8) returns setof record as \$\$
declare
begin
v_datname := current_database();
for v_role,v_nspname,v_relname in select rolname,nspname,relname from pg_authid t1 , pg_class t2 , pg_namespace t3 where t1.oid=t2.relowner and t2.relnamespace=t3.oid and t2.relkind='S'
LOOP
execute 'select (max_value-last_value)/increment_by from "'||v_nspname||'"."'||v_relname||'" where not is_cycled' into v_times_remain;
return next;
end loop;
end;
\$\$ language plpgsql;
select * from f() where v_times_remain is not null and v_times_remain < 10240000 order by v_times_remain limit 10;
EOF
done
序列剩余使用次數到了之后, 將無法使用, 報錯, 請開發人員關注.
19、索引活躍度監控
《PostgreSQL pg_stat_ pg_statio_ 統計信息(scan,read,fetch,hit)源碼解讀》
繪圖:趨勢、當前狀態餅圖
正排、倒排
1、IO活躍度
使用較少、較多的索引(idx_blks_read + idx_blks_hit)
消耗物理IO較多、較少的索引(idx_blks_read)
pg_statio_all_indexes
View "pg_catalog.pg_statio_all_indexes"
Column | Type | Modifiers
---------------+--------+-----------
relid | oid |
indexrelid | oid |
schemaname | name |
relname | name |
indexrelname | name |
idx_blks_read | bigint | -- pg_stat_get_blocks_fetched(i.oid) - pg_stat_get_blocks_hit(i.oid)
idx_blks_hit | bigint | -- pg_stat_get_blocks_hit(i.oid)
2、訪問活躍度
使用較少、較多的索引(idx_scan)
掃描較少、較多index item的索引(idx_tup_read)
從索引讀取HEAP TUPLE較多、較少的索引(idx_tup_fetch)
postgres=# \d+ pg_stat_all_indexes
View "pg_catalog.pg_stat_all_indexes"
Column | Type | Modifiers | Storage | Description
---------------+--------+-----------+---------+-------------
relid | oid | | plain |
indexrelid | oid | | plain |
schemaname | name | | plain |
relname | name | | plain |
indexrelname | name | | plain |
idx_scan | bigint | | plain | -- pg_stat_get_numscans(i.oid)
idx_tup_read | bigint | | plain | -- pg_stat_get_tuples_returned(i.oid)
idx_tup_fetch | bigint | | plain | -- pg_stat_get_tuples_fetched(i.oid)
20、表活躍度監控
繪圖:趨勢、當前狀態餅圖
正排、倒排
1、IO活躍度
pg_statio_all_tables
View "pg_catalog.pg_statio_all_tables"
Column | Type | Modifiers
-----------------+--------+-----------
relid | oid |
schemaname | name |
relname | name |
heap_blks_read | bigint | -- pg_stat_get_blocks_fetched(c.oid) - pg_stat_get_blocks_hit(c.oid)
heap_blks_hit | bigint | -- pg_stat_get_blocks_hit(c.oid)
idx_blks_read | bigint | -- sum(pg_stat_get_blocks_fetched(i.indexrelid) - pg_stat_get_blocks_hit(i.indexrelid))
idx_blks_hit | bigint | -- sum(pg_stat_get_blocks_hit(i.indexrelid))
toast_blks_read | bigint | -- pg_stat_get_blocks_fetched(t.oid) - pg_stat_get_blocks_hit(t.oid)
toast_blks_hit | bigint | -- pg_stat_get_blocks_hit(t.oid)
tidx_blks_read | bigint | -- sum(pg_stat_get_blocks_fetched(x.indexrelid) - pg_stat_get_blocks_hit(x.indexrelid))
tidx_blks_hit | bigint | -- sum(pg_stat_get_blocks_hit(x.indexrelid))
使用(索引+表)較少、較多的表(heap_blks_read+heap_blks_hit+toast_blks_read+toast_blks_hit+idx_blks_read+idx_blks_hit+tidx_blks_read+tidx_blks_hit)
消耗IO較多的(索引+表)表(heap_blks_read+toast_blks_read+idx_blks_read+tidx_blks_read)
使用(索引)較少、較多的表(idx_blks_read+idx_blks_hit+tidx_blks_read+tidx_blks_hit)
消耗IO較多的(索引)表(idx_blks_read+tidx_blks_read)
使用(表)較少、較多的表(heap_blks_read+heap_blks_hit+toast_blks_read+toast_blks_hit)
消耗IO較多的(表)表(heap_blks_read+toast_blks_read)
2、訪問活躍度
postgres=# \d pg_stat_all_tables
View "pg_catalog.pg_stat_all_tables"
Column | Type | Collation | Nullable | Default
---------------------+--------------------------+-----------+----------+---------
relid | oid | | |
schemaname | name | | |
relname | name | | |
seq_scan | bigint | | | -- 全表掃描次數
seq_tup_read | bigint | | | -- 使用全表掃描方法掃過的tuple條數
idx_scan | bigint | | | -- 該表的所有索引掃描次數總和
idx_tup_fetch | bigint | | | -- 該表的所有 從索引的tid獲取HEAP tuple的條數
n_tup_ins | bigint | | | -- 插入條數
n_tup_upd | bigint | | | -- 更新條數
n_tup_del | bigint | | | -- 刪除條數
n_tup_hot_upd | bigint | | | -- 同頁面更新條數
n_live_tup | bigint | | | -- 有效記錄
n_dead_tup | bigint | | | -- 已刪除記錄
n_mod_since_analyze | bigint | | |
last_vacuum | timestamp with time zone | | |
last_autovacuum | timestamp with time zone | | |
last_analyze | timestamp with time zone | | |
last_autoanalyze | timestamp with time zone | | |
vacuum_count | bigint | | |
autovacuum_count | bigint | | |
analyze_count | bigint | | |
autoanalyze_count | bigint | | |
注釋部分的正排、倒排 TOP N。
21、數據庫年齡
檢查方法:
select datname, age(datfrozenxid) from pg_database where age(datfrozenxid) > N
判斷方法:
超過閾值
事件級別:
警告
重要程度:
重要
監控頻率:
10分鍾
處理方法:
如果超過15億,建議用戶在業務空閑時間段,執行:
postgres=# set vacuum_freeze_min_age = 0;
postgres=# vacuum freeze;
詳見
《PostgreSQL的"天氣預報" - 如何預測Freeze IO風暴》
繪圖
趨勢圖
22、表年齡
檢查方法:
select relname, age(relfrozenxid) from pg_class where relkind='r' where age(relfrozenxid) >N order by 2 desc;
判斷方法:
超過閾值
事件級別:
警告
重要程度:
重要
監控頻率:
10分鍾
處理方法:
postgres=# set vacuum_freeze_min_age = 0;
postgres=# vacuum freeze table_name;
繪圖
趨勢圖
23、WAL目錄
檢查方法:
$PGDATA/pg_xlog, $PGDATA/pg_wal
空間占用
判斷方法:
超過閾值
事件級別:
警告
重要程度:
重要
監控頻率:
10分鍾
處理方法:
《PostgreSQL 清理redo(xlog,wal,歸檔)的機制 及 如何手工清理》
繪圖
趨勢圖
24、未歸檔日志
檢查方法:
$PGDATA/ pg_wal|pg_xlog /archive_status
*.ready 表示未歸檔
判斷方法:
未歸檔文件數超過閾值
事件級別:
警告
重要程度:
重要
監控頻率:
10分鍾
處理方法:
檢查pg_log中的錯誤,是否有歸檔異常。
檢查歸檔機制、歸檔命令配置,是否正常。
檢查IO、網絡、CPU是否存在瓶頸。
必要時進行清理,記得清理時同時清理.ready文件。
《PostgreSQL 清理redo(xlog,wal,歸檔)的機制 及 如何手工清理》
繪圖
趨勢圖
25、臨時空間
檢查方法:
show temp_tablespaces; -- 參數
如果沒有單獨臨時目錄,則在表空間目錄的pgsql_tmp目錄下。
檢查空間占用
判斷方法:
超過閾值
事件級別:
警告
重要程度:
重要
監控頻率:
10分鍾
處理方法:
檢查SQL,是否有優化空間。
例如提高work_mem減少某些情況下的臨時空間使用。
繪圖
趨勢圖
26、數據庫IO、事務活躍情況
檢查方法:
pg_stat_database
View "pg_catalog.pg_stat_database"
Column | Type | Modifiers
---------------+---------+-----------
datid | oid |
datname | name |
numbackends | integer |
xact_commit | bigint | -- 提交
xact_rollback | bigint | -- 回滾
blks_read | bigint | -- 未在數據庫shared buffer中命中的讀
blks_hit | bigint | -- 在數據庫shared buffer中命中的讀
單位時間已提交事務數
單位時間回滾事務數
單位時間事務回滾比率
單位時間物理讀
單位時間命中讀
單位時間未命中讀比例
以下指標需要修改內核:
單位時間 qps
單位時間 insert qps
單位時間 select qps
單位時間 update qps
判斷方法:
超過閾值
事件級別:
警告
重要程度:
重要
監控頻率:
10分鍾
處理方法:
未命中讀過多,建議添加內存。
回滾率過高,建議業務方排查業務。
繪圖
趨勢圖
27、數據庫集群概貌
主機數
segment數
規格
集群配置
Table "pg_catalog.gp_segment_configuration"
Column | Type | Modifiers
------------------+------------+-----------
dbid | smallint | not null
content | smallint | not null
role | "char" | not null
preferred_role | "char" | not null
mode | "char" | not null
status | "char" | not null
port | integer | not null
hostname | text |
address | text |
replication_port | integer |
san_mounts | int2vector |
集群歷史事件
Table "pg_catalog.gp_configuration_history"
Column | Type | Modifiers
--------+--------------------------+-----------
time | timestamp with time zone | not null
dbid | smallint | not null
desc | text |
已使用容量
select pg_size_pretty((sum(pg_database_size(datname)))::int8) from pg_database ;
28、吞吐
單位時間吞吐(每N小時、天、。。。)
檢查方法:
1、寫入、刪除、更新記錄數(集群級、庫級、schema級、owner級、表級)。
View "pg_catalog.pg_stat_database"
Column | Type | Collation | Nullable | Default
----------------+--------------------------+-----------+----------+---------
datid | oid | | |
datname | name | | |
numbackends | integer | | |
xact_commit | bigint | | |
xact_rollback | bigint | | |
blks_read | bigint | | |
blks_hit | bigint | | |
tup_returned | bigint | | |
tup_fetched | bigint | | |
tup_inserted | bigint | | |
tup_updated | bigint | | |
tup_deleted | bigint | | |
conflicts | bigint | | |
temp_files | bigint | | |
temp_bytes | bigint | | |
deadlocks | bigint | | |
blk_read_time | double precision | | |
blk_write_time | double precision | | |
stats_reset | timestamp with time zone | | |
2、容量變化趨勢 (實例級、表、索引、SCHEMA、OWERN、DB級)。
List of functions
Schema | Name | Result data type | Argument data types | Type
------------+------------------------+------------------+---------------------+--------
pg_catalog | pg_database_size | bigint | name | normal
pg_catalog | pg_database_size | bigint | oid | normal
pg_catalog | pg_indexes_size | bigint | regclass | normal
pg_catalog | pg_relation_size | bigint | regclass | normal
pg_catalog | pg_relation_size | bigint | regclass, text | normal
pg_catalog | pg_table_size | bigint | regclass | normal
pg_catalog | pg_tablespace_size | bigint | name | normal
pg_catalog | pg_tablespace_size | bigint | oid | normal
pg_catalog | pg_total_relation_size | bigint | regclass | normal
3、WAL產生速度
pg_current_wal_lsn() -- 快照,記錄位點
pg_size_pretty(pg_wal_lsn_diff(lsn1, lsn2)) -- 計算兩個位點的WAL差異
4、臨時文件產生速度 (從審計日志生成)
判斷方法:
超過閾值
事件級別:
警告
重要程度:
重要
監控頻率:
10分鍾
處理方法:
無
繪圖
趨勢圖
29、執行計划翻轉
《執行計划維度統計\判斷執行計划翻轉\統計每種執行計划的統計信息 use pg_stat_plans in PostgreSQL 9.0, 9.1 and 9.2》
執行計划變化,不做告警,只做診斷參考。
30、安全,認證錯誤
日志文件中的密碼錯誤類錯誤,達到一定閾值,報警。
Class 28 — Invalid Authorization Specification
28000 invalid_authorization_specification
28P01 invalid_password
31、備庫延遲(PG)
1、物理備庫
View "pg_catalog.pg_stat_replication"
Column | Type | Collation | Nullable | Default
------------------+--------------------------+-----------+----------+---------
pid | integer | | |
usesysid | oid | | |
usename | name | | |
application_name | text | | |
client_addr | inet | | |
client_hostname | text | | |
client_port | integer | | |
backend_start | timestamp with time zone | | |
backend_xmin | xid | | |
state | text | | |
sent_lsn | pg_lsn | | |
write_lsn | pg_lsn | | |
flush_lsn | pg_lsn | | |
replay_lsn | pg_lsn | | |
write_lag | interval | | |
flush_lag | interval | | |
replay_lag | interval | | |
sync_priority | integer | | |
sync_state | text | | |
select pg_xlog_location_diff(pg_current_xlog_location(),flush_location), * from pg_stat_replication;
select pg_xlog_location_diff(pg_current_xlog_location(),restart_lsn), * from pg_replication_slots;
查詢閃斷的邏輯備庫,
select * from pg_stat_replication where application_name = ?;
如果沒有記錄返回,說明出現了閃斷
2、邏輯備庫
《PostgreSQL 10 流式物理、邏輯主從 最佳實踐》
《PostgreSQL 10.0 preview 邏輯復制 - 原理與最佳實踐》
32、TOP SQL(PG)
pg_stat_statements
View "public.pg_stat_statements"
Column | Type | Modifiers
---------------------+------------------+-----------
userid | oid |
dbid | oid |
queryid | bigint |
query | text |
calls | bigint |
total_time | double precision |
rows | bigint |
shared_blks_hit | bigint |
shared_blks_read | bigint |
shared_blks_dirtied | bigint |
shared_blks_written | bigint |
local_blks_hit | bigint |
local_blks_read | bigint |
local_blks_dirtied | bigint |
local_blks_written | bigint |
temp_blks_read | bigint |
temp_blks_written | bigint |
blk_read_time | double precision |
blk_write_time | double precision |
TOP SQL:
ORDER BY total cpu
ORDER BY total io
ORDER BY per cpu
ORDER BY per io
ORDER BY 產生臨時文件top
抖動TOP
ORDER BY 標准方差(SQL抖動)
數據庫維護任務
1、回收垃圾
處理方法:
1、確保如下參數打開,注意很多地方都需要依賴這兩個參數,比如pg_stat_xxx, pg_statio_xxx。
track_counts = on
track_activities = on
連接到所有數據庫,執行如下步驟
2、查詢需要回收垃圾的對象(例如超過20%的垃圾)
select schemaname, relname from pg_stat_all_tables where (n_tup_upd+n_tup_del)::float8/(n_tup_upd+n_tup_del+n_tup_ins+1) > 0.2;
3、回收垃圾
VACUUM schemaname.relname;
4、保留統計信息
insert into tbl select now(),* from pg_stat_all_tables;
5、清空統計信息
計數器清零。包括pg_stat_xxx, pg_statio_xxx等。
select pg_stat_reset();
重要程度:
重要
執行頻率:
天
2、生成統計信息。
處理方法:
連接到所有數據庫,執行如下步驟
analyze;
重要程度:
重要
執行頻率:
在導入大量數據、更新大量數據、刪除大量數據后,都建議執行。
3、備份。
處理方法:
gpcrondump
重要程度:
非常重要
執行頻率:
根據數據變更頻率執行。
如果底層存儲支持快照備份,建議快照備份。
4、元數據維護。
處理方法:
每個節點都要緩存元數據,用於QUERY一些階段使用(例如優化器,生成最優執行計划)。
如果你發現執行計划時間比以前久,也許是系統表需要維護了。
1、回收垃圾
同回收垃圾章節
檢查膨脹,並回收垃圾,此處可省略。
vacuum 系統表;
2、重建系統表索引
連接到所有數據庫,分別執行
reindex SYSTEM 庫名;
3、生成系統表統計信息
ANALYZE pg_catalog.系統表;
重要程度:
重要
執行頻率:
視情況,例如頻繁創建表,刪除表,創建刪除視圖、索引、函數等等。則系統表垃圾產生很快。
周
補丁和升級
評估並升級OS補丁、GPDB補丁。
參考
http://gpdb.docs.pivotal.io/43160/admin_guide/monitoring/monitoring.html
http://greenplum.org/docs/admin_guide/monitoring/monitoring.html
https://github.com/digoal/pgsql_admin_script/blob/master/generate_report.sh
《Use PostgreSQL collect and analyze Operation System statistics》
http://pgstatsinfo.sourceforge.net/documents/reporter3.2/html/files/report_sample.html#alerts
《PostgreSQL AWR報告(for 阿里雲ApsaraDB PgSQL)》
《如何生成和閱讀EnterpriseDB (PPAS)診斷報告》
http://docs.oracle.com/cd/E11882_01/server.112/e41573/autostat.htm#PFGRF94162
https://bucardo.org/check_postgres/
《PostgreSQL pg_stat_ pg_statio_ 統計信息(scan,read,fetch,hit)源碼解讀》