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.94.2) (envelope-from ) id 1vCFp7-00BDjF-Ro for pgsql-general@arkaria.postgresql.org; Fri, 24 Oct 2025 11:19:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1vCFp6-00FTua-MQ for pgsql-general@arkaria.postgresql.org; Fri, 24 Oct 2025 11:19:07 +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.94.2) (envelope-from ) id 1vCFp6-00FTuS-Bi for pgsql-general@lists.postgresql.org; Fri, 24 Oct 2025 11:19:07 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vCFp3-003Wh0-1X for pgsql-general@lists.postgresql.org; Fri, 24 Oct 2025 11:19:06 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Transfer-Encoding:Content-Type:MIME-Version :References:Reply-To:Message-ID:Subject:To:Sender:From:Date:Cc:Content-ID: Content-Description; bh=ewXr6zbDz0SArzwwoYwzEWe5sV8vjIpU5TjVF+sBnjk=; b=ZUCiS We81vGptQ2jf2fEdDQEj3P16tqLF8hHRWaiizYgkmaUvimmKZjd73jE5NYWUFFPTUrTB84wJ8jg+n TPZOYJ3bMcaTY5MNPnkjNmDSLASSwzDYyWdfgvFmL1KakyyAU+1nZxQP/BfZX2gQ3XRA0zUY7milp 7hu18astP+Ac=; Received: from depesz by depesz.com with local (Exim 4.96) (envelope-from ) id 1vCFp1-00Dmj4-2N for pgsql-general@lists.postgresql.org; Fri, 24 Oct 2025 13:19:03 +0200 Date: Fri, 24 Oct 2025 13:19:03 +0200 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: PostgreSQL General Subject: Re: Why is this query touching 4gb of buffers? Message-ID: Reply-To: depesz@depesz.com References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Oct 24, 2025 at 01:01:48PM +0200, hubert depesz lubaczewski wrote: > Hi, > I have weird-ish case, that I can't grok, or at least explain in > hand-wavy way. A bit more info. Due to how the database is setup we have MANY "copies" of the same table - same name, same columns, different schema, different (but similar) data. So, I tested the problem on another instance of this table. And there was something really, really weird. (this query has distinct on two columns, but it doesn't seem to be relevant, and tests whether having it, or not, showed not to generate any reasonable differences). First run of the query generated: QUERY PLAN ══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ HashAggregate (cost=181382.51..181745.19 rows=36268 width=16) (actual time=27962.826..27962.826 rows=0 loops=1) Group Key: communication_channel_id, root_account_id Batches: 1 Memory Usage: 1561kB Buffers: shared hit=4624 read=117838 dirtied=486 -> Index Scan using index_some_table_pending on some_table (cost=0.42..178322.57 rows=611988 width=16) (actual time=27962.567..27962.567 rows=0 loops=1) Index Cond: (send_at <= '2025-10-23 12:35:48'::timestamp without time zone) Buffers: shared hit=4624 read=117838 dirtied=486 Planning: Buffers: shared hit=174 Planning Time: 1.863 ms Execution Time: 27963.620 ms (11 rows) Then, immediately I reran it, without reindex, without analyze, without anything. And I got: QUERY PLAN ══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ HashAggregate (cost=181388.32..181751.00 rows=36268 width=16) (actual time=0.653..0.653 rows=0 loops=1) Group Key: communication_channel_id, root_account_id Batches: 1 Memory Usage: 1561kB Buffers: shared hit=424 -> Index Scan using index_some_table_pending on some_table (cost=0.42..178328.27 rows=612009 width=16) (actual time=0.438..0.438 rows=0 loops=1) Index Cond: (send_at <= '2025-10-23 12:35:48'::timestamp without time zone) Buffers: shared hit=424 Planning Time: 0.123 ms Execution Time: 1.237 ms (9 rows) Time is irrelevant, the point is that we are going down from ~120k buffers "touched" to 424 buffers. What is going on? I tested the same case on yet another setup, and ran simlar query multiple times in a row on secondary, and got: Buffers: shared hit=113849 read=198047 => Execution Time: 1359.661 ms Buffers: shared hit=311896 => Execution Time: 246.143 ms But when I ran the query on primary server tghere was very visiolble change in buffers accesses: Buffers: shared hit=114893 read=197776 dirtied=5528 => Execution Time: 75863.479 ms Buffers: shared hit=775 => Execution Time: 2.360 ms This didn't change buffers "touches" on secondary, though. What am I missing? Best regards, depesz