PostgreSQL建表动作分析

时间:2021-05-17 08:40:09

首先,建立表:

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=# 

基本就是这些了。