Postgresql 中文操作指南

F.30. pg_prewarm — preload relation data into buffer caches #

pg_prewarm 模块提供了一种便捷的方法,可以将关系数据加载到操作系统缓冲区高速缓存或 PostgreSQL 缓冲区高速缓存中。预热可以使用 pg_prewarm 函数手动执行,也可以通过在 shared_preload_libraries 中包含 pg_prewarm 自动执行。在后一种情况下,系统将运行一个后台工作程序,该工作程序会定期将共享缓冲区的内容记录到名为 autoprewarm.blocks 的文件中,并将使用 2 个后台工作程序在重新启动后重新加载这些相同的块。

The pg_prewarm module provides a convenient way to load relation data into either the operating system buffer cache or the PostgreSQL buffer cache. Prewarming can be performed manually using the pg_prewarm function, or can be performed automatically by including pg_prewarm in shared_preload_libraries. In the latter case, the system will run a background worker which periodically records the contents of shared buffers in a file called autoprewarm.blocks and will, using 2 background workers, reload those same blocks after a restart.

F.30.1. Functions #

pg_prewarm(regclass, mode text default 'buffer', fork text default 'main',
           first_block int8 default null,
           last_block int8 default null) RETURNS int8

第一个参数是要预热的表。第二个参数是要使用的预热方法,如下文进一步讨论;第三个是要预热的表叉,通常是 main。第四个参数是要预热的第一个块号(NULL 被接受为零的同义词)。第五个参数是要预热的最后一个块号(NULL 表示预热到表中的最后一个块)。返回值是预热的块数。

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, usually main. 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.

有三种预热的可用方法。如果系统支持,_prefetch_会向操作系统发出异步预取请求,否则会引发错误。_read_读取请求的块范围;这与_prefetch_不同,它是同步的,并且在所有平台和版本上都受支持,但可能更慢。_buffer_会将请求的块的范围读入数据库缓冲高速缓存。

There are three available prewarming methods. prefetch issues asynchronous prefetch requests to the operating system, if this is supported, or throws an error otherwise. read reads the requested range of blocks; unlike prefetch, 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.

请注意,对于这些方法中的任何一个,尝试预热比可以使用_prefetch_或_read_(使用操作系统时)或可以使用_buffer_(使用PostgreSQL时)缓存更多的块可能会导致读取较高编号块时驱逐较低编号块。预热的数​​据不会享受针对缓存驱逐的特殊保护,因此其他系统活动可能在读取新预热的块后不久就会将它们驱逐;相反,预热还可能会将其他数​​据从高速缓存中驱逐。因此,预热通常在启动时最有用,此时高速缓存基本上是空的。

Note that with any of these methods, attempting to prewarm more blocks than can be cached — by the OS when using prefetch or read, or by PostgreSQL when using buffer — will likely result in lower-numbered blocks being evicted as higher numbered blocks are read in. Prewarmed data also enjoys no special protection from cache evictions, so it is possible that 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, when caches are largely empty.

autoprewarm_start_worker() RETURNS void

启动主自动预热工作程序。这通常会自动发生,但如果在服务器启动时未配置自动预热,且您希望稍后启动该工作程序,则此操作非常有用。

Launch the main autoprewarm worker. This will normally happen automatically, but is useful if automatic prewarm was not configured at server startup time and you wish to start up the worker at a later time.

autoprewarm_dump_now() RETURNS int8

立即更新_autoprewarm.blocks_。在自动预热工作程序未运行,但您预计在下次重启后运行该工作程序的情况下,此方法可能很有用。返回值是写到_autoprewarm.blocks_ 的记录数。

Update autoprewarm.blocks immediately. This may be useful if the autoprewarm worker is not running but you anticipate running it after the next restart. The return value is the number of records written to autoprewarm.blocks.

F.30.2. Configuration Parameters #

  • pg_prewarm.autoprewarm (boolean)

    • Controls whether the server should run the autoprewarm worker. This is on by default. This parameter can only be set at server start.

  • pg_prewarm.autoprewarm_interval (integer)

    • This is the interval between updates to autoprewarm.blocks. The default is 300 seconds. If set to 0, the file will not be dumped at regular intervals, but only when the server is shut down.

这些参数必须在 postgresql.conf 中设置。典型用法可能是:

These parameters must be set in postgresql.conf. Typical usage might be:

# postgresql.conf
shared_preload_libraries = 'pg_prewarm'

pg_prewarm.autoprewarm = true
pg_prewarm.autoprewarm_interval = 300s