Re: printing JsonbPair values of input JSONB on server side?

From: T L <tinlyx(at)gmail(dot)com>
To: Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: printing JsonbPair values of input JSONB on server side?
Date: 2019-03-18 21:57:31
Message-ID: CAOb=C0ebwj_BfLQRHKDOJQRgRPbYt6ktqtEgM_da3Y9HmB921w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

My fault on the first line.
You are right. The value type isn't actually numeric.

I changed the problem lines to:

//problem lines!!! //either elog crashes pg server
char *buf = pnstrdup(ptr->key.val.string.val,
ptr->key.val.string.len);
elog(NOTICE, "print_kv_pair(): k = %s", buf); //debug
if (ptr->value.type != jbvNumeric) {
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("value must be numeric")));
}
elog(NOTICE, "print_kv_pair(): v = %s",
DatumGetCString(DirectFunctionCall1(numeric_out,
NumericGetDatum(ptr->value.val.numeric))) ); //debug

Below is my test. It prints a strange character instead of "a"; and says
that the value isn't numeric.

I don't know why the value isn't numeric. I tried:
select print_kv_pair('{"a":1, "b": 2)');
and
select print_kv_pair('{"a":1.0, "b": 2.0}'::jsonb);

It seems there are other problems in my code, as if the memory `ptr` refers
to is invalid.
I suspect I didn't extract the JsonbPair correctly.

=> select print_kv_pair('{"a":1.0, "b": 2.0}'::jsonb);
NOTICE: print_kv_pair(): ok0
NOTICE: print_kv_pair(): ok1
NOTICE: print_kv_pair(): ok2
NOTICE: print_kv_pair(): ok3, nPairs = 2
NOTICE: print_kv_pair(): k = �J
ERROR: value must be numeric

On Mon, Mar 18, 2019 at 4:29 PM Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>
wrote:

> >>>>> "T" == T L <tinlyx(at)gmail(dot)com> writes:
>
> T> //problem lines!!! //either elog crashes pg server
> T> char *buf = pnstrdup(ptr->key.val.string.val,
> T> ptr-> key.val.string.len);
> T> elog(NOTICE, "print_kv_pair(): k = %s",
> T> (ptr->key).val.string.val); //debug
>
> It doesn't help to make a null-terminated copy of the string if you're
> then just going to try and print the original.
>
> elog(NOTICE, "print_kv_pair(): k = %s", buf);
>
> T> elog(NOTICE, "print_kv_pair(): v = %s",
> T> DatumGetCString(DirectFunctionCall1(numeric_out,
> T> NumericGetDatum(ptr->value.val.numeric))) ); //debug
>
> That should work, _provided_ that value.type == jbvNumeric - did you
> consider checking that first?
>
> --
> Andrew (irc:RhodiumToad)
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Andrew Gierth 2019-03-18 23:11:47 Re: printing JsonbPair values of input JSONB on server side?
Previous Message Andrew Gierth 2019-03-18 21:29:06 Re: printing JsonbPair values of input JSONB on server side?