您好,登錄后才能下訂單哦!
這篇文章給大家介紹PostgreSql數(shù)據(jù)庫中對(duì)象的作用有哪些,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
PostgreSql數(shù)據(jù)庫對(duì)象主要有數(shù)據(jù)庫、表、視圖、索引、schema、函數(shù)、觸發(fā)器等。PostgreSql提供了information_schema schema,其中包括返回?cái)?shù)據(jù)庫對(duì)象的視圖。如用戶有訪問權(quán)限,可以也在pg_catalog schema中查詢表、視圖等對(duì)象。
1.1 表查詢
PostgreSql 表信息可以從information_schema.tables 或 pg_catalog.pg_tables 視圖中查詢:
select * from information_schema.tables; select * from pg_catalog.pg_tables;
獲取用戶當(dāng)前選擇的schema:
select current_schema();
返回?cái)?shù)據(jù)庫中所有schema:
select * from information_schema.schemata; select * from pg_catalog.pg_namespace
查詢當(dāng)前選擇的數(shù)據(jù)庫:
select current_database();
返回服務(wù)器上所有數(shù)據(jù)庫:
select * from pg_catalog.pg_database
1.4 查詢視圖
查詢數(shù)據(jù)庫中所有schema中的所有視圖:
select * from information_schema.views select * from pg_catalog.pg_views;
1.5 查詢表的列信息
查詢某個(gè)表的列信息:
SELECT * FROM information_schema.columns WHERE table_name = 'employee' ORDER BY ordinal_position;
1.6 查詢索引信息
查詢數(shù)據(jù)庫中所有索引信息;
select * from pg_catalog.pg_indexes;
1.6 查詢函數(shù)信息
返回?cái)?shù)據(jù)庫中所有函數(shù)。對(duì)于用戶定義函數(shù),routine_definition 列會(huì)有函數(shù)體:
select * from information_schema.routines where routine_type = 'FUNCTION';
查詢數(shù)據(jù)庫中所有觸發(fā)器,action_statemen類別包括觸發(fā)器body信息:
select * from information_schema.triggers;
實(shí)際應(yīng)用中,通常需要表占用磁盤空間情況,我們可以利用系統(tǒng)表實(shí)現(xiàn):
SELECT nspname || '.' || relname AS "relation", pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size" FROM pg_class C LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) WHERE nspname NOT IN ('pg_catalog', 'information_schema') AND C.relkind <> 'i' AND nspname !~ '^pg_toast' ORDER BY pg_total_relation_size(C.oid) DESC LIMIT 5;
示例輸出:
relation | total_size |
---|---|
public.snapshots | 823 MB |
public.invoice_items | 344 MB |
public.messages | 267 MB |
public.topics | 40 MB |
public.invoices | 35 MB |
(5 rows)
2.2 查詢數(shù)據(jù)庫占用空間
SELECT pg_database.datname AS "database_name", pg_size_pretty(pg_database_size (pg_database.datname)) AS size_in_mb FROM pg_database ORDER BY size_in_mb DESC;
2.3 查詢表的記錄數(shù)
可以通過統(tǒng)計(jì)系統(tǒng)表進(jìn)行查詢:
SELECT schemaname,relname,n_live_tup FROM pg_stat_user_tables ORDER BY n_live_tup DESC LIMIT 12;
順便說下MySQL對(duì)于查詢,讀者可以對(duì)比學(xué)習(xí):
SELECT table_name, table_rows FROM information_schema.tables WHERE table_schema = (SELECT database()) ORDER BY table_rows DESC LIMIT 12;
4. 系統(tǒng)表和系統(tǒng)視圖
查看數(shù)據(jù)庫系統(tǒng)表命令:
\dt pg_*
表名字 | 用途 |
---|---|
pg_aggregate | 聚集函數(shù) |
pg_am | 索引訪問方法 |
pg_amop | 訪問方法操作符 |
pg_amproc | 訪問方法支持過程 |
pg_attrdef | 字段缺省值 |
pg_attribute | 表的列(也稱為”屬性”或”字段”) |
pg_authid | 認(rèn)證標(biāo)識(shí)符(角色) |
pg_auth_members | 認(rèn)證標(biāo)識(shí)符成員關(guān)系 |
pg_autovacuum | 每個(gè)關(guān)系一個(gè)的自動(dòng)清理配置參數(shù) |
pg_cast | 轉(zhuǎn)換(數(shù)據(jù)類型轉(zhuǎn)換) |
pg_class | 表、索引、序列、視圖(“關(guān)系”) |
pg_constraint | 檢查約束、唯一約束、主鍵約束、外鍵約束 |
pg_conversion | 編碼轉(zhuǎn)換信息 |
pg_database | 本集群內(nèi)的數(shù)據(jù)庫 |
pg_depend | 數(shù)據(jù)庫對(duì)象之間的依賴性 |
pg_description | 數(shù)據(jù)庫對(duì)象的描述或注釋 |
pg_index | 附加的索引信息 |
pg_inherits | 表繼承層次 |
pg_language | 用于寫函數(shù)的語言 |
pg_largeobject | 大對(duì)象 |
pg_listener | 異步通知 |
pg_namespace | 模式 |
pg_opclass | 索引訪問方法操作符類 |
pg_operator | 操作符 |
pg_pltemplate | 過程語言使用的模板數(shù)據(jù) |
pg_proc | 函數(shù)和過程 |
pg_rewrite | 查詢重寫規(guī)則 |
pg_shdepend | 在共享對(duì)象上的依賴性 |
pg_shdescription | 共享對(duì)象上的注釋 |
pg_statistic | 優(yōu)化器統(tǒng)計(jì) |
pg_tablespace | 這個(gè)數(shù)據(jù)庫集群里面的表空間 |
pg_trigger | 觸發(fā)器 |
pg_type | 數(shù)據(jù)類型 |
列出所有pg開頭的系統(tǒng)示圖:
\dv pg_*
視圖名 | 用途 |
---|---|
pg_cursors | 打開的游標(biāo) |
pg_group | 數(shù)據(jù)庫用戶的組 |
pg_indexes | 索引 |
pg_locks | 當(dāng)前持有的鎖 |
pg_prepared_statements | 預(yù)備語句 |
pg_prepared_xacts | 預(yù)備事務(wù) |
pg_roles | 數(shù)據(jù)庫角色 |
pg_rules | 規(guī)則 |
pg_settings | 參數(shù)設(shè)置 |
pg_shadow | 數(shù)據(jù)庫用戶 |
pg_stats | 規(guī)劃器統(tǒng)計(jì) |
pg_tables | 表 |
pg_timezone_abbrevs | 時(shí)區(qū)縮寫 |
pg_timezone_names | 時(shí)區(qū)名 |
pg_user | 數(shù)據(jù)庫用戶 |
pg_views | 視圖 |
關(guān)于PostgreSql數(shù)據(jù)庫中對(duì)象的作用有哪些就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。