Re: BUG #13514: PostgreSQL backend process crashes on jsonb_object_agg() - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #13514: PostgreSQL backend process crashes on jsonb_object_agg()
Date
Msg-id 26929.1437621293@sss.pgh.pa.us
Whole thread Raw
In response to BUG #13514: PostgreSQL backend process crashes on jsonb_object_agg()  (b.yordanov2@gmail.com)
Responses Re: BUG #13514: PostgreSQL backend process crashes on jsonb_object_agg()
List pgsql-bugs
b.yordanov2@gmail.com writes:
> jsonb_object_agg() appears to fail when the second argument (in this case
> attributes->z->'tt') is a nonexistent jsonb column (there is
> attributes->z->'t' but not 'tt').

Seems to be failure to handle nulls properly.  The attached patch stops
the crash for me, but I've not checked to see if the results are "right",
nor scouted around to see if there are similar bugs nearby.

            regards, tom lane

diff --git a/src/backend/utils/adt/jsonb.c b/src/backend/utils/adt/jsonb.c
index e689722..b82bde0 100644
*** a/src/backend/utils/adt/jsonb.c
--- b/src/backend/utils/adt/jsonb.c
*************** jsonb_agg_transfn(PG_FUNCTION_ARGS)
*** 1606,1612 ****

      memset(&elem, 0, sizeof(JsonbInState));

!     datum_to_jsonb(val, false, &elem, tcategory, outfuncoid, false);

      jbelem = JsonbValueToJsonb(elem.res);

--- 1606,1612 ----

      memset(&elem, 0, sizeof(JsonbInState));

!     datum_to_jsonb(val, PG_ARGISNULL(1), &elem, tcategory, outfuncoid, false);

      jbelem = JsonbValueToJsonb(elem.res);

*************** jsonb_object_agg_transfn(PG_FUNCTION_ARG
*** 1759,1765 ****

      memset(&elem, 0, sizeof(JsonbInState));

!     datum_to_jsonb(val, false, &elem, tcategory, outfuncoid, true);

      jbkey = JsonbValueToJsonb(elem.res);

--- 1759,1765 ----

      memset(&elem, 0, sizeof(JsonbInState));

!     datum_to_jsonb(val, PG_ARGISNULL(1), &elem, tcategory, outfuncoid, true);

      jbkey = JsonbValueToJsonb(elem.res);

*************** jsonb_object_agg_transfn(PG_FUNCTION_ARG
*** 1777,1783 ****

      memset(&elem, 0, sizeof(JsonbInState));

!     datum_to_jsonb(val, false, &elem, tcategory, outfuncoid, false);

      jbval = JsonbValueToJsonb(elem.res);

--- 1777,1783 ----

      memset(&elem, 0, sizeof(JsonbInState));

!     datum_to_jsonb(val, PG_ARGISNULL(2), &elem, tcategory, outfuncoid, false);

      jbval = JsonbValueToJsonb(elem.res);


pgsql-bugs by date:

Previous
From: b.yordanov2@gmail.com
Date:
Subject: BUG #13514: PostgreSQL backend process crashes on jsonb_object_agg()
Next
From: Michael Paquier
Date:
Subject: Memory leak in XLOG reader facility when decoding records (caused by WAL refactoring)