From: hubert depesz lubaczewski <depesz@depesz.com>
To: Alvaro Herrera <alvherre@alvh.no-ip.org>
Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: Getting pg_stat_database data takes significant time
Date: Wed, 11 Aug 2021 18:33:55 +0200
Message-ID: <20210811163355.GA20926@depesz.com> (raw)
In-Reply-To: <202108111416.4ddlilf6hr6r@alvherre.pgsql>
References: <20210811131247.GA27197@depesz.com>
<202108111416.4ddlilf6hr6r@alvherre.pgsql>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: depesz@depesz.com, alvherre@alvh.no-ip.org
Subject: Re: Getting pg_stat_database data takes significant time
In-Reply-To: <20210811163355.GA20926@depesz.com>
References: <20210811131247.GA27197@depesz.com> <202108111416.4ddlilf6hr6r@alvherre.pgsql> <20210811163355.GA20926@depesz.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox