PostgreSQL 常用命令【转】

 

原文:http://www.open-open.com/lib/view/open1351266245207.html

  1. --查看数据库  
  2. select * from pg_database;  
  3.   
  4. --查看表空间  
  5. select * from pg_tablespace;  
  6.   
  7. --查看语言  
  8. select * from pg_language;  
  9.   
  10. --查看角色用户  
  11. select * from pg_user;  
  12. select * from pg_shadow;  
  13. select * from pg_roles;  
  14.   
  15. --查看会话进程  
  16. select * from pg_stat_activity;  
  17.   
  18. --查看表  
  19. SELECT * FROM pg_tables where schemaname = 'public';  
  20.   
  21. --查看表字段  
  22. select * from information_schema.columns where table_schema = 'public' and table_name = 'pf_vip_org';  
  23.   
  24. --查看视图  
  25. select * from pg_views where schemaname = 'public';  
  26. select * from information_schema.views where table_schema = 'public';  
  27.   
  28. --查看触发器  
  29. select * from information_schema.triggers;  
  30.   
  31. --查看序列  
  32. select * from information_schema.sequences where sequence_schema = 'public';  
  33.   
  34.  --查看约束  
  35. select * from pg_constraint where contype = 'p'    
  36. --u unique,p primary,f foreign,c check,t trigger,x exclusion  
  37.   
  38. select a.relname as table_name,b.conname as constraint_name,b.contype as constraint_type from pg_class a,pg_constraint b where a.oid = b.conrelid and a.relname = 'cc';  
  39.   
  40. --查看索引  
  41. select * from pg_index ;  
  42.   
  43. --查看表上存在哪些索引以及大小  
  44. select relname,n.amname as index_type from pg_class m,pg_am n where m.relam = n.oid and m.oid in (  
  45. select b.indexrelid from pg_class a,pg_index b where a.oid = b.indrelid and a.relname = 'cc');  
  46.   
  47. SELECT c.relname,c2.relname, c2.relpages*8 as size_kb  
  48. FROM pg_class c, pg_class c2, pg_index i  
  49. WHERE c.relname = 'cc' AND  
  50. c.oid = i.indrelid AND  
  51. c2.oid = i.indexrelid  
  52. ORDER BY c2.relname;   
  53.   
  54. --查看索引定义  
  55. select b.indexrelid from pg_class a,pg_index b where a.oid = b.indrelid and a.relname = 'cc';  
  56. select pg_get_indexdef(b.indexrelid);  
  57.   
  58. --查看过程函数定义  
  59. select oid,* from pg_proc where proname = 'insert_platform_action_exist'--oid = 24610  
  60. select * from pg_get_functiondef(24610);  
  61.   
  62. --查看表大小(不含索引等信息)  
  63. select pg_relation_size('cc');                         --368640 byte  
  64. select pg_size_pretty(pg_relation_size('cc'))   --360 kB  
  65.   
  66. --查看DB大小  
  67. select pg_size_pretty(pg_database_size('smiletao'));   --12M  
  68.   
  69. --查看服务器DB运行状态  
  70. [postgres@eyar ~]$ pg_ctl status -D $PGDATA  
  71. pg_ctl: server is running (PID: 2373)  
  72. /home/postgres/bin/postgres "-D" "/database/pgdata"   
  73.   
  74. --查看每个DB的使用情况(读,写,缓存,更新,事务等)  
  75. select * from pg_stat_database  
  76.   
  77. --查看索引的使用情况  
  78. select * from pg_stat_user_indexes;  
  79.   
  80. --查看表所对应的数据文件路径与大小  
  81. SELECT pg_relation_filepath(oid), relpages FROM pg_class WHERE relname = 'empsalary';  
  82.   
  83. --查看索引与相关字段及大小  
  84.  SELECT n.nspname AS schema_name,  
  85.         r.rolname as table_owner,  
  86.        bc.relname AS table_name,  
  87.        ic.relname AS index_name,  
  88.        a.attname  AS column_name,  
  89.        bc.relpages*8 as index_size_kb       
  90.   FROM pg_namespace n,  
  91.        pg_class bc,             -- base class  
  92.        pg_class ic,             -- index class  
  93.        pg_index i,  
  94.        pg_attribute a,           -- att in base  
  95.        pg_roles r  
  96.   WHERE bc.relnamespace = n.oid  
  97.      and i.indrelid = bc.oid  
  98.      and i.indexrelid = ic.oid  
  99.      and bc.relowner = r.oid  
  100.      and i.indkey[0] = a.attnum  
  101.      and i.indnatts = 1  
  102.      and a.attrelid = bc.oid  
  103.      and n.nspname = 'public'  
  104.      and bc.relname = 'cc'  
  105.   ORDER BY schema_name, table_name, index_name, attname;  
  106.   
  107. --查看PG锁  
  108. select * from pg_locks;  
  109.   
  110. 备注:relpages*8 是实际所占磁盘大小  
  111.   
  112. --查看表空间大小  
  113. select pg_tablespace_size('pg_default');  
  114.   
  115. --查看序列与表的对应关系  
  116.   WITH fq_objects AS (SELECT c.oid,c.relname AS fqname ,  
  117.                            c.relkind, c.relname AS relation  
  118.                     FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace ),  
  119.   
  120.      sequences AS (SELECT oid,fqname FROM fq_objects WHERE relkind = 'S'),   
  121.      tables    AS (SELECT oid, fqname FROM fq_objects WHERE relkind = 'r' )   
  122.          SELECT  
  123.        s.fqname AS sequence,  
  124.        '->' as depends,  
  125.        t.fqname AS table  
  126.       FROM  
  127.        pg_depend d JOIN sequences s ON s.oid = d.objid   
  128.                  JOIN tables t ON t.oid = d.refobjid   
  129.           WHERE  
  130.        d.deptype = 'a' and t.fqname = 'cc';
posted @ 2018-02-01 16:23  小天儿  阅读(158)  评论(0)    收藏  举报