1.查看表列表

  1. SELECT
  2. c.oid,
  3. obj_description (c.oid),
  4. c.relhasoids AS hasoids,
  5. n.nspname AS schemaname,
  6. c.relname AS tablename,
  7. c.relkind,
  8. pg_get_userbyid (c.relowner) AS tableowner,
  9. t.spcname AS "tablespace",
  10. c.relhasindex AS hasindexes,
  11. c.relhasrules AS hasrules,
  12. c.relhastriggers AS hastriggers,
  13. ft.ftoptions,
  14. fs.srvname,
  15. c.relacl,
  16. c.reltuples,
  17. (
  18. (
  19. SELECT
  20. count (*)
  21. FROM
  22. pg_inherits
  23. WHERE
  24. inhparent = c.oid
  25. ) > 0
  26. ) AS inhtable,
  27. i2.nspname AS inhschemaname,
  28. i2.relname AS inhtablename,
  29. c.reloptions AS param,
  30. c.relpersistence AS unlogged
  31. FROM
  32. pg_class c
  33. LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
  34. LEFT JOIN pg_tablespace t ON t.oid = c.reltablespace
  35. LEFT JOIN (
  36. pg_inherits i
  37. INNER JOIN pg_class c2 ON i.inhparent = c2.oid
  38. LEFT JOIN pg_namespace n2 ON n2.oid = c2.relnamespace
  39. ) i2 ON i2.inhrelid = c.oid
  40. LEFT JOIN pg_foreign_table ft ON ft.ftrelid = c.oid
  41. LEFT JOIN pg_foreign_server fs ON ft.ftserver = fs.oid
  42. WHERE
  43. (
  44. (c.relkind = 'r' :: "char")
  45. OR (c.relkind = 'f' :: "char")
  46. )
  47. AND n.nspname = 'public';

2.查看表列属性定义

  1. SELECT
  2. A.attnum,
  3. A.attname AS field,
  4. T.typname AS TYPE,
  5. A.attlen AS LENGTH,
  6. A.atttypmod AS lengthvar,
  7. A.attnotnull AS NOTNULL,
  8. b.description AS COMMENT
  9. FROM
  10. pg_class C,
  11. pg_attribute A
  12. LEFT OUTER JOIN pg_description b ON A.attrelid = b.objoid
  13. AND A.attnum = b.objsubid,
  14. pg_type T
  15. WHERE
  16. C.relname = 't_sku'
  17. AND A.attnum > 0
  18. AND A.attrelid = C.oid
  19. AND A.atttypid = T.oid
  20. ORDER BY
  21. A.attnum;

3.pg 正在执行的sql

  1. SELECT
  2. pid,
  3. datname,
  4. usename,
  5. client_addr,
  6. application_name,
  7. STATE,
  8. backend_start,
  9. xact_start,
  10. xact_stay,
  11. query_start,
  12. query_stay,
  13. REPLACE ( query, chr( 10 ), ' ' ) AS query
  14. FROM
  15. (
  16. SELECT
  17. pgsa.pid AS pid,
  18. pgsa.datname AS datname,
  19. pgsa.usename AS usename,
  20. pgsa.client_addr client_addr,
  21. pgsa.application_name AS application_name,
  22. pgsa.STATE AS STATE,
  23. pgsa.backend_start AS backend_start,
  24. pgsa.xact_start AS xact_start,
  25. EXTRACT ( epoch FROM ( now( ) - pgsa.xact_start ) ) AS xact_stay,
  26. pgsa.query_start AS query_start,
  27. EXTRACT ( epoch FROM ( now( ) - pgsa.query_start ) ) AS query_stay,
  28. pgsa.query AS query
  29. FROM
  30. pg_stat_activity AS pgsa
  31. WHERE
  32. pgsa.STATE != 'idle'
  33. AND pgsa.STATE != 'idle in transaction'
  34. AND pgsa.STATE != 'idle in transaction (aborted)'
  35. ) idleconnections
  36. ORDER BY
  37. query_stay DESC

4.pg中事务信息

  1. SELECT
  2. procpid,
  3. START,
  4. now() - START AS lap,
  5. current_query
  6. FROM
  7. (
  8. SELECT
  9. backendid,
  10. pg_stat_get_backend_pid (S.backendid) AS procpid,
  11. pg_stat_get_backend_activity_start (S.backendid) AS START,
  12. pg_stat_get_backend_activity (S.backendid) AS current_query
  13. FROM
  14. (
  15. SELECT
  16. pg_stat_get_backend_idset () AS backendid
  17. ) AS S
  18. ) AS S
  19. WHERE
  20. current_query <> '<IDLE>'
  21. ORDER BY
  22. lap DESC;