Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jjoOC-0001X3-0p for pgsql-performance@arkaria.postgresql.org; Fri, 12 Jun 2020 18:26:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jjoOA-0007Cm-UF for pgsql-performance@arkaria.postgresql.org; Fri, 12 Jun 2020 18:26:50 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jjlUf-0000M9-M0 for pgsql-performance@lists.postgresql.org; Fri, 12 Jun 2020 15:21:21 +0000 Received: from n3.nabble.com ([162.255.23.22]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jjlUd-0007dq-6u for pgsql-performance@postgresql.org; Fri, 12 Jun 2020 15:21:20 +0000 Received: from n3.nabble.com (localhost [127.0.0.1]) by n3.nabble.com (Postfix) with ESMTP id CB7B81E7777FA for ; Fri, 12 Jun 2020 08:21:18 -0700 (MST) Date: Fri, 12 Jun 2020 08:21:18 -0700 (MST) From: regrog To: pgsql-performance@postgresql.org Message-ID: <1591975278775-0.post@n3.nabble.com> Subject: view reading information_schema is slow in PostgreSQL 12 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I'm facing performance issues migrating from postgres 10 to 12 (also from 11 to 12) even with a new DB. Th performance difference is huge 300ms in pg10 vs 3 minutes in pg12. I have a view that abstracts the data in the database: CREATE OR REPLACE VIEW public.my_constraints AS SELECT lower(tc.constraint_name) AS constraint_name, tc.constraint_type, tc.table_schema, lower(tc.table_name) AS table_name, lower(kcu.column_name) AS column_name, ccu.table_schema AS reference_table_schema, lower(ccu.table_name) AS reference_table_name, lower(ccu.column_name) AS reference_column_name, rc.update_rule, rc.delete_rule FROM information_schema.table_constraints tc LEFT JOIN information_schema.key_column_usage kcu ON tc.constraint_catalog = kcu.constraint_catalog AND tc.constraint_schema = kcu.constraint_schema AND tc.constraint_name = kcu.constraint_name LEFT JOIN information_schema.referential_constraints rc ON tc.constraint_catalog = rc.constraint_catalog AND tc.constraint_schema = rc.constraint_schema AND tc.constraint_name = rc.constraint_name LEFT JOIN information_schema.constraint_column_usage ccu ON rc.unique_constraint_catalog = ccu.constraint_catalog AND rc.unique_constraint_schema = ccu.constraint_schema AND rc.unique_constraint_name = ccu.constraint_name WHERE tc.constraint_catalog = 'my_catalog' AND tc.constraint_schema = 'public' AND tc.constraint_type <> 'CHECK'; The simple query: select * from my_constraints is normal but as soon as I add where constraint_type = 'FOREIGN KEY' it takes a lot of time. I don't have data in my tables at the moment, I have around 600 tables in my schema. I've analyzed the query but can't figure out what's wrong, this is the query with the filter without the view: select * from (SELECT lower(tc.constraint_name) AS constraint_name, tc.constraint_type, tc.table_schema, lower(tc.table_name) AS table_name, lower(kcu.column_name) AS column_name, ccu.table_schema AS reference_table_schema, lower(ccu.table_name) AS reference_table_name, lower(ccu.column_name) AS reference_column_name, rc.update_rule, rc.delete_rule FROM information_schema.table_constraints tc LEFT JOIN information_schema.key_column_usage kcu ON tc.constraint_catalog = kcu.constraint_catalog AND tc.constraint_schema = kcu.constraint_schema AND tc.constraint_name = kcu.constraint_name LEFT JOIN information_schema.referential_constraints rc ON tc.constraint_catalog = rc.constraint_catalog AND tc.constraint_schema = rc.constraint_schema AND tc.constraint_name = rc.constraint_name LEFT JOIN information_schema.constraint_column_usage ccu ON rc.unique_constraint_catalog = ccu.constraint_catalog AND rc.unique_constraint_schema = ccu.constraint_schema AND rc.unique_constraint_name = ccu.constraint_name WHERE tc.constraint_catalog = 'my_catalog' AND tc.constraint_schema = 'public' AND tc.constraint_type <> 'CHECK' ) as a where constraint_type = 'FOREIGN KEY' postgres 10 plan https://explain.depesz.com/s/mEmv postgres 12 plan https://explain.depesz.com/s/lovP -- Sent from: https://www.postgresql-archive.org/PostgreSQL-performance-f2050081.html