Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sNCNW-007Kqt-42 for pgsql-hackers@arkaria.postgresql.org; Fri, 28 Jun 2024 14:15:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sNCNT-00517e-Is for pgsql-hackers@arkaria.postgresql.org; Fri, 28 Jun 2024 14:15:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sNCNT-00517W-6V for pgsql-hackers@lists.postgresql.org; Fri, 28 Jun 2024 14:15:03 +0000 Received: from mail-pg1-x533.google.com ([2607:f8b0:4864:20::533]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sNCNQ-0040Ct-Qx for pgsql-hackers@postgresql.org; Fri, 28 Jun 2024 14:15:02 +0000 Received: by mail-pg1-x533.google.com with SMTP id 41be03b00d2f7-707040e3017so462244a12.3 for ; Fri, 28 Jun 2024 07:15:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719584098; x=1720188898; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:subject:from:user-agent:mime-version:date:message-id:from:to :cc:subject:date:message-id:reply-to; bh=rOzQO+l26xTiY7cdib1Nv2M8PEBGHi6oMtxC8QVgqWs=; b=Fb91casRIlrNI95AjbQfbr90YN3guORIs0suh+d4SCkG2eph8sp6/tOqnMg8i7acND PUFsTzzXmG8T3wGOCWvYrTnRWMmxTZRN6vF7BilWeECzmvl6EdDPA1MWyzkQmN0Q+l7b cEojXOIO4jF7AFyDOaYVJ4Lwxv9bYTRfbX4RPchm/7J4RtN05mdKGAkcJWLdVAvgoKDU 2lxpAQuwwPZp2hDaFATyvNSSrzWJM9ZMWtI1mjq4DKUhvE5wZEe5LmReTUMLoEtuuU8Z 2GHarYeoHsnxA2vkOzB3WnWc2ePKkATFm01zeYy05wHzql1bHNG5NwHhWL9apidVs3Qe O43g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719584098; x=1720188898; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:subject:from:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=rOzQO+l26xTiY7cdib1Nv2M8PEBGHi6oMtxC8QVgqWs=; b=EeTsxysM9PzpU95mNs1vD1I/P7xpWZbFKKbmw+PlB9yTe7NIXBJY5BLCKdW1odhmPJ P0ROuFYqvuQVjmCUMcIs9oqwet8tCbLl+pcE/rkwqMgm7CYJBg+AUiYZgY3iZBSziPLM y5XPGTdDBRsNrX5Fph863CoBG+TK3XjDleuZyoD587LS14jq8teLEWihdfKMcPhRJRx0 Jk84uPN3m3bRki5VX+P6wt5DNkheM8ToBRf53k9jtSj4M8sCTYJoozY+QiZu85vNvobo Bq6kmSPLATa6yXz8Q6X5dofB+6deg++PBmsdh1dvJiPj6ByeB9jQNaZyZf5kEDGnBEsd 5Hzg== X-Gm-Message-State: AOJu0YxzWwVVSosbjf+XCnFeKvEL9TrmwcjuCftL01M7hwDULv0RsiII LmX/VrzVj71nM8s3suNmmXotEzockkpNCVv8P+9RakthB22FkfBT X-Google-Smtp-Source: AGHT+IE4McH1Y/2ct3J79DfvnxufSinUt8hHXxW692XAlT3cexQ1mmoHTvIl23BhBk+CtJIL4+4Vow== X-Received: by 2002:a17:90b:14d:b0:2c7:ad68:f99f with SMTP id 98e67ed59e1d1-2c8614ab757mr14258132a91.46.1719584098176; Fri, 28 Jun 2024 07:14:58 -0700 (PDT) Received: from [192.168.1.40] (cm-171-100-250-52.revip10.asianet.co.th. [171.100.250.52]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2c91d3eed7csm1651088a91.55.2024.06.28.07.14.56 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 28 Jun 2024 07:14:57 -0700 (PDT) Message-ID: Date: Fri, 28 Jun 2024 21:14:53 +0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: Andrei Lepikhov Subject: Re: Check lateral references within PHVs for memoize cache keys To: Richard Guo Cc: PostgreSQL-development , David Rowley , Tom Lane References: <1724688.1688837323@sss.pgh.pa.us> Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 6/18/24 08:47, Richard Guo wrote: > On Mon, Mar 18, 2024 at 4:36 PM Richard Guo wrote: >> Here is another rebase over master so it applies again. I also added a >> commit message to help review. Nothing else has changed. > > AFAIU currently we do not add Memoize nodes on top of join relation > paths. This is because the ParamPathInfos for join relation paths do > not maintain ppi_clauses, as the set of relevant clauses varies > depending on how the join is formed. In addition, joinrels do not > maintain lateral_vars. So we do not have a way to extract cache keys > from joinrels. > > (Besides, there are places where the code doesn't cope with Memoize path > on top of a joinrel path, such as in get_param_path_clause_serials.) > > Therefore, when extracting lateral references within PlaceHolderVars, > there is no need to consider those that are due to be evaluated at > joinrels. > > Hence, here is v7 patch for that. In passing, this patch also includes > a comment explaining that Memoize nodes are currently not added on top > of join relation paths (maybe we should have a separate patch for this?). Hi, I have reviewed v7 of the patch. This improvement is good enough to be applied, thought. Here is some notes: Comment may be rewritten for clarity: "Determine if the clauses in param_info and innerrel's lateral_vars" - I'd replace lateral_vars with 'lateral references' to combine in one phrase PHV from rel and root->placeholder_list sources. I wonder if we can add whole PHV expression instead of the Var (as discussed above) just under some condition: if (!bms_intersect(pull_varnos(root, (Node *) phinfo->ph_var->phexpr), innerrelids)) { // Add whole PHV } else { // Add only pulled vars } I got the point about Memoize over join, but as a join still calls replace_nestloop_params to replace parameters in its clauses, why not to invent something similar to find Memoize keys inside specific JoinPath node? It is not the issue of this patch, though - but is it doable? IMO, the code: if (bms_nonempty_difference(outerPlan->chgParam, node->keyparamids)) cache_purge_all(node); is a good place to check an assertion: is it really the parent query parameters that make a difference between memoize keys and node list of parameters? Generally, this patch looks good for me to be committed. -- regards, Andrei Lepikhov