heapam.cpp--存储引擎(6)

引言

接着上一篇博客,我们继续解读内存访问(access)文件夹下的另一个子文件夹heap,主要对heapam.cpp进行学习和解读。本次主要学习heap_inplace_update和heap_bcm_redo两个函数。

文件路径

opengauss-server\src\gausskernel\storage\access\heap\heapam.cpp

Function name:heap_inplace_update

这个函数,heap_inplace_update,用于在PostgreSQL数据库中原地更新一个元组。这个函数是OpenGauss的堆访问方法(heapam)的一部分,提供了访问堆结构(无序)表中存储数据的函数。 以下是函数的源码以及完整注释:

 
  1. /*
  2. * This function, heap_inplace_update, is used to update a tuple in-place within
  3. a OpenGauss database.
  4. */
  5. void heap_inplace_update(Relation relation, HeapTuple tuple, bool waitFlush)
  6. {
  7. Buffer buffer;
  8. Page page;
  9. OffsetNumber offnum, maxoff;
  10. ItemId lp = NULL;
  11. HeapTupleHeader htup;
  12. uint32 oldlen;
  13. uint32 newlen;
  14. errno_t rc;
  15. /*
  16. * For now, parallel operations are required to be strictly read-only.
  17. * Unlike a regular update, this should never create a combo CID, so it
  18. * might be possible to relax this restriction, but not without more
  19. * thought and testing. It's not clear that it would be useful, anyway.
  20. */
  21. if (false) {
  22. ereport(ERROR,
  23. (errcode(ERRCODE_INVALID_TRANSACTION_STATE), errmsg("cannot update tuples during a parallel operation")));
  24. }
  25. buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(&(tuple->t_self)));
  26. //Reads the buffer: It reads the buffer that contains the block where the tuple to be updated is located
  27. LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
  28. //Locks the buffer: It locks the buffer exclusively to prevent other processes from accessing it during the update
  29. page = (Page)BufferGetPage(buffer);
  30. // Get the pages from the Buffer
  31. offnum = ItemPointerGetOffsetNumber(&(tuple->t_self));
  32. maxoff = PageGetMaxOffsetNumber(page);
  33. if (maxoff >= offnum) {
  34. lp = PageGetItemId(page, offnum);
  35. }
  36. if (maxoff < offnum || !ItemIdIsNormal(lp)) {
  37. ereport(ERROR, (errcode(ERRCODE_DATA_CORRUPTED), errmsg("heap_inplace_update: invalid lp")));
  38. }
  39. //Retrieve the offset of the tuple on the page and check its validity.
  40. htup = (HeapTupleHeader)PageGetItem(page, lp);
  41. oldlen = ItemIdGetLength(lp) - htup->t_hoff;
  42. newlen = tuple->t_len - tuple->t_data->t_hoff;
  43. if (oldlen != newlen || htup->t_hoff != tuple->t_data->t_hoff) {
  44. ereport(ERROR, (errcode(ERRCODE_DATA_CORRUPTED), errmsg("heap_inplace_update: wrong tuple length")));
  45. }
  46. /* NO EREPORT(ERROR) from here till changes are logged */
  47. START_CRIT_SECTION();
  48. //This is a critical section that includes operations for updating tuple data and marking the buffer as dirty.
  49. rc = memcpy_s((char*)htup + htup->t_hoff, newlen, (char*)tuple->t_data + tuple->t_data->t_hoff, newlen);
  50. securec_check(rc, "\0", "\0");
  51. MarkBufferDirty(buffer);
  52. XLogRecPtr recptr = InvalidXLogRecPtr;
  53. //This line of code defines a log record pointer and initializes it with an invalid value.
  54. //This pointer will be used later in the code to record the log position.
  55. /* XLOG stuff */
  56. if (RelationNeedsWAL(relation)) {
  57. xl_heap_inplace xlrec;
  58. xlrec.offnum = ItemPointerGetOffsetNumber(&tuple->t_self);
  59. XLogBeginInsert();
  60. XLogRegisterData((char*)&xlrec, SizeOfHeapInplace);
  61. XLogRegisterBuffer(0, buffer, REGBUF_STANDARD);
  62. XLogRegisterBufData(0, (char*)htup + htup->t_hoff, newlen);
  63. recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_INPLACE);
  64. PageSetLSN(page, recptr);
  65. }
  66. //If Write-Ahead Logging (WAL) is enabled for the relation, it constructs a WAL record for the update and inserts it into the WAL buffer
  67. END_CRIT_SECTION();
  68. UnlockReleaseBuffer(buffer);
  69. if (waitFlush && (recptr != InvalidXLogRecPtr)) {
  70. XLogWaitFlush(recptr);
  71. }
  72. /*
  73. * Send out shared cache inval if necessary. Note that because we only
  74. * pass the new version of the tuple, this mustn't be used for any
  75. * operations that could change catcache lookup keys. But we aren't
  76. * bothering with index updates either, so that's true a fortiori.
  77. */
  78. if (!IsBootstrapProcessingMode()) {
  79. CacheInvalidateHeapTuple(relation, tuple, NULL);
  80. }
  81. }

