首页 > 其他分享 >MogDB openGauss常用查询汇总

MogDB openGauss常用查询汇总

时间:2024-03-14 12:12:47浏览次数:19  
标签:MogDB granted 汇总 start pg query openGauss where select

MogDB/openGauss 常用查询汇总

概述

在 MogDB/openGauss 日常运维过程中,会经常通过 SQL 来获取想要查看的信息,这些 SQL 可以作为监控指标、巡检指标,也可以临时查询使用。

通过系统线程 id 查对应的 query

#!/bin/bash

source ~/.bashrc

thread_sets=`ps -ef |grep -i gaussdb |grep -v grep|awk -F ' ' '{print $2}'|xargs top -n 1 -bHp |grep -i ' worker'|awk -F ' ' '{print $1}'|tr "\n" ","|sed -e 's/,$/\n/'`

gsql -p 26000 postgres -c "select  pid,lwtid,state,query from pg_stat_activity a,dbe_perf.thread_wait_status s where a.pid=s.tid and lwtid in($thread_sets);"

查看复制槽

select slot_name,coalesce(plugin,'_') as plugin,slot_type,datoid,coalesce(database,'_') as database,
       (case active when 't' then 1 else 0 end)as active,coalesce(xmin,'_') as xmin,dummy_standby,
       pg_xlog_location_diff(CASE WHEN pg_is_in_recovery() THEN restart_lsn ELSE pg_current_xlog_location() END , restart_lsn)  AS delay_lsn
from pg_replication_slots;

查看主备延迟

--主库
select client_addr,sync_state,pg_xlog_location_diff(pg_current_xlog_location(),receiver_replay_location) from pg_stat_replication;

--备库
select now() AS now,
       coalesce(pg_last_xact_replay_timestamp(), now()) replay,
       extract(EPOCH FROM (now() - coalesce(pg_last_xact_replay_timestamp(), now()))) AS diff;

慢 SQL 查询

select datname,usename,client_addr,pid,query_start::text,extract(epoch from (now() - query_start)) as query_runtime,xact_start::text,extract(epoch from(now() - xact_start)) as xact_runtime,state,query
from pg_stat_activity
where state not in('idle') and query_start is not null;

锁阻塞详情

with tl as (select usename,granted,locktag,query_start,query
            from pg_locks l,pg_stat_activity a
            where l.pid=a.pid and locktag in(select locktag from pg_locks where granted='f'))
select ts.usename locker_user,ts.query_start locker_query_start,ts.granted locker_granted,ts.query locker_query,tt.query locked_query,tt.query_start locked_query_start,tt.granted locked_granted,tt.usename locked_user,extract(epoch from now() - tt.query_start) as locked_times
from (select * from tl where granted='t') as ts,(select * from tl where granted='f') tt
where ts.locktag=tt.locktag
order by 1;

锁阻塞源统计

with tl as (select usename,granted,locktag,query_start,query
            from pg_locks l,pg_stat_activity a
            where l.pid=a.pid and locktag in(select locktag from pg_locks where granted='f'))
select usename,query_start,granted,query,count(query) count
from tl
where granted='t'
group by usename,query_start,granted,query
order by 5 desc;

数据表大小排序

SELECT CURRENT_CATALOG AS datname,nsp.nspname,rel.relname,
             pg_total_relation_size(rel.oid)       AS bytes,
             pg_relation_size(rel.oid)             AS relsize,
             pg_indexes_size(rel.oid)              AS indexsize,
             pg_total_relation_size(reltoastrelid) AS toastsize
FROM pg_namespace nsp JOIN pg_class rel ON nsp.oid = rel.relnamespace
WHERE nspname NOT IN ('pg_catalog', 'information_schema','snapshot') AND rel.relkind = 'r'
order by 4 desc limit 100;

索引大小排序

select CURRENT_CATALOG AS datname,schemaname schema_name,relname table_name,indexrelname index_name,pg_table_size(indexrelid) as index_size
from pg_stat_user_indexes
where schemaname not in('pg_catalog', 'information_schema','snapshot')
order by 4 desc limit 100;

表膨胀率排序

select CURRENT_CATALOG AS datname,schemaname,relname,n_live_tup,n_dead_tup,round((n_dead_tup::numeric/(case (n_dead_tup+n_live_tup) when 0 then 1 else (n_dead_tup+n_live_tup) end ) *100),2) as dead_rate
from pg_stat_user_tables
where (n_live_tup + n_dead_tup) > 10000
order by 5 desc limit 100;

session 按状态分类所占用内存大小

