Thread

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Clarify the ra != rb case in compareJsonbContainers().

  2. Silence uninitialized-value warnings in compareJsonbContainers().

  1. patch: Use pg_assume in jsonb_util.c to fix GCC 15 warnings

    Dmitry Mityugov <d.mityugov@postgrespro.ru> — 2025-07-10T20:00:00Z

    When compiled with Assert() macro disabled, GCC 15 produces warnings 
    about possibly uninitialized variables in 
    src/backend/utils/adt/jsonb_util.c module. This problem was discussed in 
    detail in this thread, in April 2025: 
    https://www.postgresql.org/message-id/988bf1bc-3f1f-99f3-bf98-222f1cd9dc5e@xs4all.nl 
    .
    
    Recently introduced pg_assume() macro let fix such problems easily. The 
    attached patch fixes them in jsonb_util.c module. I verified that 
    PostgreSQL compiles clearly with this patch and GCC 15.1.1 on an x86 
    64-bit machine (with and without --enable-cassert), and with GCC 14.2.1 
    on a 64-bit ARM machine. `make check` also passes.
    
    I'm attaching the patch.
    
    Regards,
    Dmitry
  2. Re: patch: Use pg_assume in jsonb_util.c to fix GCC 15 warnings

    Erik Rijkers <er@xs4all.nl> — 2025-07-11T10:20:36Z

    Op 7/10/25 om 22:00 schreef Dmitry Mityugov:
    > When compiled with Assert() macro disabled, GCC 15 produces warnings 
    > about possibly uninitialized variables in 
    > src/backend/utils/adt/jsonb_util.c module. This problem was discussed in 
    > detail in this thread, in April 2025: 
    > https://www.postgresql.org/message-id/988bf1bc-3f1f-99f3-bf98-222f1cd9dc5e@xs4all.nl .
    > 
    > Recently introduced pg_assume() macro let fix such problems easily. The 
    > attached patch fixes them in jsonb_util.c module. I verified that 
    > PostgreSQL compiles clearly with this patch and GCC 15.1.1 on an x86 
    > 64-bit machine (with and without --enable-cassert), and with GCC 14.2.1 
    > on a 64-bit ARM machine. `make check` also passes.
    
    Yes, compiles fine for me with gcc 15.1.0  (with and without 
    --enable-cassert). It's nice to finally get a silent compile again.
    
    Thanks,
    
    Erik
    
    > 
    > I'm attaching the patch.
    > 
    > Regards,
    > Dmitry
    
    
    
    
  3. Re: patch: Use pg_assume in jsonb_util.c to fix GCC 15 warnings

    Tom Lane <tgl@sss.pgh.pa.us> — 2025-07-12T17:42:54Z

    Dmitry Mityugov <d.mityugov@postgrespro.ru> writes:
    > When compiled with Assert() macro disabled, GCC 15 produces warnings 
    > about possibly uninitialized variables in 
    > src/backend/utils/adt/jsonb_util.c module. This problem was discussed in 
    > detail in this thread, in April 2025: 
    > https://www.postgresql.org/message-id/988bf1bc-3f1f-99f3-bf98-222f1cd9dc5e@xs4all.nl 
    > .
    
    > Recently introduced pg_assume() macro let fix such problems easily. The 
    > attached patch fixes them in jsonb_util.c module. I verified that 
    > PostgreSQL compiles clearly with this patch and GCC 15.1.1 on an x86 
    > 64-bit machine (with and without --enable-cassert), and with GCC 14.2.1 
    > on a 64-bit ARM machine. `make check` also passes.
    
    I don't care for this patch: replacing an Assert with pg_assume just
    seems like a very bad idea.  If the assertion condition ever failed,
    which doesn't seem all that impossible in logic as complicated as
    this, then instead of an identifiable assertion trap we'd get
    unspecified and impossible-to-debug behavior.
    
    We could conceivably do
    
    +#ifdef USE_ASSERT_CHECKING
                Assert(ra != WJB_END_ARRAY && ra != WJB_END_OBJECT);
                Assert(rb != WJB_END_ARRAY && rb != WJB_END_OBJECT);
    +#else
    +           pg_assume(ra != WJB_END_ARRAY && ra != WJB_END_OBJECT);
    +           pg_assume(rb != WJB_END_ARRAY && rb != WJB_END_OBJECT);
    +#endif
    
    but that seems ugly.  In any case, it's just doubling down on the
    assumption that a compiler capable of detecting the "may be used
    uninitialized" issue will conclude that rejecting WJB_END_ARRAY
    and WJB_END_OBJECT (but not WJB_DONE) eliminates the possibility of
    va.type/vb.type not being set.
    
    What I think we should do about this is what I mentioned in the
    other thread: adjust the code to guarantee that va.type/vb.type
    are defined in all cases.  There are a couple of ways we could
    do that, but after reflection I think the best way is to modify
    JsonbIteratorNext to make that guarantee.  I've checked that
    the attached silences the warning on gcc 15.1.1 (current
    Fedora 42).
    
    			regards, tom lane
    
    
  4. Re: patch: Use pg_assume in jsonb_util.c to fix GCC 15 warnings

    Andres Freund <andres@anarazel.de> — 2025-07-12T17:55:32Z

    Hi,
    
    On 2025-07-12 13:42:54 -0400, Tom Lane wrote:
    > Dmitry Mityugov <d.mityugov@postgrespro.ru> writes:
    > > When compiled with Assert() macro disabled, GCC 15 produces warnings 
    > > about possibly uninitialized variables in 
    > > src/backend/utils/adt/jsonb_util.c module. This problem was discussed in 
    > > detail in this thread, in April 2025: 
    > > https://www.postgresql.org/message-id/988bf1bc-3f1f-99f3-bf98-222f1cd9dc5e@xs4all.nl 
    > > .
    > 
    > > Recently introduced pg_assume() macro let fix such problems easily. The 
    > > attached patch fixes them in jsonb_util.c module. I verified that 
    > > PostgreSQL compiles clearly with this patch and GCC 15.1.1 on an x86 
    > > 64-bit machine (with and without --enable-cassert), and with GCC 14.2.1 
    > > on a 64-bit ARM machine. `make check` also passes.
    > 
    > I don't care for this patch: replacing an Assert with pg_assume just
    > seems like a very bad idea.  If the assertion condition ever failed,
    > which doesn't seem all that impossible in logic as complicated as
    > this, then instead of an identifiable assertion trap we'd get
    > unspecified and impossible-to-debug behavior.
    
    That shouldn't be a problem - pg_assume() is defined to be an Assert() in
    USE_ASSERT_CHECKING builds.
    
    
    > We could conceivably do
    > 
    > +#ifdef USE_ASSERT_CHECKING
    >             Assert(ra != WJB_END_ARRAY && ra != WJB_END_OBJECT);
    >             Assert(rb != WJB_END_ARRAY && rb != WJB_END_OBJECT);
    > +#else
    > +           pg_assume(ra != WJB_END_ARRAY && ra != WJB_END_OBJECT);
    > +           pg_assume(rb != WJB_END_ARRAY && rb != WJB_END_OBJECT);
    > +#endif
    > 
    > but that seems ugly.  In any case, it's just doubling down on the
    > assumption that a compiler capable of detecting the "may be used
    > uninitialized" issue will conclude that rejecting WJB_END_ARRAY
    > and WJB_END_OBJECT (but not WJB_DONE) eliminates the possibility of
    > va.type/vb.type not being set.
    
    I had played with using pg_assume here too, but I couldn't really convince
    myself that it's a good idea...
    
    
    > What I think we should do about this is what I mentioned in the
    > other thread: adjust the code to guarantee that va.type/vb.type
    > are defined in all cases.  There are a couple of ways we could
    > do that, but after reflection I think the best way is to modify
    > JsonbIteratorNext to make that guarantee.  I've checked that
    > the attached silences the warning on gcc 15.1.1 (current
    > Fedora 42).
    
    WFM.
    
    Greetings,
    
    Andres Freund
    
    
    
    
  5. Re: patch: Use pg_assume in jsonb_util.c to fix GCC 15 warnings

    Tom Lane <tgl@sss.pgh.pa.us> — 2025-07-12T18:04:53Z

    Andres Freund <andres@anarazel.de> writes:
    > On 2025-07-12 13:42:54 -0400, Tom Lane wrote:
    >> I don't care for this patch: replacing an Assert with pg_assume just
    >> seems like a very bad idea.
    
    > That shouldn't be a problem - pg_assume() is defined to be an Assert() in
    > USE_ASSERT_CHECKING builds.
    
    Ah, my bad.  But there's still the question of exactly what reasoning
    the compiler is using to arrive at the conclusion that it need not
    warn given these assertions, and whether we want to rely on that
    reasoning not changing.  I'd prefer to simplify matters.
    
    			regards, tom lane
    
    
    
    
  6. Re: patch: Use pg_assume in jsonb_util.c to fix GCC 15 warnings

    Tom Lane <tgl@sss.pgh.pa.us> — 2025-07-12T19:23:26Z

    After further study I've understood what was bothering me about
    the logic in compareJsonbContainers (lines 280ff).  Because
    JsonbIteratorNext doesn't presently guarantee to set val->type
    when returning WJB_DONE, the stanza appears to be at risk of
    undefined behavior should one iterator return that while the other
    returns something else.  The comment fails to discuss this case,
    and the code doesn't consider it either.  But in reality, the case
    cannot happen for the exact same reason that WJB_END_ARRAY and
    WJB_END_OBJECT can't occur: our earlier tests of object and array
    structure & length matching guarantee that we can't reach the end
    of one input before the other.
    
    So I think we need to rewrite that comment and extend the assertions,
    along the lines of the separate patch attached.  This gets through
    check-world, which is unsurprising because if it did not we'd have
    seen use-of-undefined-value Valgrind complaints long since.
    
    I still think that we should silence the compiler warnings by
    removing the undefined-ness per my previous patch.  I'm inclined
    to back-patch that (for people compiling old branches with latest
    compiler) but apply this bit to HEAD only (since it's just a code
    clarification).
    
    			regards, tom lane
    
    
  7. Re: patch: Use pg_assume in jsonb_util.c to fix GCC 15 warnings

    Peter Geoghegan <pg@bowt.ie> — 2025-07-15T22:35:01Z

    On Sat, Jul 12, 2025 at 1:55 PM Andres Freund <andres@anarazel.de> wrote:
    > I had played with using pg_assume here too, but I couldn't really convince
    > myself that it's a good idea...
    
    In the past, it was often necessary to work around MSVC's inability to
    see that a block containing elog(ERROR) doesn't actually need to
    initialize variables that'll never actually be used in code that comes
    after that block. We still have many "keep compiler quiet" variable
    initializations due to this.
    
    Is that still something that we need to worry about? I don't recall
    running into it in quite a few years, though that might just be a
    coincidence.
    
    It looks like MSVC uses __assume for this now, by way of
    elog/ereport's use of pg_unreachable. You also used __assume to
    implement pg_assume for MSVC. It seems reasonable to surmise that we
    can officially stop worrying about elog(ERROR) related warnings/to
    suppose that we no longer have to add "keep compiler quiet" variable
    initializations after an elog(ERROR). If it works for MSVC +
    pg_assume, then it ought to also work for MSVC + pg_unreachable.
    Right?
    
    -- 
    Peter Geoghegan
    
    
    
    
  8. Re: patch: Use pg_assume in jsonb_util.c to fix GCC 15 warnings

    Tom Lane <tgl@sss.pgh.pa.us> — 2025-07-15T22:58:10Z

    Peter Geoghegan <pg@bowt.ie> writes:
    > In the past, it was often necessary to work around MSVC's inability to
    > see that a block containing elog(ERROR) doesn't actually need to
    > initialize variables that'll never actually be used in code that comes
    > after that block. We still have many "keep compiler quiet" variable
    > initializations due to this.
    
    > Is that still something that we need to worry about?
    
    Good question.  It's not very clear what set of compiler versions
    people are still using, but maybe it'd be okay to stop worrying
    about suppressing such warnings.
    
    			regards, tom lane
    
    
    
    
  9. Re: patch: Use pg_assume in jsonb_util.c to fix GCC 15 warnings

    Peter Geoghegan <pg@bowt.ie> — 2025-07-15T23:05:13Z

    On Tue, Jul 15, 2025 at 6:58 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
    > Good question.  It's not very clear what set of compiler versions
    > people are still using, but maybe it'd be okay to stop worrying
    > about suppressing such warnings.
    
    It would be nice to eliminate all existing "keep compiler quiet"
    variable initializations that follow an elog/ereport with elevel >=
    ERROR. My guess is that we have several hundred.
    
    -- 
    Peter Geoghegan
    
    
    
    
  10. Re: patch: Use pg_assume in jsonb_util.c to fix GCC 15 warnings

    Tom Lane <tgl@sss.pgh.pa.us> — 2025-07-15T23:27:18Z

    Peter Geoghegan <pg@bowt.ie> writes:
    > It would be nice to eliminate all existing "keep compiler quiet"
    > variable initializations that follow an elog/ereport with elevel >=
    > ERROR. My guess is that we have several hundred.
    
    There are a lot of them, for sure.  I'd be a bit worried about
    creating a back-patching mine-field.  But maybe these are all
    in spots we're unlikely to touch?
    
    			regards, tom lane
    
    
    
    
  11. Re: patch: Use pg_assume in jsonb_util.c to fix GCC 15 warnings

    Peter Geoghegan <pg@bowt.ie> — 2025-07-15T23:30:21Z

    On Tue, Jul 15, 2025 at 7:27 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
    > There are a lot of them, for sure.
    
    It's *very* common for switch statements to have a "can't happen"
    elog(ERROR) as their default block. Many of these default blocks also
    initialize related variables to placate the compiler.
    
    > I'd be a bit worried about
    > creating a back-patching mine-field.  But maybe these are all
    > in spots we're unlikely to touch?
    
    That seems like much less of a problem for a purely subtractive change
    such as this.
    
    -- 
    Peter Geoghegan
    
    
    
    
  12. Re: patch: Use pg_assume in jsonb_util.c to fix GCC 15 warnings

    Tom Lane <tgl@sss.pgh.pa.us> — 2025-07-15T23:52:05Z

    Peter Geoghegan <pg@bowt.ie> writes:
    > On Tue, Jul 15, 2025 at 7:27 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
    >> I'd be a bit worried about
    >> creating a back-patching mine-field.  But maybe these are all
    >> in spots we're unlikely to touch?
    
    > That seems like much less of a problem for a purely subtractive change
    > such as this.
    
    Not really convinced.  Taking a sample at random (from ExecRenameStmt
    in alter.c):
    
                    return address;
                }
    
            default:
                elog(ERROR, "unrecognized rename stmt type: %d",
                     (int) stmt->renameType);
                return InvalidObjectAddress;    /* keep compiler happy */
        }
    }
    
    Assume we remove the "return InvalidObjectAddress;" line and later
    need to back-patch a change touching this area.
    
    If we were to add/change something in front of the "default:", we're
    probably fine because the "default:" and the elog() would be enough
    context lines to allow patch(1) to figure out where to put the
    addition/change.  However, if we wanted to add something after the
    switch construct, we'd get an apply failure and have to fix it
    manually --- or worse, patch(1) would apply the delta in the
    wrong place.
    
    I'm not sure how likely such scenarios are, but it doesn't seem
    zero-risk.  And if we do hundreds of these, the odds of trouble
    will increase.
    
    			regards, tom lane
    
    
    
    
  13. Re: patch: Use pg_assume in jsonb_util.c to fix GCC 15 warnings

    Peter Geoghegan <pg@bowt.ie> — 2025-07-16T00:01:57Z

    On Tue, Jul 15, 2025 at 7:52 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
    > I'm not sure how likely such scenarios are, but it doesn't seem
    > zero-risk.  And if we do hundreds of these, the odds of trouble
    > will increase.
    
    I agree that it's not zero-risk -- hardly anything ever is. Still
    seems worth considering.
    
    -- 
    Peter Geoghegan
    
    
    
    
  14. Re: patch: Use pg_assume in jsonb_util.c to fix GCC 15 warnings

    Peter Eisentraut <peter@eisentraut.org> — 2025-07-16T09:07:41Z

    On 16.07.25 01:35, Peter Geoghegan wrote:
    > On Sat, Jul 12, 2025 at 1:55 PM Andres Freund <andres@anarazel.de> wrote:
    >> I had played with using pg_assume here too, but I couldn't really convince
    >> myself that it's a good idea...
    > 
    > In the past, it was often necessary to work around MSVC's inability to
    > see that a block containing elog(ERROR) doesn't actually need to
    > initialize variables that'll never actually be used in code that comes
    > after that block. We still have many "keep compiler quiet" variable
    > initializations due to this.
    > 
    > Is that still something that we need to worry about? I don't recall
    > running into it in quite a few years, though that might just be a
    > coincidence.
    
    Yes, this continues to be an unsolved problem.  See here for a recent 
    discussion: 
    https://www.postgresql.org/message-id/CAApHDvrFdXjbrV6KCx_GHKYSufUbNDYSsjppcJQiGOURfJE6qg@mail.gmail.com