Re: Making jsonb_agg() faster

jian he <jian.universality@gmail.com>

From: jian he <jian.universality@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-hackers@lists.postgresql.org
Date: 2025-08-14T09:41:33Z
Lists: pgsql-hackers

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Fix jsonb_object_agg crash after eliminating null-valued pairs.

  2. Micro-optimize datatype conversions in datum_to_jsonb_internal.

  3. Remove fundamentally-redundant processing in jsonb_agg() et al.

  4. Revise APIs for pushJsonbValue() and associated routines.

  5. Add a macro for the declared typlen of type timetz.

On Thu, Aug 14, 2025 at 4:59 PM jian he <jian.universality@gmail.com> wrote:
>
> > 0001 is a somewhat invasive refactoring of the API for
> > pushJsonbValue and friends.

in pushJsonbValue:

    /*
     * pushJsonbValueScalar handles all cases not involving pushing a
     * container object as an ELEM or VALUE.
     */
    if (!jbval || (seq != WJB_ELEM && seq != WJB_VALUE))
    {
        pushJsonbValueScalar(pstate, seq, jbval);
        return;
    }
    /* If it's not a jbvBinary value, again it goes to pushJsonbValueScalar */
    if (jbval->type != jbvBinary)
    {
        pushJsonbValueScalar(pstate, seq, jbval);
        return;
    }

we can combine above these two into
    if (!jbval || (seq != WJB_ELEM && seq != WJB_VALUE) ||
IsAJsonbScalar(jbval))
    {
        pushJsonbValueScalar(pstate, seq, jbval);
        return;
    }
and put it in the earlier position of pushJsonbValue.