Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o9lQI-0003Id-Cm for pgsql-general@arkaria.postgresql.org; Fri, 08 Jul 2022 10:41:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1o9lQE-0001Fi-Cf for pgsql-general@arkaria.postgresql.org; Fri, 08 Jul 2022 10:41:18 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o97xi-0004pI-BM for pgsql-general@lists.postgresql.org; Wed, 06 Jul 2022 16:33:14 +0000 Received: from mail-ej1-x630.google.com ([2a00:1450:4864:20::630]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1o97xg-0000Xd-7q for pgsql-general@postgresql.org; Wed, 06 Jul 2022 16:33:13 +0000 Received: by mail-ej1-x630.google.com with SMTP id u12so28005539eja.8 for ; Wed, 06 Jul 2022 09:33:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:content-language:to:from :subject:content-transfer-encoding; bh=A4WPEapv4skbJrTO1zTJhRQYPQyOtmzACcTLkGlBw5g=; b=DQV8DOMDOXyEBPn5t3MnV7PVRLuZ8Ze7cgIib/iy9OuAdwlhUsmvam4+A5GdzhEds5 vkz7AzktQE8hYcwzUq9sGKLi83ox8mRHlFeG65oarXurYLGyyxdWMqygmmr9Gb5WQk2/ VIiW7wkeX0QY4oDrUB7njNAvxrf3efRgReu5FOsPoEp5S7NPPy0QONMmzBgQHJ46yawG GyCghKsCAbanqnvNO7BNDNnh3hilAZNmLtPj9cWg0JoZlMQfZpureTXXE0glC1vEgLm6 ijKKKKJBY9EDQrLnuoVsCq65c7uBVcGGBy9v61Bi2uKZzKCipJ9DlNiijTOX7LmEyn9J 56zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent :content-language:to:from:subject:content-transfer-encoding; bh=A4WPEapv4skbJrTO1zTJhRQYPQyOtmzACcTLkGlBw5g=; b=10zfumntZJO0YDr+WfUtWRqlxM8PB9BI3W3DmZBVm9OZpm8/0RI7CTwUQkYBGABNqV RyMHD0XFfh7daNkQFSd2qZtRubvS6/xzzmkEtyBrlxLHly7M9Q1Ed1GEVtLnSgzfST8y naclnnA+Vtz6FSjozJN3vjM+mkRd8gwFs4FhW+ShS0wkARvh9gtr2sToX5RtSs3SfIq5 HAxCHzLufG972EY5R0rzxuinZ2Tkj2y0BujSFYyYDjpH5w+rn8NMk1KuUDNoWCkw51W/ YRmOXH3nfwyW2LUwJaDPULLsoDA26UwDVhNyFEoTiV/tD3UxUB+KLwh3V2L01KqDZNmR 3Puw== X-Gm-Message-State: AJIora+VgSHvsCXcYyZs29969VKWVmHUuJ5EliGU/8HJIFuba5A2PQ6i p3D1erZfdj8nLKsfAjA3PrFWq8DKGPE= X-Google-Smtp-Source: AGRyM1uQPhwMU/dZCvg5Y1yn5PQfBeF95PE+K2kdg0yMLkZ7khLP0xrGYqK+V892wWECYT1kcSsMUQ== X-Received: by 2002:a17:907:6e01:b0:726:9a7b:59b7 with SMTP id sd1-20020a1709076e0100b007269a7b59b7mr41407164ejc.752.1657125191092; Wed, 06 Jul 2022 09:33:11 -0700 (PDT) Received: from [192.168.1.18] (83.24.16.142.ipv4.supernova.orange.pl. [83.24.16.142]) by smtp.gmail.com with ESMTPSA id h7-20020a1709070b0700b00711d8696de9sm17539853ejl.70.2022.07.06.09.33.09 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 06 Jul 2022 09:33:10 -0700 (PDT) Message-ID: Date: Wed, 6 Jul 2022 18:33:08 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Content-Language: en-US To: pgsql-general@postgresql.org From: =?UTF-8?Q?Aleksander_=c5=81ukasz?= Subject: Why does pg_statio_user_tables report heap_blks_hit after index only scan? 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 Hey pgsql-general, while debugging some performance issues, I've noticed that index only scan (with number of heap fetches reported as 0) can still cause heap_blks_hit as reported by pg_statio_user_tables to increment (in my case by 1). For example, on 14.4:   create table test (a int);   CREATE TABLE   insert into test (a) select i from generate_series(1, 100000) i;   INSERT 0 100000   create index ix_test on test (a);   CREATE INDEX   vacuum analyze test;   VACUUM   select pg_stat_reset();   pg_stat_reset   ---------------   (1 row)   select pg_sleep(1);   pg_sleep   ----------   (1 row)   Expanded display is on.   select * from pg_statio_user_tables;   -[ RECORD 1 ]---+-------   relid           | 16384   schemaname      | public   relname         | test   heap_blks_read  | 447   heap_blks_hit   | 102657   idx_blks_read   | 1   idx_blks_hit    | 0   toast_blks_read |   toast_blks_hit  |   tidx_blks_read  |   tidx_blks_hit   |   Expanded display is off.   explain analyze   select a from test where a = 1;                                                       QUERY PLAN -------------------------------------------------------------------------------------------------------------------   Index Only Scan using ix_test on test  (cost=0.29..4.31 rows=1 width=4) (actual time=0.043..0.045 rows=1 loops=1)     Index Cond: (a = 1)     Heap Fetches: 0   Planning Time: 0.146 ms   Execution Time: 0.063 ms   (5 rows)   select pg_sleep(1);   pg_sleep   ----------   (1 row)   Expanded display is on.   select * from pg_statio_user_tables;   -[ RECORD 1 ]---+-------   relid           | 16384   schemaname      | public   relname         | test   heap_blks_read  | 447   heap_blks_hit   | 102658   idx_blks_read   | 3   idx_blks_hit    | 1   toast_blks_read |   toast_blks_hit  |   tidx_blks_read  |   tidx_blks_hit   | That is heap_blks_hit incremented by one after index only scan that supposedly did not fetch anything from the table. Am I right assuming this is due to some non data related read, like checking visibility map, and this will always happen? Or maybe my methodology or other assumptions are wrong? Thanks for your feedback. PS: I'm doing those pg_sleeps because I've noticed those are (usually) needed for stats to refresh. Also, not sure why first pg_reset is not clearing pg_statio table (as this works for me in many different contexts)... Apparently I'm not fully understanding when those stats are affected and maybe this is the reason I've trouble interpreting this increment?