This may be premature, postgres 9.4 is still in beta. I've been experimenting with the jsonb type. I am not having any luck passing a jsonb type to a plv8 function, it comes in as type string. I was wondering if I was doing it wrong?
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$;
then, when I run using json:
psql=# select jt('{"member":"test"}'::json);
INFO: type is object
INFO: argument is [object Object]
INFO: member is test
but, when I run with jsonb:
psql=# select jt('{"member":"test"}'::jsonb);
INFO: type is string
INFO: argument is {"member": "test"}
INFO: member is undefined
-g
The JSONB data type stores JSON (JavaScript Object Notation) data as a binary representation of the JSONB value, which eliminates whitespace, duplicate keys, and key ordering. JSONB supports GIN indexes.
In general, most applications should prefer to store JSON data as jsonb , unless there are quite specialized needs, such as legacy assumptions about ordering of object keys. RFC 7159 specifies that JSON strings should be encoded in UTF8.
JSONB stands for “JSON Binary” or “JSON better” depending on whom you ask. It is a decomposed binary format to store JSON. JSONB supports indexing the JSON data, and is very efficient at parsing and querying the JSON data. In most cases, when you work with JSON in PostgreSQL, you should be using JSONB.
PL/V8 will need to add support for JSONB before this works how you expect.
In the mean time you can still pass plain json
; just cast the jsonb
to json
.
plv8 project changed after 2014, as this comment of the project's sponsor and main contributor, JerrySievert, in 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
The last revision history show some clues about JSONb starting date:
in 2015-05-26 (version 1.4.4 ) "Add jsonb type coercion in function boundary".
in 2019-03-23 (version 2.3.10) "add direct jsonb conversion option", "add memory context for jsonb conversion".
The suggestion is to use JSONB with plv8 version 2.3.10 or later... Still, for older versions of plv8, Jarry confirmed that almost everything will be fine:
JSONB support has been active and complete since April 22, 2015.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With