pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Ken Tanzer <ken.tanzer@gmail.com>
Cc: pgsql-performance@lists.postgresql.org
Subject: Re: Speeding up query pulling comments from pg_catalog
Date: Sat, 20 Jul 2019 10:46:44 -0400
Message-ID: <19788.1563634004@sss.pgh.pa.us> (raw)
In-Reply-To: <CAD3a31UEEojeQ9VYZHO0u2j0eyyh5jaYd8Pc=7yfJEHrfzELFg@mail.gmail.com>
References: <CAD3a31UEEojeQ9VYZHO0u2j0eyyh5jaYd8Pc=7yfJEHrfzELFg@mail.gmail.com>

Ken Tanzer <ken.tanzer@gmail.com> writes:
> Hi.  I've got an app that queries pg_catalog to find any table columns that
> have comments.  After setting up PgBadger, it was #2 on my list of time
> consuming queries, with min/max/avg duration of 199/2351/385 ms (across
> ~12,000 executions logged).
> I'm wondering if there are any ways to speed this query up, including if
> there are better options for what to query.

> ag_reach=> EXPLAIN (ANALYZE, VERBOSE,BUFFERS,TIMING, COSTS)
> SELECT c.relname AS table,a.attname AS
> column,pg_catalog.col_description(a.attrelid, a.attnum) AS comment
> FROM pg_catalog.pg_attribute a, pg_class c
> WHERE  a.attrelid = c.oid
> AND pg_catalog.col_description(a.attrelid, a.attnum) IS NOT NULL;

Unfortunately, the planner isn't smart enough to inline the
col_description() function.  But if you do so manually you'll end up
with something like

SELECT c.relname AS table, a.attname AS column, d.description AS comment
FROM
  pg_catalog.pg_attribute a JOIN pg_catalog.pg_class c ON a.attrelid = c.oid
  LEFT JOIN pg_catalog.pg_description d ON d.classoid = c.tableoid and d.objoid = c.oid and d.objsubid = a.attnum
WHERE d.description IS NOT NULL;

For me, that formulation is quite a bit faster than the original ---
what you wrote basically forces a nestloop join against pg_description,
and then to add insult to injury, has to search pg_description a second
time for each hit.

			regards, tom lane





view thread (6+ messages)  latest in thread

Message-ID: <19788.1563634004@sss.pgh.pa.us>
Permalink:  ../19788.1563634004@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/19788.1563634004@sss.pgh.pa.us

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: tgl@sss.pgh.pa.us, ken.tanzer@gmail.com, pgsql-performance@lists.postgresql.org
  Subject: Re: Speeding up query pulling comments from pg_catalog
  In-Reply-To: <19788.1563634004@sss.pgh.pa.us>

* 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