偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

PostgreSQL里面的一些命令小結(jié)

數(shù)據(jù)庫(kù) 其他數(shù)據(jù)庫(kù) PostgreSQL
近兩天總結(jié)了下PostgreSQL的基本操作命令,對(duì)PostgreSQL也有了一個(gè)基本的認(rèn)識(shí)。PostgreSQL的功能還是很豐富的,有序列,支持db link,基本Oracle里有的概念它這里也有,目前來看不支持package。

[[223726]]

近兩天總結(jié)了下PostgreSQL的基本操作命令,對(duì)PostgreSQL也有了一個(gè)基本的認(rèn)識(shí)。

PostgreSQL的功能還是很豐富的,有序列,支持db link,基本Oracle里有的概念它這里也有,目前來看不支持package。

風(fēng)格和Oracle也類似,沒有MySQL中快捷方便的show create table 這樣的語(yǔ)句。

從我的使用習(xí)慣來說,我基本關(guān)注以下的一些方面。

  1. 查看數(shù)據(jù)庫(kù)的配置

  2. 查看用戶信息

  3. 查看會(huì)話連接信息

  4. show tables的類似方法

  5. 用戶的權(quán)限查看

  6. 建表語(yǔ)句

  7. 表空間信息

  8. 對(duì)象存儲(chǔ)信息

  9. 查看鎖的信息

  10. 查看數(shù)據(jù)庫(kù)參數(shù)

  11. 顯示數(shù)據(jù)庫(kù)的運(yùn)行狀態(tài)

  12. 查看數(shù)據(jù)字典的信息

  13. 查看索引的信息

  14. 查看執(zhí)行計(jì)劃

  15. 查看存儲(chǔ)過程

  16. 存儲(chǔ)過程的調(diào)度執(zhí)行

  17. 事務(wù)隔離級(jí)別 

1.查看數(shù)據(jù)庫(kù)的配置

可以直接使用\l 選項(xiàng)列出所有的數(shù)據(jù)庫(kù)來,字符集,基本的配置都一目了然,有點(diǎn)Oracle 12c中的show pdbs的感覺。

從進(jìn)程情況來看,PG是多進(jìn)程多線程的架構(gòu)設(shè)計(jì)。

如果查看當(dāng)前數(shù)據(jù)庫(kù),可以使用current_database()。 

  1. postgres=# select current_database();  
  2.  current_database   
  3. ------------------  
  4.  postgres  

2.查看用戶信息

可以使用\dn來得到schema的相關(guān)信息,在PG里面的schema和user還是有一些差別,在其他數(shù)據(jù)庫(kù)schema基本就是user了。 

  1. postgres-# \dn  
  2.   List of schemas  
  3.   Name  |  Owner    
  4. --------+----------  
  5.  public | postgres  

我們創(chuàng)建一個(gè)schema,然后使用\dn來查看。 

  1. postgres=# create schema jeanron100;  
  2. CREATE SCHEMA  
  3. postgres=# \dn  
  4.     List of schemas  
  5.     Name    |  Owner   
  6. ------------+----------  
  7.  jeanron100 | postgres  
  8.  public     | postgres  

或者使用數(shù)據(jù)字典pg_authid來查看。 

  1. postgres=# select *from pg_authid; 

關(guān)于schema的概念,我們可以創(chuàng)建一個(gè)表test, 

  1. postgres=# create table test(id int);  
  2. CREATE TABLE  

可以看到這個(gè)是一個(gè)public的schema

 

  1. postgres=# \d  
  2.         List of relations  
  3.  Schema | Name | Type  |  Owner     
  4. --------+------+-------+----------  
  5.  public | test | table | postgres 

 

如果使用\d來查看字段信息,結(jié)果如下:

  1. postgres=# \d test  
  2.      Table "public.test"  
  3.  Column |  Type   | Modifiers   
  4. --------+---------+-----------  
  5.  id     | integer |  

 

還可以使用pg_users來查看,比如我創(chuàng)建了一個(gè)用戶replica,就會(huì)有相應(yīng)的配置。

 

  1. select *from pg_user;  
  2.  usename  | usesysid | usecreatedb | usesuper | userepl | usebypassrls |  passwd  | valuntil | useconfig   
  3. ----------+----------+-------------+----------+---------+--------------+----------+----------  
  4.  postgres |       10 | t           | t        | t       | t            | ******** |          |   
  5.  replica  |    16384 | f           | f        | t       | f            | ******** |          |  

查看當(dāng)前的schema信息,可以使用current_schema()

3.查看會(huì)話連接信息

如果查看PG中的會(huì)話信息,可以使用select * from pg_stat_activity;

