标签:
mydb=> select ctid,* from test; ctid | id | name -------+------+-------- (0,1) | 1001 | lottu (0,2) | 1002 | rax (0,3) | 1003 | xuan (0,4) | 1004 | li0924 (0,5) | 1001 | ak
mydb=> delete from test where ctid not in (select min(ctid) from test group by id); DELETE 1 mydb=> select ctid,* from test; ctid | id | name -------+------+-------- (0,1) | 1001 | lottu (0,2) | 1002 | rax (0,3) | 1003 | xuan (0,4) | 1004 | li0924 (4 rows)
刚刚我们删除了(0,5)这条记录; 现在我们把这条记录插入下;看下;
mydb=> insert into test values (1001,‘ak‘); INSERT 0 1 mydb=> select ctid,* from test; ctid | id | name -------+------+-------- (0,1) | 1001 | lottu (0,2) | 1002 | rax (0,3) | 1003 | xuan (0,4) | 1004 | li0924 (0,6) | 1001 | ak (5 rows)
奇怪了;为什么不是(0,5),而是(0,6)这个跟postgresql多版本事务有关;跟伪列cmin,cmax有关;跟本文讲的ctid没点关系;这是postgresql的特性;也就是这样;postgresql里面没有回滚段的概念;那怎么把(0,5)在显示呢;想这块(0,5)的空间再存放数据;postgresql里面有AUTOVACUUM进程;当然我们也可以手动回收这段空间;
mydb=> delete from test where name = ‘ak‘; DELETE 1 mydb=> vacuum test; VACUUM mydb=> insert into test values (1001,‘ak‘); INSERT 0 1 mydb=> select ctid,* from test; ctid | id | name -------+------+-------- (0,1) | 1001 | lottu (0,2) | 1002 | rax (0,3) | 1003 | xuan (0,4) | 1004 | li0924 (0,5) | 1001 | ak (5 rows)
2. 我们刚刚说道 0表示块id; test数据太少了;不好解释;新建一个表test2
mydb=> drop table test2; DROP TABLE mydb=> create table test2 (id int primary key, name varchar(10)); CREATE TABLE mydb=> insert into test2 select generate_series(1,1000),‘lottu‘ || generate_series(1,1000); INSERT 0 1000
我们看下id=1000的ctid的blockid是多少;答案是5;意思是说该表的记录记录到第6个块;(因为是从0开始的)
mydb=> select ctid,* from test2 where id = 1000; ctid | id | name --------+------+----------- (5,75) | 1000 | lottu1000 (1 row)
mydb=> analyze test2; ANALYZE mydb=> select relpages,reltuples from pg_class where relname = ‘test2‘; relpages | reltuples ----------+----------- 6 | 1000 (1 row)
标签:
原文地址:http://www.cnblogs.com/lottu/p/5613098.html