From 1d257577e08d3e598011d6850fd1025858de8c8c Mon Sep 17 00:00:00 2001 From: Thomas Munro Date: Thu, 8 Apr 2021 23:03:43 +1200 Subject: Optionally prefetch referenced data in recovery. Introduce a new GUC recovery_prefetch, disabled by default. When enabled, look ahead in the WAL and try to initiate asynchronous reading of referenced data blocks that are not yet cached in our buffer pool. For now, this is done with posix_fadvise(), which has several caveats. Better mechanisms will follow in later work on the I/O subsystem. The GUC maintenance_io_concurrency is used to limit the number of concurrent I/Os we allow ourselves to initiate, based on pessimistic heuristics used to infer that I/Os have begun and completed. The GUC wal_decode_buffer_size is used to limit the maximum distance we are prepared to read ahead in the WAL to find uncached blocks. Reviewed-by: Alvaro Herrera (parts) Reviewed-by: Andres Freund (parts) Reviewed-by: Tomas Vondra (parts) Tested-by: Tomas Vondra Tested-by: Jakub Wartak Tested-by: Dmitry Dolgov <9erthalion6@gmail.com> Tested-by: Sait Talha Nisanci Discussion: https://postgr.es/m/CA%2BhUKGJ4VJN8ttxScUFM8dOKX0BrBiboo5uz1cq%3DAovOddfHpA%40mail.gmail.com --- doc/src/sgml/config.sgml | 83 ++++++++++++++++++++++++++++++++++++++++++ doc/src/sgml/monitoring.sgml | 86 ++++++++++++++++++++++++++++++++++++++++++-- doc/src/sgml/wal.sgml | 17 +++++++++ 3 files changed, 184 insertions(+), 2 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index ea5cf3a2dc0..26628f3e6d3 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -3565,6 +3565,89 @@ include_dir 'conf.d' + + + Recovery + + + configuration + of recovery + general settings + + + + This section describes the settings that apply to recovery in general, + affecting crash recovery, streaming replication and archive-based + replication. + + + + + + recovery_prefetch (boolean) + + recovery_prefetch configuration parameter + + + + + Whether to try to prefetch blocks that are referenced in the WAL that + are not yet in the buffer pool, during recovery. Prefetching blocks + that will soon be needed can reduce I/O wait times in some workloads. + See also the and + settings, which limit + prefetching activity. + This setting is disabled by default. + + + This feature currently depends on an effective + posix_fadvise function, which some + operating systems lack. + + + + + + recovery_prefetch_fpw (boolean) + + recovery_prefetch_fpw configuration parameter + + + + + Whether to prefetch blocks that were logged with full page images, + during recovery. Often this doesn't help, since such blocks will not + be read the first time they are needed and might remain in the buffer + pool after that. However, on file systems with a block size larger + than + PostgreSQL's, prefetching can avoid a + costly read-before-write when a blocks are later written. + The default is off. + + + + + + wal_decode_buffer_size (integer) + + wal_decode_buffer_size configuration parameter + + + + + A limit on how far ahead the server can look in the WAL, to find + blocks to prefetch. Setting it too high might be counterproductive, + if it means that data falls out of the + kernel cache before it is needed. If this value is specified without + units, it is taken as bytes. + The default is 512kB. + + + + + + + Archive Recovery diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml index da16c461f08..f637fe04152 100644 --- a/doc/src/sgml/monitoring.sgml +++ b/doc/src/sgml/monitoring.sgml @@ -337,6 +337,13 @@ postgres 27093 0.0 0.0 30096 2752 ? Ss 11:34 0:00 postgres: ser + + pg_stat_prefetch_recoverypg_stat_prefetch_recovery + Only one row, showing statistics about blocks prefetched during recovery. + See for details. + + + pg_stat_subscriptionpg_stat_subscription At least one row per subscription, showing information about @@ -2917,6 +2924,78 @@ SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event i copy of the subscribed tables. + + <structname>pg_stat_prefetch_recovery</structname> View + + + + Column + Type + Description + + + + + + prefetch + bigint + Number of blocks prefetched because they were not in the buffer pool + + + skip_hit + bigint + Number of blocks not prefetched because they were already in the buffer pool + + + skip_new + bigint + Number of blocks not prefetched because they were new (usually relation extension) + + + skip_fpw + bigint + Number of blocks not prefetched because a full page image was included in the WAL and was set to off + + + skip_seq + bigint + Number of blocks not prefetched because of repeated access + + + distance + integer + How far ahead of recovery the prefetcher is currently reading, in bytes + + + queue_depth + integer + How many prefetches have been initiated but are not yet known to have completed + + + avg_distance + float4 + How far ahead of recovery the prefetcher is on average, while recovery is not idle + + + avg_queue_depth + float4 + Average number of prefetches in flight while recovery is not idle + + + +
+ + + The pg_stat_prefetch_recovery view will contain only + one row. It is filled with nulls if recovery is not running or WAL + prefetching is not enabled. See + for more information. The counters in this view are reset whenever the + , + or + setting is changed and + the server configuration is reloaded. + + <structname>pg_stat_subscription</structname> View @@ -5049,8 +5128,11 @@ SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event i all the counters shown in the pg_stat_bgwriter view, archiver to reset all the counters shown in - the pg_stat_archiver view or wal - to reset all the counters shown in the pg_stat_wal view. + the pg_stat_archiver view, + wal to reset all the counters shown in the + pg_stat_wal view or + prefetch_recovery to reset all the counters shown + in the pg_stat_prefetch_recovery view. This function is restricted to superusers by default, but other users diff --git a/doc/src/sgml/wal.sgml b/doc/src/sgml/wal.sgml index 7d48f427109..0f13c43095e 100644 --- a/doc/src/sgml/wal.sgml +++ b/doc/src/sgml/wal.sgml @@ -803,6 +803,23 @@ counted as wal_write and wal_sync in pg_stat_wal, respectively. + + + The parameter can + be used to improve I/O performance during recovery by instructing + PostgreSQL to initiate reads + of disk blocks that will soon be needed but are not currently in + PostgreSQL's buffer pool. + The and + settings limit prefetching + concurrency and distance, respectively. The + prefetching mechanism is most likely to be effective on systems + with full_page_writes set to + off (where that is safe), and where the working + set is larger than RAM. By default, prefetching in recovery is enabled + on operating systems that have posix_fadvise + support. + -- cgit v1.2.3