HighgoDB查询慢SQL和阻塞SQL

文章目录

  • 环境
  • 文档用途
  • 详细信息

环境

系统平台:N/A
版本:6.0,5.6.5,5.6.4,5.6.3,5.6.1,4.5.2,4.5,4.3.4.9,4.3.4.8,4.3.4.7,4.3.4.6,4.3.4.5,4.3.4.4,4.3.4.3,4.3.4.2,4.3.4,4.7.8,4.7.7,4.7.6,4.7.5,4.3.2

文档用途

本文介绍了如何对数据库日志进行分析,并获取执行慢的SQL语句。并提供了查询阻塞会话的SQL语句。

详细信息

1.修改配置,记录SQL信息
修改配置文件postgresql.conf,设置以下参数

#开启日志记录logging_collector = on#设置日志输出格式,格式有stderr(默认), csvlog , sysloglog_destination = 'csvlog’#设置日志存放位置,下面设置表示日志存放在$PGDATA下hgdb_log日志中log_directory = 'hgdb_log’#设置日志截断log_truncate_on_rotation = on#设置日志的名称log_filename = 'highgodb_%d.log’#设置跟踪的SQL语句级别,级别包含none(默认,只记录出错信息), ddl, mod, alllog_statement = all#记录执行超过以下时间的SQL语句,单位毫秒log_min_duration_statement = 5000

数据库日志保留数量通过设置参数log_truncate_on_rotation和log_filename进行控制,参数log_truncate_on_rotation设置为on后,通过log_filename控制日志保留时间,常用设置如下

log_filename = 'highgodb-%I.log' #最多保存12小时的日志,每小时一个文件log_filename = 'highgodb-%H.log' #最多保存24小时的日志,每小时一个文件log_filename = 'highgodb-%w.log' #最多保存一周的日志,每天一个文件log_filename = 'highgodb-%d.log' #最多保存一个月的日志,每天一个文件log_filename = 'highgodb-%j.log' #最多保存一年的日志,每天一个文件

数据库日志文件一般较大,直接打开不方便分析,可以通过将日志文件导入到数据库中,使用SQL进行分析,建表语句如下

CREATE TABLE highgodb_log(  log_time timestamp(3) with time zone,user_name text,database_name text,process_id integer,connection_from text,session_id text,session_line_num bigint,command_tag text,session_start_time timestamp with time zone,virtual_transaction_id text,transaction_id bigint,error_severity text,sql_state_code text,message text,detail text,hint text,internal_query text,internal_query_pos integer,context text,query text,query_pos integer,location text,application_name text,PRIMARY KEY (session_id, session_line_num));

使用如下语句将日志导入到数据库

COPY highgodb_log FROM '/path/to/highgodb-1.csv' WITH csv;

使用SQL语句进行查询,此处给出的示例是按执行时间排序,由于执行时间信息存放在message列中,需要对该列进行截取才能进行排序,根据日志语言,需要将“执行时间”和截取字符量进行修改。

select log_time,database_name,user_name,application_name,substr(message, 7,8),message from hgdblog where message like '%执行时间%' order by substr(message, 7,8) desc;

2.查询阻塞会话的SQL
语句如下

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.virtualtransaction,a.virtualxid,a.transactionid,a.fastpath,    b.state,b.query,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.virtualtransaction,a.virtualxid,a.transactionid,a.fastpath,   b.state,b.query,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.virtualxid is not distinct from w.virtualxid 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,virtualxid,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||' , FastPath: '||case when fastpath is null then 'NULL' else fastpath::text end||' , VirtualTransaction: '||case when virtualtransaction is null then 'NULL' else virtualtransaction::text end||' , Session_State: '||case when state is null then 'NULL' else state::text end||chr(10)||   '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||' , Query_Elapse: '||case when (now()-query_start) is null then 'NULL' else (now()-query_start)::text end||chr(10)||    'SQL (Current SQL in Transaction): '||chr(10)||  case when query is null then 'NULL' else query::text end,    chr(10)||'--------'||chr(10)    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,   (case when granted then 0 else 1 end)  ) as lock_conflict  from t_unionall   group by   locktype,datname,relation,page,tuple,virtualxid,transactionid::text,classid,objid,objsubid ;  执行结果如下,如有大量锁时,可以使用\x进行行列转换,查看起来更方便。highgo-# locktype,datname,relation,page,tuple,virtualxid,transactionid::text,classid,objid,objsubid ;  locktype    | datname  | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid |                                                                     lock_conflict                                                                     ---------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------transactionid     | highgo     |          |      |       |            | 760            |         |       |          | Pid: 24355                                                                                                                                           +|          |          |      |       |            |               |         |       |          | Lock_Granted: true , Mode: ExclusiveLock , FastPath: false , VirtualTransaction: 5/527 , Session_State: idle in transaction                          +|          |          |      |       |            |               |         |       |          | Username: postgres , Database: postgres , Client_Addr: NULL , Client_Port: -1 , Application_Name: psql                                               +|          |          |      |       |            |               |         |       |          | Xact_Start: 2021-01-28 15:15:46.810264+08 , Query_Start: 2021-01-28 15:16:06.108277+08 , Xact_Elapse: 00:00:55.57579 , Query_Elapse: 00:00:36.277777 +|          |          |      |       |            |               |         |       |          | SQL (Current SQL in Transaction):                                                                                                                    +|          |          |      |       |            |               |         |       |          | delete from host ;                                                                                                                                   +|          |          |      |       |            |               |         |       |          | --------                                                                                                                                             +|          |          |      |       |            |               |         |       |          | Pid: 24383                                                                                                                                           +|          |          |      |       |            |               |         |       |          | Lock_Granted: false , Mode: ShareLock , FastPath: false , VirtualTransaction: 4/14615 , Session_State: active                                        +|          |          |      |       |            |               |         |       |          | Username: postgres , Database: postgres , Client_Addr: NULL , Client_Port: -1 , Application_Name: psql                                               +|          |          |      |       |            |               |         |       |          | Xact_Start: 2021-01-28 15:16:13.619987+08 , Query_Start: 2021-01-28 15:16:31.022072+08 , Xact_Elapse: 00:00:28.766067 , Query_Elapse: 00:00:11.363982+|          |          |      |       |            |               |         |       |          | SQL (Current SQL in Transaction):                                                                                                                    +|          |          |      |       |            |               |         |       |          | delete from host ;(1 row)

