1. 程式人生 > >PostgreSQL psql中如何檢視快捷功能的對應函式

PostgreSQL psql中如何檢視快捷功能的對應函式

 

在psql中,我們可以通過一系列的的快捷命令檢視資料庫元素,如:\d 檢視當前搜尋路徑下的表,那麼內部用到的SQL語句是什麼呢,可以通過命令來設定是否打印出來:

 

apple=# \set ECHO_HIDDEN on
apple=# \c mydb
You are now connected to database "mydb" as user "apple".
mydb=# \d
********* QUERY **********
SELECT n.nspname as "Schema",
  c.relname as "Name",
  CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' END as "Type",
  pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','v','m','S','f','')
      AND n.nspname <> 'pg_catalog'
      AND n.nspname <> 'information_schema'
      AND n.nspname !~ '^pg_toast'
  AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;
**************************

                  List of relations
 Schema |      Name       |     Type      |  Owner
--------+-----------------+---------------+----------
 public | employees       | table         | postgres
 public | indent          | table         | apple
 public | indent_id_seq   | sequence      | apple
 public | student         | table         | postgres
 public | table_new       | table         | postgres
 public | test1           | table         | apple
 public | test_cur        | table         | postgres
 public | test_from_apple | foreign table | apple
 public | test_id_seq     | sequence      | apple
 public | test_time       | table         | apple
 public | test_type       | table         | apple
(11 rows)

mydb=# \set ECHO_HIDDEN off
mydb=# \d
                  List of relations
 Schema |      Name       |     Type      |  Owner
--------+-----------------+---------------+----------
 public | employees       | table         | postgres
 public | indent          | table         | apple
 public | indent_id_seq   | sequence      | apple
 public | student         | table         | postgres
 public | table_new       | table         | postgres
 public | test1           | table         | apple
 public | test_cur        | table         | postgres
 public | test_from_apple | foreign table | apple
 public | test_id_seq     | sequence      | apple
 public | test_time       | table         | apple
 public | test_type       | table         | apple
(11 rows)