Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of BufferAlloc function in PostgreSQL

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

< newPartitionLock) { //按顺序锁定 LWLockAcquire(oldPartitionLock, LW_EXCLUSIVE); LWLockAcquire(newPartitionLock, LW_EXCLUSIVE); } else if (oldPartitionLock >

< 0) 4.1释放newPartitionLock 4.2执行循环,寻找合适的buffer 4.2.1确保在自旋锁尚未持有时,有一个空闲的refcount入口(条目) -->

ReservePrivateRefCountEntry

(gdb) n1056 LWLockRelease (newPartitionLock); (gdb) 1065 ReservePrivateRefCountEntry (); (gdb)

4.2.2 Select a buffer to be eliminated

(gdb) n1071 buf = StrategyGetBuffer (strategy, & buf_state); (gdb) n1073 Assert (BUF_STATE_GET_REFCOUNT (buf_state) = = 0) (gdb) p buf$9 = (BufferDesc *) 0x7f85e705fd80 (gdb) p * buf$10 = {tag = {rnode = {spcNode = 0, dbNode = 0, relNode = 0}, forkNum = InvalidForkNumber, blockNum = 4294967295}, buf_id = 104, state = {value = 4194304}, wait_backend_pid = 0, freeNext =-2, content_lock = {tranche = 54, state = {value = 536870912}, waiters = {head = 2147483647, tail = 2147483647}} (gdb)

4.2.3 copy buffer flags into oldFlags

(gdb) n1076 oldFlags = buf_state & BUF_FLAG_MASK; (gdb)

4.2.4Pin buffer, then release the buffer spin lock

(gdb) 1079 PinBuffer_Locked (buf); (gdb)

4.2.5 such as buffer tag bit BM_DIRTY,FlushBuffer

1088 if (oldFlags & BM_DIRTY) (gdb)

4.2.6 if buffer is marked as BM_TAG_VALID, calculate the hashcode and partition lock ID of the original tag, and lock the new and old partition locks

Otherwise, you need a new partition, lock the new partition lock, reset the original partition lock and the original hash value

(gdb) 1166 if (oldFlags & BM_TAG_VALID) (gdb) 1200 LWLockAcquire (newPartitionLock, LW_EXCLUSIVE); (gdb) 1202 oldPartitionLock = NULL; (gdb) 1204 oldHash = 0; (gdb) p oldFlags$11 = 4194304 (gdb)

4.2.7 try to construct an hash table entry using buffer's new tag

(gdb) 1214 buf_id = BufTableInsert (& newTag, newHash, buf- > buf_id); (gdb) n1216 if (buf_id > = 0) (gdb) p buf_id$12 =-1 (gdb)

4.2.9 No conflicts exist (buf_id

< 0),锁定buffer header,如缓冲区没有变脏或者被pinned,则已找到buf,跳出循环 否则,解锁buffer header,删除hash表入口,释放锁,重新寻找buffer (gdb) n1267 buf_state = LockBufHdr(buf);(gdb) 1275 oldFlags = buf_state & BUF_FLAG_MASK;(gdb) 1276 if (BUF_STATE_GET_REFCOUNT(buf_state) == 1 && !(oldFlags & BM_DIRTY))(gdb) 1277 break;(gdb) 4.3可以重新设置buffer tag,完成后解锁buffer header,删除原有的hash表入口,释放分区锁 1301 buf->

Tag = newTag; (gdb) 1302 buf_state & = ~ (BM_VALID | BM_DIRTY | BM_JUST_DIRTIED | (gdb) 1305 if (relpersistence = = RELPERSISTENCE_PERMANENT | | forkNum = = INIT_FORKNUM) (gdb) 1306 buf_state | = BM_TAG_VALID | BM_PERMANENT | BUF_USAGECOUNT_ONE; (gdb) 1310 UnlockBufHdr (buf, buf_state); (gdb) 1312 if (oldPartitionLock! = NULL) (gdb) 1319 LWLockRelease (newPartitionLock) (gdb) p * buf$13 = {tag = {rnode = {spcNode = 1663, dbNode = 16402, relNode = 51439}, forkNum = MAIN_FORKNUM, blockNum = 0}, buf_id = 104, state = {value = 2181300225}, wait_backend_pid = 0, freeNext =-2, content_lock = {tranche = 54, state = {value = 536870912}, waiters = {head = 2147483647, tail = 2147483647}} (gdb)

4.4 execute StartBufferIO and set the * foundPtr flag

(gdb) 1326 if (StartBufferIO (buf, true)) (gdb) n1327 * foundPtr = false; (gdb)

4.5 returns buf

(gdb) 1331 return buf; (gdb) 1332} (gdb)

Execution completed

(gdb) ReadBuffer_common (smgr=0x2267430, relpersistence=112 'packs, forkNum=MAIN_FORKNUM, blockNum=0, mode=RBM_NORMAL, strategy=0x0, hit=0x7ffcc97fb5eb) at bufmgr.c:747747 if (found) (gdb) 750pgBufferUsage.shared_blks_read++; (gdb) at this point, the study on "what is the role of BufferAlloc functions in PostgreSQL" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report