pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: hubert depesz lubaczewski <depesz@depesz.com>
To: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Getting pg_stat_database data takes significant time
Date: Wed, 11 Aug 2021 15:12:47 +0200
Message-ID: <20210811131247.GA27197@depesz.com> (raw)

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






view thread (13+ messages)  latest in thread

Message-ID: <20210811131247.GA27197@depesz.com>
Permalink:  ../20210811131247.GA27197@depesz.com/
Also on:    postgresql.org/message-id/20210811131247.GA27197@depesz.com

 · 

reply

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
  Subject: Re: Getting pg_stat_database data takes significant time
  In-Reply-To: <20210811131247.GA27197@depesz.com>
  References: <20210811131247.GA27197@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