Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plv8 stored procedure with jsonb type

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

like image 609
Greg Avatar asked Aug 21 '14 18:08

Greg


People also ask

What is Jsonb data type?

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.

Should I use Jsonb or JSON?

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.

What is Jsonb in Postgres?

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.


1 Answers

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.

News since ~2015

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.

like image 75
Craig Ringer Avatar answered Oct 11 '22 18:10

Craig Ringer