Re: Extract numeric filed in JSONB more effectively

Andy Fan <zhihui.fan1213@gmail.com>

From: Andy Fan <zhihui.fan1213@gmail.com>
To: jian he <jian.universality@gmail.com>
Cc: Chapman Flack <chap@anastigmatix.net>, Pavel Stehule <pavel.stehule@gmail.com>, Tom Lane <tgl@sss.pgh.pa.us>, pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2023-09-04T11:43:50Z
Lists: pgsql-hackers
Hi Jian,

 SELECT (test_json -> 'field1')::int4 FROM test_jsonb WHERE json_type
> = 'object';
> -ERROR:  cannot cast jsonb string to type integer
> +ERROR:  unknown jsonb type: 1125096840
>

Thanks for the report!  The reason is I return the address of a local
variable.

jsonb_object_field_start(PG_FUNCTION_ARGS)
{

    JsonbValue  *v;
    JsonbValue  vbuf;
    v = getKeyJsonValueFromContainer(&jb->root,
                                     VARDATA_ANY(key),\
                                     VARSIZE_ANY_EXHDR(key),
                                     &vbuf);
    PG_RETURN_POINTER(v);
}

Here the v points to vbuf which is a local variable in stack.  I'm confused
that why it works on my local machine and also works in the most queries
in cfbot, the fix is below

    v = getKeyJsonValueFromContainer(&jb->root,
                                     VARDATA_ANY(key),\
                                     VARSIZE_ANY_EXHDR(key),
                                     NULL);


I will send an updated version soon.

-- 
Best Regards
Andy Fan

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Add trailing commas to enum definitions