SlideShare a Scribd company logo
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

More Related Content

PDF
PostgreSQL Enterprise Class Features and Capabilities
PDF
Oracle to Postgres Migration - part 2
PDF
Migration From Oracle to PostgreSQL
PDF
[EPPG] Oracle to PostgreSQL, Challenges to Opportunity
PDF
Oracle to Postgres Migration - part 1
PDF
Lessons PostgreSQL learned from commercial databases, and didn’t
PDF
Lightening Talk - PostgreSQL Worst Practices
PDF
Go faster with_native_compilation Part-2
PostgreSQL Enterprise Class Features and Capabilities
Oracle to Postgres Migration - part 2
Migration From Oracle to PostgreSQL
[EPPG] Oracle to PostgreSQL, Challenges to Opportunity
Oracle to Postgres Migration - part 1
Lessons PostgreSQL learned from commercial databases, and didn’t
Lightening Talk - PostgreSQL Worst Practices
Go faster with_native_compilation Part-2

What's hot (20)

PDF
Oracle to PostgreSQL migration
PDF
Query Parallelism in PostgreSQL: What's coming next?
PDF
Presto in my_use_case
PDF
Porting Oracle Applications to PostgreSQL
PDF
Migrating Oracle database to PostgreSQL
PDF
Understanding Presto - Presto meetup @ Tokyo #1
PPTX
Compression talk
PDF
Presto Testing Tools: Benchto & Tempto (Presto Boston Meetup 10062015)
PDF
Spark Workflow Management
PDF
Standalone Spark Deployment for Stability and Performance
PDF
Spark Summit 2014: Spark Job Server Talk
PDF
Postgres Vision 2018: WAL: Everything You Want to Know
 
PDF
Presto - Analytical Database. Overview and use cases.
PDF
Optimizing Apache Spark UDFs
PPTX
Tuning and Monitoring Deep Learning on Apache Spark
PDF
Presto in my_use_case2
PDF
Building a High-Performance Database with Scala, Akka, and Spark
PDF
Re-Architecting Spark For Performance Understandability
PDF
Presto changes
PDF
Spark on YARN
Oracle to PostgreSQL migration
Query Parallelism in PostgreSQL: What's coming next?
Presto in my_use_case
Porting Oracle Applications to PostgreSQL
Migrating Oracle database to PostgreSQL
Understanding Presto - Presto meetup @ Tokyo #1
Compression talk
Presto Testing Tools: Benchto & Tempto (Presto Boston Meetup 10062015)
Spark Workflow Management
Standalone Spark Deployment for Stability and Performance
Spark Summit 2014: Spark Job Server Talk
Postgres Vision 2018: WAL: Everything You Want to Know
 
Presto - Analytical Database. Overview and use cases.
Optimizing Apache Spark UDFs
Tuning and Monitoring Deep Learning on Apache Spark
Presto in my_use_case2
Building a High-Performance Database with Scala, Akka, and Spark
Re-Architecting Spark For Performance Understandability
Presto changes
Spark on YARN
Ad

Viewers also liked (20)

PDF
PostgreSQL Rocks Indonesia
PDF
Go Faster With Native Compilation
PDF
(Ab)using 4d Indexing
PDF
PostgreSQL 9.6 Performance-Scalability Improvements
PDF
PostgreSQL on Amazon RDS
PDF
Big Data and PostgreSQL
PDF
Introduction to Vacuum Freezing and XID
PDF
PostgreSQL: Past present Future
PDF
Swapping Pacemaker Corosync with repmgr
PDF
Security Best Practices for your Postgres Deployment
PDF
PostgreSQL WAL for DBAs
PDF
Use Case: PostGIS and Agribotics
PDF
How to teach an elephant to rock'n'roll
PDF
Why we love pgpool-II and why we hate it!
PDF
Best Practices for Becoming an Exceptional Postgres DBA
 
PDF
Postgresql database administration volume 1
PDF
SP DIT Bonding Day - 05062015
PDF
pgDay Asia 2016 - Swapping Pacemaker-Corosync for repmgr (1)
PDF
PUGS Meetup Presentation - 11062015
PDF
Puppet Camp Singapore 2015 - 19th Nov 2015 Presentation (1)
PostgreSQL Rocks Indonesia
Go Faster With Native Compilation
(Ab)using 4d Indexing
PostgreSQL 9.6 Performance-Scalability Improvements
PostgreSQL on Amazon RDS
Big Data and PostgreSQL
Introduction to Vacuum Freezing and XID
PostgreSQL: Past present Future
Swapping Pacemaker Corosync with repmgr
Security Best Practices for your Postgres Deployment
PostgreSQL WAL for DBAs
Use Case: PostGIS and Agribotics
How to teach an elephant to rock'n'roll
Why we love pgpool-II and why we hate it!
Best Practices for Becoming an Exceptional Postgres DBA
 
Postgresql database administration volume 1
SP DIT Bonding Day - 05062015
pgDay Asia 2016 - Swapping Pacemaker-Corosync for repmgr (1)
PUGS Meetup Presentation - 11062015
Puppet Camp Singapore 2015 - 19th Nov 2015 Presentation (1)
Ad

Similar to There is Javascript in my SQL (20)

PPT
The NoSQL Way in Postgres
 
PDF
No sql way_in_pg
PPT
Do More with Postgres- NoSQL Applications for the Enterprise
 
PDF
Json in Postgres - the Roadmap
 
PDF
Webscale PostgreSQL - JSONB and Horizontal Scaling Strategies
PDF
JSON Processing in the Database using PostgreSQL 9.4 :: Data Wranglers DC :: ...
PDF
Oh, that ubiquitous JSON !
PDF
NoSQL on ACID - Meet Unstructured Postgres
 
