zoukankan      html  css  js  c++  java
  • postgresql常用SQL

    1. --查看数据库
    2. select * from pg_database;
    3. --查看表空间
    4. select * from pg_tablespace;
    5. --查看语言
    6. select * from pg_language;
    7. --查看角色用户
    8. select * from pg_user;
    9. select * from pg_shadow;
    10. select * from pg_roles;
    11. --查看会话进程
    12. select * from pg_stat_activity;
    13. --查看表
    14. SELECT * FROM pg_tables where schemaname = 'public';
    15. --查看表字段
    16. select * from information_schema.columns where table_schema = 'public' and table_name = 'pf_vip_org';
    17. --查看视图
    18. select * from pg_views where schemaname = 'public';
    19. select * from information_schema.views where table_schema = 'public';
    20. --查看触发器
    21. select * from information_schema.triggers;
    22. --查看序列
    23. select * from information_schema.sequences where sequence_schema = 'public';
    24. --查看约束
    25. select * from pg_constraint where contype = 'p' 
    26. --u unique,p primary,f foreign,c check,t trigger,x exclusion
    27. 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';
    28. --查看索引
    29. select * from pg_index ;
    30. --查看表上存在哪些索引以及大小
    31. select relname,n.amname as index_type from pg_class m,pg_am n where m.relam = n.oid and m.oid in (
    32. select b.indexrelid from pg_class a,pg_index b where a.oid = b.indrelid and a.relname = 'cc');
    33. SELECT c.relname,c2.relname, c2.relpages*8 as size_kb
    34. FROM pg_class c, pg_class c2, pg_index i
    35. WHERE c.relname = 'cc' AND
    36. c.oid = i.indrelid AND
    37. c2.oid = i.indexrelid
    38. ORDER BY c2.relname;
    39. --查看索引定义
    40. select b.indexrelid from pg_class a,pg_index b where a.oid = b.indrelid and a.relname = 'cc';
    41. select pg_get_indexdef(b.indexrelid);
    42. --查看过程函数定义
    43. select oid,* from pg_proc where proname = 'insert_platform_action_exist'; --oid = 24610
    44. select * from pg_get_functiondef(24610);
    45. --查看表大小(不含索引等信息)
    46. select pg_relation_size('cc');                         --368640 byte
    47. select pg_size_pretty(pg_relation_size('cc'))   --360 kB
    48. --查看DB大小
    49. select pg_size_pretty(pg_database_size('smiletao'));   --12M
    50. --查看服务器DB运行状态
    51. [postgres@eyar ~]$ pg_ctl status -D $PGDATA
    52. pg_ctl: server is running (PID: 2373)
    53. /home/postgres/bin/postgres "-D" "/database/pgdata"
    54. --查看每个DB的使用情况(读,写,缓存,更新,事务等)
    55. select * from pg_stat_database
    56. --查看索引的使用情况
    57. select * from pg_stat_user_indexes;
    58. --查看表所对应的数据文件路径与大小
    59. SELECT pg_relation_filepath(oid), relpages FROM pg_class WHERE relname = 'empsalary';
    60. --查看索引与相关字段及大小
    61. SELECT n.nspname AS schema_name,
    62.         r.rolname as table_owner,
    63.        bc.relname AS table_name,
    64.        ic.relname AS index_name,
    65.        a.attname  AS column_name,
    66.        bc.relpages*8 as index_size_kb    
    67.   FROM pg_namespace n,
    68.        pg_class bc,             -- base class
    69.        pg_class ic,             -- index class
    70.        pg_index i,
    71.        pg_attribute a,           -- att in base
    72.        pg_roles r
    73.   WHERE bc.relnamespace = n.oid
    74.      and i.indrelid = bc.oid
    75.      and i.indexrelid = ic.oid
    76.      and bc.relowner = r.oid
    77.      and i.indkey[0] = a.attnum
    78.      and i.indnatts = 1
    79.      and a.attrelid = bc.oid
    80.      and n.nspname = 'public'
    81.      and bc.relname = 'cc'
    82.   ORDER BY schema_name, table_name, index_name, attname;
    83. --查看PG锁
    84. select * from pg_locks;
    85. 备注:relpages*8 是实际所占磁盘大小
    86. --查看表空间大小
    87. select pg_tablespace_size('pg_default');
    88. --查看序列与表的对应关系
    89.   WITH fq_objects AS (SELECT c.oid,c.relname AS fqname ,
    90.                            c.relkind, c.relname AS relation
    91.                     FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace ),
    92.      sequences AS (SELECT oid,fqname FROM fq_objects WHERE relkind = 'S'),
    93.      tables    AS (SELECT oid, fqname FROM fq_objects WHERE relkind = 'r' )
    94.          SELECT
    95.        s.fqname AS sequence,
    96.        '->' as depends,
    97.        t.fqname AS table
    98.       FROM
    99.        pg_depend d JOIN sequences s ON s.oid = d.objid
    100.                  JOIN tables t ON t.oid = d.refobjid
    101.           WHERE
    102.        d.deptype = 'a' and t.fqname = 'cc';
  • 相关阅读:
    IEEE 754 浮点数的表示方法
    .NET Core 3.0及以上的EFCore连接MySql
    一些常见错误/技巧/结论总结
    2-sat学习笔记
    动态DP学习笔记
    动态规划优化算法——wqs二分 and 折线优化
    扩展莫队小总结(二) (回滚莫队/二次离线莫队)
    CF1504X Codeforces Round #712
    CF1500D Tiles for Bathroom (递推+大讨论)
    CF1486X Codeforces Round #703
  • 原文地址:https://www.cnblogs.com/velion-oracle/p/5029194.html
Copyright © 2011-2022 走看看