PostgreSQL数据库锁表查询

2021/10/11 19:14:24

本文主要是介绍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

 



这篇关于PostgreSQL数据库锁表查询的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程