PPTX
NoSQL on ACID: Meet Unstructured Postgres
 
PDF
PostgreSQL 9.3 and JSON - talk at PgOpen 2013
PDF
Andrew Dunstan 9.3 JSON Presentation @ Postgres Open 2013
PDF
Postgres vs Mongo / Олег Бартунов (Postgres Professional)
PDF
PDF
Working with JSON Data in PostgreSQL vs. MongoDB
PPTX
Json and Jsonpath in Postgres 12
 
PDF
Native JSON Support in SQL2016
PDF
JSON Support in DB2 for z/OS
PPTX
Power JSON with PostgreSQL
 
PDF
EDB NoSQL German Webinar 2015
 
PDF
NoSQL Now: Postgres - The NoSQL Cake You Can Eat
The NoSQL Way in Postgres
 
No sql way_in_pg
Do More with Postgres- NoSQL Applications for the Enterprise
 
Json in Postgres - the Roadmap
 
Webscale PostgreSQL - JSONB and Horizontal Scaling Strategies
JSON Processing in the Database using PostgreSQL 9.4 :: Data Wranglers DC :: ...
Oh, that ubiquitous JSON !
NoSQL on ACID - Meet Unstructured Postgres
 
NoSQL on ACID: Meet Unstructured Postgres
 
PostgreSQL 9.3 and JSON - talk at PgOpen 2013
Andrew Dunstan 9.3 JSON Presentation @ Postgres Open 2013
Postgres vs Mongo / Олег Бартунов (Postgres Professional)
Working with JSON Data in PostgreSQL vs. MongoDB
Json and Jsonpath in Postgres 12
 
Native JSON Support in SQL2016
JSON Support in DB2 for z/OS
Power JSON with PostgreSQL
 
EDB NoSQL German Webinar 2015
 
NoSQL Now: Postgres - The NoSQL Cake You Can Eat

More from PGConf APAC (17)

PDF
PGConf APAC 2018: Sponsored Talk by Fujitsu - The growing mandatory requireme...
PDF
PGConf APAC 2018: PostgreSQL 10 - Replication goes Logical
PDF
PGConf APAC 2018 - Lightening Talk #3: How To Contribute to PostgreSQL
PDF
PGConf APAC 2018 - Lightening Talk #2 - Centralizing Authorization in PostgreSQL
PDF
Sponsored Talk @ PGConf APAC 2018 - Choosing the right partner in your Postgr...
PDF
PGConf APAC 2018 - A PostgreSQL DBAs Toolbelt for 2018
PDF
PGConf APAC 2018 - Patroni: Kubernetes-native PostgreSQL companion
PDF
PGConf APAC 2018 - High performance json postgre-sql vs. mongodb
PDF
PGConf APAC 2018 - Monitoring PostgreSQL at Scale
PDF
PGConf APAC 2018 - Where's Waldo - Text Search and Pattern in PostgreSQL
PDF
PGConf APAC 2018 - Managing replication clusters with repmgr, Barman and PgBo...
PDF
PGConf APAC 2018 - PostgreSQL HA with Pgpool-II and whats been happening in P...
PDF
PGConf APAC 2018 - PostgreSQL performance comparison in various clouds
PDF
Sponsored Talk @ PGConf APAC 2018 - Migrating Oracle to EDB Postgres Approach...
PDF
PGConf APAC 2018 - Tale from Trenches
PDF
PGConf APAC 2018 Keynote: PostgreSQL goes eleven
PDF
Amazon (AWS) Aurora
PGConf APAC 2018: Sponsored Talk by Fujitsu - The growing mandatory requireme...
PGConf APAC 2018: PostgreSQL 10 - Replication goes Logical
PGConf APAC 2018 - Lightening Talk #3: How To Contribute to PostgreSQL
PGConf APAC 2018 - Lightening Talk #2 - Centralizing Authorization in PostgreSQL
Sponsored Talk @ PGConf APAC 2018 - Choosing the right partner in your Postgr...
PGConf APAC 2018 - A PostgreSQL DBAs Toolbelt for 2018
PGConf APAC 2018 - Patroni: Kubernetes-native PostgreSQL companion
PGConf APAC 2018 - High performance json postgre-sql vs. mongodb
PGConf APAC 2018 - Monitoring PostgreSQL at Scale
PGConf APAC 2018 - Where's Waldo - Text Search and Pattern in PostgreSQL
PGConf APAC 2018 - Managing replication clusters with repmgr, Barman and PgBo...
PGConf APAC 2018 - PostgreSQL HA with Pgpool-II and whats been happening in P...
PGConf APAC 2018 - PostgreSQL performance comparison in various clouds
Sponsored Talk @ PGConf APAC 2018 - Migrating Oracle to EDB Postgres Approach...
PGConf APAC 2018 - Tale from Trenches
PGConf APAC 2018 Keynote: PostgreSQL goes eleven
Amazon (AWS) Aurora

Recently uploaded (20)

PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
MYSQL Presentation for SQL database connectivity
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Modernizing your data center with Dell and AMD
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Encapsulation theory and applications.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Big Data Technologies - Introduction.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
MYSQL Presentation for SQL database connectivity
The AUB Centre for AI in Media Proposal.docx
Empathic Computing: Creating Shared Understanding
Advanced methodologies resolving dimensionality complications for autism neur...
Digital-Transformation-Roadmap-for-Companies.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Modernizing your data center with Dell and AMD
Diabetes mellitus diagnosis method based random forest with bat algorithm
Encapsulation theory and applications.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
NewMind AI Monthly Chronicles - July 2025
Big Data Technologies - Introduction.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Electronic commerce courselecture one. Pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...

There is Javascript in my SQL

  • 1. Postgres – There is JavaScript in my SQL!
  • 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,...]);