Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g4MZb-0004Or-Bb for pgsql-general@arkaria.postgresql.org; Mon, 24 Sep 2018 08:50:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g4MZY-0007bl-H1 for pgsql-general@arkaria.postgresql.org; Mon, 24 Sep 2018 08:50:28 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g4MZY-0007bR-66 for pgsql-general@lists.postgresql.org; Mon, 24 Sep 2018 08:50:28 +0000 Received: from mx1.mailbox.org ([2001:67c:2050:104:0:1:25:1]) by makus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1g4MZU-0002bV-KF for pgsql-general@lists.postgresql.org; Mon, 24 Sep 2018 08:50:26 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.mailbox.org (Postfix) with ESMTPS id 971B849A79 for ; Mon, 24 Sep 2018 10:50:17 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter01.heinlein-hosting.de (spamfilter01.heinlein-hosting.de [80.241.56.115]) (amavisd-new, port 10030) with ESMTP id fYX2FgNSdT7d for ; Mon, 24 Sep 2018 10:50:16 +0200 (CEST) Message-ID: <0979b9133184be5bdec8a99324854bf95656942f.camel@cybertec.at> Subject: Mysteriously varying index scan costs From: Laurenz Albe To: pgsql-general@lists.postgresql.org Date: Mon, 24 Sep 2018 10:50:15 +0200 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 Last week I encountered the following at a customer site on PostgreSQL 9.6, and I cannot explain it. The first run gave me this: Index Scan using device_outbound_messages_status on device_outbound_messages (cost=0.43..20.46 rows=97 width=128) (actual time=34.021..35.545 rows=133 loops=1) Index Cond: ((status)::text = ANY ('{WAITING_FOR_TX,WAITING_FOR_IMMEDIATE_TX}'::text[])) Buffers: shared hit=74917 dirtied=707 Subsequent runs of the same query gave me: Index Scan using device_outbound_messages_status on device_outbound_messages (cost=0.43..20.46 rows=97 width=128) (actual time=2.129..3.907 rows=133 loops=1) Index Cond: ((status)::text = ANY ('{WAITING_FOR_TX,WAITING_FOR_IMMEDIATE_TX}'::text[])) Buffers: shared hit=1185 There were no concurrent changes to the data (test environment). This was part of a bigger statement. I understand that some pages can be dirtied because table fetches that cause hint bits to be set. But how can it be that the first run has to touch 74917 blocks, while whe second run only needs to touch 1185? Thanks for any ideas! Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com