首页 > 数据库 >Postgresql Common Commands

Postgresql Common Commands

时间:2024-03-24 16:55:19浏览次数:23  
标签:Commands set Postgresql pg bytes Common query select size

PSQL 快捷命令

cat ~/.psqlrc

-- check active session
\set active_session 'select pid,usename,datname,application_name,client_addr,age(clock_timestamp(), query_start),query from pg_stat_activity where pid<>pg_backend_pid() and state=\'active\' order by query_start desc;'

-- all_reslove_session
\set session 'select pid,usename,datname,application_name,client_addr,age(clock_timestamp(), query_start),query from pg_stat_activity where pid<>pg_backend_pid() and state=\'idle\' and upper(query) not like \'SET%\' and upper(query) not like \'SHOW%\' and query != \'COMMIT\' order by query_start desc;'
-- check wait events
\set wait_event 'select pid,application_name,client_addr,age(clock_timestamp(),query_start),state,wait_event_type,wait_event from pg_stat_activity where pid<>pg_backend_pid() and wait_event is not null order by wait_event_type;'

-- table_size
\set table_size 'select table_name,pg_size_pretty(total_bytes) AS total, pg_size_pretty(index_bytes) AS idx , pg_size_pretty(toast_bytes) AS toast , pg_size_pretty(table_bytes) AS relsize from (select *, total_bytes-index_bytes-COALESCE(toast_bytes,0) AS table_bytes FROM ( SELECT c.oid,nspname AS table_schema, relname AS table_name , c.reltuples AS row_estimate , pg_total_relation_size(c.oid) AS total_bytes , pg_indexes_size(c.oid) AS index_bytes , pg_total_relation_size(reltoastrelid) AS toast_bytes FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace WHERE relkind = \'r\' and relname in (select tablename from pg_tables where schemaname=\'public\') ) a) a order by total_bytes desc;'

--database size
\set database_size 'SELECT d.datname AS Name,pg_catalog.pg_get_userbyid(d.datdba) AS Owner,CASE WHEN pg_catalog.has_database_privilege(d.datname, \'CONNECT\') THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))  ELSE \'No Access\'  END AS SIZE FROM pg_catalog.pg_database d ORDER BY CASE WHEN pg_catalog.has_database_privilege(d.datname, \'CONNECT\') THEN pg_catalog.pg_database_size(d.datname) ELSE NULL END DESC LIMIT 20;'

--table static
\set table_stat 'select relname,seq_scan,seq_tup_read,idx_scan,idx_tup_fetch,n_tup_ins,n_tup_upd,n_tup_del,n_tup_hot_upd,n_live_tup,n_dead_tup,n_mod_since_analyze,n_ins_since_vacuum,last_vacuum,last_analyze,vacuum_count from pg_stat_user_tables where relname in (\'sxacc-devices\',\'device_inform_time\',\'device_view\',\'group_devices_view\');'

--redshift running sqls
\set redshift_run 'SELECT pid,starttime,duration,trim(user_name) AS user,trim(query) AS querytxt FROM stv_recents WHERE STATUS = \'Running\' order by starttime desc;'
--redshift run2
\set redshift_run2 'select pid, trim(starttime) as start, to_char(sysdate,\'HH24:MI:SS\') as cur,duration, trim(user_name) as user,substring (query,1,40) as querytxt from stv_recents where status = \'Running\' order by starttime desc;'

\set redshift_disk 'select owner, host, diskno, used, capacity,(used-tossed)/capacity::numeric *100 as pctused from stv_partitions order by owner;'


--redshift disk usage
\set redshift_disk_usage 'select node, used,round(used/2097152.0*100,2) as pct from stv_node_storage_capacity order by node;'

--green running
\set gp_active_session 'select procpid,usename,datname,application_name,client_addr,age(clock_timestamp(), query_start),current_query from pg_stat_activity where current_query !=\'<IDLE>\' order by query_start desc;'

\set gp_run 'select procpid,application_name,client_addr,age(clock_timestamp(), query_start),SUBSTRING(current_query,0,100) from pg_stat_activity where current_query !=\'<IDLE>\' order by query_start desc;'

--redshfit_vacuum_status
\set redshift_vacuum 'select * from svv_vacuum_progress;'

 

