pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Amit Langote <amitlangote09@gmail.com>
Cc: Tender Wang <tndrwang@gmail.com>
Cc: Michael Paquier <michael@paquier.xyz>
Cc: exclusion@gmail.com
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #18657: Using JSON_OBJECTAGG with volatile function leads to segfault
Date: Fri, 18 Oct 2024 13:22:05 -0400
Message-ID: <907538.1729272125@sss.pgh.pa.us> (raw)
In-Reply-To: <CA+HiwqGWJDVEscvUuXsUhFw6_=T+MbdbKVLb_NxtH4byJoMiJg@mail.gmail.com>
References: <18657-1b90ccce2b16bdb8@postgresql.org>
	<Zw8Gk3xtrSmQh9mD@paquier.xyz>
	<CA+HiwqFpzUYXOqcJ4LdAoLjAxDJ-e4-mVe=4z9-jC5vhV0ZwEg@mail.gmail.com>
	<CAHewXNmSvzsSqqf6fG7OEkmB2aUuCa7SM4pfVrs9vBeCWZu7yQ@mail.gmail.com>
	<CA+HiwqGDZMwi6O9w64vtd0fFTeow4GnKfWraSaJFxC-y7mCm+w@mail.gmail.com>
	<CAHewXN=hHQepA9-JHaW9z-orPE-qA2UEFNvAvLpYZr6e1PGq2Q@mail.gmail.com>
	<CA+HiwqF0iihKd28ycs5BJcYprAWUBDqHv03vqLMteBid2YDCyA@mail.gmail.com>
	<CAHewXNnc35-OX=wA+MHkj+asYa0oezLwV5by=D00ga+EawN7Fg@mail.gmail.com>
	<CA+HiwqGJUv2Y=qCRO1kL-6cfZ79zAgag_zJ_=U4q-RaNUv9r2g@mail.gmail.com>
	<71060.1729088799@sss.pgh.pa.us>
	<CA+HiwqGZV2YWvso8B4bpPAEJDuEhDos3oUTxDTfr9kpX=nOAgA@mail.gmail.com>
	<508107.1729126612@sss.pgh.pa.us>
	<569213.1729138329@sss.pgh.pa.us>
	<CA+HiwqHpU4VARMpEih6O5xd+psvODEzutf8Rkx7m+sU8OPV-bA@mail.gmail.com>
	<711200.1729197017@sss.pgh.pa.us>
	<CA+HiwqHyYCb2XbyCwHjP+atoscATGeWrXmdcwEOLY0mGYL3UwA@mail.gmail.com>
	<CA+HiwqHPMcVQWB_9JEfWvdAww8P4QUntfww-bDT7_hUtkrNEjQ@mail.gmail.com>
	<CA+HiwqG05q! j0Z7m2KQk3m2hvA1JsbHfEKevYfWK68umK90beow@mail.gmail.com>
	<CA+HiwqGWJDVEscvUuXsUhFw6_=T+MbdbKVLb_NxtH4byJoMiJg@mail.gmail.com>

Amit Langote <amitlangote09@gmail.com> writes:
> Another version which expands the comment of JsonValueExpr a little further.

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 = eval_const_expressions_mutator((Node *) jve->formatted_expr,
                                                   context);
        if (formatted && IsA(formatted, Const))
            return formatted;
        newnode = makeNode(JsonValueExpr);
	newnode->formatted_expr = formatted;
	newnode->raw_expr = eval_const_expressions_mutator((Node *) jve->raw_expr,
                                                           context);
        newnode->format = 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





view thread (31+ messages)  latest in thread

Message-ID: <907538.1729272125@sss.pgh.pa.us>
Permalink:  ../907538.1729272125@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/907538.1729272125@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, amitlangote09@gmail.com, tndrwang@gmail.com, michael@paquier.xyz, exclusion@gmail.com, pgsql-bugs@lists.postgresql.org
  Subject: Re: BUG #18657: Using JSON_OBJECTAGG with volatile function leads to segfault
  In-Reply-To: <907538.1729272125@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox