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 1rdbIx-006SHm-W0 for pgsql-bugs@arkaria.postgresql.org; Fri, 23 Feb 2024 19:33:56 +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 1rdbIw-002i4G-Jg for pgsql-bugs@arkaria.postgresql.org; Fri, 23 Feb 2024 19:33:55 +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 1rdbIw-002i47-BR for pgsql-bugs@lists.postgresql.org; Fri, 23 Feb 2024 19:33:54 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rdbIu-000ZMR-64 for pgsql-bugs@lists.postgresql.org; Fri, 23 Feb 2024 19:33:54 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 41NJXnUo891307; Fri, 23 Feb 2024 14:33:49 -0500 From: Tom Lane To: exclusion@gmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18360: Invalid memory access occurs when using geqo In-reply-to: <831847.1708710339@sss.pgh.pa.us> References: <18360-a23caf3157f34e62@postgresql.org> <831847.1708710339@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Fri, 23 Feb 2024 12:45:39 -0500" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <891278.1708716812.0@sss.pgh.pa.us> Date: Fri, 23 Feb 2024 14:33:49 -0500 Message-ID: <891306.1708716829@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <891278.1708716812.1@sss.pgh.pa.us> I wrote: > Indeed. Curiously, the in_operators list, which I thought was > parallel to that, seems fine. Anyway, something's being careless > about which context it creates that data structure in. Shouldn't > be too hard to fix. The problem seems to arise from build_child_join_sjinfo(), which makes a translated version of the semi_rhs_exprs that is later propagated into a UniquePath for a base relation. This breaks GEQO's intention that base-relation structs will be long-lived while only join-relation data is short-lived. (in_operators is not modified so the original long-lived list is used for that, explaining why it's not trashed at the same time.) The simplest fix is as attached: just do a quick copyObject in create_unique_path. That's rather ugly, but create_unique_path is already taking explicit responsibility for the context that the Path is built in, so it doesn't quite exceed my threshold of pain. (The alternative of making build_child_join_sjinfo force its output to be long-lived doesn't look good: that gets invoked quite a few times during a GEQO cycle.) I chose to make it copy the in_operators list as well, even though that's not minimally necessary to fix the bug --- it would look odd if we didn't, and the copy is pretty cheap since it's only a short integer list. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="0001-fix-bug-18360.patch"; charset="us-ascii" Content-ID: <891278.1708716812.2@sss.pgh.pa.us> Content-Description: 0001-fix-bug-18360.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/optimizer/util/pathnode.c b/src/backend/optimizer= /util/pathnode.c index b3902b0032..6f79b2e3fe 100644 --- a/src/backend/optimizer/util/pathnode.c +++ b/src/backend/optimizer/util/pathnode.c @@ -1707,8 +1707,13 @@ create_unique_path(PlannerInfo *root, RelOptInfo *r= el, Path *subpath, pathnode->path.pathkeys =3D NIL; = pathnode->subpath =3D subpath; - pathnode->in_operators =3D sjinfo->semi_operators; - pathnode->uniq_exprs =3D sjinfo->semi_rhs_exprs; + + /* + * Under GEQO, the sjinfo might be short-lived, so we'd better make copi= es + * of data structures we extract from it. + */ + pathnode->in_operators =3D copyObject(sjinfo->semi_operators); + pathnode->uniq_exprs =3D copyObject(sjinfo->semi_rhs_exprs); = /* * If the input is a relation and it has a unique index that proves the ------- =_aaaaaaaaaa0--