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