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 1mDp1q-00079i-44 for pgsql-general@arkaria.postgresql.org; Wed, 11 Aug 2021 14:16: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 1mDp1p-0006z6-2R for pgsql-general@arkaria.postgresql.org; Wed, 11 Aug 2021 14:16:21 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mDp1o-0006yy-QB for pgsql-general@lists.postgresql.org; Wed, 11 Aug 2021 14:16:20 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mDp1m-00069u-JL for pgsql-general@lists.postgresql.org; Wed, 11 Aug 2021 14:16:19 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 7E24C5C0107; Wed, 11 Aug 2021 10:16:17 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Wed, 11 Aug 2021 10:16:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; bh=931y+NOKiidgb0sK039fJnDb1/4QYJZjgvtI+SG2Tuc=; b=XPX4FvUU WmqXNo6JuwL3SQXR9uEThsvJi4fzmypA8mgEwCr0Q2HNxYib0seOmkO8yfDli1IA xqUcrpvJUPhrZWjJxP9RP7YcWw8AIOj6Y9TmzgAjOMXdae/OWWpomoRW0N8DF9XF AKtWUzGRQKyqO1XRrHmNz+Hut51NeUvrB5z54nzjWGxylnKd2tYP+3WOTKaTARxb Mz1sKwzuyQcG0/E3FeQtkbVyOnJfu0AKWFajXsWa0ps1pH6J96eHFgw6Q5ZjJ6ZE C3qEJkSlvHE5U0t1iHfna5KgAYKMC/aM4/b6cKqD0CytNKH+TJ33dbKp5UTRnPYJ D/FmxtfNFMOhlQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrkedugdejgecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpeffhffvuffkgggtugfgjgesthekredttddtjeenucfhrhhomheptehlvhgrrhho ucfjvghrrhgvrhgruceorghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgheqne cuggftrfgrthhtvghrnhepudejveejffejveduudetvdekieeuiedtvdeileefkeevjedu feeguedvjefhueelnecuffhomhgrihhnpegvnhhtvghrphhrihhsvggusgdrtghomhenuc evlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrlhhvhhgv rhhrvgesrghlvhhhrdhnohdqihhprdhorhhg X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 11 Aug 2021 10:16:16 -0400 (EDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id C07432A0868; Wed, 11 Aug 2021 10:16:13 -0400 (-04) Date: Wed, 11 Aug 2021 10:16:13 -0400 From: Alvaro Herrera To: hubert depesz lubaczewski Cc: pgsql-general Subject: Re: Getting pg_stat_database data takes significant time Message-ID: <202108111416.4ddlilf6hr6r@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20210811131247.GA27197@depesz.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Two things, 1. this depends on reading the stats file; that's done once per transaction. So if you run the query twice in a transaction, the second time will take less time. You can know how much time is spent reading that file by subtracting both times. 2. EXPLAIN (VERBOSE) will tell you which functions are being called by the query. One of those loops across all live backends. Is that significant? You could measure by creating an identical view but omitting pg_stat_db_numbackends. Does it take the same time as the view? If not, then you know that looping around all live backends is slow. If the problem is (1) then you could have less tables, so that the file is smaller and thus faster to read, but I don't think you'll like that answer; and if the problem is (2) then you could reduce max_connections, but I don't think you'll like that either. I suspect there's not much you can do, other than patch the monitoring system to not read that view as often. -- Álvaro Herrera Valdivia, Chile — https://www.EnterpriseDB.com/ "Use it up, wear it out, make it do, or do without"