PostgreSQL hangout: 3 things to know about the JSON features in PostgreSQL v9.4

18
3 Things you should know about JSON Features in PostgreSQL v9.4 27 January 2015

Transcript of PostgreSQL hangout: 3 things to know about the JSON features in PostgreSQL v9.4

3 Things you should know about JSON Features in

PostgreSQL v9.4

27 January 2015

- JSON was introduced in PostgreSQL v9.2

- Store unstructured data in JSON columns

- Store nested documents and array

- Functions for converting relational data to JSON

- JSON Parsing support introduced in PostgreSQL v9.3

- JSON operator support for parsing the JSON documents

History of JSON in PostgreSQL

2

So JSON has been there since PostgreSQL v9.2!

So what’s the fuss all about in v9.4?

• New datatype with storage format – JSONB

• New operators for matching JSON documents

• Index your JSONB fields

What’s new in v9.4

4

New Datatype- JSONB

New Storage format

- Is this a new FORMAT? NO!

- It is still JSON that you would have to deal with

- Internally PostgreSQL would use a hstore like ‘binary’ representation to store the JSON Object!

- This makes the parsing more efficient and opens up the door for different access methods (operators) and faster access!

- How? Let’s go over to the next one!

It’s not JSON! It is JSONB!!!

6

create table item_catalog

( item_id varchar(50) primary key,

item_description varchar(250),

attributes jsonb );

Example – Catalog of an online store

7

Example – Sample Insert

“category “

is an array

“features” is

an array of

sub-

documents

8

Example – Another record can be completely different

“features” has different

set of members

New fields which suit the

details of this specific type

of product

9

New OperatorsLeverage upon the parsing capability of new format

- So far you could do get various elements of a JSON document

- Get the an array element at specific position- Get a specific member of document- Get JSON object a specific path

- Binary representation of JSONB makes it easier to compare JSON documents

- Check for containment of one JSON object in another- Check for containment of one member in a JSON object- Check of containment of ‘any’ or ‘all’ elements of an array as

members in JSON object

New Operators!

11

New JSONB Operators

@>Does the left JSON value contain

within it the right value?

'{"a":1, "b":2}'::jsonb @>

'{"b":2}'::jsonb

<@Is the left JSON value contained within

the right value?

'{"b":2}'::jsonb <@ '{"a":1,

"b":2}'::jsonb

?Does the key/element string exist

within the JSON value?'{"a":1, "b":2}'::jsonb ? 'b'

?|Do any of these

key/element strings exist?

'{"a":1, "b":2, "c":3}'::jsonb

?| array['b', 'c']

?&Do all of these

key/element strings exist?

'["a", "b"]'::jsonb ?&

array['a', 'b']

12

Search Product Catalog for SAMSUNG products-

select item_description

from item_catalog

where attributes @>'{"brand":"SAMSUNG"}'::jsonb;

Search Product Catalog for SAMSUNG Mobiles along with their Price-

select item_description, attributes->'price'from item_catalogwhere attributes @> '{"brand":"SAMSUNG"}'::jsonband attributes->'category' ?| array ['Electronics/Mobile and Accessories/Mobiles'];

Example

13

Faster Access with IndexesBinary Format stored in Key value pairs is easy to index

- Since JSONB means JSON-Binary, it can be indexed!

- Create GIN indexes to speed up the parsing of JSONB columns

- You have two options to choose from-- The default operator class for operators- @>, ?, ?& and ?|- The non-default operator class (jsonb_path_ops) supports only @>

- Example- CREATE INDEX idxgin_item_catalog ON item_catalog USING gin

(attributes);- CREATE INDEX idxgin_item_catalog_2 ON item_catalog USING gin

(attributes jsonb_path_ops);- CREATE INDEX idxgin_item_catalog_category ON item_catalog USING

gin (( attributes->'category' ));

If PostgreSQL was a Cocktail Party, I would order GIN to go with my JSON!

15

- Send in your suggestions!

- If we get enough requests our next hangout will cover a demo on below lines -

- Create a Table with JSONB field

- Populate it using JSON functions from a relational table

- Create GIN index with default operator class

- Use operators to extract and parse JSON data

- Check if index is being used

- Send in suggestions if there is anything specific that you would like to see

Do you want a Live Demo?

16

Send us your suggestions and questions

[email protected]

Stay Tuned!

Website: www.ashnik.com

Send us your suggestions and questions

[email protected]

Stay Tuned!

Website: www.ashnik.com