in-place

要想实现in-place,同时保证事务的原子性,恢复未提交事务,WAL是通常情况下最行之有效的手段。 WAL,全称为Write-Ahead Logging,是数据库系统中常见的一种手段,用于保证数据操作的原子性和持久性。在计算机科学中,预写式日志(Write-ahead logging,缩写 WAL)是关系数据库系统中用于提供原子性和持久性(ACID 属性中的两个)的一系列技术。在函数heap_inplace_update如下位置应用

 
  1. if (RelationNeedsWAL(relation)) {
  2. ......
  3. }

在使用WAL的系统中,所有的修改在提交之前都要先写入log文件中。log文件中通常包括redo和undo信息。 这样做的目的可以通过一个例子来说明:假设一个程序在执行某些操作的过程中机器掉电了。在重新启动时,程序可能需要知道当时执行的操作是成功了还是部分成功或者是失败了。如果使用了WAL,程序就可以检查log文件,并对突然掉电时计划执行的操作内容跟实际上执行的操作内容进行比较。在这个比较的基础上,程序就可以决定是撤销已做的操作还是继续完成已做的操作,或者是保持原样。

WAL允许用in-place方式更新数据库。另一种用来实现原子更新的方法是shadow paging,它并不是in-place方式。用in-place方式做更新的主要优点是减少索引和块列表的修改

举个例子,假设我们有一个数据库表,其中包含了一列是员工的薪水。现在,我们需要给所有员工的薪水增加10%。如果我们使用in-place更新,那么每个员工的薪水将直接在原始位置增加10%,而不会改变它们在磁盘上的位置。这意味着我们不需要更新任何索引或块列表,因为数据的物理位置没有改变。

相反,如果我们不使用in-place更新,那么每次薪水增加时,新的薪水值将在磁盘上的其他位置写入,然后我们需要更新索引和块列表以反映新值的位置。这将需要额外的计算和I/O操作,从而降低了性能。 如下图所示:

Function name:heap_bcm_redo

