溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PostgreSql數(shù)據(jù)庫中對(duì)象的作用有哪些

發(fā)布時(shí)間:2020-12-28 14:53:54 來源:億速云 閱讀:319 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章給大家介紹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. 查詢數(shù)據(jù)庫對(duì)象

1.1 表查詢

PostgreSql 表信息可以從information_schema.tables 或 pg_catalog.pg_tables 視圖中查詢:

select * from information_schema.tables;
select * from pg_catalog.pg_tables;

1.2 查詢Schema

獲取用戶當(dāng)前選擇的schema:

select current_schema();

返回?cái)?shù)據(jù)庫中所有schema:

select * from information_schema.schemata;
select * from pg_catalog.pg_namespace

1.3 查詢數(shù)據(jù)庫

查詢當(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';

1.7 觸發(fā)器

查詢數(shù)據(jù)庫中所有觸發(fā)器,action_statemen類別包括觸發(fā)器body信息:

select * from information_schema.triggers;

2. 查詢表占用空間

2.1 查詢表占用空間

實(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;

示例輸出:


relationtotal_size
public.snapshots823 MB
public.invoice_items344 MB
public.messages267 MB
public.topics40 MB
public.invoices35 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ò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(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)容。

AI