Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add jsonb type support for pg >- 90400
This change is backward-incompatible with existing applications that assume jsonb type to be interpreted in a string rather than a json object at the function boundary. The change is resonable as plv8 already does auto-coercion for the json type. Edits to Makefile and cosmetic issues by Hitoshi Harada.
- Loading branch information
1 parent
79d862b
commit 2e09144
Showing
5 changed files
with
127 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
CREATE FUNCTION convb(o jsonb) RETURNS jsonb AS $$ | ||
if (o instanceof Array) { | ||
o[1] = 10; | ||
} else if (typeof(o) == 'object') { | ||
o.i = 10; | ||
} | ||
return o; | ||
$$ LANGUAGE plv8; | ||
SELECT convb('{"i": 3, "b": 20}'); | ||
convb | ||
-------------------- | ||
{"b": 20, "i": 10} | ||
(1 row) | ||
|
||
SELECT convb('[1, 2, 3]'); | ||
convb | ||
------------ | ||
[1, 10, 3] | ||
(1 row) | ||
|
||
CREATE FUNCTION get_keyb(key text, json_raw jsonb) RETURNS jsonb | ||
LANGUAGE plv8 IMMUTABLE STRICT | ||
AS $$ | ||
var val = json_raw[key]; | ||
var ret = {}; | ||
ret[key] = val; | ||
return ret; | ||
$$; | ||
CREATE TABLE jsonbonly ( | ||
data jsonb | ||
); | ||
COPY jsonbonly (data) FROM stdin; | ||
-- Call twice to test the function cache. | ||
SELECT get_keyb('ok', data) FROM jsonbonly; | ||
get_keyb | ||
-------------- | ||
{"ok": true} | ||
(1 row) | ||
|
||
SELECT get_keyb('ok', data) FROM jsonbonly; | ||
get_keyb | ||
-------------- | ||
{"ok": true} | ||
(1 row) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
CREATE FUNCTION convb(o jsonb) RETURNS jsonb AS $$ | ||
if (o instanceof Array) { | ||
o[1] = 10; | ||
} else if (typeof(o) == 'object') { | ||
o.i = 10; | ||
} | ||
return o; | ||
$$ LANGUAGE plv8; | ||
|
||
SELECT convb('{"i": 3, "b": 20}'); | ||
SELECT convb('[1, 2, 3]'); | ||
|
||
CREATE FUNCTION get_keyb(key text, json_raw jsonb) RETURNS jsonb | ||
LANGUAGE plv8 IMMUTABLE STRICT | ||
AS $$ | ||
var val = json_raw[key]; | ||
var ret = {}; | ||
ret[key] = val; | ||
return ret; | ||
$$; | ||
|
||
CREATE TABLE jsonbonly ( | ||
data jsonb | ||
); | ||
|
||
COPY jsonbonly (data) FROM stdin; | ||
{"ok": true} | ||
\. | ||
|
||
-- Call twice to test the function cache. | ||
SELECT get_keyb('ok', data) FROM jsonbonly; | ||
SELECT get_keyb('ok', data) FROM jsonbonly; |
2e09144
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think jsonb support should be lazy, by demand. Current implementation is naive and will have many problems with serialization of big documents, whereas one needs only one key:value.
2e09144
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's easy to do using the built in operators and an execute.