PostgreSQL系统相关查询[1]

前端之家收集整理的这篇文章主要介绍了PostgreSQL系统相关查询[1]前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PG 系统查询脚本
--查询用户进程及文本
SELECT a.pid AS Id,a.usename AS User,a.client_addr AS Host,a.client_port AS Port,datname AS db,query AS Command,a.state AS Idle,l.mode as State,query_start AS Time,CASE WHEN c.relname IS NOT NULL THEN 'Locked Object: ' || c.relname ELSE 'Locked Transaction:' || l.virtualtransaction END AS Info 
FROM pg_stat_activity a 
LEFT JOIN pg_locks l ON a.pid = l.pid 
LEFT JOIN pg_class c ON l.relation = c.oid 
ORDER BY a.pid ASC,c.relname ASC

--查询表
[2015-03-03 17:38:35.668] [002232] [localhost] [PGsql]
SELECT tablename,schemaname FROM pg_tables WHERE schemaname='public' ORDER BY schemaname,tablename

--查询视图
[2015-03-03 17:38:35.731] [002232] [localhost] [PGsql]
SELECT viewname,schemaname FROM pg_views WHERE schemaname='public' ORDER BY schemaname,viewname

--查询所属视图及其定义
SELECT c.oid,c.relname,c.relacl,pg_get_userbyid(c.relowner) AS viewowner,pg_get_viewdef(c.oid) AS definition,obj_description(c.oid),n.nspname 
FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace 
WHERE c.relkind = 'v'::"char" AND n.nspname = 'public' 

--查询架构名下表、表注释及表行数
SELECT c.oid,c.relhasoids AS hasoids,n.nspname AS schemaname,c.relname AS tablename,pg_get_userbyid(c.relowner) AS tableowner,t.spcname AS "tablespace",c.relhasindex AS hasindexes,c.relhasrules AS hasrules,c.relhastriggers AS hastriggers,c.reltuples,((SELECT count(*) FROM pg_inherits WHERE inhparent = c.oid) > 0) AS inhtable,i2.relname AS inhtablename,c.reloptions AS param 
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_inherits i INNER JOIN pg_class c2 ON i.inhparent = c2.oid) i2 ON i2.inhrelid = c.oid 
WHERE (c.relkind = 'r'::"char") AND n.nspname = 'public'
order by reltuples

--查询数据库及所属用户和字符集
SELECT d.datname,d.oid,pg_get_userbyid(d.datdba) AS owner,shobj_description(d.oid,'pg_database') AS comment,t.spcname,d.datacl,d.datlastsysoid,d.encoding,pg_encoding_to_char(d.encoding) AS encodingname 
FROM pg_database d LEFT JOIN pg_tablespace t ON d.dattablespace=t.oid


--EOF

原文链接:https://www.f2er.com/postgresql/195346.html

猜你在找的Postgre SQL相关文章