这个函数用于在数据库恢复过程中重做BCM(Block Change Map)的修改。BCM是一个数据结构,用于跟踪数据库中哪些块已经被修改过,从而在数据库崩溃恢复时只需要检查这些块。 以下是函数的源码以及完整注释:

 
  1. /*
  2. * Function name:heap_bcm_redo
  3. * xlrec:This is a pointer to the xl_heap_bcm structure, which contains information about the Block Change Map (BCM),
  4. such as block numbers, statuses, and counts.
  5. * node:This is a relation file node, which identifies a relation (i.e., a table) within the database. In PostgreSQL and openGauss, each table is composed of one or more files,
  6. each with an associated relation file node
  7. * lsn:This is a Log Sequence Number (LSN), which identifies a position within the log. During database recovery, LSN is used to determine which operations need to be redone.
  8. * Description:This function is used to redo modifications to the Block Change Map (BCM) during the database recovery process.
  9. * The BCM is a data structure used to track which blocks in the database have been modified, allowing for efficient checking of these blocks during database crash recovery.
  10. */
  11. void heap_bcm_redo(xl_heap_bcm* xlrec, RelFileNode node, XLogRecPtr lsn)
  12. {
  13. int col = xlrec->col;
  14. Relation reln = CreateFakeRelcacheEntry(node);
  15. Buffer bcmbuffer = InvalidBuffer;
  16. //If the number of columns is greater than 0, it indicates a column-stored table.
  17. //In this code block, the function processes each BCM for every column.
  18. if (col > 0) { /* cloumn store */
  19. BlockNumber curBcmBlock = 0;
  20. BlockNumber nextBcmBlock = 0;
  21. int i = 0;
  22. /* read current bcm block */
  23. curBcmBlock = HEAPBLK_TO_BCMBLOCK(xlrec->block + i);
  24. nextBcmBlock = curBcmBlock;
  25. BCM_CStore_pin(reln, col, ((xlrec->block + i) * ALIGNOF_CUSIZE), &bcmbuffer);
  26. LockBuffer(bcmbuffer, BUFFER_LOCK_EXCLUSIVE);
  27. do {
  28. /* deal with bcm block switch */
  29. if (nextBcmBlock != curBcmBlock) {
  30. curBcmBlock = nextBcmBlock;
  31. /* release last bcm block and read in the next one */
  32. UnlockReleaseBuffer(bcmbuffer);
  33. BCM_CStore_pin(reln, col, ((xlrec->block + i) * ALIGNOF_CUSIZE), &bcmbuffer);
  34. LockBuffer(bcmbuffer, BUFFER_LOCK_EXCLUSIVE);
  35. }
  36. /*
  37. * Don't set the bit if replay has already passed this point.
  38. * and we are in t_thrd.xlog_cxt.InRecovery, no need to consider log_heap_bcm.
  39. */
  40. if (!XLByteLE(lsn, PageGetLSN(BufferGetPage(bcmbuffer)))) {
  41. BCMSetStatusBit(reln, xlrec->block + i, bcmbuffer, xlrec->status, col);
  42. ereport(DEBUG2,
  43. (errmsg("BCMSetStatusBit: oid:%u col:%d block:%lu status: %d",
  44. reln->rd_node.relNode,
  45. col,
  46. xlrec->block + i,
  47. NOTSYNCED)));
  48. }
  49. i++;
  50. nextBcmBlock = HEAPBLK_TO_BCMBLOCK(xlrec->block + i);
  51. } while (i < xlrec->count);
  52. UnlockReleaseBuffer(bcmbuffer);//Unlock and release the buffer.
  53. } else { /* row store */
  54. BCM_pin(reln, xlrec->block, &bcmbuffer);
  55. //Get the particular BCM buffer.
  56. LockBuffer(bcmbuffer, BUFFER_LOCK_EXCLUSIVE);
  57. if (!XLByteLE(lsn, PageGetLSN(BufferGetPage(bcmbuffer)))) {
  58. BCMSetStatusBit(reln, xlrec->block, bcmbuffer, xlrec->status, col);
  59. }//Judge the lsn to ensure that only after the modifications to BCM have been recorded in the log
  60. //will they be redone during the recovery process.
  61. UnlockReleaseBuffer(bcmbuffer);
  62. }
  63. FreeFakeRelcacheEntry(reln);
  64. }

BCM

Block Change Map(BCM)是数据库系统中用于跟踪哪些数据块已经被修改过的数据结构。在数据库恢复过程中,BCM可以帮助系统确定哪些数据块需要被检查和恢复。 SQL Server物理文件的结构如下图所示:

对于PFS,GAM等更多知识可以参考链接 页和区 | Microsoft Learn

在SQL Server中,BCM页面用于跟踪自上次日志备份操作以来由于批量记录操作而修改的范围。 在数据库文件里,BCM页是第7页。BCM对每个跟踪的页都有一个位。如果这个位标记是1,表示对应区在上次日志备份后因为大容量日志操作而修改。如果这个位标记为0,表示在上次日子备份后因为大容量日志操作而未被修改。一个BCM页可以保存近64000个区的信息。BCM页在每511232页重复一次。一个BCM页可以跟踪63904个区的信息。第2个BCM页会出现在第511239页 在批量记录恢复模式下,当执行日志备份时,SQL Server会扫描BCM页面,并将标记为已更改的范围包含在日志备份中,以及事务日志。这样,如果从数据库备份和一系列事务日志备份中恢复数据库,SQL Server可以使批量记录操作可恢复。

小结

heapam.cpp的框架如下表所示

模块描述
relation_open通过关系的OID(对象标识符)打开任何关系
relation_openrv通过RangeVar指定的方式打开任何关系
relation_close关闭任何关系
heap_open通过关系的OID打开堆关系
heap_openrv通过RangeVar指定的方式打开堆关系
heap_close关闭堆关系(现在通常是一个用于relation_close的宏)
heap_beginscan开始对关系的扫描
heap_rescan重新启动关系扫描
heap_endscan结束关系扫描
heap_getnext检索扫描中的下一个元组
heap_fetch根据给定的TID(元组标识符)检索元组
heap_insert将元组插入关系
heap_multi_insert将多个元组插入关系
heap_delete从关系中删除元组
heap_update用另一个元组替换关系中的元组
heap_markpos标记扫描位置
heap_restrpos将位置恢复到标记的位置
heap_sync同步堆,用于当未写入WAL(Write-Ahead Logging)时的情况

本次继续对heapam.cpp中的函数做了学习和解读,并且对heapam.cpp的函数框架做了初步总结,在下一篇博客当中,我将全面学习heapam.cpp的内容,并且对文件的函数做一个总结。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值