如果新增了一個(gè)連接,開啟了一個(gè)會(huì)話,在服務(wù)端是會(huì)有一個(gè)影子進(jìn)程存在的。可以根據(jù)pid找到對(duì)應(yīng)的會(huì)話。包括執(zhí)行的SQL都可以看到,如果有多個(gè)會(huì)話,就是多條記錄。

 

  1. postgres=# select * from pg_stat_activity;  
  2. -[ RECORD 1 ]----+--------------------------------  
  3. datid            | 13241  
  4. datname          | postgres  
  5. pid              | 20644  
  6. usesysid         | 10 
  7. usename          | postgres  
  8. application_name | psql  
  9. client_addr      |   
  10. client_hostname  |   
  11. client_port      | -1  
  12. backend_start    | 2018-03-25 05:38:16.988057+08  
  13. xact_start       | 2018-03-25 05:48:08.113649+08  
  14. query_start      | 2018-03-25 05:48:08.113649+08  
  15. state_change     | 2018-03-25 05:48:08.113653+08  
  16. waiting          | f  
  17. state            | active  
  18. backend_xid      |   
  19. backend_xmin     | 1753  
  20. query            | select * from pg_stat_activity; 

 

4.show tables的類似方法

PG里面暫時(shí)沒有找到show tables這種的快捷方式,目前發(fā)現(xiàn)有兩類方式。

一種是通過數(shù)據(jù)字典pg_tables來查看,相當(dāng)于Oracle里面的all_tables

或者是使用information_schema里面的tables來查看。

 

  1. postgres=# select *from information_schema.tables;  
  2. postgres=# select *from pg_tables; 

 

PG里面的information_schema比較特別,在數(shù)據(jù)庫(kù)中直接\l無法看到,但是確確實(shí)實(shí)存在,著數(shù)據(jù)字典風(fēng)格和MySQL很相似。

5.用戶的權(quán)限查看

查看權(quán)限可以使用\dp來完成,或者等價(jià)的命令\z來實(shí)現(xiàn)。

 

  1. postgres=# \dp  
  2.                             Access privileges  
  3.  Schema | Name | Type  | Access privileges | Column privileges | Policies   
  4. --------+------+-------+-------------------+-------------------+----------  
  5.  public | test | table |                   |                   |   

6.建表語(yǔ)句

建表語(yǔ)句,目前還沒有發(fā)現(xiàn)show create table這種快捷的方式,不過可以通過pg_dump或者根據(jù)數(shù)據(jù)字典的信息來拼接了。 

7.表空間信息

表空間的部分相對(duì)比較清晰,可以直接使用\db來完成。

 

  1. postgres=# \db  
  2.        List of tablespaces  
  3.     Name    |  Owner   | Location   
  4. ------------+----------+----------  
  5.  pg_default | postgres |   
  6.  pg_global  | postgres |  

或者使用pg_tablespace

 

  1. postgres=# select *from pg_tablespace;    
  2.   spcname   | spcowner | spcacl | spcoptions   
  3. ------------+----------+--------+------------  
  4.  pg_default |       10 |        |   
  5.  pg_global  |       10 |        |   

8.對(duì)象存儲(chǔ)信息

這部分信息可以參考pg_tables,還有一些更細(xì)節(jié)的

更多的細(xì)節(jié)還有待求證和發(fā)現(xiàn)。

9.查看鎖的信息

查看鎖的信息可以使用pg_locks來得到。

 

  1. postgres=# select *from pg_locks;  
  2. -[ RECORD 1 ]------+----------------  
  3. locktype           | relation  
  4. database           | 13241  
  5. relation           | 11673  
  6. page               |  
  7. tuple              |   
  8. virtualxid         |   
  9. transactionid      |   
  10. classid            |   
  11. objid              |   
  12. objsubid           |   
  13. virtualtransaction | 4/81  
  14. pid                | 20644  
  15. mode               | AccessShareLock  
  16. granted            | t  
  17. fastpath           | t 

 

10.查看數(shù)據(jù)庫(kù)參數(shù)

這部分的功能不是很理解,因?yàn)闆]有找到很便捷的方式。

比如查看緩存的設(shè)置

 

  1. postgres=#  show shared_buffers;  
  2. -[ RECORD 1 ]--+------  
  3. shared_buffers | 128MB 

 

或者根據(jù)參數(shù)文件postgresql.conf來查看。

11.顯示數(shù)據(jù)庫(kù)的運(yùn)行狀態(tài)

這個(gè)信息毫無疑問,建議還是從pg_stats_activity來查看。 

12.查看數(shù)據(jù)字典的信息

這應(yīng)該是本小節(jié)的重點(diǎn),通過查看視圖可以看到,有100多個(gè)視圖。

 

  1. postgres=# select count(*)from pg_views;  
  2. -[ RECORD 1 ]  
  3. count | 112 

 

