pg_prewarm模块可以方便的把相关的数据加载到系统的缓存或者是数据库的缓存中。在数据量大的情况下,内存中的数据可能会排出内存。
适合数据量比较小的表,另外数据很少进行更改的表。
安装步骤:
cd contrib/pg_prewarm
make
make install
pg_prewarm.so 将安装到lib目录下。
pg_prewarm--1.0.sql和pg_prewarm.control文件到share/extension/目录下
psql执行创建扩展
create extension pg_prewarm;
把需要缓存的表加入到buffer中
select pg_prewarm('department_info','buffer');
shared_buffers设置稍微大一点,把表的数据都加载到buffer中。
<pre>
F.27. pg_prewarm
The pg_prewarm module provides a convenient way to load relation data into either the operating system buffer cache or the PostgreSQL buffer cache.
F.27.1. Functions
<b>
pg_prewarm(regclass, mode text default 'buffer', fork text default 'main', first_block int8 default null, last_block int8 default null) RETURNS int8
</b>
The first argument is the relation to be prewarmed. The second argument is the prewarming method to be used, as further discussed below; the third is the relation fork to be prewarmed, usuallymain. The fourth argument is the first block number to prewarm (NULL is accepted as a synonym for zero). The fifth argument is the last block number to prewarm (NULL means prewarm through the last block in the relation). The return value is the number of blocks prewarmed.
There are three available prewarming methods.prefetch issues asynchronous prefetch requests to the operating system, if this is supported, or throws an error therwise.read reads the requested range of blocks; unlikeprefetch, this is synchronous and supported on all platforms and builds, but may be slower.buffer reads the requested range of blocks into the database buffer cache.
Note that with any of these methods, attempting to prewarm more blocks than can be cached — by the OS when singprefetch orread, or by PostgreSQL when usingbuffer— will likely result in lower-numbered blocks being evicted as igher numbered blocks are read in. Prewarmed data also enjoys no special protection from cache evictions, so it is ossible for other system activity may evict the newly prewarmed blocks shortly after they are read; conversely, prewarming may also evict other data from cache. For these reasons, prewarming is typically most useful at startup, hen caches are largely empty.
</pre>