暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PostgreSQL学习随笔12 实验infomask = xmin_frozen

1691



0 ~ 2是保留的txid,它们比任何普通txid都要旧。

0invalidtransactionid,表示无效的事务id

1bootstraptransactionid,表示系统表初始化时的事务id,比任何普通的事务id都旧。

2frozentransactionid,冻结的事务id,比任何普通的事务id都旧。

大于2的事务id都是普通的事务id,即从3开始就是普通的事务id


PostgreSQL 9.3或者之前的版本事务冻结,xmin 会被改成2.

PostgreSQL 9.4或者之后的版本中事务冻结,infomask = xmin_frozen.


/************************************************************************************/

/*

 * information stored in t_infomask:

 */

#define HEAP_HASNULL 0x0001 /* has null attribute(s) */

#define HEAP_HASVARWIDTH 0x0002 /* has variable-width attribute(s) */

#define HEAP_HASEXTERNAL 0x0004 /* has external stored attribute(s) */

#define HEAP_HASOID_OLD 0x0008 /* has an object-id field */

#define HEAP_XMAX_KEYSHR_LOCK 0x0010 /* xmax is a key-shared locker */

#define HEAP_COMBOCID 0x0020 /* t_cid is a combo cid */

#define HEAP_XMAX_EXCL_LOCK 0x0040 /* xmax is exclusive locker */

#define HEAP_XMAX_LOCK_ONLY 0x0080 /* xmax, if valid, is only a locker */

 /* xmax is a shared locker */

#define HEAP_XMAX_SHR_LOCK (HEAP_XMAX_EXCL_LOCK | HEAP_XMAX_KEYSHR_LOCK)

#define HEAP_LOCK_MASK (HEAP_XMAX_SHR_LOCK | HEAP_XMAX_EXCL_LOCK | \

HEAP_XMAX_KEYSHR_LOCK)

#define HEAP_XMIN_COMMITTED 0x0100 /* t_xmin committed */

#define HEAP_XMIN_INVALID 0x0200 /* t_xmin invalid/aborted */

#define HEAP_XMIN_FROZEN (HEAP_XMIN_COMMITTED|HEAP_XMIN_INVALID)

#define HEAP_XMAX_COMMITTED 0x0400 /* t_xmax committed */

#define HEAP_XMAX_INVALID 0x0800 /* t_xmax invalid/aborted */

#define HEAP_XMAX_IS_MULTI 0x1000 /* t_xmax is a MultiXactId */

#define HEAP_UPDATED 0x2000 /* this is UPDATEd version of row */

#define HEAP_MOVED_OFF 0x4000 /* moved to another place by pre-9.0

* VACUUM FULL; kept for binary

* upgrade support */

#define HEAP_MOVED_IN 0x8000 /* moved from another place by pre-9.0

* VACUUM FULL; kept for binary

* upgrade support */

#define HEAP_MOVED (HEAP_MOVED_OFF | HEAP_MOVED_IN)


#define HEAP_XACT_MASK 0xFFF0 /* visibility-related bits */



/************************************************************************************/

HEAP_XMIN_FROZEN(HEAP_XMIN_COMMITTED|HEAP_XMIN_INVALID)

HEAP_XMIN_COMMITTED 0x0100 /* t_xmin committed */

HEAP_XMIN_INVALID 0x0200 /* t_xmin invalid/aborted */


pageinspect 模块提供函数让你从低层次观察数据库页面的内容

postgres=# create extension pageinspect;

CREATE EXTENSION

postgres=# \dx

                                List of installed extensions

    Name     | Version |   Schema   |                      Description                      

-------------+---------+------------+-------------------------------------------------------

 pageinspect | 1.6     | public     | inspect the contents of database pages at a low level

 plpgsql     | 1.0     | pg_catalog | PL/pgSQL procedural language

(2 rows)


制造表膨胀场景,执行vacuum freeze test;

制造表膨胀

Session 1

开启一个事务,让其持有backend_xid

postgres=# create table test(id int,info text);

CREATE TABLE

postgres=# insert into test values(1,'test');

INSERT 0 1

postgres=# insert into test values(2,'test');

INSERT 0 1

postgres=# begin;

BEGIN

postgres=# select pg_backend_pid();

pg_backend_pid

----------------

3968

(1 row)

postgres=# select txid_current();

txid_current

--------------

611

(1 row)

 

表膨胀实验 2

session 2

另外一个会话不断进行更新

postgres=# update test set info = 'c1c2';\watch 0.01

 

等一会就可以看到表膨胀了

db1=# \dt+ test

                    List of relations

 Schema | Name | Type  |  Owner   |  Size  | Description

--------+------+-------+----------+--------+-------------

 public | test | table | postgres | 712 kB |

(1 row)

 

执行vacuum freeze test;

vacuum freeze test;

 

postgres=# select t_infomask,to_hex(t_infomask) from heap_page_items(get_raw_page('test', 0));

 t_infomask | to_hex 

------------+--------

       1794 | 702

       1794 | 702

       9474 | 2502

       9474 | 2502

       9474 | 2502

       9474 | 2502

       9474 | 2502

       9474 | 2502

       9474 | 2502

       9474 | 2502


拆分

0x2502

----------

0x2000

0x0500 - > 0x0100 0x0400

0x0002 


HEAP_XMIN_FROZEN(HEAP_XMIN_COMMITTED|HEAP_XMIN_INVALID)

HEAP_XMIN_COMMITTED 0x0100 /* t_xmin committed */

HEAP_XMIN_INVALID 0x0200 /* t_xmin invalid/aborted */


#define HEAP_XMIN_FROZEN (HEAP_XMIN_COMMITTED|HEAP_XMIN_INVALID)


0x01000x0200 = 768

hex(768) = 0x0300

int('0x0300',16) = 768





文章转载自CP的PostgreSQL厨房,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论