1.创建一张表test_hide_attr,通过这张表获知PostgreSQL表的隐藏属性。
1
2
|
test_zqs=# create table test_hide_attr(hide int4); CREATE TABLE |
2.查询PostgreSQL的系统表pg_class,该表存储数据库对象信息,如表、视图、索引等。一个元组存储一个数据库对象信息,并且每一个都会被分配一个oid作为唯一标识,该oid将作为该元组的一个隐藏属性存储。其中relname字段代表数据库对象的名称。
1
2
3
4
5
|
test_zqs=# select oid,relname from pg_class where relname= 'test_hide_attr' ; oid | relname -------+---------------- 17168 | test_hide_attr (1 row) |
3.查询系统表pg_attribute,该表用来存储表的属性信息,包括隐藏属性,一个属性对应一个元组。其中,attrelid代表属性所属表的oid,而attname则代表属性名称。
1
2
3
4
5
6
7
8
9
10
11
|
test_zqs=# select attrelid,attname from pg_attribute where attrelid=17168; attrelid | attname ----------+---------- 17168 | cmax 17168 | cmin 17168 | ctid 17168 | hide 17168 | tableoid 17168 | xmax 17168 | xmin (7 rows ) |
4.对表test_hide_attr,实际用户仅创建了hide字段,其余字段均为该表的隐藏字段。
1
2
3
4
5
6
7
8
9
|
test_zqs=# \d test_hide_attr Table "public.test_hide_attr" Column | Type | Modifiers --------+---------+----------- hide | integer | test_zqs=# select tableoid from test_hide_attr; tableoid ---------- (0 rows ) |
5.查看表隐藏属性tableoid、pg_class隐藏属性oid、pg_attribute属性attrelid三者之间是相同的(对同一张表来说)
1
2
3
4
5
6
7
|
test_zqs=# insert into test_hide_attr values (1); INSERT 0 1 test_zqs=# select cmax,cmin,ctid,hide,tableoid,xmax,xmin from test_hide_attr; cmax | cmin | ctid | hide | tableoid | xmax | xmin ------+------+-------+------+----------+------+------ 0 | 0 | (0,1) | 1 | 17168 | 0 | 2260 (1 row) |
下面,对表的隐藏属性所代表的含义做了些整理,见下文
delete from emp a
where a.ctid <>
(
select min(b.ctid) from emp b
where a.id = b.id
);