首页 > 数据库 >postgresql/lightdb CommandCounterIncrement()函数的作用

postgresql/lightdb CommandCounterIncrement()函数的作用

时间:2022-09-04 21:26:17浏览次数:122  
标签:1111111111111111111111111111110000000000 postgresql lightdb zjh CommandCounterIn

  CommandCounterIncrement的作用是使当前事务中前面语句的修改对本语句可见,相当于oracle中的当前读概念(current read,只不过oracle区分,pg不区分)。事务中每执行一个语句后,对后续语句都会直接可见。如下:

zjh@postgres=# START TRANSACTION ISOLATION LEVEL REPEATABLE READ;
START TRANSACTION
zjh@postgres=*# select * from t;
 id | v  
----+----
  1 | v1
(1 row)

zjh@postgres=*# insert into t values(2,'v2');
INSERT 0 1
zjh@postgres=*# select * from t;
 id | v  
----+----
  1 | v1
  2 | v2
(2 rows)

zjh@postgres=*# rollback;
ROLLBACK

该函数的作用和cid没有关系,cid是标记当前行是被事务中的第几个语句修改。存储在元组头src/include/access/htup_details.h中,如下:

typedef struct HeapTupleFields
{
    TransactionId t_xmin;        /* inserting xact ID */
    TransactionId t_xmax;        /* deleting or locking xact ID */

    union
    {
        CommandId    t_cid;        /* inserting or deleting command ID, or both */
        TransactionId t_xvac;    /* old-style VACUUM FULL xact ID */
    }            t_field3;
} HeapTupleFields;

可通过pageinspect查询每行记录的元组头,如下:

zjh@postgres=# SELECT * FROM heap_page_items(get_raw_page('pg_class', 0));
 lp | lp_off | lp_flags | lp_len | t_xmin | t_xmax | t_field3 | t_ctid | t_infomask2 | t_infomask | t_hoff |                  t_bits                  | t_oid |                                
                                                                                                                                                        t_data                                 
                                                                                                                                                       
----+--------+----------+--------+--------+--------+----------+--------+-------------+------------+--------+------------------------------------------+-------+--------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------
  1 |     47 |        2 |      0 |        |        |          |        |             |            |        |                                          |       | 
  2 |   7584 |        1 |    172 |    488 |    488 |        0 | (0,5)  |       16417 |       1313 |     32 | 1111111111111111111111111111110000000000 |       | \x004000007461626c655f666f725f7
66163756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009808000002400000000000000a0000000200000000400000000000000000000000000000000000000000
00000000707202000000000000000001640000000000e801000001000000
  3 |   7408 |        1 |    172 |    488 |    493 |        6 | (0,13) |          33 |       1281 |     32 | 1111111111111111111111111111110000000000 |       | \x0340000070675f746f6173745f313
633383400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006300000004400000000000000a0000000200000003400000000000000000000000000000000000000000
000001007074030000000000000000016e0000000000e801000001000000
  4 |   7232 |        1 |    172 |    488 |    493 |        6 | (0,15) |          33 |       1281 |     32 | 1111111111111111111111111111110000000000 |       | \x0540000070675f746f6173745f313
63338345f696e64657800000000000000000000000000000000000000000000000000000000000000000000000000000000000000006300000000000000000000000a0000009301000005400000000000000100000000000000000000000000
000000007069020000000000000000016e00000000000000000000000000
  5 |   7056 |        1 |    172 |    488 |    493 |        6 | (0,11) |       32801 |       9473 |     32 | 1111111111111111111111111111110000000000 |       | \x004000007461626c655f666f725f7
66163756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009808000002400000000000000a0000000200000000400000000000000b0000000000fa440b0000000340
00000000707202000000000000000001640000000000e801000001000000
zjh@postgres=# SELECT * FROM heap_page_items(get_raw_page('t', 0));
 lp | lp_off | lp_flags | lp_len | t_xmin | t_xmax | t_field3 | t_ctid | t_infomask2 | t_infomask | t_hoff | t_bits | t_oid |      t_data      