标签:Commands,set,Postgresql,pg,bytes,Common,query,select,size
From: https://www.cnblogs.com/tben/p/18092641

相关文章

  • SpringBoot3集成PostgreSQL
    标签:PostgreSQL.Druid.Mybatis.Plus;一、简介PostgreSQL是一个功能强大的开源数据库系统,具有可靠性、稳定性、数据一致性等特点,且可以运行在所有主流操作系统上,包括Linux、Unix、Windows等。通过官方文档可以找到大量描述如何安装和使用PostgreSQL的信息。环境搭建,基于Centos......
  • 史上最全:PostgreSQL SQL的基础使用及技巧
    1、数据类型总体介绍referto:https://www.postgresql.org/docs/14/datatype.htmlNameAliasesDescriptionbigintint8signedeight-byteintegerbigserialserial8autoincrementingeight-byteintegerbit[(*n*)]fixed-lengthbitstringbitvary......
  • foxy rviz2 "rviz_common/Time"报错问题
    报错内容Theclassrequiredforthispanel,'rviz_common/Time',couldnotbeloaded.Error:Accordingtotheloadedplugindescriptionstheclassrviz_common/Timewithbaseclasstyperviz_common::Paneldoesnotexist.DeclaredtypesareTeleopPanel......
  • PostgreSQL中的CTE(公共表表达式)知多少? - (中级)
    与我联系:微信公众号:数据库杂记   个人微信: iiihero我是iihero. 也可以叫我Sean.iihero@CSDN(https://blog.csdn.net/iihero) Sean@墨天轮 (https://www.modb.pro/u/16258)数据库领域的资深爱好者一枚。水木早期数据库论坛发起人 db2@smth就是俺,早期多年水木......
  • PostgreSQL从小白到高手教程 - 第48讲:PG高可用实现keepalived
       PostgreSQL从小白到专家,是从入门逐渐能力提升的一个系列教程,内容包括对PG基础的认知、包括安装使用、包括角色权限、包括维护管理、、等内容,希望对热爱PG、学习PG的同学们有帮助,欢迎持续关注CUUGPG技术大讲堂。 第48讲:PG高可用实现 内容1:keepalived简介内容2:Kee......
  • 我的postgresql数据库报端口错误,连接失败
    原因是服务没有启动下面这个命令是启动方式pg_ctl.exerestart-D"D:\ProgramFiles\PostgreSQL\14\data"""引号内的内容是下载postgresql数据库时候的data路径。一般就在postgresql安装路径里面D:\ProgramFiles\PostgreSQL\14\bin>pg_ctl.exerestart-D"D:\ProgramFiles......
  • springboot下postgresql指定schema问题
    首先明确用的postgresql版本是PostgreSQL9.5.25,compiledbyVisualC++build1800,64-bitspringboot是2.3.5.RELEASE。现有的文档提供的连接数据库配置方式是url:jdbc:postgresql://IP:端口/数据库名?currentSchema=模式名&stringtype=unspecified通常postgresq......
  • 【PostgreSQL PGCE-091题目解析14】PostgreSQL中使用CONCURRENTLY选项创建索引时,允许
    本文为云贝教育刘峰(微信:yunbee_DBA)原创,请尊重知识产权,转发请注明出处,不接受任何抄袭、演绎和未经注明出处的转载。PostgreSQL中使用CONCURRENTLY选项创建索引时,允许增删改数据表。A.正确B.错误参考答案:A解析:我们知道,PG是有行级琐的,在创建索引的时候,会在行上加琐......
  • openGauss与postgresql日常使用差异
    openGauss与postgresql日常使用差异密码加密postgresql默认密码加密方式是md5。openGauss默认密码加密方式是sha256。使用navicate、pgadmin3等客户端开发工具访问og,需要修改加密方式。如果在本地用用户名密码登陆数据库没问题。但是用其他工具连接数据......
  • PostgreSQL与openGauss之分区性能
    PostgreSQL与openGauss之分区性能概述PostgreSQL与openGauss分区表定义差异,请参考https://www.modb.pro/db/41393。openGauss1.1.0开始支持hash/list分区,hash分区表最多支持64个分区,否则会报:ERROR:Un-supportfeatureDETAIL:Thepartition’slengthshouldbe......