Re: Extract numeric filed in JSONB more effectively

From: Andy Fan <zhihui(dot)fan1213(at)gmail(dot)com>
To: jian he <jian(dot)universality(at)gmail(dot)com>
Cc: Chapman Flack <chap(at)anastigmatix(dot)net>, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Extract numeric filed in JSONB more effectively
Date: 2023-09-04 11:43:50
Message-ID: CAKU4AWqQjMREZpOOECC6duV-VbEYYAdP6EL+VaK=-X7q-JQjFw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Drouvot, Bertrand 2023-09-04 12:14:58 Re: Autogenerate some wait events code and documentation
Previous Message Alvaro Herrera 2023-09-04 11:00:05 Re: cataloguing NOT NULL constraints