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 1mDrB4-0002Pq-V3 for pgsql-general@arkaria.postgresql.org; Wed, 11 Aug 2021 16:34:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mDrB3-0004Ij-VX for pgsql-general@arkaria.postgresql.org; Wed, 11 Aug 2021 16:34:01 +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 1mDrB3-0004Ib-Jy for pgsql-general@lists.postgresql.org; Wed, 11 Aug 2021 16:34:01 +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 1mDrAz-00086y-4u for pgsql-general@lists.postgresql.org; Wed, 11 Aug 2021 16:34:01 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Type:MIME-Version:References:Reply-To: Message-ID:Subject:Cc:To:Sender:From:Date:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=9wM/UAIWWJ1Kt6W5+R6cqrVEkW6D5RrlSiqk6H3vy7U=; b=C0swpt/fCxJWEPwl8Kn6PtYVJH TNxNKoalG0Qt+EOdtpKe34Gpoug1xBZ8blKUNSlWsKPKaSRm46mfo7HjvmAnNOLm8lplYcK4aCRho qq+LflAjnXE+hxWUHA46Jw9zhCTpHFRhuUfMB6VJZL2uYkoXcAB4WRtgrBtPMmYAVt/k=; Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by depesz.com with esmtpa (Exim 4.92) (envelope-from ) id 1mDrAy-0003Ti-3W; Wed, 11 Aug 2021 18:33:56 +0200 Date: Wed, 11 Aug 2021 18:33:55 +0200 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: Alvaro Herrera Cc: pgsql-general Subject: Re: Getting pg_stat_database data takes significant time Message-ID: <20210811163355.GA20926@depesz.com> Reply-To: depesz@depesz.com References: <20210811131247.GA27197@depesz.com> <202108111416.4ddlilf6hr6r@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <202108111416.4ddlilf6hr6r@alvherre.pgsql> 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 On Wed, Aug 11, 2021 at 10:16:13AM -0400, Alvaro Herrera wrote: > 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. Yeah. I noticed. Looks like loading the stats file is the culprit. But does that mean that the whole stats file has to be read at once? I just need stats on db, not on relations? > 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. Even `select datid, datname, xact_commit, xact_rollback from pg_stat_database;` takes (now), a second. Second call in the same connection, different txn, 0.8s. Second call in the same transaction as first - 0.053ms. So it definitely suggests that loading the stats file is the problem. > 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. max_connections would be related, if I understand correctly, if I had faster starts without pg_stat_db_numbackends. And I don't - takes basically the same time. > I suspect there's not much you can do, other than patch the monitoring > system to not read that view as often. Once every 15 seconds doesn't seem to be too often, but perhaps I can do something about it... Best regards, depesz