Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x65DF-0000xy-2M for pgsql-hackers@arkaria.postgresql.org; Mon, 14 Sep 2026 11:51:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x65DF-00CYR2-0Y for pgsql-hackers@arkaria.postgresql.org; Mon, 14 Sep 2026 11:51:05 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x65DE-00CYQn-2F for pgsql-hackers@lists.postgresql.org; Mon, 14 Sep 2026 11:51:04 +0000 Received: from mail-ej2-x05.google.com ([2a00:1450:4864:34::5]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1x65DC-000000000aq-33yq for pgsql-hackers@lists.postgresql.org; Mon, 14 Sep 2026 11:51:03 +0000 Received: by mail-ej2-x05.google.com with SMTP id a640c23a62f3a-c299e97dc0aso59986566b.1 for ; Mon, 14 Sep 2026 04:51:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=planetscale.com; s=google; t=1789386661; x=1789991461; darn=lists.postgresql.org; h=content-transfer-encoding:content-type:in-reply-to:from:cc :content-language:references:to:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to:content-type; bh=5aVpcznH8ZWz+2XUP38GWOJigkRxZwpk6Hdc9iEUesc=; b=Ex8ABlpCdgZy3q6QsTm1UG/qqe03JgUYK6YevJSLC6U+yDLOB0KnR0dzvbkpPU86EY kqUD4ovfv000WSwRGvWQl3L1YkCXdrXsfAMu9TbRvX8Dg2NfsE6PJkSfUIGBTEIkDpSH KOcS2haZsnhfUIuHiMbENqEGudXVzPZpMBKv3KkQcmnsWk3oGYKkSC43XpoP1MJZ+p68 aFu8E8fWO9mbEvY7c+nFdEukz8XiHj2WAW0rJJqdcxCDVNE8GPtumLbdSD0nVwK3IN4r pOK/WIkgbVAgWGAqW+8Gr7XTTEIQhtyq1D3uqfwN+w+vm8a85Mq7fsKFLS8oev+Do9CK ZvNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789386661; x=1789991461; h=content-transfer-encoding:content-type:in-reply-to:from:cc :content-language:references:to:subject:user-agent:mime-version:date :message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=5aVpcznH8ZWz+2XUP38GWOJigkRxZwpk6Hdc9iEUesc=; b=jCSzorBw1mKcN4dQ5KRB0IkRNMaRnlZnRuduJ+zPR0JOcP3vcAyXCvYQjHtUgAth4E DyceMbrWYcL92KglPs+ID0npl7FrO6ZzB1ZWUzqDRIlL8yjtwrYHS+wJRBRUjEYctgV1 mEi+Qo9JFzq0kojbJLxTo0dZbR2yp5esolbp/hXoPR5HKVWj/gnOEDaQLLx0Ib6gTIHS obUUK61M+U7T//7GoRqc77zBZNGnPzMnRcLML4UrRy5TWExlB4sQZz7XKo8MTxB4vPKD YIPvSOG5SWpKA/KEHqZ2BNMDlbgi9QFkUxXQvNUmzzqk+ylT36Yy8ynQaTFQ9KbK0HTJ vsGQ== X-Gm-Message-State: AFuF++lYSEYDbcOZ5LJYymwcABR//efVZuGVbiJ+eBIe3pPDMBHeigK2 6xWvZCPJ7N/lL/eEl6gbQejKjmTqTLD1iackv18HaOojBuhZoV97ZMkoB8ovu+r+bR2ZCExreAv ytFN2NYcOezFQ X-Gm-Gg: AYBFou0Rd7Th+2GzvjTQAu9NGLIPjy/JyBoPaaDtcPJfe7kaWcDrc9wbUtMhkqg7DXA ecZOrAv3bf0ZHp8GDC/e00WDp61/pxKM7McE9H5raBjMM382bTlRdHLG49ALCOMwlObgJglKwfv Od52C7/rKs6elKTbhWR5IipYhqGd5x32KTJDhyTt6vDWnYB1TDrruvhYp2OU6BFLDdjZAkOS4Jk iDPBS6sye5FmQbc8Od8DN4rtM058kTPATME3zGcs3XgtwVpPEqQA70kiMJhthz7iB3y76sAU+Lh u+/9m/eQaNzDK0LxL/T7PRRq4E8mtdxXeI5a8pIqfsYLm5lyZ0guXwygEJh469ioh+NBqSeLumR As8EEpYBPHMZr0y0wfWdC4A10Hk1NnjLRuMhCpIhNNWkpyBvNIqO5zzMreOSylD6rrMzzMek7go 7i/XecQ2TJf/Of2PbnHDwt6lz5Blx+ZrS8Fouaw4hcbfDR3ZOJHtFtB//i4GwPs2awh83CoZvxz R5NQRh7NbIR5F/y98T9yrNJpJ7YaqA= X-Received: by 2002:a17:907:806:b0:c25:2d27:5652 with SMTP id a640c23a62f3a-c29b86b7570mr249194466b.11.1789386660970; Mon, 14 Sep 2026 04:51:00 -0700 (PDT) Received: from [192.168.1.146] ([139.91.207.69]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-c29660654e4sm408856066b.30.2026.09.14.04.51.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Sep 2026 04:51:00 -0700 (PDT) Message-ID: Date: Mon, 14 Sep 2026 14:50:59 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] Avoid useless prefetches in case of recent FPI WAL records To: Michael Zhilin References: <9bdb428a-fc8e-4801-8fff-1ad0bc6fa76b@postgrespro.ru> Content-Language: en-US Cc: PostgreSQL Hackers From: Markos Fountoulakis In-Reply-To: <9bdb428a-fc8e-4801-8fff-1ad0bc6fa76b@postgrespro.ru> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2025-03-30 00:57 +0200, Michael Zhilin wrote: > Hi, > > One customer complained that physical standby has significant replay lag (few hours) on modern powerful bare metal servers. According to output of "top", startup process and few kernel workers utilized CPU, mostly in kernel space. > > The incident has been resolved by disabling recovery_prefetch after quick analysis of perf and strace outputs. > > Problem description > --------------------- > > Actual XLog prefetching code doesn't consider that shared buffer may be loaded from WAL FPI rather than from disk and makes fadvise calls in such cases. > It results into amount of useless fadvise calls and slowness of WAL apply process. > > The full_page_writes are enabled by default, so after checkpoint any first modification is logged into WAL with full page. The startup process will read full page images from WAL stream and load them into shared buffer by XLogReadBufferForRedoExtended. > > It seems there is no point to prefetch shared buffer from disk if FPI are logged recently. > > Test case > ---------- > > To reproduce case of useless prefetches, please create master instance and physical standby with default shared buffer (128MiB), then insert test data with size a bit more than shared buffers (32818 pages): > >         drop table if exists t1; >         create table t1 (id int, k int, val text ) with (fillfactor = 10, autovacuum_enabled=off); >         insert into t1 (id,k,val) >         select i, 0, repeat('x', 800) from generate_series(1,32*1024 + 50) as i; >         checkpoint; > > Reset stats on standby server: > >         select pg_stat_reset_shared('io'); >         select pg_stat_reset_shared('recovery_prefetch'); > > Update table on master and check stats on standby: > >         checkpoint; >         update t1 set k = k + 1; > >         select object, context,reads, stats_reset from pg_stat_io where backend_type = 'startup'; >         select * from pg_stat_recovery_prefetch ; > > Here is output: > >         postgres=#  select object, context,reads, stats_reset from pg_stat_io where backend_type = 'startup'; >           object  |  context  | reads |          stats_reset > ----------+-----------+-------+------------------------------- >          relation | bulkread  |     0 | 2025-03-29 16:38:45.664648+03 >          relation | bulkwrite |     0 | 2025-03-29 16:38:45.664648+03 >          relation | init      |     0 | 2025-03-29 16:38:45.664648+03 >          relation | normal    |     9 | 2025-03-29 16:38:45.664648+03 >          relation | vacuum    |     0 | 2025-03-29 16:38:45.664648+03 >          wal      | init      |       | 2025-03-29 16:38:45.664648+03 >          wal      | normal    |  8100 | 2025-03-29 16:38:45.664648+03 >         (7 rows) > >         postgres=# select * from pg_stat_recovery_prefetch ; >                   stats_reset          | prefetch | hit | skip_init | skip_new | skip_fpw | skip_rep | wal_distance | block_distance | io_depth > -------------------------------+----------+-----+-----------+----------+----------+----------+--------------+----------------+---------- >          2025-03-29 16:38:45.665322+03 |    32383 | 435 | 0 |        0 |    32818 |    32818 |            0 |              0 |        0 >         (1 row) > > The startup process called fadvise 32383 times, skipped 32818 FPW blocks, but read relations only 9 times. > > Idea how to fix it > ------------------- > As of now XLogPrefetcher skips FPW blocks without any further tracking. > It seems that book-keeping of this block to avoid further prefetching is more correct. > > Please find attached patch for this idea. It adds block into recent_block & recent_rlocator. > > After patch the stats are following: > >         postgres=#  select object, context,reads, stats_reset from pg_stat_io where backend_type = 'startup'; >           object  |  context  | reads |          stats_reset > ----------+-----------+-------+------------------------------- >          relation | bulkread  |     0 | 2025-03-30 01:27:45.981169+03 >          relation | bulkwrite |     0 | 2025-03-30 01:27:45.981169+03 >          relation | init      |     0 | 2025-03-30 01:27:45.981169+03 >          relation | normal    |     9 | 2025-03-30 01:27:45.981169+03 >          relation | vacuum    |     0 | 2025-03-30 01:27:45.981169+03 >          wal      | init      |       | 2025-03-30 01:27:45.981169+03 >          wal      | normal    |  7895 | 2025-03-30 01:27:45.981169+03 >         (7 rows) > >         postgres=#  select * from pg_stat_recovery_prefetch ; >                   stats_reset          | prefetch | hit | skip_init | skip_new | skip_fpw | skip_rep | wal_distance | block_distance | io_depth > -------------------------------+----------+-----+-----------+----------+----------+----------+--------------+----------------+---------- >          2025-03-30 01:27:46.971602+03 |        0 |   0 | 0 |        0 |    32818 |    65636 |            0 |              0 |        0 >         (1 row) > > i.e. there is no more useless fadvise() calls and amount of skipped repeated blocks are increased by amount of modified blocks. > > Please feel free to ask any questions! > > Thank you! > > -- > Michael Zhilin > Postgres Professional > https://www.postgrespro.ru > Dear Michael, I did some IOPS throttled block device testing as described in [1] and by applying this patch the recovery redo elapsed time drops from ~18 seconds to ~6 seconds which is a very good improvement. Disabling fadvise altogether brings the elapsed time down to ~0.6 seconds so there may be some other low hanging fruit to be found. This is definitely worth exploring with a lot of potential for performance improvements. [1] https://www.postgresql.org/message-id/1c9fe4ba-1f49-4116-9c44-add3ed0310e5%40planetscale.com -- Markos Fountoulakis PlanetScale Postgres Core Team