首页 > 数据库 >PostgreSQL - Check blocking SQL statements

PostgreSQL - Check blocking SQL statements

时间:2023-12-02 22:33:48浏览次数:35  
标签:PostgreSQL activity pid locks pg SQL statements blocking blocked

pg_locks view

Looking at pg_locks shows you what locks are granted and what processes are waiting for locks to be acquired. A good query to start looking for lock problems:

  select relation::regclass, * from pg_locks where not granted;

pg_stat_activity view

  • Figuring out what the processes holding or waiting for locks is easier if you cross-reference against the information in pg_stat_activity

Сombination of blocked and blocking activity

The following query may be helpful to see what processes are blocking SQL statements (these only find row-level locks, not object-level locks).

  SELECT blocked_locks.pid     AS blocked_pid,
         blocked_activity.usename  AS blocked_user,
         blocking_locks.pid     AS blocking_pid,
         blocking_activity.usename AS blocking_user,
         blocked_activity.query    AS blocked_statement,
         blocking_activity.query   AS current_statement_in_blocking_process
   FROM  pg_catalog.pg_locks         blocked_locks
    JOIN pg_catalog.pg_stat_activity blocked_activity  ON blocked_activity.pid = blocked_locks.pid
    JOIN pg_catalog.pg_locks         blocking_locks 
        ON blocking_locks.locktype = blocked_locks.locktype
        AND blocking_locks.database IS NOT DISTINCT FROM blocked_locks.database
        AND blocking_locks.relation IS NOT DISTINCT FROM blocked_locks.relation
        AND blocking_locks.page IS NOT DISTINCT FROM blocked_locks.page
        AND blocking_locks.tuple IS NOT DISTINCT FROM blocked_locks.tuple
        AND blocking_locks.virtualxid IS NOT DISTINCT FROM blocked_locks.virtualxid
        AND blocking_locks.transactionid IS NOT DISTINCT FROM blocked_locks.transactionid
        AND blocking_locks.classid IS NOT DISTINCT FROM blocked_locks.classid
        AND blocking_locks.objid IS NOT DISTINCT FROM blocked_locks.objid
        AND blocking_locks.objsubid IS NOT DISTINCT FROM blocked_locks.objsubid
        AND blocking_locks.pid != blocked_locks.pid
    JOIN pg_catalog.pg_stat_activity blocking_activity ON blocking_activity.pid = blocking_locks.pid
   WHERE NOT blocked_locks.granted;

 

Here's an alternate view of that same data that includes an idea how old the state is

SELECT a.datname,
       a.application_name,
       l.relation::regclass,
       l.transactionid,
       l.mode,
       l.locktype,
       l.GRANTED,
       a.usename,
       a.query,
       a.query_start,
       age(now(), a.query_start) AS "age",
       a.pid
FROM pg_stat_activity a
JOIN pg_locks l ON l.pid = a.pid
ORDER BY a.query_start;

 

Copied from: https://wiki.postgresql.org/wiki/Lock_Monitoring

标签:PostgreSQL,activity,pid,locks,pg,SQL,statements,blocking,blocked
From: https://www.cnblogs.com/zhangzhihui/p/17872371.html

相关文章

  • C#连接mysql
    本文章是建立在已经安装MySQL数据库的前提,默认安装在C:\ProgramFiles(x86)\MySQL,建议在安装时选中Connector.NET6.9的安装,里面有MySQL与C#连接的动态链接库。帮助文档C:\ProgramFiles(x86)\MySQL\Connector.NET6.9\Documentation\ConnectorNET.chm是我撰写此文章的主......
  • 快速配置mysql(非docker)
    蠢新从大二开始用起mysql,直到工作了还需要去网上找博客查怎么装。Windows不管,因为我自己的工位电脑已经有了。以下的操作为假设你有一台腾讯云的服务器,或者版本至少在20以上的Ubuntu。root用户登录。使用包管理器安装mysql8.0apt-getupdateapt-getinstallmysql-serversys......
  • MySQL8.0在Ubuntu系统安装
    目录前言一、Mysql是什么?二、安装步骤1.安装操作需root权限:2.更新apt仓库信息3.查询库中mysql版本3.安装mysql4.启动MySQL5.登陆MySQL设置密码6.设置密码7.退出MySQL控制台8.对MySQL进行初始化1.输入密码:2.是否开启密码验证插件,如果需要增强密码安全性,输入y并回车,不需要直接回车(本......
  • SQLServer性能优化之二
    SQLServer性能优化之二背景优化了机器的硬件配置之后性能好了很多但是偶尔还是会出现阻塞.SQL总是奇奇怪怪的.其实第一天时就感觉可能是索引存在问题.但是dbcc重建所有数据库的索引太慢了.所以作罢了,从HDD传输到SSD后大部分功能已经可以用了以为问题就此解决,但是......
  • SQLBI_精通DAX课程笔记_02_数据类型
    SQLBI_精通DAX课程笔记_02_数据类型PowerBi和AnalysisServices在数据加载环节,无论数据源是什么类型,都会自动将数据转化为DAX可用的数据类型集。以下链接为微软官方文档,也可以参考浏览:https://learn.microsoft.com/zh-cn/power-bi/connect-data/desktop-data-types一:数据类......
  • 数据库总结复习(sql应用题 一)
    目录前言mysql基础语句ddl示例1创建表dcl授权收回权限dml结合事务索引分类格式视图行列子集视图可更新性存储过程示例1带返回值示例2游标示例3结合简单事务触发器前言本文针对考纲上的30分sql应用题所涉及到的知识进行归纳总结。会分为两篇文章,此篇为mysql语句。mysql基......
  • 20211316郭佳昊 《信息安全系统设计与实现(上)》 第十二周学习总结 MySQL数据库系统
    一、任务要求[1]知识点归纳以及自己最有收获的内容,选择至少2个知识点利用chatgpt等工具进行苏格拉底挑战,并提交过程截图,提示过程参考下面内容(4分)我在学****知识点,请你以苏格拉底的方式对我进行提问,一次一个问题核心是要求GPT:请你以苏格拉底的方式对我进行提问然后GPT就会......
  • 【mysql】limit实现分页
    MySQL中使用LIMIT实现分页格式:LIMIT[位置偏移量,]行数第一个“位置偏移量”参数指示MySQL从哪一行开始显示,是一个可选参数,如果不指定“位置偏移量”,将会从表中的第一条记录开始(第一条记录的位置偏移量是0,第二条记录的位置偏移量是1,以此类推);第二个参数“行数”指示返回的记录条......
  • [VBA] 实现SQLserver数据库的增删改查
    [VBA]实现SQLserver数据库的增删改查问题背景用于库存管理的简单Excel系统实现,能够让库管员录入每日出入库信息并进能够按日期查询导出数据,生成简要报表,以及数据修改与删除。非科班且对VB语言和数据库语言未系统学习,有一点C语言与Python基础,有不足之处还请指教。实现过程数......
  • 如何使用mysql实现分布式锁
    如何使用mysql实现可重入的分布式锁目录什么是分布式锁?如何实现分布式锁?定义分布式表结构定义锁统一接口使用mysql来实现分布式锁①生成线程标记ID②加锁③解锁④重置锁写在最后1.什么是分布式锁?百度百科:分布式锁是控制分布式系统之间同步访问共享资源的一......