詳解PostgreSql數(shù)據(jù)庫(kù)對(duì)象信息及應(yīng)用
PostgreSql數(shù)據(jù)庫(kù)對(duì)象主要有數(shù)據(jù)庫(kù)、表、視圖、索引、schema、函數(shù)、觸發(fā)器等。PostgreSql提供了information_schema schema,其中包括返回?cái)?shù)據(jù)庫(kù)對(duì)象的視圖。如用戶(hù)有訪問(wèn)權(quán)限,可以也在pg_catalog schema中查詢(xún)表、視圖等對(duì)象。
1. 查詢(xún)數(shù)據(jù)庫(kù)對(duì)象
下面通過(guò)示例分別展示如何查詢(xún)各種數(shù)據(jù)庫(kù)對(duì)象。
1.1 表查詢(xún)
PostgreSql 表信息可以從information_schema.tables 或 pg_catalog.pg_tables 視圖中查詢(xún):
select * from information_schema.tables; select * from pg_catalog.pg_tables;
1.2 查詢(xún)Schema
獲取用戶(hù)當(dāng)前選擇的schema:
select current_schema();
返回?cái)?shù)據(jù)庫(kù)中所有schema:
select * from information_schema.schemata; select * from pg_catalog.pg_namespace
1.3 查詢(xún)數(shù)據(jù)庫(kù)
查詢(xún)當(dāng)前選擇的數(shù)據(jù)庫(kù):
select current_database();
返回服務(wù)器上所有數(shù)據(jù)庫(kù):
select * from pg_catalog.pg_database
1.4 查詢(xún)視圖
查詢(xún)數(shù)據(jù)庫(kù)中所有schema中的所有視圖:
select * from information_schema.views select * from pg_catalog.pg_views;
1.5 查詢(xún)表的列信息
查詢(xún)某個(gè)表的列信息:
SELECT * FROM information_schema.columns WHERE table_name = 'employee' ORDER BY ordinal_position;
1.6 查詢(xún)索引信息
查詢(xún)數(shù)據(jù)庫(kù)中所有索引信息;
select * from pg_catalog.pg_indexes;
1.6 查詢(xún)函數(shù)信息
返回?cái)?shù)據(jù)庫(kù)中所有函數(shù)。對(duì)于用戶(hù)定義函數(shù),routine_definition 列會(huì)有函數(shù)體:
select * from information_schema.routines where routine_type = 'FUNCTION';
1.7 觸發(fā)器
查詢(xún)數(shù)據(jù)庫(kù)中所有觸發(fā)器,action_statemen類(lèi)別包括觸發(fā)器body信息:
select * from information_schema.triggers;
2. 查詢(xún)表占用空間
2.1 查詢(xún)表占用空間
實(shí)際應(yīng)用中,通常需要表占用磁盤(pán)空間情況,我們可以利用系統(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 查詢(xún)數(shù)據(jù)庫(kù)占用空間
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 查詢(xún)表的記錄數(shù)
可以通過(guò)統(tǒng)計(jì)系統(tǒng)表進(jìn)行查詢(xún):
SELECT schemaname,relname,n_live_tup FROM pg_stat_user_tables ORDER BY n_live_tup DESC LIMIT 12;
順便說(shuō)下MySQL對(duì)于查詢(xún),讀者可以對(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ù)庫(kù)系統(tǒng)表命令:
\dt pg_*
| 表名字 | 用途 |
|---|---|
| pg_aggregate | 聚集函數(shù) |
| pg_am | 索引訪問(wèn)方法 |
| pg_amop | 訪問(wèn)方法操作符 |
| pg_amproc | 訪問(wèn)方法支持過(guò)程 |
| pg_attrdef | 字段缺省值 |
| pg_attribute | 表的列(也稱(chēng)為”屬性”或”字段”) |
| 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ù)類(lèi)型轉(zhuǎn)換) |
| pg_class | 表、索引、序列、視圖(“關(guān)系”) |
| pg_constraint | 檢查約束、唯一約束、主鍵約束、外鍵約束 |
| pg_conversion | 編碼轉(zhuǎn)換信息 |
| pg_database | 本集群內(nèi)的數(shù)據(jù)庫(kù) |
| pg_depend | 數(shù)據(jù)庫(kù)對(duì)象之間的依賴(lài)性 |
| pg_description | 數(shù)據(jù)庫(kù)對(duì)象的描述或注釋 |
| pg_index | 附加的索引信息 |
| pg_inherits | 表繼承層次 |
| pg_language | 用于寫(xiě)函數(shù)的語(yǔ)言 |
| pg_largeobject | 大對(duì)象 |
| pg_listener | 異步通知 |
| pg_namespace | 模式 |
| pg_opclass | 索引訪問(wèn)方法操作符類(lèi) |
| pg_operator | 操作符 |
| pg_pltemplate | 過(guò)程語(yǔ)言使用的模板數(shù)據(jù) |
| pg_proc | 函數(shù)和過(guò)程 |
| pg_rewrite | 查詢(xún)重寫(xiě)規(guī)則 |
| pg_shdepend | 在共享對(duì)象上的依賴(lài)性 |
| pg_shdescription | 共享對(duì)象上的注釋 |
| pg_statistic | 優(yōu)化器統(tǒng)計(jì) |
| pg_tablespace | 這個(gè)數(shù)據(jù)庫(kù)集群里面的表空間 |
| pg_trigger | 觸發(fā)器 |
| pg_type | 數(shù)據(jù)類(lèi)型 |
列出所有pg開(kāi)頭的系統(tǒng)示圖:
\dv pg_*
| 視圖名 | 用途 |
|---|---|
| pg_cursors | 打開(kāi)的游標(biāo) |
| pg_group | 數(shù)據(jù)庫(kù)用戶(hù)的組 |
| pg_indexes | 索引 |
| pg_locks | 當(dāng)前持有的鎖 |
| pg_prepared_statements | 預(yù)備語(yǔ)句 |
| pg_prepared_xacts | 預(yù)備事務(wù) |
| pg_roles | 數(shù)據(jù)庫(kù)角色 |
| pg_rules | 規(guī)則 |
| pg_settings | 參數(shù)設(shè)置 |
| pg_shadow | 數(shù)據(jù)庫(kù)用戶(hù) |
| pg_stats | 規(guī)劃器統(tǒng)計(jì) |
| pg_tables | 表 |
| pg_timezone_abbrevs | 時(shí)區(qū)縮寫(xiě) |
| pg_timezone_names | 時(shí)區(qū)名 |
| pg_user | 數(shù)據(jù)庫(kù)用戶(hù) |
| pg_views | 視圖 |
4. 總結(jié)
本文介紹PostgreSQL系統(tǒng)表及視圖;通過(guò)系統(tǒng)表或視圖查詢(xún)數(shù)據(jù)庫(kù)對(duì)象及常用統(tǒng)計(jì)信息。
到此這篇關(guān)于PostgreSql數(shù)據(jù)庫(kù)對(duì)象信息及應(yīng)用的文章就介紹到這了,更多相關(guān)PostgreSql數(shù)據(jù)庫(kù)應(yīng)用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis調(diào)用PostgreSQL存儲(chǔ)過(guò)程實(shí)現(xiàn)數(shù)組入?yún)鬟f
這篇文章主要介紹了mybatis調(diào)用postgresql自定義函數(shù)傳遞數(shù)組參數(shù)的解決方案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
PostgreSQL 數(shù)據(jù)庫(kù)性能提升的幾個(gè)方面
PostgreSQL提供了一些幫助提升性能的功能。主要有一些幾個(gè)方面。2009-09-09
PostgreSQL 實(shí)現(xiàn)sql放入文件批量執(zhí)行
這篇文章主要介紹了PostgreSQL 實(shí)現(xiàn)sql放入文件批量執(zhí)行,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
postgresql數(shù)據(jù)庫(kù)設(shè)置id自增的基本方法舉例例子解析
這篇文章主要給大家介紹了關(guān)于postgresql數(shù)據(jù)庫(kù)設(shè)置id自增的基本方法舉例,文章介紹了在PostgreSQL中實(shí)現(xiàn)自增ID的兩種方法,分別是使用序列和觸發(fā)器,序列方法簡(jiǎn)單直接,而觸發(fā)器和函數(shù)方法則提供了更大的靈活性,需要的朋友可以參考下2024-11-11
PostgreSQL如何殺死被鎖死的進(jìn)程問(wèn)題
文章總結(jié):文章主要介紹了如何使用PostgreSQL提供的pg_cancel_backend()和pg_terminate_backend()函數(shù)來(lái)解決數(shù)據(jù)庫(kù)表被鎖住的問(wèn)題,以及如何查詢(xún)哪些表、哪些進(jìn)程被鎖住了2024-12-12
PostgreSQL基礎(chǔ)知識(shí)之SQL操作符實(shí)踐指南
這篇文章主要給大家介紹了關(guān)于PostgreSQL基礎(chǔ)知識(shí)之SQL操作符實(shí)踐的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用PostgreSQL具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

