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 1hTAew-00081n-Ul for pgsql-hackers@arkaria.postgresql.org; Tue, 21 May 2019 19:42:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hTAev-0006tZ-Iu for pgsql-hackers@arkaria.postgresql.org; Tue, 21 May 2019 19:42:49 +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 1hTAev-0006tO-6f for pgsql-hackers@lists.postgresql.org; Tue, 21 May 2019 19:42:49 +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.89) (envelope-from ) id 1hTAes-0008PR-KP for pgsql-hackers@lists.postgresql.org; Tue, 21 May 2019 19:42:47 +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 x4LJgewV014720; Tue, 21 May 2019 15:42:40 -0400 From: Tom Lane To: pgsql-hackers@lists.postgresql.org cc: PegoraroF10 , Peter Eisentraut Subject: Re: =?UTF-8?Q?Re:_Refresh_Publication_takes_hours_and_doesn=C2=B4t_finish?= In-reply-to: <12632.1558387804@sss.pgh.pa.us> References: <1551385426763-0.post@n3.nabble.com> <1558383480253-0.post@n3.nabble.com> <12632.1558387804@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Mon, 20 May 2019 17:30:04 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <14476.1558467596.0@sss.pgh.pa.us> Date: Tue, 21 May 2019 15:42:40 -0400 Message-ID: <14719.1558467760@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <14476.1558467596.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable [ redirecting to pgsql-hackers as the more relevant list ] I wrote: > PegoraroF10 writes: >> I tried sometime ago ... but with no responses, I ask you again. >> pg_publication_tables is a view that is used to refresh publication, bu= t as >> we have 15.000 tables, it takes hours and doesn't complete. If I change= that >> view I can have an immediate result. The question is: Can I change that= view >> ? There is some trouble changing those system views ? > Hmm ... given that pg_get_publication_tables() shouldn't return any > duplicate OIDs, it does seem unnecessarily inefficient to put it in > an IN-subselect condition. Peter, is there a reason why this isn't > a straight lateral join? I get a much saner-looking plan from > FROM pg_publication P, pg_class C > - JOIN pg_namespace N ON (N.oid =3D C.relnamespace) > - WHERE C.oid IN (SELECT relid FROM pg_get_publication_tables(P.pubnam= e)); > + JOIN pg_namespace N ON (N.oid =3D C.relnamespace), > + LATERAL pg_get_publication_tables(P.pubname) > + WHERE C.oid =3D pg_get_publication_tables.relid; For the record, the attached seems like what to do here. It's easy to show that there's a big performance gain even for normal numbers of tables, eg if you do CREATE PUBLICATION mypub FOR ALL TABLES; SELECT * FROM pg_publication_tables; in the regression database, the time for the select drops from ~360ms to ~6ms on my machine. The existing view's performance will drop as O(N^2) the more publishable tables you have ... Given that this change impacts the regression test results, project rules say that it should come with a catversion bump. Since we are certainly going to have a catversion bump before beta2 because of the pg_statistic_ext permissions business, that doesn't seem like a reason not to push it into v12 --- any objections? regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="fix-pg_publication_tables-performance.patch"; charset="us-ascii" Content-ID: <14476.1558467596.2@sss.pgh.pa.us> Content-Description: fix-pg_publication_tables-performance.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/sy= stem_views.sql index 566100d..52a6c31 100644 --- a/src/backend/catalog/system_views.sql +++ b/src/backend/catalog/system_views.sql @@ -258,9 +258,10 @@ CREATE VIEW pg_publication_tables AS P.pubname AS pubname, N.nspname AS schemaname, C.relname AS tablename - FROM pg_publication P, pg_class C - JOIN pg_namespace N ON (N.oid =3D C.relnamespace) - WHERE C.oid IN (SELECT relid FROM pg_get_publication_tables(P.pubname= )); + FROM pg_publication P, + LATERAL pg_get_publication_tables(P.pubname) GPT, + pg_class C JOIN pg_namespace N ON (N.oid =3D C.relnamespace) + WHERE C.oid =3D GPT.relid; = CREATE VIEW pg_locks AS SELECT * FROM pg_lock_status() AS L; diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expect= ed/rules.out index 0c392e5..4363ca1 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -1441,10 +1441,10 @@ pg_publication_tables| SELECT p.pubname, n.nspname AS schemaname, c.relname AS tablename FROM pg_publication p, + LATERAL pg_get_publication_tables((p.pubname)::text) gpt(relid), (pg_class c JOIN pg_namespace n ON ((n.oid =3D c.relnamespace))) - WHERE (c.oid IN ( SELECT pg_get_publication_tables.relid - FROM pg_get_publication_tables((p.pubname)::text) pg_get_publi= cation_tables(relid))); + WHERE (c.oid =3D gpt.relid); pg_replication_origin_status| SELECT pg_show_replication_origin_status.lo= cal_id, pg_show_replication_origin_status.external_id, pg_show_replication_origin_status.remote_lsn, ------- =_aaaaaaaaaa0--