当前位置:   article > 正文

PostgreSQL排查及常见脚本_pg数据库脚本

pg数据库脚本

– 远程登陆PG数据库

-- 参数说明:-h 服务器 -p 端口 -U 用户 -d 数据库
psql -h 127.0.0.1 -U dbuser -p 5832 -d database
  • 1
  • 2

– 数据库当前连接

SELECT 
    pg_stat_get_backend_pid(s.backendid)        AS procpid,
    pg_stat_get_backend_activity(s.backendid)   AS current_query 
FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;

  • 1
  • 2
  • 3
  • 4
  • 5

– 数据库当前执行SQL


SELECT procpid,
       start,
       now() - start AS lap,
       current_query
FROM (
    SELECT backendid,
           pg_stat_get_backend_pid(S.backendid) AS procpid,
           pg_stat_get_backend_activity_start(S.backendid) AS start,
           pg_stat_get_backend_activity(S.backendid) AS current_query
    FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS S
) AS S
WHERE current_query <> ''
-- 可根据procpid查询
--  AND procpid = '67080'
ORDER BY lap DESC;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

– 数据库全部表和对应表信息

-- 全部表
select * from pg_tables;

-- 表字段/注释/类型
select a.attnum                                                                             AS serial_num,
       a.attname                                                                            AS field,
       concat_ws('',t.typname,SUBSTRING(format_type(a.atttypid,a.atttypmod) from '\(.*\)')) AS type,
       d.description                                                                        AS description
from pg_class c,pg_attribute a,pg_type t,pg_description d
where a.attnum>0
    AND a.attrelid=c.oid
    AND a.atttypid=t.oid
    AND d.objoid=a.attrelid
    AND d.objsubid=a.attnum
    -- 对应表名
    AND c.relname='table_name';

-- 或者
SELECT n.nspname                   AS schemaname,
       c.relname                   AS tablename,
       d.description               AS table_comment,
       pg_get_userbyid(c.relowner) AS tableowner,
       t.spcname                   AS tablespace,
       c.relhasindex               AS hasindexes,
       c.relhasrules               AS hasrules,
       c.relhastriggers            AS hastriggers,
       c.relrowsecurity            AS rowsecurity
FROM pg_class c
         LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
         LEFT JOIN pg_tablespace t ON t.oid = c.reltablespace
         LEFT JOIN pg_description d ON d.objoid = c.oid
WHERE c.relname = 'table_name';
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

后续更新…

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/981906
推荐阅读
相关标签
  

闽ICP备14008679号