Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gL6aD-0005qb-Hn for pgsql-general@arkaria.postgresql.org; Fri, 09 Nov 2018 13:12:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gL6aA-00041r-6V for pgsql-general@arkaria.postgresql.org; Fri, 09 Nov 2018 13:12:18 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gL6a9-00041k-UX for pgsql-general@lists.postgresql.org; Fri, 09 Nov 2018 13:12:17 +0000 Received: from mx2a.mailbox.org ([2001:67c:2050:104:0:2:25:2] helo=mx2.mailbox.org) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gL6a2-0006FQ-QZ for pgsql-general@postgresql.org; Fri, 09 Nov 2018 13:12:17 +0000 Received: from smtp2.mailbox.org (smtp2.mailbox.org [80.241.60.241]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by mx2.mailbox.org (Postfix) with ESMTPS id F3E12A10F9; Fri, 9 Nov 2018 14:12:05 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by spamfilter05.heinlein-hosting.de (spamfilter05.heinlein-hosting.de [80.241.56.123]) (amavisd-new, port 10030) with ESMTP id Qynfsh4Nrwj0; Fri, 9 Nov 2018 14:12:03 +0100 (CET) Message-ID: <96183def4b971c718367fdbc6021f6b9706f03ea.camel@cybertec.at> Subject: Re: index only scan question From: Laurenz Albe To: Daniel Westermann , "pgsql-general@postgresql.org" Date: Fri, 09 Nov 2018 14:12:02 +0100 In-Reply-To: References: Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Daniel Westermann wrote: > question: Given these steps: > > > postgres=# explain (analyze,buffers,costs off) select a from t1 where b = 5; > QUERY PLAN > -------------------------------------------------------------------------- > Index Only Scan using i2 on t1 (actual time=0.056..0.058 rows=1 loops=1) > Index Cond: (b = 5) > Heap Fetches: 0 > Buffers: shared hit=4 > Planning time: 0.421 ms > Execution time: 0.111 ms > (6 rows) > > postgres=# update t1 set a = 30 where b = 5; > UPDATE 1 > postgres=# explain (analyze,buffers,costs off) select a from t1 where b = 5; > QUERY PLAN > -------------------------------------------------------------------------- > Index Only Scan using i2 on t1 (actual time=0.039..0.042 rows=1 loops=1) > Index Cond: (b = 5) > Heap Fetches: 2 > Buffers: shared hit=5 > Planning time: 0.176 ms > Execution time: 0.082 ms > > The 2 heap fetches for the second run are clear to me, because of the pointer from the old version of the row to the new one. But why does the next execution only need one heap fetch? > > > postgres=# explain (analyze,buffers,costs off) select a from t1 where b = 5; > QUERY PLAN > -------------------------------------------------------------------------- > Index Only Scan using i2 on t1 (actual time=0.046..0.049 rows=1 loops=1) > Index Cond: (b = 5) > Heap Fetches: 1 > Buffers: shared hit=5 > Planning time: 0.194 ms > Execution time: 0.097 ms > > Is that because of some sort of caching? I'd say the old index tuple was killed during the first scan: https://www.cybertec-postgresql.com/en/killed-index-tuples/ Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com