March 25th, 2014 by depesz | Tags: , , , , , , | 7 comments »
Did it help? If yes - maybe you can help me? Donate BTC to 19zPa5diT2LZqGtTi8f8bfApLn8rw9zBHx

Portuguese Brazil Version

On 23rd of March, Andrew Dunstan committed patch:

Introduce jsonb, a structured format for storing json.
The new format accepts exactly the same data as the json type. However, it is
stored in a format that does not require reparsing the orgiginal text in order
to process it, making it much more suitable for indexing and other operations.
Insignificant whitespace is discarded, and the order of object keys is not
preserved. Neither are duplicate object keys kept - the later value for a given
key is the only one stored.
The new type has all the functions and operators that the json type has,
with the exception of the json generation functions (to_json, json_agg etc.)
and with identical semantics. In addition, there are operator classes for
hash and btree indexing, and two classes for GIN indexing, that have no
equivalent in the json type.
This feature grew out of previous work by Oleg Bartunov and Teodor Sigaev, which
was intended to provide similar facilities to a nested hstore type, but which
in the end proved to have some significant compatibility issues.
Authors: Oleg Bartunov,  Teodor Sigaev, Peter Geoghegan and Andrew Dunstan.
Review: Andres Freund

After it got committed, it got covered pretty well, but I decided to write about it too, with some examples.

First, let's see how it works.

I'll start with some test values:

    "a": "abc",
    "d": "def",
    "z": [1, 2, 3]



First, let's see what will happen after casting these to json and jsonb:

select '{"a":"abc","d":"def","z":[1,2,3]}'::json;
(1 row)
select '{"a":"abc","d":"def","z":[1,2,3]}'::jsonb;
 {"a": "abc", "d": "def", "z": [1, 2, 3]}
(1 row)

All fine in here, but jsonb output is reformatted. In here it didn't do much, but it did add some whitespaces. That's nice.

