这可能为时过早,postgres 9.4 仍处于测试阶段。我一直在试验 jsonb 类型。我没有运气将 jsonb 类型传递给 plv8 函数,它以字符串类型出现。我在想我是不是做错了?
create or replace function jt ( o json ) returns integer language plv8 immutable
AS $function$
plv8.elog(INFO, 'type is ', typeof o);
plv8.elog(INFO, 'argument is ', o);
plv8.elog(INFO, 'member is ', o['member']);
$function$;
create or replace function jt ( o jsonb ) returns integer language plv8 immutable
AS $function$
plv8.elog(INFO, 'type is ', typeof o);
plv8.elog(INFO, 'argument is ', o);
plv8.elog(INFO, 'member is ', o['member']);
$function$;
然后,当我使用 json 运行时:
psql=# select jt('{"member":"test"}'::json);
INFO: type is object
INFO: argument is [object Object]
INFO: member is test
但是,当我使用 jsonb 运行时:
psql=# select jt('{"member":"test"}'::jsonb);
INFO: type is string
INFO: argument is {"member": "test"}
INFO: member is undefined
-g
最佳答案
PL/V8 需要添加对 JSONB 的支持,然后才能按照您的预期工作。
与此同时,您仍然可以传递纯 json
;只需将 jsonb
转换为 json
。
2015 年以来的新闻
plv8 project 在 2014 年之后更改为 this comment of the project's sponsor and main contributor, JerrySievert , 2017 年:
the JSONB vs JSON speed differences were well known and addressed,
JSONB now does a direct conversion in C++ into v8 objects and runs faster than a JSON conversion
last revision history显示一些关于 JSONb 开始日期的线索:
2015-05-26(版本 1.4.4)“在函数边界添加 jsonb 类型强制转换”。
在 2019-03-23(版本 2.3.10)“添加直接 jsonb 转换选项”,“添加用于 jsonb 转换的内存上下文”。
建议将 JSONB 与 plv8 版本 2.3.10 或更高版本一起使用...不过,对于旧版本的 plv8,Jarry confirmed几乎一切都会好起来的:
JSONB support has been active and complete since April 22, 2015.
关于postgresql - jsonb类型的plv8存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25433445/