還可以使用information_schema中的信息來補(bǔ)充。 

13.查看索引的信息

查看索引的信息,可以使用\di來完成,非??旖荨?nbsp;

14.查看執(zhí)行計(jì)劃

查看執(zhí)行計(jì)劃一般可以根據(jù)explain來得到,但是還有幾類方法,對(duì)結(jié)果做格式化處理,比如轉(zhuǎn)化為json或者xml的格式等。

 

  1. postgres=# explain select *from test;  
  2. -[ RECORD 1 ]------------------------------------------------------  
  3. QUERY PLAN | Seq Scan on test  (cost=0.00..35.50 rows=2550 width=4)  
  4. 得到j(luò)son格式的執(zhí)行計(jì)劃。  
  5. postgres=# explain(format json) select *from test;  
  6. -[ RECORD 1 ]------------------------------  
  7. QUERY PLAN | [                             +  
  8.            |   {                           +  
  9.            |     "Plan": {                 +  
  10.            |       "Node Type""Seq Scan",+  
  11.            |       "Relation Name""test",+  
  12.            |       "Alias""test",        +  
  13.            |       "Startup Cost": 0.00,   +  
  14.            |       "Total Cost": 35.50,    +  
  15.            |       "Plan Rows": 2550,      +  
  16.            |       "Plan Width": 4         +  
  17.            |     }                         +  
  18.            |   }                           +  
  19.            | ] 

 

或者做一些分析,能夠得到更細(xì)節(jié)的執(zhí)行信息。

 

  1. postgres=# explain analyze select *from test;  
  2.                                             QUERY PLAN                                        
  3.  
  4. --------------------------------------------------------------------------------------------------  
  5.  Seq Scan on test  (cost=0.00..35.50 rows=2550 width=4) (actual time=0.001..0.001 rows=0 loops=1)  
  6.  Planning time: 0.018 ms  
  7.  Execution time: 0.009 ms 

 

15.查看存儲(chǔ)過程

查看存儲(chǔ)過程就是比較單薄的??梢灾苯邮褂胮g_proc來得到詳細(xì)的信息。

pg_proc 

16.存儲(chǔ)過程的調(diào)度執(zhí)行

目前沒有看到很直接的方式,這部分感覺還不夠強(qiáng)大。 

17.事務(wù)隔離級(jí)別

根據(jù)公司現(xiàn)狀和業(yè)務(wù)規(guī)模的不斷擴(kuò)大,其實(shí)技術(shù)上也是不斷地改進(jìn)和積累,事務(wù)方面的處理也是如此,等規(guī)模達(dá)到了一定的量級(jí),這部分的要求就會(huì)很明確。所以很多開發(fā)同學(xué)對(duì)于鎖機(jī)制都很感興趣。

查看事務(wù)隔離級(jí)別的兩種SQL語(yǔ)句。

 

  1. postgres=#  show default_transaction_isolation;  
  2.  default_transaction_isolation   
  3. -------------------------------  
  4.  read committed 

 

查看當(dāng)前的事務(wù)隔離級(jí)別設(shè)置。

 

  1. postgres=# show transaction_isolation;  
  2.  transaction_isolation   
  3. -----------------------  
  4.  read committed  

 

責(zé)任編輯:龐桂玉 來源: 楊建榮的學(xué)習(xí)筆記
相關(guān)推薦

2010-04-07 16:55:14

Unix命令

2012-06-14 13:20:44

MySQL網(wǎng)站架構(gòu)

2023-09-04 16:55:18

2010-01-15 10:34:59

Linux命令行操作

2010-05-12 15:41:21

MySQL數(shù)據(jù)庫(kù)

2013-06-26 14:00:40

routeros軟路由routero

2022-11-09 19:02:10

Linux

2025-01-15 09:00:20

2010-03-11 14:16:16

Linux route

2021-09-15 10:31:52

網(wǎng)絡(luò)安全網(wǎng)絡(luò)攻擊網(wǎng)絡(luò)威脅

2021-12-04 14:51:38

隱私數(shù)據(jù)安全網(wǎng)絡(luò)安全

2009-11-23 10:14:10

Linuxroute命令輸出信息

2022-01-12 17:35:50

Linux命令命令行工具

2014-05-04 11:17:39

Unix命令Linux命令

2013-03-29 13:17:53

XCode調(diào)試技巧iOS開發(fā)

2011-07-13 09:13:56

Android設(shè)計(jì)

2011-03-15 17:46:43

2012-05-21 10:13:05

XCode調(diào)試技巧

2009-07-21 09:55:45

iBATIS分頁(yè)

2009-06-18 14:54:52

Spring AOP
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)