select '{"a":"abc","d";"def","z":[1x2,3]}'::json;
ERROR:  invalid input syntax for type json
LINE 1: select '{"a":"abc","d";"def","z":[1x2,3]}'::json;
DETAIL:  Token ";" is invalid.
CONTEXT:  JSON data, line 1: {"a":"abc","d";...
select '{"a":"abc","d";"def","z":[1x2,3]}'::jsonb;
ERROR:  invalid input syntax for type json
LINE 1: select '{"a":"abc","d";"def","z":[1x2,3]}'::jsonb;
DETAIL:  Token ";" is invalid.
CONTEXT:  JSON data, line 1: {"a":"abc","d";...

In both cases it correctly reported error, but in the jsonb case it said
“invalid input syntax for type json". It's probably due to cast order, and
should be OK normally. And anyway – JSON and JSONB are similar enough not to
cause problems.

select '{
    "a": "abc",
    "d": "def",
    "z": [1, 2, 3]
 {                 +
     "a": "abc",   +
     "d": "def",   +
     "z": [1, 2, 3]+
(1 row)
select '{
    "a": "abc",
    "d": "def",
    "z": [1, 2, 3]
 {"a": "abc", "d": "def", "z": [1, 2, 3]}
(1 row)

In here we see the whitespace removal. I'd say it's pretty cool. Of course not if (for whatever reason) you want to retain the spaces, but these shouldn't be meaningful, so depending on them being there doesn't sound wise.

select '{"a":"abc","d":"def","z":[1,2,3],"d":"overwritten"}'::json;
(1 row)
select '{"a":"abc","d":"def","z":[1,2,3],"d":"overwritten"}'::jsonb;
 {"a": "abc", "d": "overwritten", "z": [1, 2, 3]}
(1 row)

And the value being overwritten. I'd say that it's great.

As for disk space usage – it will definitely depend on a case, but quick test shows that jsonb can use significantly more disk space:

$ select pg_column_size('{"a":"abc","d":"def","z":[1,2,3]}'::jsonb);
(1 row)
$ select pg_column_size('{"a":"abc","d":"def","z":[1,2,3]}'::json);
(1 row)

On the other hand for this JSON:

{"widget": {
    "debug": "on",
    "window": {
        "title": "Sample Konfabulator Widget",
        "name": "main_window",
        "width": 500,
        "height": 500
    "image": { 
        "src": "Images/Sun.png",
        "name": "sun1",
        "hOffset": 250,
        "vOffset": 250,
        "alignment": "center"
    "text": {
        "data": "Click Here",
        "size": 36,
        "style": "bold",
        "name": "text1",
        "hOffset": 250,
        "vOffset": 100,
        "alignment": "center",
        "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"

JSON value takes 605 bytes, and JSONb – 524.

What's more…

For jsonb we have more operators. For example – equality:

$ select '{"a":1, "b":2}'::jsonb = '{"b":2, "a":1}'::jsonb;
(1 row)

More operators are described the docs.

What's more – the new datatype can use indexes to search for elements.

I made test table:

create table test (v jsonb);

and loaded there 100k rows, which look like:

{"i": 42, "s": "ryzdaoop"}

Some of the rows (~ 1%) have additional value in the json – key “r" with value 1.

Now, I can create index on this:

create index whatever on test using gin (v);

and now:

explain analyze select * from test where v ? 'r';
                                                      QUERY PLAN                                                       
 Bitmap Heap Scan on test  (cost=16.77..307.23 rows=100 width=42) (actual time=0.554..2.670 rows=1024 loops=1)
   Recheck Cond: (v ? 'r'::text)
   Heap Blocks: exact=644
   ->  Bitmap Index Scan on whatever  (cost=0.00..16.75 rows=100 width=0) (actual time=0.416..0.416 rows=1024 loops=1)
         Index Cond: (v ? 'r'::text)
 Planning time: 0.475 ms
 Total runtime: 2.788 ms
(7 rows)

I could have also did:

explain analyze select * from test where v @> '{"i":42}';
                                                      QUERY PLAN                                                      
 Bitmap Heap Scan on test  (cost=28.77..319.23 rows=100 width=42) (actual time=1.132..1.707 rows=103 loops=1)
   Recheck Cond: (v @> '{"i": 42}'::jsonb)
   Heap Blocks: exact=99
   ->  Bitmap Index Scan on whatever  (cost=0.00..28.75 rows=100 width=0) (actual time=1.089..1.089 rows=103 loops=1)
         Index Cond: (v @> '{"i": 42}'::jsonb)
 Planning time: 0.482 ms
 Total runtime: 1.783 ms
(7 rows)

Or even:

explain analyze select * from test where v @> '{"i":42, "r":1}';
                                                     QUERY PLAN                                                     
 Bitmap Heap Scan on test  (cost=52.77..343.23 rows=100 width=42) (actual time=1.171..1.191 rows=3 loops=1)
   Recheck Cond: (v @> '{"i": 42, "r": 1}'::jsonb)
   Heap Blocks: exact=3
   ->  Bitmap Index Scan on whatever  (cost=0.00..52.75 rows=100 width=0) (actual time=1.143..1.143 rows=3 loops=1)
         Index Cond: (v @> '{"i": 42, "r": 1}'::jsonb)
 Planning time: 0.530 ms
 Total runtime: 1.256 ms
(7 rows)

Now, that's really cool.

Unfortunately you can't use the indexes to search “deep". What does it mean?

Let's assume you have JSON value like:

{"a": [1,2,3,4]}

You can index it, and search for " value ? ‘a' “, or " value @> ‘{“a":[1,2,3,4]}', but you can't search (using index) for “rows, which have 3 in the array that is under key “a" in the json".

Of course – you can work around it by creating index on ((value -> ‘a')) if that's what you really need.

Anyway – I really like it. It seems to work nicely, and I hope to get more features in there in the future.

Thanks guys.

  1. 7 comments

  2. # Peter Geoghegan
    Mar 26, 2014

    “You can index it, and search for ” value ? ‘a’ “, or ” value @> ‘{“a”:[1,2,3,4]}’, but you can’t search for “rows, which have 3 in the array that is under key “a” in the json”.”

    Actually, yes, you can, and if containment is tested the query may use a non-expression index:

    [local]/postgres=# create table w as select ‘{“a”: [1,2,3,4]}’::jsonb t;
    SELECT 1
    [local]/postgres=# select * from w where t @> ‘{“a”:[3]}’;
    {“a”: [1, 2, 3, 4]}
    (1 row)

    What’s really cool about the jsonb_hash_ops opclass is that it allows for really flexible and effective indexing when the jsonb column is directly indexed, without ending up with gigantic index.

  3. Mar 26, 2014

    I meant that you can’t search using the index. Corrected. Searching without usage of index is of course possible.

  4. # Peter Geoghegan
    Mar 26, 2014

    Such queries can use an index:

    [local]/postgres=# \d+ w
    Table “public.w”
    Column | Type | Modifiers | Storage | Stats target | Description
    t | jsonb | | extended | |
    “ff” gin (t jsonb_hash_ops)
    Has OIDs: no

    [local]/postgres=# explain analyze select * from w where t @> ‘{“a”:[1]}’;
    Bitmap Heap Scan on w (cost=8.00..12.01 rows=1 width=32) (actual time=0.053..0.056 rows=1 loops=1)
    Recheck Cond: (t @> ‘{“a”: [1]}’::jsonb)
    Heap Blocks: exact=1
    -> Bitmap Index Scan on ff (cost=0.00..8.00 rows=1 width=0) (actual time=0.027..0.027 rows=1 loops=1)
    Index Cond: (t @> ‘{“a”: [1]}’::jsonb)
    Planning time: 0.162 ms
    Total runtime: 0.124 ms
    (7 rows)

  5. Mar 26, 2014

    @Peter: Hmmm .. you’re right. Looks like misunderstood the part in docs that start with:

    “However, the index could not be used for queries like the following, due to the aforementioned nesting restriction:”


  6. # Peter Geoghegan
    Mar 26, 2014

    I understand your confusion. Perhaps that could stand to be clearer.

    The nesting restriction referred to is really just that you cannot query against a “sub-document” extracted using the -> operator, for example, unless you have an appropriate expression index. This might be a particular problem if you were checking existence in your predicate (the ‘?’ operator), because that simply doesn’t work past the first nesting level (those are the semantics, but it would be hard to make “nested existence” checking work with the current approach to GIN indexing, plus that mostly isn’t too compelling anyway). There may be some minor advantages to testing “existence” rather than “containment”. Perhaps even the term “containment” is misleading, since it’s a kind of nested containment, but that’s a holdover from hstore.

    We want to discourage people from indexing everything, which is more or less what a straight GIN index on a jsonb column represents. People should continue to make informed decisions about what to index in a way consistent with actual querying patterns, since presumably in general it isn’t all that useful to be able to use an index scan to find *any* one thing.

    Having said all that, if you’re using a jsonb_hash_ops GIN index, this approach actually works surprisingly well.

  7. Jun 1, 2014

    I really like the new operators (and how they bring most of the hstore functionality to jsonb).

    However, there still appear to be some operations missing. Notably, the subtraction operator, and, it seems, any operators that actually change, rather than compare objects:

  8. # Matt
    Jul 22, 2014

    No doubt that JSONB will be useful to web developers, but it seems useless or annoying to everyone else as a regular document store. One of the attractions to PostgreSQL is its comprehensive data type support, as well as its user defined data types. If it requires casting dates and others into strings or integers, then those types have to be cast back for each query that reads them. Besides being very tedious to code around, this negates any benefit from being stored as binary.

    I suppose if one wants a hierarchical data store, the xml type may be preferable as XML can be used in various ways to deal with rich data types. But that is still a pain. I would rather have something like hstore2, but with all data types supported. That way, the I/O through the database driver is just like any column in the database.

Leave a comment