1.查看系统CPU使用率
sar -u -f /var/log/sa/sa27 (sa27 根据时间变化,sa27是27号信息的记录)
结果输出:
15时52分01秒 CPU %user %nice %system %iowait %steal %idle 15时53分01秒 all 0.32 0.00 0.69 0.00 0.00 98.99 15时54分01秒 all 0.30 0.00 0.68 0.00 0.00 99.02 15时55分01秒 all 0.30 0.00 0.70 0.00 0.00 99.00 15时56分01秒 all 0.29 0.00 0.68 0.00 0.00 99.03 15时57分01秒 all 0.30 0.00 0.69 0.00 0.00 99.01 15时58分01秒 all 0.30 0.00 0.69 0.00 0.00 99.01 15时59分01秒 all 0.30 0.00 0.71 0.00 0.00 98.99 16时00分01秒 all 0.30 0.00 0.69 0.00 0.00 99.00 16时01分01秒 all 0.30 0.00 0.69 0.00 0.00 99.01 平均时间: all 0.33 0.00 0.61 0.03 0.00 99.02
2.查看数据库连接
select state,count(*) from pg_stat_activity group by state;
3.查看数据库慢SQL
1)整体慢SQL查询
select db_name,unique_query_id,count(*) from statement_history group by db_name,unique_query_id;
openGauss=# select db_name,unique_query_id,count(*) from statement_history group by db_name,unique_query_id;
db_name | unique_query_id | count
---------+-----------------+-------
gzdz | 2533841608 | 1
gzdz | 2066747755 | 3
gzdz | 1116594456 | 1
(3 rows)
2)具体慢SQL查询
openGauss=# select unique_query_id,query from statement_history;
unique_query_id | query -----------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------- 2066747755 | INSERT INTO sys_stu.log_stu (client, create_time, description, header, ip, method, params, result, sign, stu_id, time, url) + | VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) + | RETURNING id 2533841608 | INSERT INTO sys.log_change (change_content, change_type, create_time, person_id, person_type, sign, table_chinese_name, table_name, table_primary_key) + | VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) + | RETURNING id 2066747755 | INSERT INTO sys_stu.log_stu (client, create_time, description, header, ip, method, params, result, sign, stu_id, time, url) + | VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) + | RETURNING id 1116594456 | INSERT INTO sys_stu.log_operation_stu (certificate_no, certificate_type, create_time, description, ip, log_change_ids, log_stu_id, operation, sign, stu_id, stu_name)+ | VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) + | RETURNING id 2066747755 | INSERT INTO sys_stu.log_stu (client, create_time, description, header, ip, method, params, result, sign, stu_id, time, url) + | VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) + | RETURNING id (5 rows)
openGauss=# select db_name,unique_query_id,count(*) from statement_history group by db_name,unique_query_id order by 3; db_name | unique_query_id | count ---------+-----------------+------- gzdz | 1587254874 | 1 gzdz | 1961954918 | 1 gzdz | 2833510906 | 1 gzdz | 3913769193 | 1 gzdz | 2771099244 | 2 gzdz | 1116594456 | 5 gzdz | 2533841608 | 10 gzdz | 2533841608 | 11 gzdz | 4233328942 | 1441 (9 rows)
openGauss=# select query,finish_time-start_time exec_time from statement_history where unique_query_id ='4233328942' limit 1; query | exec_time -------------------------------------------------------------------------------------+----------------- UPDATE exam.exam_place AS e +| 00:00:01.144499 SET applied_capacity = e.applied_capacity + ? +| WHERE e.is_valid AND e.exam_place_code = $1 AND e.capacity - e.applied_capacity > ? | (1 row)
openGauss=# select query_plan from statement_history where unique_query_id ='4233328942' limit 1; query_plan ----------------------------------------------------------------------------------------------------------- Datanode Name: dn_6001_6002_6003 + Update on exam_place e (cost=0.00..2.48 rows=1 width=180) + -> Index Scan using idx_exam_place_exam_place_code on exam_place e (cost=0.00..2.48 rows=1 width=180)+ Index Cond: ((exam_place_code)::text = '***'::text) + Filter: (is_valid AND ((capacity - applied_capacity) > '***')) + + (1 row)
4. 锁等待查询:
1.创建视图v_locks_monitor
\c gzdz
create view v_locks_monitor as with t_wait as ( select a.mode,a.locktype,a.database,a.relation,a.page,a.tuple,a.classid,a.granted, a.objid,a.objsubid,a.pid,a.transactionid, b.xact_start,b.query_start,b.usename,b.datname,b.client_addr,b.client_port,b.application_name from pg_locks a,pg_stat_activity b where a.pid=b.pid and not a.granted ), t_run as ( select a.mode,a.locktype,a.database,a.relation,a.page,a.tuple,a.classid,a.granted, a.objid,a.objsubid,a.pid,a.transactionid, b.xact_start,b.query_start,b.usename,b.datname,b.client_addr,b.client_port,b.application_name from pg_locks a,pg_stat_activity b where a.pid=b.pid and a.granted ), t_overlap as ( select r.* from t_wait w join t_run r on ( r.locktype is not distinct from w.locktype and r.database is not distinct from w.database and r.relation is not distinct from w.relation and r.page is not distinct from w.page and r.tuple is not distinct from w.tuple and r.transactionid is not distinct from w.transactionid and r.classid is not distinct from w.classid and r.objid is not distinct from w.objid and r.objsubid is not distinct from w.objsubid and r.pid <> w.pid ) ), t_unionall as ( select r.* from t_overlap r union all select w.* from t_wait w ) select locktype,datname,relation::regclass,page,tuple,transactionid::text,classid::regclass,objid,objsubid, string_agg( 'Pid: '||case when pid is null then 'NULL' else pid::text end||chr(10)|| 'Lock_Granted: '||case when granted is null then 'NULL' else granted::text end|| ' , Mode: '||case when mode is null then 'NULL' else mode::text end|| ' , Username: '||case when usename is null then 'NULL' else usename::text end|| ' , Database: '||case when datname is null then 'NULL' else datname::text end|| ' , Client_Addr: '||case when client_addr is null then 'NULL' else client_addr::text end|| ' , Client_Port: '||case when client_port is null then 'NULL' else client_port::text end|| ' , Application_Name: '||case when application_name is null then 'NULL' else application_name::text end||chr(10)|| ' , Xact_Start: '||case when xact_start is null then 'NULL' else xact_start::text end|| ' , Query_Start: '||case when query_start is null then 'NULL' else query_start::text end|| ' , Xact_Elapse: '||case when (now()-xact_start) is null then 'NULL' else (now()-xact_start)::text end|| chr(10)||'--------'||chr(10), (case when granted then '0' else '1' end) order by ( case mode when 'INVALID' then 0 when 'AccessShareLock' then 1 when 'RowShareLock' then 2 when 'RowExclusiveLock' then 3 when 'ShareUpdateExclusiveLock' then 4 when 'ShareLock' then 5 when 'ShareRowExclusiveLock' then 6 when 'ExclusiveLock' then 7 when 'AccessExclusiveLock' then 8 else 0 end ) desc ) as lock_conflict from t_unionall group by locktype,datname,relation,page,tuple,transactionid::text,classid,objid,objsubid;
2.查询锁等待
\c gzdz
select * from v_locks_monitor;
标签:postgreSQL,name,text,0.00,when,id,query,openGauss,数据库 From: https://www.cnblogs.com/shoulisun/p/18193162