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

How does PostgreSQL allocate shared cache at startup

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to allocate shared cache when PostgreSQL starts". In daily operation, I believe many people have doubts about how to allocate shared cache when PostgreSQL is started. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to allocate shared cache when PostgreSQL starts". Next, please follow the editor to study!

I believe many people know that the parameter shared_buffers, which sets the size of the shared cache, this article briefly describes how it is allocated.

1. Parameter setting (src/backend/utils/misc/guc.c)

/ * We sometimes multiply the number of shared buffers by two without * checking for overflow, so we mustn't allow more than INT_MAX / 2. * / {{"shared_buffers", PGC_POSTMASTER, RESOURCES_MEM, gettext_noop ("Sets the number of shared memory buffers used by the server."), NULL GUC_UNIT_BLOCKS}, & NBuffers, 1024, 16, INT_MAX / 2, NULL, NULL, NULL}

As you can see, this parameter is not the actual memory size in the kernel, but the number of blocks GUC_UNIT_BLOCKS. (you can read the guc code if you are interested, and it seems necessary to write a separate article. )

Suppose we set 512m and select the block size 8k at compile time, then the number of blocks should be allocated is 5121024102424max 8192, for a total of 65536 blocks.

2. Allocation of shared memory

In fact, the shared cache is only part of the shared memory, which is not the focus of this article, so it's just a simple mention. Calculate the total shared memory when the database is started, and then apply from OS (src/backend/storage/buffer/buf_init.c):

SizeBufferShmemSize (void) {Size size = 0; / * sizeof buffer descriptors * / size = add_size (size, mul_size (NBuffers, sizeof (BufferDescPadded)); / * to allow aligning buffer descriptors * / size = add_size (size, PG_CACHE_LINE_SIZE); / * sizeof data pages * / size = add_size (size, mul_size (NBuffers, BLCKSZ)); Size = add_size (size, mul_size (NBuffers, sizeof (LWLockMinimallyPadded)); / * to allow aligning the above * / size = add_size (size, PG_CACHE_LINE_SIZE); / * sizeof checkpoint sort array in bufmgr.c * / size = add_size (size, mul_size (NBuffers, sizeof (CkptSortItem));

There is also the allocation of block descriptors, locks, and checkpoint sorting areas, which have been changed too much. in order to make better use of cache line, an earlier version of a single structure was split into multiple.

If you are interested, you can compare with the previous code. I won't see this change without writing this article. I don't pay enough attention to community patches.

3. Share cache initialization

After applying for OS shared memory (see PGSharedMemoryCreate, there are multiple platform versions), then ShmemInitStruct the sections seen above, which can be found in src/backend/storage/buffer/buf_init.c:

BufferBlocks = (char *) ShmemInitStruct ("BufferBlocks", NBuffers * (Size) BLCKSZ, & foundBufs)

This is the request for the cache part, and you can see that it allocates 512m (65536mm 8192) from the shared memory.

Then there is initialization, various locks and states.

4. More consideration

You can see from the above code why PG cannot dynamically increase the shared cache because its block description is allocated once and then cannot be changed. Consider the Oracle parameter SGA_MAX_SIZE, which allows dynamic allocation but with this upper limit, it can be inferred that it also seems to have something similar to cache block descriptors, and one-time allocation is reserved for later use. It is not very difficult for PG to implement dynamic management, depending on how shared memory is allocated. You can read PGSharedMemoryCreate to learn more, or you can compare it with previous versions to see how the allocation implementation is different (remember 9.1,9.2).

At this point, the study on "how to allocate the shared cache when PostgreSQL starts" is over. I hope to be able to solve your 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

Internet Technology

Wechat

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

12
Report