PostgreSQL之如何敲开PG的大门?
零、声明
0.1将信将疑
《哈姆雷特》是莎士比亚的四大悲剧之一。莎士比亚曾经说过“一万个读者有一万个哈姆雷特”,意思是每个人的教育背景不同,心境不同,领悟不同,对于《哈姆雷特》的解读当然也就不同。现在这句话适用范围更广泛,表达的意思就是各人有各人的理解,对同一事物理解不尽相同。
– https://jikipedia.com/definition/baidu_qa/205772626
以下大部分内容都是个人观点,非官方术语,不保证正确性,仅供参考。
(如果大家觉得有什么地方不对,可以提出来,一起探讨,共同学习。)
对于网络上的一些资料也该如此:将信将疑、胆大猜测、小心求证。
0.2明确主题
引语
-
个人分享会。
-
如何敲开pg的大门?(怎么才算是敲开了pg的大门?)
0.3目标
立个能达到的小目标:比方说先赚他1个亿!
目标:走进pg。🍻
一、啥是pg?
官方介绍:https://www.postgresql.org/about/
PostgreSQL一个开源的关系数据库管理系统。
(大家觉得上面这句话有什么问题吗?🤔)
数据库 | 初始版本 | 历史 | 实现语言(源码) |
---|---|---|---|
Oracle | 1980 | 42年历史 | C and C++ |
MySQL | 1995 | 27年历史 | C and C++ |
Microsoft SQL Server | 1989 | 36年历史 | C++ |
PostgreSQL | 1989 | 36年历史 | C |
热度:基本稳居第四。
https://db-engines.com/en/ranking
总结:开源+丰富的数据类型(文档、几何、自定义数据类型等)+插件+SQL标准(对SQL标准兼容的最多)。
ps:附
pg源码:
https://git.postgresql.org/gitweb/?p=postgresql.git;a=tree
https://github.com/postgres/postgres
文档:
二、如何敲开pg的大门?
大力出奇迹!!!
错了,再来……
2.1 日常现状
2.1.1 数据库语言(DDL,DML,DQL,DCL)
DDL(data definition language)数据定义语言:用来创建数据库中的各种对象–表、视图、索引、同义词、聚簇等。create,drop,truncate,alter,show,desc。
DML(data Manipulation language)数据操纵语言:插入(insert),更新(update),删除(delete)。
DCL(data control language)数据控制语言:用来授予或回收访问数据库的某种特权,并控制数据库操纵事务发生的时间及效果,对数据库实行监视等。新建用户(create user),授权(grant),收回(revoke),开启事务(begin),回滚(rollback),提交(commit)
DQL(data query language)数据查询语言:SELECT子句,FROM子句,WHERE子句组成的查询块。
2.1.2 日常
日常:select,insert,update,delete;实际上大部分都是select,其他大多通过对于语言实现。
还有就是数据库函数。
但是其实这些在pg里面都还是比较表象的东西,因为其他大部分数据库基本上也都有,我觉得应该还不算是“敲开了pg的大门”。(注意:这并没有什么贬低的意思,因为不可否认select在大部分数据库中都是常用、基础且关键的操作。)
思考🤔(引语):那该如何敲开pg的大门呢?难道需要从了解源码开始???额……虽然有些C语言基础,但是需要看懂应该还是难度比较大的!那肯定不是这个,敲门又何需如此。
2.1.3 何为PostgreSQL?
回顾这句话有没有问题:“PostgreSQL一个开源的关系数据库管理系统。”?
(咋一看好像没什么问题,认真一看确实也可以说是没有问题。但是忽略了一个重要的特性!
研究pg一年多了,也是最近才发现这个问题,一直以来都被惯性思维所影响(先入为主),产生了一定的误解。)
http://www.postgres.cn/docs/12/intro-whatis.html
RDMS(Relational Database Management System):关系数据库管理系统。
ORDMS(Object-Relational Database Management System):对象-关系数据库管理系统。
2.2.面向对象–思维、思想
面向对象(Object Oriented)是软件开发方法,一种编程范式。
中心思想:抽象。
特征:对象唯一性、抽象性(类)、继承性(子类和父类)、多态性。
为什么突然又说起面向对象,因为pg就是面向对象来进行设计的。
pg vs 面向对象:
- 对象唯一性:内建字段oid
- 抽象性:类,pg_class
- 继承性:表分区(表分区是通过继承表来实现的)
- 多态性:多态类型(
anyelement
,anyarray
,anynonarray
, 或anyenum
)
说明:
oid 都是 postgresql 数据库表的隐藏列,如果select *
不显示,可以指定,如select oid,*
。
2.2.1 pg的类 pg_class
pg_class
记录表和几乎所有具有列或者像表的东西。这包括索引(但还要参见pg_index
)、序列(但还要参见pg_sequence
)、视图、物化视图、组合类型和TOAST表。
中文文档:http://postgres.cn/docs/12/catalog-pg-class.html
select * from pg_class;
-- rel(relation table 关系表简写) 对象类型
-- relkind 对象类型: r = 普通表(relation table), i = 索引(index), S = 序列(sequence), t = TOAST表(The Oversized-Attribute Storage Technique 超尺寸属性存储技术), v = 视图(view), m = 物化视图(materialized view), c = 组合类型(combination type), f = 外部表(foreign table), p = 分区表(partition table), I = 分区索引(partitioned index)
/*
-- relkind 对象类型:
r = 普通表(relation table),
i = 索引(index),
S = 序列(sequence),
t = TOAST表(The Oversized-Attribute Storage Technique 超尺寸属性存储技术),
v = 视图(view),
m = 物化视图(materialized view),
c = 组合类型(combination type),
f = 外部表(foreign table),
p = 分区表(partition table),
I = 分区索引(partitioned index)
*/
select distinct relkind from pg_class;
-- 查看各类型对象
select * from pg_class WHERE relkind = 'v';
select * from pg_class WHERE relkind = 'i';
select * from pg_class WHERE relkind = 't';
select * from pg_class WHERE relkind = 'r';
select * from pg_class WHERE relkind = 'f';
select * from pg_class WHERE relkind = 'm';
select * from pg_class WHERE relkind = 'S';
-- 常用字段:对象id,表名
select oid,relname from pg_class;
2.2.1.1 pg_class表字段说明
select * from pg_class limit 10;
详细字段
postgres=# \d pg_class
数据表 "pg_catalog.pg_class"
栏位 | 类型 | Collation | Nullable | 说明
---------------------+--------------+-----------+----------+---------
oid | oid | | not null |对象id
relname | name | | not null |对象名
relnamespace | oid | | not null |命名空间
reltype | oid | | not null |
reloftype | oid | | not null |
relowner | oid | | not null |
relam | oid | | not null |
relfilenode | oid | | not null |对象对应的文件名(同对象id)
reltablespace | oid | | not null |
relpages | integer | | not null |对象所占页面数(默认一页8k)
reltuples | real | | not null |对象记录数
relallvisible | integer | | not null |
reltoastrelid | oid | | not null |对象对应的toast的oid
relhasindex | boolean | | not null |
relisshared | boolean | | not null |
relpersistence | "char" | | not null |
relkind | "char" | | not null |对象类型
relnatts | smallint | | not null |
relchecks | smallint | | not null |
relhasoids | boolean | | not null |
relhasrules | boolean | | not null |
relhastriggers | boolean | | not null |
relhassubclass | boolean | | not null |
relrowsecurity | boolean | | not null |
relforcerowsecurity | boolean | | not null |
relispopulated | boolean | | not null |
relreplident | "char" | | not null |
relispartition | boolean | | not null |
relrewrite | oid | | not null |
relfrozenxid | xid | | not null |
relminmxid | xid | | not null |
relacl | aclitem[] | | |
reloptions | text[] | | |
relpartbound | pg_node_tree | | |
索引:
"pg_class_oid_index" UNIQUE, btree (oid)
"pg_class_relname_nsp_index" UNIQUE, btree (relname, relnamespace)
"pg_class_tblspc_relfilenode_index" btree (reltablespace, relfilenode)
2.2.1.2 pg_class常用语句
常用语句
1.查看某个表的相关信息
-- 查看某个表的相关信息
select * from pg_class where relname = 'test';
select * from pg_class where relname like '%test%';
-- 查看系统的内置对象
select * from pg_class where relname like 'pg\_%';
-- 也可以这样 11 pg_catalog 系统Schema(系统目录)
select oid,* from pg_namespace ORDER BY oid;
select * from pg_class where relnamespace =11;
2.oid对象id,唯一标识,对象对应的文件名。
select oid,* from pg_class where relname = 'test' limit 10;
对应的本地文件:
上一层路径16393就是数据库的oid.
3.查看某个对象的大小:
-- 查看表的总的大小
select pg_total_relation_size(oid);
select pg_total_relation_size(24648); --默认单位kb,163840
select pg_size_pretty(pg_total_relation_size(24648));--显示成可读性数值 160 kB
2.2.2 pg的函数 pg_proc
pg_proc
存放有关函数、过程、聚集函数以及窗口函数(共称为例程)的信息。
中文文档:http://postgres.cn/docs/12/catalog-pg-proc.html
select * from pg_proc;
-- prokind 类型:f表示普通函数,p表示过程,a表示聚集函数,w表示窗口函数
select distinct prokind from pg_proc;
-- 常用字段: 函数名、函数定义
select proname,prosrc from pg_proc;
2.2.2.1 pg_proc表字段说明
postgres=# \d pg_proc
数据表 "pg_catalog.pg_proc"
栏位 | 类型 | Collation | Nullable | 说明
-----------------+--------------+-----------+----------+---------
oid | oid | | not null |对象id
proname | name | | not null |对象名
pronamespace | oid | | not null |
proowner | oid | | not null |
prolang | oid | | not null |
procost | real | | not null |
prorows | real | | not null |
provariadic | oid | | not null |
protransform | regproc | | not null |
prokind | "char" | | not null |类型
prosecdef | boolean | | not null |
proleakproof | boolean | | not null |
proisstrict | boolean | | not null |
proretset | boolean | | not null |
provolatile | "char" | | not null |
proparallel | "char" | | not null |
pronargs | smallint | | not null |
pronargdefaults | smallint | | not null |
prorettype | oid | | not null |
proargtypes | oidvector | | not null |
proallargtypes | oid[] | | |
proargmodes | "char"[] | | |
proargnames | text[] | | |
proargdefaults | pg_node_tree | | |
protrftypes | oid[] | | |
prosrc | text | | not null |函数定义
probin | text | | |
proconfig | text[] | | |
proacl | aclitem[] | | |
索引:
"pg_proc_oid_index" UNIQUE, btree (oid)
"pg_proc_proname_args_nsp_index" UNIQUE, btree (proname, proargtypes, pronamespace)
2.2.2.2 pg_proc 常用语句
-- 查看某个函数的相关信息
select * from pg_proc where proname = 'length';
-- 查看某个表在哪个函数中使用过
select * from pg_proc where prosrc like '%test%';
2.2.3 pg的内置功能(表、函数、属性)
元数据库,元数据库的对象(表、函数等)命名都是有规律的,都是以pg_
开头(不确定是否有例外),因此我们后续在命名一些对象的时候也需要去注意一下,尽量不要带有这个标识,较少不必要的麻烦(关键字和标识符也是一样的道理)。
开源的好处之一就是规范,命名规范亦是其中之一。
知道这些内置对象有什么好处呢?
(1)哪里不会点那里:比如查pg有哪些内置对象:select * from pg_class where relname like 'pg\_%';
, 查系统中有哪些索引:select * from pg_class where relkind = 'i';
。
(2)pg的统计性的表、视图:比如pg索引 pg_indexes , pg_stat_activity等。
2.2.4 pg的扩展–插件
对应上面的“pg的内置功能”,那么这里就介绍一下:pg的外置功能(扩展)–插件。
就像是Chrome、IDEA、VS Code的插件一样,都是软件的灵魂之一。
-- 查看所有插件
select * from pg_available_extensions;
-- 查看已安装插件
select * from pg_available_extensions where installed_version is not null;
已安装插件:
必备插件:
- pg_stat_statement 插件:track execution statistics of all SQL statements executed 跟踪所有已执行SQL语句的执行统计信息
- pg_repack 插件:Reorganize tables in PostgreSQL databases with minimal locks 用最少的锁重新组织PostgreSQL数据库中的表
思考🤔(引语):师傅领进门,修行靠个人。已经介绍了如何敲开pg的大门了,至于进去怎么走,这就要看大家的了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?