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 1t1qfz-000iPo-Pi for pgsql-bugs@arkaria.postgresql.org; Fri, 18 Oct 2024 17:22:11 +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 1t1qfx-007mLN-CK for pgsql-bugs@arkaria.postgresql.org; Fri, 18 Oct 2024 17:22:09 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t1qfx-007mLF-3b for pgsql-bugs@lists.postgresql.org; Fri, 18 Oct 2024 17:22:09 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t1qfu-001ZgN-To for pgsql-bugs@lists.postgresql.org; Fri, 18 Oct 2024 17:22:08 +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 49IHM57L907539; Fri, 18 Oct 2024 13:22:05 -0400 From: Tom Lane To: Amit Langote cc: Tender Wang , Michael Paquier , exclusion@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18657: Using JSON_OBJECTAGG with volatile function leads to segfault In-reply-to: References: <18657-1b90ccce2b16bdb8@postgresql.org> <71060.1729088799@sss.pgh.pa.us> <508107.1729126612@sss.pgh.pa.us> <569213.1729138329@sss.pgh.pa.us> <711200.1729197017@sss.pgh.pa.us> Comments: In-reply-to Amit Langote message dated "Fri, 18 Oct 2024 15:14:06 +0900" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <907537.1729272125.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 18 Oct 2024 13:22:05 -0400 Message-ID: <907538.1729272125@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Amit Langote writes: > Another version which expands the comment of JsonValueExpr a little furt= her. I still don't like this approach to eval_const_expressions one bit. It's undocumented and it pessimizes as many cases as it optimizes. Sure, in the case where we can fold formatted_expr to a constant, we avoid processing raw_expr at all --- but if we fail to do that, we end by recursively simplifying formatted_expr twice (and raw_expr once). That's not a win. I think it should look more like /* * If we can fold formatted_expr to a constant, we can elide * the JsonValueExpr altogether. Otherwise we must process * raw_expr too. But JsonFormat is a flat node and requires * no simplification, only copying. */ formatted =3D eval_const_expressions_mutator((Node *) jve->formatt= ed_expr, context); if (formatted && IsA(formatted, Const)) return formatted; newnode =3D makeNode(JsonValueExpr); newnode->formatted_expr =3D formatted; newnode->raw_expr =3D eval_const_expressions_mutator((Node *) jve->raw_ex= pr, context); newnode->format =3D copyObject(jve->format); return newnode; (Untested, probably requires more casts than I wrote.) Also, in primnodes.h, personally I'd write + Expr *raw_expr; /* user-specified expression */ "raw" is a misnomer here, and even if we're not going to rename the field, we're not helping anyone by using that word in the comment. regards, tom lane