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 1jT8DM-0002Ki-Lg for pgsql-performance@arkaria.postgresql.org; Mon, 27 Apr 2020 18:10:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jT8DL-0000vV-J3 for pgsql-performance@arkaria.postgresql.org; Mon, 27 Apr 2020 18:10:43 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jT8DL-0000vO-8z for pgsql-performance@lists.postgresql.org; Mon, 27 Apr 2020 18:10:43 +0000 Received: from mail-qv1-xf42.google.com ([2607:f8b0:4864:20::f42]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jT8DI-0000nR-11 for pgsql-performance@lists.postgresql.org; Mon, 27 Apr 2020 18:10:42 +0000 Received: by mail-qv1-xf42.google.com with SMTP id v18so8997227qvx.9 for ; Mon, 27 Apr 2020 11:10:39 -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=R1ZKj4o22NaO3AsBn5sRSbIA3IF5/dBG+f09tyzY0XM=; b=RvKtgW7HlX5njEvSgYJQqDD3tI8diOmgm3r14PydjpvNyHbNWeq4SFN//g2Yu/ahuW LXUeQVYwBfD4VxRHc0L6MGpuUveOf+wxdEPZom2LH9P7DT9saoy/AeHw4h1pY1YOo0pV 2SDxpmp8DsOAalazNXjgQGDp6lIlU4mlDQV1LphjlmC+9xb563Q5D53ZYPLhHkCXqMHu Cl19AcOg6hGGNzqdBYOlPV5UtOgRD5ptemWQp28c5s0USK2ujhv0MxOJ6jz91F7iLiVd PZCUrKO0ew5X4kbcRRl8paDnubZ376X9tWvPSLrLFEobqb2KgNzOeyK1ffdiV/XdaLxt Rs2Q== 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=R1ZKj4o22NaO3AsBn5sRSbIA3IF5/dBG+f09tyzY0XM=; b=Po3jqM4HON51cfpfa6YoWtIKulzoPM8q2xjYPFp29rqyhuRDNTFhcG/aPYyT7hvvBN o4e7iOtDKN5cvoMADbwdhjt8C8xe2peeaECS/YqvhVRgyvh8p1J1EnnhNZIvMDJxxwOc utrXe+zFHaTfWa4WxMY7b9DkL2rh3HUNZRwzUGh7STlWtoT70Dmp7+SV6jQsSp2A1Cgt qnt8vVTzDq+NnWLAs0d9B1306JnRdmhIN6qpHkQKcjin1YSkJCPZ5Sd2v30wtD00aGeR uHyJfPsEEnUZFmVrlYsVoVFTJAsPrDVc3O467zKscaTiAiiJc5q8DALZ/a/8ebKo4eb2 i6vg== X-Gm-Message-State: AGi0PuYL0vBdu5tLa31K27MVwgonT1dES8jC6Hmu0sDBncIfqz0aF19z UGm0hNm1z/ZEi5rcRuP/R84xLNxUWXo= X-Google-Smtp-Source: APiQypL8lm7/Chubu3fDMzdJHaSKi073XJQiYzIuKBNS/mVBPG10ctxrwSOO7zI3XYmtN5kt2suZQA== X-Received: by 2002:a0c:b651:: with SMTP id q17mr22353316qvf.135.1588011037818; Mon, 27 Apr 2020 11:10:37 -0700 (PDT) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id i4sm6460826qkh.66.2020.04.27.11.10.36 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 Apr 2020 11:10:36 -0700 (PDT) Received: by pryzbyj (Postfix, from userid 1000) id 88A2C800897; Mon, 27 Apr 2020 13:10:34 -0500 (CDT) Date: Mon, 27 Apr 2020 13:10:34 -0500 From: Justin Pryzby To: Jean-Christophe Boggio Cc: pgsql-performance@lists.postgresql.org Subject: Re: Recursive query slow on strange conditions Message-ID: <20200427181034.GA28974@telsasoft.com> References: <4a3b0246-f3ef-0767-62d1-6ceb4e2fc09c@thefreecat.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4a3b0246-f3ef-0767-62d1-6ceb4e2fc09c@thefreecat.org> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, Apr 27, 2020 at 07:49:50PM +0200, Jean-Christophe Boggio wrote: > I have a performance/regression problem on a complicated query (placed into > a function) when some tables are empty. > I first sent a message to the pgsql-bugs mailing list : > https://www.postgresql.org/message-id/16390-e9866af103d5a03a%40postgresql.org => BUG #16390: Regression between 12.2 and 11.6 on a recursive query : very slow and overestimation of rows The most obvious explanation is due to this change: https://www.postgresql.org/docs/12/release-12.html |Allow common table expressions (CTEs) to be inlined into the outer query (Andreas Karlsson, Andrew Gierth, David Fetter, Tom Lane) |Specifically, CTEs are automatically inlined if they have no side-effects, are not recursive, and are referenced only once in the query. Inlining can be prevented by specifying MATERIALIZED, or forced for multiply-referenced CTEs by specifying NOT MATERIALIZED. Previously, CTEs were never inlined and were always evaluated before the rest of the query. So you could try the query with ".. AS MATERIALIZED". > On Pg 11.6 the query takes 121ms > On Pg 12.2 it takes 11450ms > > Since the query plan is more than 560 lines and the query itself ~400 lines, > I'm not sure it's efficient to post everything in an email. You can also send a link to the plan on https://explain.depesz.com/ Which maybe more people will look at than if it requires downloading and restoring a DB. FYI, I had a similar issue: https://www.postgresql.org/message-id/flat/20171110204043.GS8563%40telsasoft.com And my solution was to 1) create an child table: CREATE TABLE x_child() INHERITS(x) and, 2) change the query to use select from ONLY. (1) allows the planner to believe that the table really is empty, a conclusion it otherwise avoids and (2) avoids decending into the child (for which the planner would likewise avoid the conclusion that it's actually empty). -- Justin