查看账本历史操作记录

前提条件

  • 系统中需要有审计管理员或者具有审计管理员权限的角色。
  • 数据库正常运行,并且对防篡改数据库执行了一系列增、删、改等操作,保证在查询时段内有账本操作记录结果产生。

背景信息

  • 只有拥有AUDITADMIN属性的用户才可以查看账本历史操作记录。有关数据库用户及创建用户的办法请参见用户
  • 查询全局区块表命令是直接查询gs_global_chain表,操作为:

    1. SELECT * FROM gs_global_chain;

    该表有11个字段,每个字段的含义见章节GS_GLOBAL_CHAIN

  • 查询用户历史表的命令是直接查询BLOCKCHAIN模式下的用户历史表,操作为:

    例如用户表所在的模式为ledgernsp,表名为usertable,则对应的用户历史表名为blockchain.ledgernsp_usertable_hist;

    1. SELECT * FROM blockchain.ledgernsp_usertable_hist;

    用户历史表有4个字段,每个字段的含义见表1

    查看账本历史操作记录 - 图1 说明: 用户历史表的表名一般为blockchain.__hist形式。当防篡改用户表模式名或者表名过长导致前述方式生成的表名超出表名长度限制,则会采用blockchain.__hist的方式命名。

操作步骤

  1. 以操作系统用户omm登录数据库主节点。
  2. 使用如下命令连接数据库。

    1. gsql -d postgres -p 8000

    postgres为需要连接的数据库名称,8000为端口号。

    连接成功后,系统显示类似如下信息:

    1. gsql ((GaussDB Kernel VxxxRxxxCxx build 290d125f) compiled at 2020-05-08 02:59:43 commit 2143 last mr 131
    2. Non-SSL connection (SSL connection is recommended when requiring high-security)
    3. Type "help" for help.
    4. openGauss=#
  3. 查询全局区块表记录。

    1. openGauss=# SELECT * FROM gs_global_chain;

    查询结果如下:

    1. blocknum | dbname | username | starttime | relid | relnsp | relname | relhash | globalhash |
    2. txcommand
    3. ----------+----------+----------+-------------------------------+-------+-----------+-----------+------------------+----------------------------------+------------------
    4. ------------------------------------------------------------
    5. 0 | postgres | omm | 2021-04-14 07:00:46.32757+08 | 16393 | ledgernsp | usertable | a41714001181a294 | 6b5624e039e8aee36bff3e8295c75b40 | insert into ledge
    6. rnsp.usertable values(1, 'alex'), (2, 'bob'), (3, 'peter');
    7. 1 | postgres | omm | 2021-04-14 07:01:19.767799+08 | 16393 | ledgernsp | usertable | b3a9ed0755131181 | 328b48c4370faed930937869783c23e0 | update ledgernsp.
    8. usertable set name = 'bob2' where id = 2;
    9. 2 | postgres | omm | 2021-04-14 07:01:29.896148+08 | 16393 | ledgernsp | usertable | 0ae4b4e4ed2fcab5 | aa8f0a236357cac4e5bc1648a739f2ef | delete from ledge
    10. rnsp.usertable where id = 3;

    该结果表明,用户omm连续执行了三条DML命令,包括INSERT、UPDATE和DELETE操作。

  4. 查询历史表记录。

    1. openGauss=# SELECT * FROM blockchain.ledgernsp_usertable_hist;

    查询结果如下:

    1. rec_num | hash_ins | hash_del | pre_hash
    2. ---------+------------------+------------------+----------------------------------
    3. 0 | 1f2e543c580cb8c5 | | e1b664970d925d09caa295abd38d9b35
    4. 1 | 8fcd74a8a6a4b484 | | dad3ed8939a141bf3682043891776b67
    5. 2 | f51b4b1b12d0354b | | 53eb887fc7c4302402343c8914e43c69
    6. 3 | 437761affbb7c605 | 8fcd74a8a6a4b484 | c2868c5b49550801d0dbbbaa77a83a10
    7. 4 | | f51b4b1b12d0354b | 9c512619f6ffef38c098477933499fe3
    8. (5 rows)

    查询结果显示,用户omm对ledgernsp.usertable表插入了3条数据,更新了1条数据,随后删除了1行数据,最后剩余2行数据,hash值分别为1f2e543c580cb8c5和437761affbb7c605。

  5. 查询用户表数据及hash校验列。

    1. openGauss=# SELECT *, hash FROM ledgernsp.usertable;

    查询结果如下:

    1. id | name | hash
    2. ----+------+------------------
    3. 1 | alex | 1f2e543c580cb8c5
    4. 2 | bob2 | 437761affbb7c605
    5. (2 rows)

    查询结果显示,用户表中剩余2条数据,与步骤4中的记录一致。