上面结果中,Lock_Granted: true 表示获取锁的会话,可以通过pid找到对应进程。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/pingmian/91897.shtml
繁体地址,请注明出处:http://hk.pswp.cn/pingmian/91897.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

day15 SPI

1串行外设接口概述1.1基本概念SPI&#xff08;串行外设接口&#xff09;是一种高速、全双工、同步的串行通信协议。串行外设接口一般是需要4根线来进行通信&#xff08;NSS、MISO、MOSI、SCK&#xff09;&#xff0c;但是如果打算实现单向通信&#xff08;最少3根线&#xff09…

阿里云微服务引擎 MSE 及 API 网关 2025 年 7 月产品动态

点击此处&#xff0c;了解微服务引擎 MSE 产品详情。

RFID技术在汽车倍速链中的应用:驱动智能制造的隐形引擎

RFID技术在汽车倍速链中的应用&#xff1a;驱动智能制造的隐形引擎某汽车汽车倍速链现场存在问题&#xff1a;&#xff08;1&#xff09;条形码需人工扫描&#xff0c;数据采集延迟率高达15%&#xff0c;导致生产调度与实际工况脱节&#xff1b;&#xff08;2&#xff09;涂装车…

ES集群调优策略

Index(写)调优 副本数置0 如果是集群首次灌入数据,可以将副本数设置为0&#xff0c;写入完毕再调整回去&#xff0c;这样副本分片只需要拷贝&#xff0c;节省了索引过 程。 PUT /my_temp_index/_settings { "number_of_replicas": 0 } 自动生成doc ID  通过Elast…

mysql的安装步骤

安装步骤1.下载软件包&#xff0c;安装依赖环境2.安装mysql服务器3.基础安装路径&#xff0c;创用户&#xff0c;与权限4.初始化5.添加环境变量&#xff0c;把服务放到开机自启里面6.启动mysql服务器7.获取初始密码并且修改一&#xff0c;下载软件&#xff08;yum&#xff0c;源…

Ant Design Vue notification自定义

