Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hoqdQ-0005mu-VG for pgsql-performance@arkaria.postgresql.org; Sat, 20 Jul 2019 14:46:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hoqdP-0000pW-HU for pgsql-performance@arkaria.postgresql.org; Sat, 20 Jul 2019 14:46:51 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hoqdP-0000on-5d for pgsql-performance@lists.postgresql.org; Sat, 20 Jul 2019 14:46:51 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1hoqdM-0000Bg-4t for pgsql-performance@lists.postgresql.org; Sat, 20 Jul 2019 14:46:50 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x6KEki3t019789; Sat, 20 Jul 2019 10:46:44 -0400 From: Tom Lane To: Ken Tanzer cc: pgsql-performance@lists.postgresql.org Subject: Re: Speeding up query pulling comments from pg_catalog In-reply-to: References: Comments: In-reply-to Ken Tanzer message dated "Fri, 19 Jul 2019 16:03:27 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <19787.1563634004.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sat, 20 Jul 2019 10:46:44 -0400 Message-ID: <19788.1563634004@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Ken Tanzer writes: > Hi. I've got an app that queries pg_catalog to find any table columns t= hat > 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=3D> 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 =3D 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 =3D c= .oid LEFT JOIN pg_catalog.pg_description d ON d.classoid =3D c.tableoid and d= .objoid =3D c.oid and d.objsubid =3D 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