這篇文章主要介紹“PostgreSQL中ReadBuffer_common函數有什么作用”,在日常操作中,相信很多人在PostgreSQL中ReadBuffer_common函數有什么作用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”PostgreSQL中ReadBuffer_common函數有什么作用”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
在余杭等地區,都構建了全面的區域性戰略布局,加強發展的系統性、市場前瞻性、產品創新能力,以專注、極致的服務理念,為客戶提供做網站、成都網站建設 網站設計制作按需網站設計,公司網站建設,企業網站建設,成都品牌網站建設,成都營銷網站建設,成都外貿網站建設,余杭網站建設費用合理。
BufferDesc
共享緩沖區的共享描述符(狀態)數據
/* * Flags for buffer descriptors * buffer描述器標記 * * Note: TAG_VALID essentially means that there is a buffer hashtable * entry associated with the buffer's tag. * 注意:TAG_VALID本質上意味著有一個與緩沖區的標記相關聯的緩沖區散列表條目。 */ //buffer header鎖定 #define BM_LOCKED (1U << 22) /* buffer header is locked */ //數據需要寫入(標記為DIRTY) #define BM_DIRTY (1U << 23) /* data needs writing */ //數據是有效的 #define BM_VALID (1U << 24) /* data is valid */ //已分配buffer tag #define BM_TAG_VALID (1U << 25) /* tag is assigned */ //正在R/W #define BM_IO_IN_PROGRESS (1U << 26) /* read or write in progress */ //上一個I/O出現錯誤 #define BM_IO_ERROR (1U << 27) /* previous I/O failed */ //開始寫則變DIRTY #define BM_JUST_DIRTIED (1U << 28) /* dirtied since write started */ //存在等待sole pin的其他進程 #define BM_PIN_COUNT_WAITER (1U << 29) /* have waiter for sole pin */ //checkpoint發生,必須刷到磁盤上 #define BM_CHECKPOINT_NEEDED (1U << 30) /* must write for checkpoint */ //持久化buffer(不是unlogged或者初始化fork) #define BM_PERMANENT (1U << 31) /* permanent buffer (not unlogged, * or init fork) */ /* * BufferDesc -- shared descriptor/state data for a single shared buffer. * BufferDesc -- 共享緩沖區的共享描述符(狀態)數據 * * Note: Buffer header lock (BM_LOCKED flag) must be held to examine or change * the tag, state or wait_backend_pid fields. In general, buffer header lock * is a spinlock which is combined with flags, refcount and usagecount into * single atomic variable. This layout allow us to do some operations in a * single atomic operation, without actually acquiring and releasing spinlock; * for instance, increase or decrease refcount. buf_id field never changes * after initialization, so does not need locking. freeNext is protected by * the buffer_strategy_lock not buffer header lock. The LWLock can take care * of itself. The buffer header lock is *not* used to control access to the * data in the buffer! * 注意:必須持有Buffer header鎖(BM_LOCKED標記)才能檢查或修改tag/state/wait_backend_pid字段. * 通常來說,buffer header lock是spinlock,它與標記位/參考計數/使用計數組合到單個原子變量中. * 這個布局設計允許我們執行原子操作,而不需要實際獲得或者釋放spinlock(比如,增加或者減少參考計數). * buf_id字段在初始化后不會出現變化,因此不需要鎖定. * freeNext通過buffer_strategy_lock鎖而不是buffer header lock保護. * LWLock可以很好的處理自己的狀態. * 務請注意的是:buffer header lock不用于控制buffer中的數據訪問! * * It's assumed that nobody changes the state field while buffer header lock * is held. Thus buffer header lock holder can do complex updates of the * state variable in single write, simultaneously with lock release (cleaning * BM_LOCKED flag). On the other hand, updating of state without holding * buffer header lock is restricted to CAS, which insure that BM_LOCKED flag * is not set. Atomic increment/decrement, OR/AND etc. are not allowed. * 假定在持有buffer header lock的情況下,沒有人改變狀態字段. * 持有buffer header lock的進程可以執行在單個寫操作中執行復雜的狀態變量更新, * 同步的釋放鎖(清除BM_LOCKED標記). * 換句話說,如果沒有持有buffer header lock的狀態更新,會受限于CAS, * 這種情況下確保BM_LOCKED沒有被設置. * 比如原子的增加/減少(AND/OR)等操作是不允許的. * * An exception is that if we have the buffer pinned, its tag can't change * underneath us, so we can examine the tag without locking the buffer header. * Also, in places we do one-time reads of the flags without bothering to * lock the buffer header; this is generally for situations where we don't * expect the flag bit being tested to be changing. * 一種例外情況是如果我們已有buffer pinned,該buffer的tag不能改變(在本進程之下), * 因此不需要鎖定buffer header就可以檢查tag了. * 同時,在執行一次性的flags讀取時不需要鎖定buffer header. * 這種情況通常用于我們不希望正在測試的flag bit將被改變. * * We can't physically remove items from a disk page if another backend has * the buffer pinned. Hence, a backend may need to wait for all other pins * to go away. This is signaled by storing its own PID into * wait_backend_pid and setting flag bit BM_PIN_COUNT_WAITER. At present, * there can be only one such waiter per buffer. * 如果其他進程有buffer pinned,那么進程不能物理的從磁盤頁面中刪除items. * 因此,后臺進程需要等待其他pins清除.這可以通過存儲它自己的PID到wait_backend_pid中, * 并設置標記位BM_PIN_COUNT_WAITER. * 目前,每個緩沖區只能由一個等待進程. * * We use this same struct for local buffer headers, but the locks are not * used and not all of the flag bits are useful either. To avoid unnecessary * overhead, manipulations of the state field should be done without actual * atomic operations (i.e. only pg_atomic_read_u32() and * pg_atomic_unlocked_write_u32()). * 本地緩沖頭部使用同樣的結構,但并不需要使用locks,而且并不是所有的標記位都使用. * 為了避免不必要的負載,狀態域的維護不需要實際的原子操作 * (比如只有pg_atomic_read_u32() and pg_atomic_unlocked_write_u32()) * * Be careful to avoid increasing the size of the struct when adding or * reordering members. Keeping it below 64 bytes (the most common CPU * cache line size) is fairly important for performance. * 在增加或者記錄成員變量時,小心避免增加結構體的大小. * 保持結構體大小在64字節內(通常的CPU緩存線大小)對于性能是非常重要的. */ typedef struct BufferDesc { //buffer tag BufferTag tag; /* ID of page contained in buffer */ //buffer索引編號(0開始) int buf_id; /* buffer's index number (from 0) */ /* state of the tag, containing flags, refcount and usagecount */ //tag狀態,包括flags/refcount和usagecount pg_atomic_uint32 state; //pin-count等待進程ID int wait_backend_pid; /* backend PID of pin-count waiter */ //空閑鏈表鏈中下一個空閑的buffer int freeNext; /* link in freelist chain */ //緩沖區內容鎖 LWLock content_lock; /* to lock access to buffer contents */ } BufferDesc;
BufferTag
Buffer tag標記了buffer存儲的是磁盤中哪個block
/* * Buffer tag identifies which disk block the buffer contains. * Buffer tag標記了buffer存儲的是磁盤中哪個block * * Note: the BufferTag data must be sufficient to determine where to write the * block, without reference to pg_class or pg_tablespace entries. It's * possible that the backend flushing the buffer doesn't even believe the * relation is visible yet (its xact may have started before the xact that * created the rel). The storage manager must be able to cope anyway. * 注意:BufferTag必須足以確定如何寫block而不需要參照pg_class或者pg_tablespace數據字典信息. * 有可能后臺進程在刷新緩沖區的時候深圳不相信關系是可見的(事務可能在創建rel的事務之前). * 存儲管理器必須可以處理這些事情. * * Note: if there's any pad bytes in the struct, INIT_BUFFERTAG will have * to be fixed to zero them, since this struct is used as a hash key. * 注意:如果在結構體中有填充的字節,INIT_BUFFERTAG必須將它們固定為零,因為這個結構體用作散列鍵. */ typedef struct buftag { //物理relation標識符 RelFileNode rnode; /* physical relation identifier */ ForkNumber forkNum; //相對于relation起始的塊號 BlockNumber blockNum; /* blknum relative to begin of reln */ } BufferTag;
ReadBuffer_common函數是所有ReadBuffer相關的通用邏輯,其實現邏輯如下:
1.初始化相關變量和執行相關判斷(是否擴展isExtend?是否臨時表isLocalBuf?)
2.如為臨時表,則調用LocalBufferAlloc獲取描述符;否則調用BufferAlloc獲取描述符;
同時,設置是否在緩存命中的標記(變量found)
3.如在緩存中命中
3.1如非擴展buffer,更新統計信息,如有需要,鎖定buffer并返回
3.2如為擴展buffer,則獲取block
3.2.1如PageIsNew返回F,則報錯
3.2.2如為本地buffer(臨時表),則調整標記
3.2.3如非本地buffer,則清除BM_VALID標記
4.沒有在緩存中命中,則獲取block
4.1如為擴展buffer,通過填充0初始化buffer,調用smgrextend擴展
4.2如為普通buffer
4.2.1如模式為RBM_ZERO_AND_LOCK/RBM_ZERO_AND_CLEANUP_LOCK,填充0
4.2.2否則,通過smgr(存儲管理器)讀取block,如需要,則跟蹤I/O時間,同時檢查垃圾數據
5.已擴展了buffer或者已讀取了block
5.1如需要,鎖定buffer
5.2如為臨時表,則調整標記;否則設置BM_VALID,中斷IO,喚醒等待的進程
5.3更新統計信息
5.4返回buffer
/* * ReadBuffer_common -- common logic for all ReadBuffer variants * ReadBuffer_common -- 所有ReadBuffer相關的通用邏輯 * * *hit is set to true if the request was satisfied from shared buffer cache. * *hit設置為T,如shared buffer中已存在此buffer */ static Buffer ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy, bool *hit) { BufferDesc *bufHdr;//buffer描述符 Block bufBlock;//相應的block bool found;//是否命中? bool isExtend;//擴展? bool isLocalBuf = SmgrIsTemp(smgr);//本地buffer? *hit = false; /* Make sure we will have room to remember the buffer pin */ //確保有空間存儲buffer pin ResourceOwnerEnlargeBuffers(CurrentResourceOwner); //如為P_NEW,則需擴展 isExtend = (blockNum == P_NEW); //跟蹤 TRACE_POSTGRESQL_BUFFER_READ_START(forkNum, blockNum, smgr->smgr_rnode.node.spcNode, smgr->smgr_rnode.node.dbNode, smgr->smgr_rnode.node.relNode, smgr->smgr_rnode.backend, isExtend); /* Substitute proper block number if caller asked for P_NEW */ //如調用方要求P_NEW,則替換適當的塊號 if (isExtend) blockNum = smgrnblocks(smgr, forkNum); if (isLocalBuf) { //本地buffer(臨時表) bufHdr = LocalBufferAlloc(smgr, forkNum, blockNum, &found); if (found) pgBufferUsage.local_blks_hit++; else if (isExtend) pgBufferUsage.local_blks_written++; else if (mode == RBM_NORMAL || mode == RBM_NORMAL_NO_LOG || mode == RBM_ZERO_ON_ERROR) pgBufferUsage.local_blks_read++; } else { //非臨時表 /* * lookup the buffer. IO_IN_PROGRESS is set if the requested block is * not currently in memory. * 搜索buffer. * 如請求的block不在內存中,則IO_IN_PROGRESS設置為T */ //獲取buffer描述符 bufHdr = BufferAlloc(smgr, relpersistence, forkNum, blockNum, strategy, &found); if (found) //在內存中命中 pgBufferUsage.shared_blks_hit++; else if (isExtend) //新的buffer pgBufferUsage.shared_blks_written++; else if (mode == RBM_NORMAL || mode == RBM_NORMAL_NO_LOG || mode == RBM_ZERO_ON_ERROR) //讀取block pgBufferUsage.shared_blks_read++; } /* At this point we do NOT hold any locks. */ //這時候,我們還沒有持有任何鎖. /* if it was already in the buffer pool, we're done */ //---------- 如果buffer已在換沖池中,工作已完成 if (found) { //------------- buffer已在緩沖池中 //已在換沖池中 if (!isExtend) { //非擴展buffer /* Just need to update stats before we exit */ //在退出前,更新統計信息 *hit = true; VacuumPageHit++; if (VacuumCostActive) VacuumCostBalance += VacuumCostPageHit; TRACE_POSTGRESQL_BUFFER_READ_DONE(forkNum, blockNum, smgr->smgr_rnode.node.spcNode, smgr->smgr_rnode.node.dbNode, smgr->smgr_rnode.node.relNode, smgr->smgr_rnode.backend, isExtend, found); /* * In RBM_ZERO_AND_LOCK mode the caller expects the page to be * locked on return. * RBM_ZERO_AND_LOCK模式,調用者期望page鎖定后才返回 */ if (!isLocalBuf) { //非臨時表buffer if (mode == RBM_ZERO_AND_LOCK) LWLockAcquire(BufferDescriptorGetContentLock(bufHdr), LW_EXCLUSIVE); else if (mode == RBM_ZERO_AND_CLEANUP_LOCK) LockBufferForCleanup(BufferDescriptorGetBuffer(bufHdr)); } //根據buffer描述符讀取buffer并返回buffer //#define BufferDescriptorGetBuffer(bdesc) ((bdesc)->buf_id + 1) return BufferDescriptorGetBuffer(bufHdr); } /* * We get here only in the corner case where we are trying to extend * the relation but we found a pre-existing buffer marked BM_VALID. * This can happen because mdread doesn't complain about reads beyond * EOF (when zero_damaged_pages is ON) and so a previous attempt to * read a block beyond EOF could have left a "valid" zero-filled * buffer. Unfortunately, we have also seen this case occurring * because of buggy Linux kernels that sometimes return an * lseek(SEEK_END) result that doesn't account for a recent write. In * that situation, the pre-existing buffer would contain valid data * that we don't want to overwrite. Since the legitimate case should * always have left a zero-filled buffer, complain if not PageIsNew. * 程序執行來到這里,進程嘗試擴展relation但發現了先前已存在的標記為BM_VALID的buffer. * 這種情況之所以發生是因為mdread對于在EOF之后的讀不會報錯(zero_damaged_pages設置為ON), * 并且先前嘗試讀取EOF的block遺留了"valid"的已初始化(填充0)的buffer. * 不幸的是,我們同樣發現因為Linux內核的bug(有時候會返回lseek/SEEK_END結果)導致這種情況. * 在這種情況下,先前已存在的buffer會存儲有效的數據,這些數據不希望被覆蓋. * 由于合法的情況下應該總是留下一個零填充的緩沖區,如果不是PageIsNew,則報錯。 */ //獲取block bufBlock = isLocalBuf ? LocalBufHdrGetBlock(bufHdr) : BufHdrGetBlock(bufHdr); if (!PageIsNew((Page) bufBlock)) //不是PageIsNew,則報錯 ereport(ERROR, (errmsg("unexpected data beyond EOF in block %u of relation %s", blockNum, relpath(smgr->smgr_rnode, forkNum)), errhint("This has been seen to occur with buggy kernels; consider updating your system."))); /* * We *must* do smgrextend before succeeding, else the page will not * be reserved by the kernel, and the next P_NEW call will decide to * return the same page. Clear the BM_VALID bit, do the StartBufferIO * call that BufferAlloc didn't, and proceed. * 在成功執行前,必須執行smgrextend,否則的話page不能被內核保留, * 同時下一個P_NEW調用會確定返回同樣的page. * 清除BM_VALID位,執行BufferAlloc沒有執行的StartBufferIO調用,然后繼續。 */ if (isLocalBuf) { //臨時表 /* Only need to adjust flags */ //只需要調整標記 uint32 buf_state = pg_atomic_read_u32(&bufHdr->state); Assert(buf_state & BM_VALID); buf_state &= ~BM_VALID; pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state); } else { //非臨時表 /* * Loop to handle the very small possibility that someone re-sets * BM_VALID between our clearing it and StartBufferIO inspecting * it. * 循環,直至StartBufferIO返回T為止 */ do { uint32 buf_state = LockBufHdr(bufHdr); Assert(buf_state & BM_VALID); //清除BM_VALID標記 buf_state &= ~BM_VALID; UnlockBufHdr(bufHdr, buf_state); } while (!StartBufferIO(bufHdr, true)); } } //------------- buffer不在緩沖池中 /* * if we have gotten to this point, we have allocated a buffer for the * page but its contents are not yet valid. IO_IN_PROGRESS is set for it, * if it's a shared buffer. * 如果到了這個份上,我們已經為page分配了buffer,但其中的內容還沒有生效. * 如果是共享內存,那么設置IO_IN_PROGRESS標記. * * Note: if smgrextend fails, we will end up with a buffer that is * allocated but not marked BM_VALID. P_NEW will still select the same * block number (because the relation didn't get any longer on disk) and * so future attempts to extend the relation will find the same buffer (if * it's not been recycled) but come right back here to try smgrextend * again. * 注意:如果smgrextend失敗,我們將以一個已分配但為設置為BM_VALID的buffer結束這次調用 */ //驗證 Assert(!(pg_atomic_read_u32(&bufHdr->state) & BM_VALID)); /* spinlock not needed */ //獲取block bufBlock = isLocalBuf ? LocalBufHdrGetBlock(bufHdr) : BufHdrGetBlock(bufHdr); if (isExtend) { //-------- 擴展block /* new buffers are zero-filled */ //新buffers使用0填充 MemSet((char *) bufBlock, 0, BLCKSZ); /* don't set checksum for all-zero page */ //對于使用全0填充的page,不要設置checksum smgrextend(smgr, forkNum, blockNum, (char *) bufBlock, false); /* * NB: we're *not* doing a ScheduleBufferTagForWriteback here; * although we're essentially performing a write. At least on linux * doing so defeats the 'delayed allocation' mechanism, leading to * increased file fragmentation. * 注意:這里我們不會執行ScheduleBufferTagForWriteback.雖然我們實質上正在執行寫操作. * 起碼,在Linux平臺,執行這個操作會破壞“延遲分配”機制,導致文件碎片. */ } else { //-------- 普通block /* * Read in the page, unless the caller intends to overwrite it and * just wants us to allocate a buffer. * 讀取page,除非調用者期望覆蓋它并且希望我們分配buffer. * */ if (mode == RBM_ZERO_AND_LOCK || mode == RBM_ZERO_AND_CLEANUP_LOCK) //如為RBM_ZERO_AND_LOCK或者RBM_ZERO_AND_CLEANUP_LOCK模式,初始化為0 MemSet((char *) bufBlock, 0, BLCKSZ); else { //其他模式 instr_time io_start,//io的起止時間 io_time; if (track_io_timing) INSTR_TIME_SET_CURRENT(io_start); //smgr(存儲管理器)讀取block smgrread(smgr, forkNum, blockNum, (char *) bufBlock); if (track_io_timing) { //需要跟蹤io時間 INSTR_TIME_SET_CURRENT(io_time); INSTR_TIME_SUBTRACT(io_time, io_start); pgstat_count_buffer_read_time(INSTR_TIME_GET_MICROSEC(io_time)); INSTR_TIME_ADD(pgBufferUsage.blk_read_time, io_time); } /* check for garbage data */ //檢查垃圾數據 if (!PageIsVerified((Page) bufBlock, blockNum)) { //如果page為通過驗證 if (mode == RBM_ZERO_ON_ERROR || zero_damaged_pages) { //出錯,則初始化 ereport(WARNING, (errcode(ERRCODE_DATA_CORRUPTED), errmsg("invalid page in block %u of relation %s; zeroing out page", blockNum, relpath(smgr->smgr_rnode, forkNum)))); //初始化 MemSet((char *) bufBlock, 0, BLCKSZ); } else //出錯,報錯 ereport(ERROR, (errcode(ERRCODE_DATA_CORRUPTED), errmsg("invalid page in block %u of relation %s", blockNum, relpath(smgr->smgr_rnode, forkNum)))); } } } //--------- 已擴展了buffer或者已讀取了block /* * In RBM_ZERO_AND_LOCK mode, grab the buffer content lock before marking * the page as valid, to make sure that no other backend sees the zeroed * page before the caller has had a chance to initialize it. * 在RBM_ZERO_AND_LOCK模式下,在標記page為有效之前獲取buffer content lock, * 確保在調用者初始化之前沒有其他進程看到已初始化為0的page * * Since no-one else can be looking at the page contents yet, there is no * difference between an exclusive lock and a cleanup-strength lock. (Note * that we cannot use LockBuffer() or LockBufferForCleanup() here, because * they assert that the buffer is already valid.) * 由于沒有其他進程可以搜索page內容,因此獲取獨占鎖和cleanup-strength鎖沒有區別. * (注意不能在這里使用LockBuffer()或者LockBufferForCleanup(),因為這些函數假定buffer有效) */ if ((mode == RBM_ZERO_AND_LOCK || mode == RBM_ZERO_AND_CLEANUP_LOCK) && !isLocalBuf) { //鎖定 LWLockAcquire(BufferDescriptorGetContentLock(bufHdr), LW_EXCLUSIVE); } if (isLocalBuf) { //臨時表 /* Only need to adjust flags */ //只需要調整標記 uint32 buf_state = pg_atomic_read_u32(&bufHdr->state); buf_state |= BM_VALID; pg_atomic_unlocked_write_u32(&bufHdr->state, buf_state); } else { //普通表 /* Set BM_VALID, terminate IO, and wake up any waiters */ //設置BM_VALID,中斷IO,喚醒等待的進程 TerminateBufferIO(bufHdr, false, BM_VALID); } //更新統計信息 VacuumPageMiss++; if (VacuumCostActive) VacuumCostBalance += VacuumCostPageMiss; //跟蹤 TRACE_POSTGRESQL_BUFFER_READ_DONE(forkNum, blockNum, smgr->smgr_rnode.node.spcNode, smgr->smgr_rnode.node.dbNode, smgr->smgr_rnode.node.relNode, smgr->smgr_rnode.backend, isExtend, found); //返回buffer //#define BufferDescriptorGetBuffer(bdesc) ((bdesc)->buf_id + 1) return BufferDescriptorGetBuffer(bufHdr); }
測試場景一:Block不在緩沖區中
腳本:
16:42:48 (xdb@[local]:5432)testdb=# select * from t1 limit 10;
啟動gdb,設置斷點
(gdb) b ReadBuffer_common Breakpoint 1 at 0x876e28: file bufmgr.c, line 711. (gdb) c Continuing. Breakpoint 1, ReadBuffer_common (smgr=0x2b7cce0, relpersistence=112 'p', forkNum=MAIN_FORKNUM, blockNum=0, mode=RBM_NORMAL, strategy=0x0, hit=0x7ffc7761dfab) at bufmgr.c:711 711 bool isLocalBuf = SmgrIsTemp(smgr); (gdb)
1.初始化相關變量和執行相關判斷(是否擴展isExtend?是否臨時表isLocalBuf?)
(gdb) n 713 *hit = false; (gdb) 716 ResourceOwnerEnlargeBuffers(CurrentResourceOwner); (gdb) 718 isExtend = (blockNum == P_NEW); (gdb) 720 TRACE_POSTGRESQL_BUFFER_READ_START(forkNum, blockNum, (gdb) 728 if (isExtend) (gdb) 731 if (isLocalBuf) (gdb) 745 bufHdr = BufferAlloc(smgr, relpersistence, forkNum, blockNum, (gdb)
2.調用BufferAlloc獲取buffer描述符
(gdb) 747 if (found) (gdb) p *bufHdr $1 = {tag = {rnode = {spcNode = 1663, dbNode = 16402, relNode = 51439}, forkNum = MAIN_FORKNUM, blockNum = 0}, buf_id = 108, state = {value = 2248409089}, wait_backend_pid = 0, freeNext = -2, content_lock = {tranche = 54, state = { value = 536870912}, waiters = {head = 2147483647, tail = 2147483647}}} (gdb) p found $2 = false (gdb) (gdb) n 750 pgBufferUsage.shared_blks_read++; --> 更新統計信息 (gdb)
4.沒有在緩存中命中,則獲取block
756 if (found) (gdb) 856 Assert(!(pg_atomic_read_u32(&bufHdr->state) & BM_VALID)); /* spinlock not needed */ (gdb) 858 bufBlock = isLocalBuf ? LocalBufHdrGetBlock(bufHdr) : BufHdrGetBlock(bufHdr); (gdb) 860 if (isExtend) (gdb) p bufBlock $4 = (Block) 0x7fe8c240e380
4.2如為普通buffer
4.2.1如模式為RBM_ZERO_AND_LOCK/RBM_ZERO_AND_CLEANUP_LOCK,填充0
4.2.2否則,通過smgr(存儲管理器)讀取block,如需要,則跟蹤I/O時間,同時檢查垃圾數據
(gdb) p mode $5 = RBM_NORMAL (gdb) (gdb) n 880 if (mode == RBM_ZERO_AND_LOCK || mode == RBM_ZERO_AND_CLEANUP_LOCK) (gdb) 887 if (track_io_timing) (gdb) 890 smgrread(smgr, forkNum, blockNum, (char *) bufBlock); (gdb) 892 if (track_io_timing) (gdb) p *smgr $6 = {smgr_rnode = {node = {spcNode = 1663, dbNode = 16402, relNode = 51439}, backend = -1}, smgr_owner = 0x7fe8ee2bc7b8, smgr_targblock = 4294967295, smgr_fsm_nblocks = 4294967295, smgr_vm_nblocks = 4294967295, smgr_which = 0, md_num_open_segs = {1, 0, 0, 0}, md_seg_fds = {0x2b0dd78, 0x0, 0x0, 0x0}, next_unowned_reln = 0x0} (gdb) p forkNum $7 = MAIN_FORKNUM (gdb) p blockNum $8 = 0 (gdb) p (char *) bufBlock $9 = 0x7fe8c240e380 "\001" (gdb)
5.已擴展了buffer或者已讀取了block
5.1如需要,鎖定buffer
5.2如為臨時表,則調整標記;否則設置BM_VALID,中斷IO,喚醒等待的進程
(gdb) n 901 if (!PageIsVerified((Page) bufBlock, blockNum)) (gdb) 932 if ((mode == RBM_ZERO_AND_LOCK || mode == RBM_ZERO_AND_CLEANUP_LOCK) && (gdb) n 938 if (isLocalBuf) (gdb) 949 TerminateBufferIO(bufHdr, false, BM_VALID); (gdb)
5.3更新統計信息
5.4返回buffer
(gdb) 952 VacuumPageMiss++; (gdb) 953 if (VacuumCostActive) (gdb) 956 TRACE_POSTGRESQL_BUFFER_READ_DONE(forkNum, blockNum, (gdb) 964 return BufferDescriptorGetBuffer(bufHdr); (gdb) 965 } (gdb)
buf為109
(gdb) ReadBufferExtended (reln=0x7fe8ee2bc7a8, forkNum=MAIN_FORKNUM, blockNum=0, mode=RBM_NORMAL, strategy=0x0) at bufmgr.c:666 666 if (hit) (gdb) 668 return buf; (gdb) p buf $10 = 109 (gdb)
測試場景二:Block已在緩沖區中
再次執行上面的SQL語句,這時候相應的block已讀入到buffer中
(gdb) del Delete all breakpoints? (y or n) y (gdb) c Continuing. ^C Program received signal SIGINT, Interrupt. 0x00007fe8ec448903 in __epoll_wait_nocancel () at ../sysdeps/unix/syscall-template.S:81 81 T_PSEUDO (SYSCALL_SYMBOL, SYSCALL_NAME, SYSCALL_NARGS) (gdb) b ReadBuffer_common Breakpoint 2 at 0x876e28: file bufmgr.c, line 711. (gdb)
found變量為T
... (gdb) 745 bufHdr = BufferAlloc(smgr, relpersistence, forkNum, blockNum, (gdb) 747 if (found) (gdb) p found $11 = true (gdb) (gdb) n 748 pgBufferUsage.shared_blks_hit++; (gdb)
進入相應的邏輯
3.如在緩存中命中
3.1如非擴展buffer,更新統計信息,如有需要,鎖定buffer并返回
3.2如為擴展buffer,則獲取block
3.2.1如PageIsNew返回F,則報錯
3.2.2如為本地buffer(臨時表),則調整標記
3.2.3如非本地buffer,則清除BM_VALID標記
(gdb) 756 if (found) (gdb) 758 if (!isExtend) (gdb) 761 *hit = true; (gdb) 762 VacuumPageHit++; (gdb) 764 if (VacuumCostActive) (gdb) 767 TRACE_POSTGRESQL_BUFFER_READ_DONE(forkNum, blockNum, (gdb) 779 if (!isLocalBuf) (gdb) 781 if (mode == RBM_ZERO_AND_LOCK) (gdb) 784 else if (mode == RBM_ZERO_AND_CLEANUP_LOCK) (gdb) 788 return BufferDescriptorGetBuffer(bufHdr); (gdb) 965 } (gdb)
到此,關于“PostgreSQL中ReadBuffer_common函數有什么作用”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注創新互聯網站,小編會繼續努力為大家帶來更多實用的文章!
分享標題:PostgreSQL中ReadBuffer_common函數有什么作用
文章URL:http://vcdvsql.cn/article0/phopio.html
成都網站建設公司_創新互聯,為您提供面包屑導航、靜態網站、企業建站、關鍵詞優化、網站改版、網站設計
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