select state,sum(totalsize)::bigint as totalsize from gs_session_memory_detail m,pg_stat_activity a  where substring_inner(sessid,position('.' in sessid) +1)=a.sessionid and usename<>'mondb' and pid != pg_backend_pid()  group by state order by sum(totalsize) desc;

查看 session 中 query 占用内存大小

select state,sum(totalsize)::bigint as totalsize
from gs_session_memory_detail m,pg_stat_activity a
where substring_inner(sessid,position('.' in sessid) +1)=a.sessionid and usename<>'mondb' and pid != pg_backend_pid()
group by state order by sum(totalsize) desc;

标签:MogDB,granted,汇总,start,pg,query,openGauss,where,select
From: https://www.cnblogs.com/renxyz/p/18072557

相关文章

  • MogDB openGauss故障排查流程
    MogDB/openGauss故障排查流程前提如果有反馈说数据库响应慢或者压测过程中数据库有报错,第一步先收集数据库服务器资源使用情况,这一步是处理所有故障的前提。--负载top命令htop命令--cpulscpu命令--内存大小free-g--磁盘大小df-Th--磁盘使用跟踪nohupiostat......
  • MogDB openGauss数据库扩缩容的几种方式
    MogDB/openGauss数据库扩缩容的几种方式文本出处:https://www.modb.pro/db/453105随着业务的发展,业务系统对数据库的架构要求也在变化,比如需要读负载均衡、机房搬迁、服务器硬件替换等等,这需要在原数据库主备架构的基础上进行扩/缩容操作,目前MogDB数据库安装方式有三种,分别是......
  • openGauss分区表
    openGauss分区表概述openGauss是基于PostgreSQL9.2.4的内核开发的,在PostgreSQL10之前要达到实现分区表的效果可以有两种方式,一种是使用继承的触发器函数来实现,一种是安装pg_pathman的插件来实现,直到PostgreSQL10才引入了partition的语法;而opengauss从开源发布就可......
  • openGauss备库wal-replay与query冲突
    openGauss备库walreplay与query冲突概述openGauss的物理流复制逻辑继承了PostgreSQL,当一条数据从主库做变更到可以在备库查询到最新的值,在PostgreSQL备库分为三个阶段,分别是写入备库操作系统(remote_write),将缓存中的数据刷入到磁盘(on==flush),从磁盘将数据库回放(remot......
  • openGauss SQL引擎插件开发指导
    开发流程①在openGauss社区Plugin仓进行兼容性相关开发(https://gitee.com/opengauss/Plugin)②通过fastcheck自测以及CI门禁③提供checkin测试报告和开发文档并通过SIG组评审开发要点开放接口函数DLL_PUBLICPG_FUNCTION_INFO_V1_PUBLIC统一管理为了避免......
  • openGauss SQL引擎插件开发指导
    开发流程①在openGauss社区Plugin仓进行兼容性相关开发(https://gitee.com/opengauss/Plugin)②通过fastcheck自测以及CI门禁③提供checkin测试报告和开发文档并通过SIG组评审开发要点开放接口函数DLL_PUBLICPG_FUNCTION_INFO_V1_PUBLIC统一管理为了避免......
  • openGauss账本数据库,你不知道的那些事儿
    openGauss账本数据库,你不知道的那些事儿摘要本文将通过对比官方文档关于“设置账本数据库”中的几个章节,结合源码来说说文档中操作步骤背后的原理。账本数据库概述你知道的那些事儿官方文档账本数据库融合了区块链思想,将用户操作记录至两种历史表中:用户历史表和全局区块表......
  • 【DL经典回顾】激活函数大汇总列表(持续更新中...)
    激活函数大汇总列表(持续更新中…)欢迎来到我们深入探索神经网络核心组成部分——激活函数的系列博客。在人工智能的世界里,激活函数扮演着不可或缺的角色,它们决定着神经元的输出,并且影响着网络的学习能力与表现力。鉴于激活函数的重要性和多样性,我们将通过一系列博客详细介绍......
  • 卸载神器小汇总
    BulkCrapUninstaller......
  • 西北师范大学956软件工程(张海藩)名词解释、简答题、画图题汇总(部分)
    文章目录一:名词解释总结(1)传统软件工程部分A:非常重要B:可以了解(2)面向对象设计部分二:简答题总结(1)传统软件工程部分A:非常重要B:可以了解(2)面向对象设计部分A:非常重要B:可以了解三:画图题总结(1)概要(各章图形及对应符号)A:传统软件工程部分B:面向对象部分(2)着重考察画法的图完......