postgrtesql as a nosql document store - the json/jsonb data type
TRANSCRIPT
Postgres – There is JavaScript in my SQL!
About Me
● Solutions integrator at Jumping Bean– Developer & Trainer– Social Media
● Twitter @mxc4● Twitter
@jumpingbeansa● LinkedIn
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
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
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?
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
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
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
JSON/JSONB Operators
JSON/JSONB Operators
JSON/JSONB Operators
Demo Querying Twitter Users
JSON Creation Functions
JSON Processing Functions
JSON Processing Functions
JSON Processing Functions
JSON Processing Functions
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
JavaScript Functions
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”
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()
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,...]);
Plv8 Function Example
The End