<script setup> import { onMounted, h, ref, watch, getCurrentInstance } from vue; import { notification, Button } from ant-design-vue; onMounted(() > { const list [{id: 11,warnLevel: 严重,...},...]showMessage(list); }); function showMessage(){ for…

蓝桥杯算法之搜索章 - 2

大家好&#xff0c;接下来&#xff0c;我将带来对于搜索篇的新内容&#xff0c;这部分我将打算围绕DFS深度优先搜索去讲解。 温馨提示&#xff1a;由于这篇文章是接着上一篇文章的&#xff0c;如果新读者没有看过前一篇的话&#xff0c;推荐去看一下&#xff0c;不然有些地方可…

蓝桥杯----AT24C02

&#xff08;5-1&#xff09;、AT24C02掉电不丢失写入与读取AT24C02就是将数据写入E2PROM&#xff0c;保证写入数据掉电不丢失。考频低&#xff0c;一般不考&#xff0c;顶天考几个数据E2PROM&#xff0c;上电立马读取。AT24C02数据读取一定放在主程序最前面&#xff0c;否则会…

【物联网】基于树莓派的物联网开发【19】——树莓派搭建MQTT客户端及MQTTX使用

场景介绍 实现测试客户端与 MQTT 服务器的连接、订阅、取消订阅、收发消息等功能。 MQTT发布消息到代理服务器 安装paho-mqtt 使用pip工具安装paho-mqtt&#xff0c;输入以下指令即可&#xff1a; sudo pip install paho-mqtt安装 MQTT 客户端库 为了方便连接到 MQTT 服务器&am…

5G-A技术浪潮勾勒通信产业新局,微美全息加快以“5.5G+ AI”新势能深化场景应用

7月31日&#xff0c;国家互联网信息办公室发布《国家信息化发展报告》。《报告》中提出&#xff0c;新一代通信技术研发取得新成果&#xff0c;5G-A地空通信&#xff08;5G-ATG&#xff09;技术研发成功并完成测试验证。5G-A技术研发测试验证移动通信技术一般代际生命周期为10年…

SQLite Where 子句详解

SQLite Where 子句详解 SQLite 是一款轻量级的数据库管理系统,广泛应用于移动设备、嵌入式系统以及个人电脑。在 SQLite 中,WHERE 子句是 SQL 查询语句中不可或缺的一部分,它用于指定查询条件,从而筛选出满足特定条件的记录。本文将详细介绍 SQLite 中的 WHERE 子句,包括…

AI IDE+AI 辅助编程-生成的大纲-一般般

引言概述 AI IDE 和 AI 辅助编程的兴起及其对开发效率的影响提出核心问题&#xff1a;AI 工具能否真正帮助程序员减少加班&#xff08;告别 996&#xff09;&#xff1f;AI IDE 与 AI 辅助编程的定义与现状解释 AI IDE&#xff08;集成 AI 的开发环境&#xff09;和 AI 辅助编程…

ABP VNext + Dapr Workflows:轻量级分布式工作流

&#x1f680; ABP VNext Dapr Workflows&#xff1a;轻量级分布式工作流 &#x1f4da; 目录&#x1f680; ABP VNext Dapr Workflows&#xff1a;轻量级分布式工作流一、引言 ✨TL;DR &#x1f525;二、环境与依赖 &#x1f6e0;️三、系统架构与流程图 &#x1f3d7;️四、…

⭐ Unity 实现UI视差滚动效果(Parallax)鼠标控制、可拓展陀螺仪与脚本控制

✨ 效果如下在许多游戏、APP 或动效页面中&#xff0c;我们常见的一种视觉效果是 视差滚动&#xff08;Parallax Scrolling&#xff09;&#xff1a;前景、中景、背景在鼠标或设备移动时以不同速率轻微移动&#xff0c;从而营造出一种空间感和深度感。目前遇到这样一个需求 所以…

【05】VM二次开发——模块参数配置--带渲染/不带渲染(WinForm界面调用 模块参数配置)

文章目录1 Winform 窗口界面 &#xff08;带渲染的参数配置控件&#xff09;2 配置代码3 运行测试4 不带渲染的参数配置控件 对比4.1 添加控件4.2 代码及演示效果模块参数配置本教程介绍如何在VM二次开发中对模块参数进行配置 1 Winform 窗口界面 &#xff08;带渲染的参数配置…

Android 之 蓝牙通信(2.0 经典)

​​一、环境配置​​1. ​​添加依赖​​在 build.gradle 中添加库依赖&#xff1a;dependencies {implementation com.github.akexorcist:bluetoothspp:1.0.0 }2. ​​权限声明&#xff08;AndroidManifest.xml&#xff09;​<uses-permission android:name"androi…

使用 Scikit-LLM 进行零样本和少样本分类

使用 Scikit-LLM 进行零样本和少样本分类 使用 Scikit-LLM 进行零样本和少样本分类 在本文中&#xff0c;您将学习&#xff1a; Scikit-LLM如何将OpenAI的GPT等大型语言模型与Scikit-learn框架集成以进行文本分析。零样本和少样本分类之间的区别以及如何使用Scikit-LLM实现它…

android内存作假通杀补丁(4GB作假8GB)

可过如下app检测&#xff1a; 安兔兔、鲁大师、白眼、AIDA64、CPU X、CPU-Z、DevCheck、DeviceInfoHW lyw235yk235:~/Extend/lyw235/V/sprdroid1_v_4/sprdroid1_v$ git diff vnd/bsp/kernel5.15/kernel5.15/mm/page_alloc.c diff --git a/vnd/bsp/kernel5.15/kernel5.15/mm/pag…

Android 之 MVC架构

介绍1. MVC架构分工​​​​Model层​​&#xff1a;处理数据验证、网络请求等业务逻辑。​​View层​​&#xff1a;XML布局定义界面&#xff0c;Activity处理用户输入和显示结果。​​Controller层​​&#xff1a;Activity作为控制器&#xff0c;协调Model和View的交互对于登…

Centos Docker 安装手册(可用)

Centos 安装 Docker # 卸载旧版 yum remove docker \docker-client \docker-client-latest \docker-common \docker-latest \docker-latest-logrotate \docker-logrotate \docker-engine \docker-selinux # 安装依赖工具 yum install -y yum-utils device-mapper-persistent-d…