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 1jjxse-0005xH-Sz for pgsql-performance@arkaria.postgresql.org; Sat, 13 Jun 2020 04:34:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jjxsc-0004dI-Sz for pgsql-performance@arkaria.postgresql.org; Sat, 13 Jun 2020 04:34:54 +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 1jjxsc-0004d9-EP for pgsql-performance@lists.postgresql.org; Sat, 13 Jun 2020 04:34:54 +0000 Received: from mail-il1-x142.google.com ([2607:f8b0:4864:20::142]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jjxsV-0005yS-19 for pgsql-performance@postgresql.org; Sat, 13 Jun 2020 04:34:53 +0000 Received: by mail-il1-x142.google.com with SMTP id p5so10675234ile.6 for ; Fri, 12 Jun 2020 21:34:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=DYhlngYQX1h1qktuyrnFjX8M+qQK/DRmfF+d/hOpYS0=; b=MFxvk5K0YF6rwvW96tCXiqfml31yhJotCdV2buMcNHNzdfSYK5xLaKVM6o0SJ3XrJv TimUxj8BTmIiNgo3u3qo+5unI2ZgZYxa4jWxPvy3O9k8+83bkSQuzNCoXN9bhAtIexC9 4EC6A1t9lZ91taIleR/kIDXcACCi5hle224hVvuhHfzHcE/wE9HfLgoZn+DWuQjz5mPi I/Jv6aNYIRMOH/VibO7/faLxwR8lo8ZfeJsbBUcXPjHb/CIobAtTA+zI4Eg4z7xCA0jZ 8W7X8FkE/9Qhm9QXo0bAD7h6CfVydt3jSFNLWs72LgKlmibnhYDoPbO5t1GP+JjyyWpU BPTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=DYhlngYQX1h1qktuyrnFjX8M+qQK/DRmfF+d/hOpYS0=; b=gzKTWv4FvGvyUvD0LczjAn2XhQwjVPAk2IBKAc3aLFlC3qFMJo0l+iTLYaKtoMM8uL 2Lhp65GDZz96nhWttRt3+a/Ou130AOxoT6glpzGRyI8GU4JdMMsrrQQye3tG30slD7cj ewxEmuyAzAN5shJdJSMbTETppasI+cxNEeD0f77iC/VR+0gqel++Oy8TM2Tq1iQFg0dV gC49el0UGVb9Zi4I+GDaj3U4jIRRMrZ4bu6ZtNSqfCnNelmwzddxZAAoaoriVWQZDmI2 0AFYDNp1jGLmjxjdSHc0xbOZii6E0viL+MhUYE9ZCDj5ymAwqsMPze75HVPhAiyThRio cbLA== X-Gm-Message-State: AOAM531XL8sExshkNZYsfmflMG1KwP2mEG57SltARaOcBhE4MnzaYAUQ zKJvOnGf6nlVT0MrDnSL708Jtg== X-Google-Smtp-Source: ABdhPJxdHR/1dzfGxEBunXaubiM9Ch7GtBuYkj7YPo5GUE1bpe9RPnIbIpC97ZKMu52gNz1MBZxC0A== X-Received: by 2002:a92:c534:: with SMTP id m20mr16767747ili.300.1592022886010; Fri, 12 Jun 2020 21:34:46 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id g15sm4090673ilq.39.2020.06.12.21.34.44 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Jun 2020 21:34:45 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id E398C800120; Fri, 12 Jun 2020 23:34:43 -0500 (CDT) Date: Fri, 12 Jun 2020 23:34:43 -0500 From: Justin Pryzby To: Tom Lane Cc: regrog , pgsql-performance@postgresql.org Subject: Re: view reading information_schema is slow in PostgreSQL 12 Message-ID: <20200613043443.GM14879@telsasoft.com> References: <1591975278775-0.post@n3.nabble.com> <798686.1592017869@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <798686.1592017869@sss.pgh.pa.us> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, Jun 12, 2020 at 11:11:09PM -0400, Tom Lane wrote: > regrog writes: > > I'm facing performance issues migrating from postgres 10 to 12 (also from 11 > > to 12) even with a new DB. > > 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 looked at this a bit. I see what's going on, but I don't see an easy > workaround :-(. The information_schema.table_constraints view contains > a UNION ALL, which in your v10 query produces this part of the plan: > To get a decent plan out of v12, the problem is to get it to produce > a better rowcount estimate for the first arm of table_constraints' > UNION. We don't necessarily need it to match the 1800 reality, but > we need it to be more than 1. Unfortunately there's no simple way > to affect that. The core misestimate is here: > I expect you're getting a fairly decent estimate for the "contype <> > ALL" condition, but the planner has no idea what to make of the CASE > construct, so it just falls back to a hard-wired default estimate. > > I don't have any good suggestions at the moment. If you had a lot more > tables (hence more rows in pg_constraint) the plan would likely shift > to something tolerable even with the crummy selectivity estimate for the > CASE. But where you are, it's hard. A conceivable workaround is to > drop the "tc.constraint_type <> 'CHECK'" condition from your view, which > would resurrect that UNION arm and probably get you back to something > similar to the v10 plan. For the purposes of making this work for v12, you might try to look at either a temporary table: CREATE TEMP TABLE constraints AS SELECT * FROM information_schema.table_constraints WHERE constraint_type='FOREIGN KEY'; ANALYZE constraints; SELECT * FROM ... LEFT JOIN constraints LEFT JOIN ... or a CTE (which, if it works, is mostly dumb luck): WITH constraints AS MATERIALIZED (SELECT * FROM information_schema.table_constraints) SELECT * FROM constraints WHERE constraint_type='FOREIGN KEY'; Or make a copy of the system view with hacks for the worst misestimates (like contype<>'c' instead of constraint_type<>'CHECK'). -- Justin