----+--------+----------+--------+--------+--------+----------+--------+-------------+------------+--------+--------+-------+------------------
  1 |   8160 |        1 |     31 |    548 |      0 |        0 | (0,1)  |           2 |       2306 |     24 |        |       | \x01000000077631
  2 |   8128 |        1 |     31 |    549 |      0 |        0 | (0,2)  |           2 |       2562 |     24 |        |       | \x02000000077632
  3 |   8096 |        1 |     31 |    550 |      0 |        0 | (0,3)  |           2 |       2562 |     24 |        |       | \x02000000077632
(3 rows)

 

https://www.postgresql.org/message-id/flat/200011041823.NAA27229%40candle.pha.pa.us#2b278d5117873db459aa8c3e3a6e9472

如果内核相关部分没有调用CommandCounterIncrement()函数,就会发生当前事务之前的修改对随后的查询不可见的情况,这是不符合sql标准要求的。https://postgrespro.com/list/id/[email protected]

标签:1111111111111111111111111111110000000000,postgresql,lightdb,zjh,CommandCounterIn
From: https://www.cnblogs.com/zhjh256/p/16653821.html

相关文章

  • 论lightdb/postgresql中的search_path及实现兼容性管理
    上一篇介绍了lightdb/postgresqlpublic、pg_catalogschema的区别及pg_namespace概念,因为最近几个版本开发下来,遇到了很多兼容性挑战。所以这一节来专门讨论一下searc......
  • lightdb使用一条sql实现高性能事务一致性归历史
    相比insertselect,delete,如下:--lightdb专有oracle匿名块写法BEGINTRANSACTIONISOLATIONLEVELREPEATABLEREAD;insertintoxxselectxxfromyywhereid<xxx;......
  • PostgreSQL-查询
    检索过程或从数据库中检索数据的命令称为查询。在SQL中,SELECT命令用于指定查询。SELECT命令的一般语法是:[WITHwith_queries]SELECTselect_listFROMtable_express......
  • PostgreSQL-从修改的行返回数据
    有时在操作修改的行时从修改的行中获取数据很有用。INSERT、UPDATE和DELETE命令都有一个可选的RETURNING子句来支持这一点。使用RETURNING可以避免执行额外的数据库......
  • PostgreSQL-插入
    创建表时,它不包含任何数据。在数据库发挥作用之前要做的第一件事就是插入数据。数据一次插入一行。您还可以在单​​个命令中插入多行,但不能插入不完整的行。即使您只知道......
  • PostgreSQL-表继承
    让我们从一个例子开始:假设我们正在尝试为城市构建一个数据模型。每个州都有许多城市,但只有一个首府。我们希望能够快速检索任何特定州的首都。这可以通过创建两张表来完成,......
  • PostgreSQL-schema
    数据库包含一个或多个命名模式,这些模式又包含表。模式还包含其他类型的命名对象,包括数据类型、函数和运算符。相同的对象名称可以在不同的模式中使用而不会发生冲突;例如,sch......
  • PostgreSQL-更改表
    当您创建一个表并意识到您犯了一个错误,或者应用程序的需求发生变化时,您可以删除该表并重新创建它。但是,如果表已经被数据填充,或者表被其他数据库对象引用(例如外键约束),这不......
  • PostgreSQL-系统列
    每个表都有几个由系统隐式定义的系统列。因此,这些名称不能用作用户定义列的名称。(请注意,这些限制与名称是否是关键字是分开的;引用名称不会让您逃避这些限制。)您实际上不需......
  • PostgreSQL-基础2
    一、类型转换的方式CAST(expressionAStype)expression::typetypename(expression)二、生成列生成的列是始终从其他列计算的特殊列。因此,对于列,视图对于表是......