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 1nhVIR-0001Q3-PX for pgsql-hackers@arkaria.postgresql.org; Thu, 21 Apr 2022 11:48:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nhVIQ-0000jj-Hi for pgsql-hackers@arkaria.postgresql.org; Thu, 21 Apr 2022 11:48:26 +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 1nhVIQ-0000jZ-8V for pgsql-hackers@lists.postgresql.org; Thu, 21 Apr 2022 11:48:26 +0000 Received: from mail-ej1-x62b.google.com ([2a00:1450:4864:20::62b]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nhVIL-0004Ne-Er for pgsql-hackers@lists.postgresql.org; Thu, 21 Apr 2022 11:48:25 +0000 Received: by mail-ej1-x62b.google.com with SMTP id y20so9465179eju.7 for ; Thu, 21 Apr 2022 04:48:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=message-id:date:mime-version:user-agent:subject:content-language:to :cc:references:from:in-reply-to:content-transfer-encoding; bh=MSY0/fId25ODAqzbu4rTYLzS7c7jNgkEYazmlthZtMA=; b=DG5NA0wOCMav0ZRkMILgweRGvDp+WetihbtclZfHSGkCSfo/rb1PjnUj0YGlSypHjD EDLTXAvdiCC4PD+9ZsuB8O2o8QY1jvLn9YC4KsOAxtEfEH+hZLFrEQXQT6FOHP+hPDvD Ktgo1I1y3zfPBx0h9z6CGtlV/Tx1pZtc4NkfXR9ZBWgLxuPffMQufGk1iCsE9jGToqDp /F+zr7QnDSwdCJNHS0pTVlPaEHo5RepQWKO5gYtRa3SKlMzdGyzGQhT/t1G5RefSfVlC QI3L0llISRnap/xq7qzEEqVupjg7JmhXQst/DRJx48Yk4bh7mxS5sg5ZKOZutP6GPOVW Q0xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=MSY0/fId25ODAqzbu4rTYLzS7c7jNgkEYazmlthZtMA=; b=7krYfU/QJ7F6Z3xRQ6gnadUgzsoh7YcV9JVtY0MVqcgHIWP6NBmQZj9MRls9yXzAsT 51Ilzac/vzfgEQxMT68AXliLoLOGe2T+dWO8LZVO+OddFIhIHhFrGX2RJ2dqAQD0NubL O3iwMgL9IaXR16n+/BsDblVXmj3fErUO3Cq+dlyRn0k0GWI32teXD4n6RelH2thMcOQQ PA+O3kjqDYBmoTthmyFHWNcq2ki/ULs7S+eRH/R/MfO0CnHNskUFk2leiXSpBCYa4tf2 FSgLYby29Gwn6nQHI5bUk4Tg1Sou1VVquaPBHNLPTdUnNJYUi3waq5NmaV/Cll5Jl4Tq 1gdA== X-Gm-Message-State: AOAM530BjjqPOQqaDvUGOBwgk+/ftBQQAAkEg0Yiyvi0sJ1464r/hro9 wEjTLQjGBQ12O0QDUmZBqBBOhl1f6y7F7GmFijV2gTcNqsAB0qxFVu2Gcf1diF5XrfAkgWoRsdg 49yVt7bS00YQ7/A3/qRoarBZyFt9kWapuKX2B4YxK3pos6NdhZrywt4JU3I9xcw1P0DuCw1QGvv 3+ZXNXHqfo4N/A8dC37DewoYmRsXdj4+cOzLBuhkLZmOHvHgMqnDpv X-Google-Smtp-Source: ABdhPJy5lCccgAVVWPc4hLcH02CUpaJoRL9HkFAtuNaVtHwN+YbgNygVzWG0b3mwOa7G7XgKSZjwXw== X-Received: by 2002:a17:907:a0c8:b0:6e8:b135:c864 with SMTP id hw8-20020a170907a0c800b006e8b135c864mr22383717ejc.233.1650541699752; Thu, 21 Apr 2022 04:48:19 -0700 (PDT) Received: from [10.137.0.17] (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id l20-20020a1709062a9400b006ce71a88bf5sm7694162eje.183.2022.04.21.04.48.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 21 Apr 2022 04:48:18 -0700 (PDT) Message-ID: <48ee60e4-7ec3-ab21-ff67-e7f9dc2e37b1@enterprisedb.com> Date: Thu, 21 Apr 2022 13:48:16 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: Assert failure in CTE inlining with view and correlated subquery Content-Language: en-US To: Richard Guo Cc: PostgreSQL Hackers References: <29196a1e-ed47-c7ca-9be2-b1c636816183@enterprisedb.com> From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 4/21/22 10:29, Richard Guo wrote: > > On Thu, Apr 21, 2022 at 3:51 PM Richard Guo > wrote: > > > On Thu, Apr 21, 2022 at 5:33 AM Tomas Vondra > > wrote: > > > it seems there's something wrong with CTE inlining when there's > a view > containing a correlated subquery referencing the CTE.  > > > BTW, seems view is not a necessary condition to reproduce this issue. > For instance: > > create table t (a int, b int); > > explain (costs off) select a from > ( >     with t_cte as (select a, b from t) >     select >         a, >         (select b from t_cte where t_cte.a = t.a) AS t_sub >     from t > ) sub; > > > Further debugging shows that in this repro the reference to the CTE is > removed when generating paths for the subquery 'sub', where we would try > to remove subquery targetlist items that are not needed. So for the > items we are to remove, maybe we need to check if they contain CTEs and > if so decrease cterefcount of the CTEs correspondingly. > Right, at some point we remove the unnecessary targetlist entries, but that ignores the entry may reference a CTE. That's pretty much what I meant by the counter being "out of sync". Updating the counter while removing the entry is one option, but maybe we could simply delay counting the CTE references until after that? regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company