设为首页 收藏本站
查看: 1235|回复: 0

[经验分享] PostgreSQL建表动作分析

[复制链接]

尚未签到

发表于 2016-11-21 09:47:01 | 显示全部楼层 |阅读模式
  首先,建立表:



pgsql=# create table tab10(id integer);
CREATE TABLE
pgsql=# select 147525::regclass;
regclass
----------
tab10
(1 row)
pgsql=#
  查看此时的文件信息:



[pgsql@localhost 16384]$ pwd
/home/pgsql/DemoDir/base/16384[pgsql@localhost 16384]$ ls -l 147525
-rw------- 1 pgsql pgsql 0 Jul  4 13:45 147525
[pgsql@localhost 16384]$
  此时,文件刚刚建立好,还是一个空文件
  同时,可以看到,因为建立了一个表,所以数据字典中有很多系统表被更新:
  例如:pg_type。
  这个确实有点超乎想象,因为我并未增加任何的新type。



pgsql=# select count(*) from pg_type;
count
-------
313
(1 row)
pgsql=# create table tab10(id integer);
CREATE TABLE
pgsql=# select count(*) from pg_type;
count
-------
315
(1 row)
  看看增加了什么:



pgsql=# \x
Expanded display is on.
pgsql=# select * from pg_type where typname='tab10';
-[ RECORD 1 ]--+------------
typname        | tab10
typnamespace   | 2200
typowner       | 10
typlen         | -1
typbyval       | f
typtype        | c
typcategory    | C
typispreferred | f
typisdefined   | t
typdelim       | ,
typrelid       | 188542
typelem        | 0
typarray       | 188543
typinput       | record_in
typoutput      | record_out
typreceive     | record_recv
typsend        | record_send
typmodin       | -
typmodout      | -
typanalyze     | -
typalign       | d
typstorage     | x
typnotnull     | f
typbasetype    | 0
typtypmod      | -1
typndims       | 0
typcollation   | 0
typdefaultbin  |
typdefault     |
pgsql=#


pgsql=# select * from pg_type where typname='_tab10';
-[ RECORD 1 ]--+-----------
typname        | _tab10
typnamespace   | 2200
typowner       | 10
typlen         | -1
typbyval       | f
typtype        | b
typcategory    | A
typispreferred | f
typisdefined   | t
typdelim       | ,
typrelid       | 0
typelem        | 188544
typarray       | 0
typinput       | array_in
typoutput      | array_out
typreceive     | array_recv
typsend        | array_send
typmodin       | -
typmodout      | -
typanalyze     | -
typalign       | d
typstorage     | x
typnotnull     | f
typbasetype    | 0
typtypmod      | -1
typndims       | 0
typcollation   | 0
typdefaultbin  |
typdefault     |
pgsql=#
  创建一个表达式后,对其他的系统表的写入,也有很多
  再看和pg_depend之间的关联:



pgsql=# drop table tab10;
DROP TABLE
pgsql=#
pgsql=# SELECT classid::regclass AS "depender object class",
CASE classid
WHEN 'pg_class'::regclass THEN objid::regclass::text
WHEN 'pg_type'::regclass THEN objid::regtype::text
WHEN 'pg_proc'::regclass THEN objid::regprocedure::text
ELSE objid::text
END AS "depender object identity",
objsubid,
refclassid::regclass AS "referenced object class",
CASE refclassid
WHEN 'pg_class'::regclass THEN refobjid::regclass::text
WHEN 'pg_type'::regclass THEN refobjid::regtype::text
WHEN 'pg_proc'::regclass THEN refobjid::regprocedure::text
ELSE refobjid::text
END AS "referenced object identity",
refobjsubid,
CASE deptype
WHEN 'p' THEN 'pinned'
WHEN 'i' THEN 'internal'
WHEN 'a' THEN 'automatic'
WHEN 'n' THEN 'normal'
END AS "dependency type"
FROM pg_catalog.pg_depend
WHERE objid >= 16384 OR refobjid >= 16384;
(No rows)
pgsql=#
pgsql=# create table tab10(id integer);
CREATE TABLE
pgsql=# SELECT classid::regclass AS "depender object class",
CASE classid
WHEN 'pg_class'::regclass THEN objid::regclass::text
WHEN 'pg_type'::regclass THEN objid::regtype::text
WHEN 'pg_proc'::regclass THEN objid::regprocedure::text
ELSE objid::text
END AS "depender object identity",
objsubid,
refclassid::regclass AS "referenced object class",
CASE refclassid
WHEN 'pg_class'::regclass THEN refobjid::regclass::text
WHEN 'pg_type'::regclass THEN refobjid::regtype::text
WHEN 'pg_proc'::regclass THEN refobjid::regprocedure::text
ELSE refobjid::text
END AS "referenced object identity",
refobjsubid,
CASE deptype
WHEN 'p' THEN 'pinned'
WHEN 'i' THEN 'internal'
WHEN 'a' THEN 'automatic'
WHEN 'n' THEN 'normal'
END AS "dependency type"
FROM pg_catalog.pg_depend
WHERE objid >= 16384 OR refobjid >= 16384;
-[ RECORD 1 ]--------------+-------------
depender object class      | pg_type
depender object identity   | tab10
objsubid                   | 0
referenced object class    | pg_class
referenced object identity | tab10
refobjsubid                | 0
dependency type            | internal
-[ RECORD 2 ]--------------+-------------
depender object class      | pg_type
depender object identity   | tab10[]
objsubid                   | 0
referenced object class    | pg_type
referenced object identity | tab10
refobjsubid                | 0
dependency type            | internal
-[ RECORD 3 ]--------------+-------------
depender object class      | pg_class
depender object identity   | tab10
objsubid                   | 0
referenced object class    | pg_namespace
referenced object identity | 2200
refobjsubid                | 0
dependency type            | normal
pgsql=#
  再看对pg_class的影响:



