PostgreSQL:查詢數據表信息

PostgreSQL 14.0

pgAdmin 4 Version 5.7

--

 

0、序章

要查詢數據表信息,需要用到 系統表或系統視圖等,比如,pg_tables、pg_class、information_schema 等。

使用 pgAdmin 4 連接數據庫,可以看到這些 系統表或系統視圖 在下面的位置:下圖中的 lib1 爲 數據庫名稱

注,pgAdmin 4 中沒有系統表的註釋及字段註釋,但可以從 官文(參考連接#2 下載) 中獲取。

 

建立測試 schema 及 其下的測試 數據表:schema2023.some_info

創建模式及表
-- SCHEMA: schema2023

-- DROP SCHEMA schema2023 ;

CREATE SCHEMA schema2023
    AUTHORIZATION postgres;

-- Table: schema2023.some_info

-- DROP TABLE schema2023.some_info;

CREATE TABLE IF NOT EXISTS schema2023.some_info
(
    id character varying(128) COLLATE pg_catalog."default" NOT NULL,
    name character varying(100) COLLATE pg_catalog."default" NOT NULL,
    money double precision,
    days integer,
    create_time timestamp without time zone NOT NULL,
    CONSTRAINT some_info_pkey PRIMARY KEY (id)
)

TABLESPACE pg_default;

ALTER TABLE schema2023.some_info
    OWNER to postgres;

COMMENT ON TABLE schema2023.some_info
    IS '一些信息';

COMMENT ON COLUMN schema2023.some_info.id
    IS '主鍵';

COMMENT ON COLUMN schema2023.some_info.name
    IS '名稱';

COMMENT ON COLUMN schema2023.some_info.money
    IS '價值:人民幣計價';

COMMENT ON COLUMN schema2023.some_info.days
    IS '有效天數';

COMMENT ON COLUMN schema2023.some_info.create_time
    IS '創建時間';

 

查詢一些全局信息

select * from pg_namespace

結果:存在模式 schema2023 的信息,其 oid 的字面值爲 222485ben發佈於博客園

 

select * from pg_tablespace

結果:

 

1、數據表本身的信息

pg_tables 中查詢:ben發佈於博客園

select * from pg_tables
where schemaname = 'schema2023' and tablename = 'some_info'

結果:

 

pg_class 中查詢:

select * from pg_class
where relname = 'some_info'
limit 2

結果:

ben發佈於博客園

pg_description 中查詢:使用函數 to_regclass

select * from pg_description
where objoid = to_regclass('schema2023' || '.' || 'some_info')::REGCLASS::OID

結果:其中 objsubid = 0 所在行 爲 表註釋,其它的爲 表字段註釋;這裏的 objoid 的字面值爲 222486,爲 數據表 對應的 oid(pg_class 表中);

 

綜合查詢

-- 整個數據庫僅有一個名爲 some_info 的表情況下
select pgc.oid, 
(select nspname from pg_namespace pgns where pgns.oid = pgc.relnamespace) as schemaname,
pgc.relname as tablename,
(select description from pg_description pgds 
 where pgds.objoid = pgc.oid and objsubid = 0
) as description
from pg_class pgc
where pgc.relname = 'some_info'

結果:

 

2、數據表中字段的信息

information_schema.columns 中查詢:ben發佈於博客園

select * from information_schema.columns
limit 4

結果:

information_schema.columns 中部分字段:

table_catalog, table_schema, table_name, column_name, ordinal_position, is_nullable, data_type

 

查詢測試表的字段信息:

select * from information_schema.columns
where table_schema = 'schema2023' and table_name = 'some_info'

結果:

ben發佈於博客園

綜合查詢:包括字段註釋

sql語句:

select 
table_catalog, table_schema, table_name, column_name, 
ordinal_position, is_nullable, data_type,
(select description from pg_description pgds 
 where pgds.objoid = to_regclass('schema2023' || '.' || 'some_info')::REGCLASS::OID 
 and objsubid = isc.ordinal_position
) as description
from information_schema.columns isc 
where table_schema = 'schema2023' and table_name = 'some_info'

結果:

ben發佈於博客園

疑問:

怎麼查詢數據表中的 主鍵?TODO

 

3、背景知識

函數:to_regclass ( text ) → regclass

 

The catalog pg_class:官文

The catalog pg_class catalogs tables and most everything else that has columns or is otherwise
similar to a table. This includes indexes (but see also pg_index), sequences (but see also pg_sequence), views, 
materialized views, composite types, and TOAST tables; see relkind. Below,
when we mean all of these kinds of objects we speak of “relations”. Not all columns are meaningful
for all relation types.

ben發佈於博客園

The catalog pg_description:官文

The catalog pg_description stores optional descriptions (comments) for each database object.
Descriptions can be manipulated with the COMMENT command and viewed with psql's \d commands.
Descriptions of many built-in system objects are provided in the initial contents of pg_description.
See also pg_shdescription, which performs a similar function for descriptions involving objects
that are shared across a database cluster.

 

The view columns:官文

The view columns contains information about all table columns (or view columns) in the database.
System columns (ctid, etc.) are not included. Only those columns are shown that the current user
has access to (by way of being the owner or having some privilege).

 

---END---

 

本文鏈接:

https://www.cnblogs.com/luo630/p/17076045.html

ben發佈於博客園

參考資料

1、PostgreSQL 中的 OID

https://www.jianshu.com/p/ffb833bd4fb5

2、PostgreSQL 官文

https://www.postgresql.org/docs/

3、

 

 

ben發佈於博客園

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章