These slides were used by Mark from Jumping Bean at pgDay Asia 2016. He presented the noSQL features - JSON and JOSNB and related operators and functions to audience.
2. About Me
● Solutions integrator
at Jumping Bean
– Developer & Trainer
– Social Media
● Twitter @mxc4
● Twitter
@jumpingbeansa
● LinkedIn
3. What is JSON?
● JSON -> JavaScript Object Notation,
● Used to transfer data in JavaScript,
● Light weight way to marshal JavaScript objects,
● Widely used in REST services,
● Used as generic data transfer object for
unstructured data
4. Why store JSON?
● Need to store semi-structured data
● Reduce time to market
– Ability to define data requirements “in-flight
● Need to store data from REST API,
● All the arguments for NoSQL
5. Why Use Postgres for NoSQL?
● Postgres is fast
● Postgres is ACID compliant,
● Best of both worlds – benefits of relational
database with those of NoSQL databases,
● Already invested in Postgres – Why learn
something new?
6. JSON/JSONB Data Type
● Core type
● Two data types
– JSON – 9.3
● Stores exact copy of input text but must be valid JSON
– JSONB – 9.4
● Stores a binary representation of the original input
● Encoding issues:
– JSON spec uses UTF-8 encoding,
– Database may use different encoding may be incompatibilities
7. JSON/JSONB
● Pros
– JSON
● Ensures its valid JSON,
● Copy of original, preserves
white space as well as order of
keys and duplicate keys
– JSONB
● Faster to process,
● Supports indexing,
● Tokenised BTree
●
● Cons
– JSON
● Must be parsed when
used in processing
functions,
– JSONB
● Duplicate keys and
unnecessary white
space discarded
8. Three Types of Functions
● Operators,
– Functions to extract elements from JSON
● Creation functions,
– Create JSON from database rows, arrays
● Processing functions
– Typically to convert from JSON to records or
recordset
18. Creating Index
● Can create GIN (Generalised inverted index) index on JSONB,
● Allows use of @ contains operator ,
● E.G
– Create index idx01 on docs using GIN(body);
● Can create index on specific fields with
– create index inx02 on docs ((doc >'col1')); – Text type index→
● Text based index,
● Select tuser 'location' from followers where tuser >'location' = “South Africa”→ →
– create index inx03 on docs((doc 'col1')); – JSON type index→
● Index on JSON data type
● select tuser->'location' from followers where tuser->'location' = '"South Africa"'::jsonb
20. JavaScript Postgres Functions
● Postgres functions can be written in
JavaScript,
– Install PLv8 extensions
● “apt-get install postgresql-9.4-plv8”
– Enable extension with:
● “Create extensions plv8”
21. PL/v8
● Plv8.execute(sql,[args]);
● Plv8.prepare(sql,[args]);
– var stmt = plv8.prepare('Select * from tbl where col1=$1',['text']);
– var rows = stmt.execute('foss'); (returns a cursor);
– stmt.free(); _> frees the prepared statement
● Cursors operations
– fetch([nrows])
– move([nrows])
– close()
22. Plv8
● Plv8.return_next()
→ stores records in internal tuple store,
→ content of store return at end of function
● Can also return an array of JSON matching
expected rowtype,
● Plv8.elog([level],message[,msg2,...]);