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 1qJse6-0000kF-5g for pgsql-hackers@arkaria.postgresql.org; Thu, 13 Jul 2023 09:29:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qJse5-0004B5-3A for pgsql-hackers@arkaria.postgresql.org; Thu, 13 Jul 2023 09:29:57 +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 1qJse4-0004Aw-Q3 for pgsql-hackers@lists.postgresql.org; Thu, 13 Jul 2023 09:29:56 +0000 Received: from mail-yb1-xb36.google.com ([2607:f8b0:4864:20::b36]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qJse1-000AB4-DI for pgsql-hackers@postgresql.org; Thu, 13 Jul 2023 09:29:55 +0000 Received: by mail-yb1-xb36.google.com with SMTP id 3f1490d57ef6-cb4de3bd997so199166276.1 for ; Thu, 13 Jul 2023 02:29:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689240593; x=1691832593; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=WgPgDz3f753V2CeQBA+3mo112bkPZeJ4RH4f8XyZa60=; b=ffZmNyuHPwgs8/drGfJhENOSQrAAhqcH2GOvrvwoxr7joNGkceqadYbNX2ZcEPR+L+ gbeQgoMP9M4S8fqlqOrQ/YrTf+vGyPiprzvB66foE2BCirDXWg0PzSKnipOLc7I7bL+c fNuEvnF5BMzRmMipfgHf/j3/9VN+CEOB2WoSzdV6njNfOG1bXRWK2bXd9u84fUKXsNjA iUyo9mUUGJrcoHXj9FtR8BnbvKUBVyVImD+xsEtC5TjEntEPCNrtOTkmZHN3DEOTLW9u eacVds2RRjeTAwyje8Rp6QS9uvn6/GZ8PL+ZOb80puB2Pmq8CchRUJVpLEaRjsI+IC04 I4Kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689240593; x=1691832593; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=WgPgDz3f753V2CeQBA+3mo112bkPZeJ4RH4f8XyZa60=; b=PlPs/Tga91ewSx/QjNCcIVpLotMSSUhy5NR2ZSzuUmooz95zR47BNMcvX49yG6fvAa B3WOdQvNnxmIwQHkQWBkjp37ABYt5sgiiNQLsa4cvN55dfhWlsgimqF+Bi6SQV0DFVut kfqikxBt8tJTS8+Wrx51jEMcPwTjRzvzcaPvUhID9XgSt3H9S3/xPoSIhx3ZPBmDHovl ByCAxLZ076bUMwAe/J9M4wFtb/f7Ie/n0v5+H99zBC3zeaLR1im/lGaSXd+Qzw+WkMsq lS8wKKb9w287O6UkL4MmmPoJJ/TrVu/8UQLCB2uylUMG9ejc3tPyxcR3NhITqJviGTNj sbNQ== X-Gm-Message-State: ABy/qLYkJA9Mj/ALk4OIrTX2jYL7tetnonv4mKmqrmTA6LtyVQJHSZlW cl0YeIOe+6rjMtRV6VmijzsIqX5d1aX9lqKC4+8= X-Google-Smtp-Source: APBJJlEGTougPFutgqftv0BU8nNn9wZ34+rIh50qKHCRfWwQZ+y8/OiMRxwvmDfeGRYw5F6Y9wWRBLL6YBj4XdDQL5w= X-Received: by 2002:a25:8d92:0:b0:bcf:d645:bd9 with SMTP id o18-20020a258d92000000b00bcfd6450bd9mr1181652ybl.3.1689240593559; Thu, 13 Jul 2023 02:29:53 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Richard Guo Date: Thu, 13 Jul 2023 17:29:42 +0800 Message-ID: Subject: Re: Check lateral references within PHVs for memoize cache keys To: David Rowley Cc: Paul A Jungwirth , PostgreSQL-development Content-Type: multipart/alternative; boundary="00000000000084f53906005af9cf" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000084f53906005af9cf Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sun, Jul 9, 2023 at 12:17=E2=80=AFPM David Rowley = wrote: > I just pushed a fix for this. Thanks for reporting it. BTW, I noticed a typo in the comment inside paraminfo_get_equal_hashops. foreach(lc, innerrel->lateral_vars) { Node *expr =3D (Node *) lfirst(lc); TypeCacheEntry *typentry; /* Reject if there are any volatile functions in PHVs */ if (contain_volatile_functions(expr)) { list_free(*operators); list_free(*param_exprs); return false; } The expressions in RelOptInfo.lateral_vars are not necessarily from PHVs. For instance explain (costs off) select * from t t1 join lateral (select * from t t2 where t1.a =3D t2.a offset 0) on true; QUERY PLAN ---------------------------------- Nested Loop -> Seq Scan on t t1 -> Memoize Cache Key: t1.a Cache Mode: binary -> Seq Scan on t t2 Filter: (t1.a =3D a) (7 rows) The lateral Var 't1.a' comes from the lateral subquery, not PHV. This seems a typo from 63e4f13d. How about we change it to the below? - /* Reject if there are any volatile functions in PHVs */ + /* Reject if there are any volatile functions in lateral vars */ Thanks Richard --00000000000084f53906005af9cf Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Sun, Jul 9, 2023 at 12:17=E2=80=AFPM D= avid Rowley <dgrowleyml@gmail.co= m> wrote:
I just pushed a fix for this.=C2=A0 Thanks for reporting it.

BTW, I noticed a typo in the comment inside paraminfo_get= _equal_hashops.

=C2=A0 =C2=A0 foreach(lc, innerrel->lateral_vars)=
=C2=A0 =C2=A0 {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 Node =C2=A0 =C2=A0 =C2= =A0 *expr =3D (Node *) lfirst(lc);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 TypeCache= Entry *typentry;

=C2=A0 =C2=A0 =C2=A0 =C2=A0 /* Reject if there are = any volatile functions in PHVs */
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (contai= n_volatile_functions(expr))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 {
=C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 list_free(*operators);
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 list_free(*param_exprs);
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 return false;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }

= The expressions in RelOptInfo.lateral_vars are not necessarily from
PHVs= .=C2=A0 For instance

explain (costs off)
select * from t t1 join<= br>=C2=A0 =C2=A0 lateral (select * from t t2 where t1.a =3D t2.a offset 0) = on true;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 QUERY PLAN
-------= ---------------------------
=C2=A0Nested Loop
=C2=A0 =C2=A0-> =C2= =A0Seq Scan on t t1
=C2=A0 =C2=A0-> =C2=A0Memoize
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0Cache Key: t1.a
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Ca= che Mode: binary
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-> =C2=A0Seq Scan = on t t2
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Filter: (= t1.a =3D a)
(7 rows)

The lateral Var 't1.a' comes from th= e lateral subquery, not PHV.

This seems a typo from 63e4f13d.=C2=A0 = How about we change it to the below?

- =C2=A0 =C2=A0 /* Reject if th= ere are any volatile functions in PHVs */
+ =C2=A0 =C2=A0 /* Reject if t= here are any volatile functions in lateral vars */

Thanks
Richard=
--00000000000084f53906005af9cf--