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 1nhdg5-0008Fu-DD for pgsql-hackers@arkaria.postgresql.org; Thu, 21 Apr 2022 20:45:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nhdg4-0006IQ-5k for pgsql-hackers@arkaria.postgresql.org; Thu, 21 Apr 2022 20:45:24 +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 1nhdg3-0006FK-Hw for pgsql-hackers@lists.postgresql.org; Thu, 21 Apr 2022 20:45:23 +0000 Received: from mail-ej1-x635.google.com ([2a00:1450:4864:20::635]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nhdfz-0007JX-VH for pgsql-hackers@lists.postgresql.org; Thu, 21 Apr 2022 20:45:22 +0000 Received: by mail-ej1-x635.google.com with SMTP id y20so12365882eju.7 for ; Thu, 21 Apr 2022 13:45:19 -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=QcnKk1q+JnTrlpID3Q2c9NWRolHaGoH5kvbqoMCrMyA=; b=KTDmukGT8U7MeeUltt+LVx/uUNwolShxKycz1M3XVfzygmX5P/z+lKGT0nBC4Wif2F rP4KUq94+GtyCEUDAAP8+kYjz1EVc1WYZX/w2X0vrUFEwwk12g9Z9G2s6B/9pyYCykng Z2iAsBQTPkbqzk+Uo39RSLCUpsv8kbqgjlPC+pMAdlzEd8FgLt3oMNAgl6uWjvbI3UAR GGaA9QcQa92WuuVKQhJBfmwYG10eouv1mIrszwAMthe97R1xoIaqUrTlLN4Y+LlQCBC2 yNReVb8zyqgn9clkoTyzIZffFQNIntVYo9n3eAgg2p4HXAO6wQNN4yf0wX9pTExIRBBk oQFQ== 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=QcnKk1q+JnTrlpID3Q2c9NWRolHaGoH5kvbqoMCrMyA=; b=Cu2dNomZE5lRcKaMlJRqsgeoThxahiEpqttS6IP5MTmqOnc6JO4E76jj7hofRMpyuo QN2QoIlCpt3WSCu+L4PFfRvvouXrfRFsrlkSZpLulGsueq030H9mbqr43g8/pTOgvkBj 4p+dS2ST4U0+SKYOiN/pog0qZf7ychfQHLZjKmrQyRWIEppVCIrnu/cTLuqV5YOP4s3X dTwlBXoxPUSX/bodl6B+kyfQCph71dNlFTH/zsguVgINFAJy5Ocnyq0189buKLEqDJ7a 1sHZkZEbMwTbbst1aksLer9VcuP5+d3NTyO+x/ozk3jGEhQ8TDjHJ5yZPz7p7a9Z0hql WX1w== X-Gm-Message-State: AOAM533eEfbckL4wiDkTrSOE+SmAZILeyRpW1G634ZCo+bcXZ+YpZo8+ y4u5GU1G5huALIINgrmU5ESDSY4tmug6GWsw2xCgMOGloZWk2XZzOKiJpi/1sfCoWWLo4q3r3BO y4heWVLeKyzC7rg6oOJ6ccn+2NiyNd3Tm60g173dNWYAa+U0z/3OnmVSaLgul3QyNM3HlS8TUUE eX1bEg9+UX0k3jLcpt2gnpggerbhNIiyjnHFXPKeLxOrMvjMIcZmw0 X-Google-Smtp-Source: ABdhPJyCML6Tuh1rvv6FnWV6EYORCS1kgAkStjlVir4pR0NSPdgiERHiDA1EVRN6TAqSlFSFjOqKxA== X-Received: by 2002:a17:907:ea1:b0:6e8:917b:6b6f with SMTP id ho33-20020a1709070ea100b006e8917b6b6fmr1114334ejc.409.1650573918633; Thu, 21 Apr 2022 13:45:18 -0700 (PDT) Received: from [10.137.0.17] (ip-86-49-250-165.net.upcbroadband.cz. [86.49.250.165]) by smtp.gmail.com with ESMTPSA id z8-20020a170906714800b006eff5936d60sm39361ejj.158.2022.04.21.13.45.17 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 21 Apr 2022 13:45:18 -0700 (PDT) Message-ID: Date: Thu, 21 Apr 2022 22:45: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: Tom Lane Cc: Richard Guo , PostgreSQL Hackers References: <29196a1e-ed47-c7ca-9be2-b1c636816183@enterprisedb.com> <48ee60e4-7ec3-ab21-ff67-e7f9dc2e37b1@enterprisedb.com> <3399045.1650567813@sss.pgh.pa.us> From: Tomas Vondra In-Reply-To: <3399045.1650567813@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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 21:03, Tom Lane wrote: > Tomas Vondra writes: >> On 4/21/22 10:29, Richard Guo wrote: >>> 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? > > I think we should just drop this cross-check altogether; it is not nearly > useful enough to justify the work that'd be involved in maintaining > cterefcount accurately for all such transformations. All it's really > there for is to be sure that we don't need to make a subplan for the > inlined CTE. > > There are two downstream consumers of cte_plan_ids, which currently just > have Asserts that we made a subplan. I think it'd be worth converting > those to real run-time tests, which leads me to something more or less as > attached. > WFM. I'm not particularly attached to the assert, so if you say it's not worth it let's get rid of it. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company