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 1mDo2Q-0005L3-BZ for pgsql-general@arkaria.postgresql.org; Wed, 11 Aug 2021 13:12:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mDo2P-0000EI-9z for pgsql-general@arkaria.postgresql.org; Wed, 11 Aug 2021 13:12:53 +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 1mDo2O-0000Ag-WF for pgsql-general@lists.postgresql.org; Wed, 11 Aug 2021 13:12:53 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mDo2K-0006Rs-UG for pgsql-general@lists.postgresql.org; Wed, 11 Aug 2021 13:12:52 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Reply-To: Message-ID:Subject:To:Sender:From:Date:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=aDQdRpWtCnkLfMYd6pEAQXHoFljLDKgpEs7hLQxXjdQ=; b=g7wtm7gRor0OzWcwYj32yrMGxy uKt32YPFcC+oSv0gJLRSnCyTMyxo/MFeomMUFRWo6SOwqsO2uAYjMbM5PRd/BrNgF4a2KD++wwwm0 XgHOIIjClFJt06kIsfR/pKGRhBSW0D11A2CnAprLm39uMNj05OBduyBNSr20ruI8cdas=; Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by depesz.com with esmtpa (Exim 4.92) (envelope-from ) id 1mDo2J-0007FS-8n for pgsql-general@lists.postgresql.org; Wed, 11 Aug 2021 15:12:47 +0200 Date: Wed, 11 Aug 2021 15:12:47 +0200 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: pgsql-general Subject: Getting pg_stat_database data takes significant time Message-ID: <20210811131247.GA27197@depesz.com> Reply-To: depesz@depesz.com MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, We have servers where there is single app db, but one that contains MANY schema/tables. This is on Pg 12.6. Simple query like: select * from pg_stat_database where datname = 'app_name' can take up to 800ms! #v+ QUERY PLAN ══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════ Subquery Scan on d (cost=0.00..2.52 rows=2 width=216) (actual time=883.623..883.631 rows=1 loops=1) -> Append (cost=0.00..2.39 rows=2 width=68) (actual time=0.019..0.025 rows=1 loops=1) -> Subquery Scan on "*SELECT* 1" (cost=0.00..0.02 rows=1 width=68) (actual time=0.003..0.003 rows=0 loops=1) -> Result (cost=0.00..0.01 rows=1 width=68) (actual time=0.002..0.003 rows=0 loops=1) One-Time Filter: NULL::boolean -> Bitmap Heap Scan on pg_database (cost=1.24..2.35 rows=1 width=68) (actual time=0.016..0.020 rows=1 loops=1) Recheck Cond: (datname = 'app_name'::name) Heap Blocks: exact=1 -> Bitmap Index Scan on pg_database_datname_index (cost=0.00..1.24 rows=1 width=0) (actual time=0.009..0.010 rows=1 loops=1) Index Cond: (datname = 'app_name'::name) Planning Time: 0.294 ms Execution Time: 883.684 ms (12 rows) #v- I checked and it looks that the problem is with pg_stat_get_db_* functions that are used in this view. For example: #v+ =# explain (analyze on, buffers on) SELECT pg_stat_get_db_temp_bytes(7436115) AS temp_bytes; QUERY PLAN ════════════════════════════════════════════════════════════════════════════════════════ Result (cost=0.00..0.01 rows=1 width=8) (actual time=465.152..465.153 rows=1 loops=1) Planning Time: 0.017 ms Execution Time: 465.175 ms (3 rows) #v- Is there anything we could do to make it faster? The problem is that on certain servers this query takes up to 10% of total query time (as reported by pg_stat_statements). This query is being called, quite a lot, by monitoring software, and disabling it is not really an option. It is called every 15 seconds. So not extremely often, but the total_time adds up "nicely". Best regards, depesz