PostgreSQL数据库锁表查询

 

--执行时间超过 10s 的语句
select * from pg_stat_activity where state<>'idle' and now()-query_start > interval '10 s'
--锁表
SELECT * FROM pg_stat_activity where datname='bms' and wait_event_type='Lock'--limit 10

 

--将进程杀掉
select pg_cancel_backend("死锁那条数据的pid值")
--运行完后,再次刷新这个表,sql就可顺利执行。
select pg_cancel_backend(27280);

 

--数据库 整理 查询(查询死元组情况)
SELECT
c.relname 表名,
(current_setting('autovacuum_analyze_threshold')::NUMERIC(12,4))+(current_setting('autovacuum_analyze_scale_factor')::NUMERIC(12,4))*reltuples AS 自动分析阈值,
(current_setting('autovacuum_vacuum_threshold')::NUMERIC(12,4))+(current_setting('autovacuum_vacuum_scale_factor')::NUMERIC(12,4))*reltuples AS 自动清理阈值,
reltuples::DECIMAL(19,0) 活元组数,
n_dead_tup::DECIMAL(19,0) 死元组数
FROM
pg_class c

LEFT JOIN pg_stat_all_tables d

ON C.relname = d.relname
WHERE
c.relname LIKE'tb%' AND reltuples > 0
AND n_dead_tup > (current_setting('autovacuum_analyze_threshold')::NUMERIC(12,4))+(current_setting('autovacuum_analyze_scale_factor')::NUMERIC(12,4))*reltuples;

 

--修改密码--重建索引
alter user bms_select with password 'QD6eXed6'
alter user bms_report with password 'Ucj1UW3BriA2KlW'
REINDEX INDEX idx_business_time

 

上一篇:一些教程链接


下一篇:更改数据库表中有数据的字段类型NUMERIC(18,2)为NUMERIC(18,6)