pgsql=# drop table tab10;
DROP TABLEpgsql=# create table tab10(id integer);
CREATE TABLEpgsql=# \x
Expanded display is on.
pgsql=# select * from pg_class where relname='tab10';
-[ RECORD 1 ]--+-------
relname        | tab10
relnamespace   | 2200
reltype        | 188562
reloftype      | 0
relowner       | 10
relam          | 0
relfilenode    | 188560
reltablespace  | 0
relpages       | 0
reltuples      | 0
reltoastrelid  | 0
reltoastidxid  | 0
relhasindex    | f
relisshared    | f
relpersistence | p
relkind        | r
relnatts       | 1
relchecks      | 0
relhasoids     | f
relhaspkey     | f
relhasrules    | f
relhastriggers | f
relhassubclass | f
relfrozenxid   | 2017
relacl         |
reloptions     |
pgsql=#
  再看对 pg_attribute的影响,生成表之后:



pgsql=# select 188563::regclass;
regclass
----------
tab10
(1 row)
pgsql=# \x
Expanded display is on.
pgsql=# select * from pg_attribute where attrelid = (select max(attrelid) from pg_attribute);
-[ RECORD 1 ]-+---------
attrelid      | 188563
attname       | tableoid
atttypid      | 26
attstattarget | 0
attlen        | 4
attnum        | -7
attndims      | 0
attcacheoff   | -1
atttypmod     | -1
attbyval      | t
attstorage    | p
attalign      | i
attnotnull    | t
atthasdef     | f
attisdropped  | f
attislocal    | t
attinhcount   | 0
attcollation  | 0
attacl        |
attoptions    |
-[ RECORD 2 ]-+---------
attrelid      | 188563
attname       | cmax
atttypid      | 29
attstattarget | 0
attlen        | 4
attnum        | -6
attndims      | 0
attcacheoff   | -1
atttypmod     | -1
attbyval      | t
attstorage    | p
attalign      | i
attnotnull    | t
atthasdef     | f
attisdropped  | f
attislocal    | t
attinhcount   | 0
attcollation  | 0
attacl        |
attoptions    |
-[ RECORD 3 ]-+---------
attrelid      | 188563
attname       | xmax
atttypid      | 28
attstattarget | 0
attlen        | 4
attnum        | -5
attndims      | 0
attcacheoff   | -1
atttypmod     | -1
attbyval      | t
attstorage    | p
attalign      | i
attnotnull    | t
atthasdef     | f
attisdropped  | f
attislocal    | t
attinhcount   | 0
attcollation  | 0
attacl        |
attoptions    |
-[ RECORD 4 ]-+---------
attrelid      | 188563
attname       | cmin
atttypid      | 29
attstattarget | 0
attlen        | 4
attnum        | -4
attndims      | 0
attcacheoff   | -1
atttypmod     | -1
attbyval      | t
attstorage    | p
attalign      | i
attnotnull    | t
atthasdef     | f
attisdropped  | f
attislocal    | t
attinhcount   | 0
attcollation  | 0
attacl        |
attoptions    |
-[ RECORD 5 ]-+---------
attrelid      | 188563
attname       | xmin
atttypid      | 28
attstattarget | 0
attlen        | 4
attnum        | -3
attndims      | 0
attcacheoff   | -1
atttypmod     | -1
attbyval      | t
attstorage    | p
attalign      | i
attnotnull    | t
atthasdef     | f
attisdropped  | f
attislocal    | t
attinhcount   | 0
attcollation  | 0
attacl        |
attoptions    |
-[ RECORD 6 ]-+---------
attrelid      | 188563
attname       | ctid
atttypid      | 27
attstattarget | 0
attlen        | 6
attnum        | -1
attndims      | 0
attcacheoff   | -1
atttypmod     | -1
attbyval      | f
attstorage    | p
attalign      | s
attnotnull    | t
atthasdef     | f
attisdropped  | f
attislocal    | t
attinhcount   | 0
attcollation  | 0
attacl        |
attoptions    |
-[ RECORD 7 ]-+---------
attrelid      | 188563
attname       | id
atttypid      | 23
attstattarget | -1
attlen        | 4
attnum        | 1
attndims      | 0
attcacheoff   | -1
atttypmod     | -1
attbyval      | t
attstorage    | p
attalign      | i
attnotnull    | f
atthasdef     | f
attisdropped  | f
attislocal    | t
attinhcount   | 0
attcollation  | 0
attacl        |
attoptions    |
pgsql=#
  基本就是这些了。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-303279-1-1.html 上篇帖子: PostgreSQL,pgAdmin3 在Ubuntu上安装指南 下篇帖子: 设置postgresql.conf参数-备查
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表