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

What's hot (20)

PDF
Oracle to PostgreSQL migration
strikr .
 
PDF
Query Parallelism in PostgreSQL: What's coming next?
PGConf APAC
 
PDF
Presto in my_use_case
wyukawa
 
PDF
Porting Oracle Applications to PostgreSQL
Peter Eisentraut
 
PDF
Migrating Oracle database to PostgreSQL
Umair Mansoob
 
PDF
Understanding Presto - Presto meetup @ Tokyo #1
Sadayuki Furuhashi
 
PPTX
Compression talk
Ilya Ganelin
 
PDF
Presto Testing Tools: Benchto & Tempto (Presto Boston Meetup 10062015)
Matt Fuller
 
PDF
Spark Workflow Management
Romi Kuntsman
 
PDF
Standalone Spark Deployment for Stability and Performance
Romi Kuntsman
 
PDF
Spark Summit 2014: Spark Job Server Talk
Evan Chan
 
PDF
Postgres Vision 2018: WAL: Everything You Want to Know
EDB
 
PDF
Presto - Analytical Database. Overview and use cases.
Wojciech Biela
 
PDF
Optimizing Apache Spark UDFs
Databricks
 
PPTX
Tuning and Monitoring Deep Learning on Apache Spark
Databricks
 
PDF
Presto in my_use_case2
wyukawa
 
PDF
Building a High-Performance Database with Scala, Akka, and Spark
Evan Chan
 
PDF
Re-Architecting Spark For Performance Understandability
Jen Aman
 
PDF
Presto changes
N Masahiro
 
PDF
Spark on YARN
Adarsh Pannu
 
Oracle to PostgreSQL migration
strikr .
 
Query Parallelism in PostgreSQL: What's coming next?
PGConf APAC
 
Presto in my_use_case
wyukawa
 
Porting Oracle Applications to PostgreSQL
Peter Eisentraut
 
Migrating Oracle database to PostgreSQL
Umair Mansoob
 
Understanding Presto - Presto meetup @ Tokyo #1
Sadayuki Furuhashi
 
Compression talk
Ilya Ganelin
 
Presto Testing Tools: Benchto & Tempto (Presto Boston Meetup 10062015)
Matt Fuller
 
Spark Workflow Management
Romi Kuntsman
 
Standalone Spark Deployment for Stability and Performance
Romi Kuntsman
 
Spark Summit 2014: Spark Job Server Talk
Evan Chan
 
Postgres Vision 2018: WAL: Everything You Want to Know
EDB
 
Presto - Analytical Database. Overview and use cases.
Wojciech Biela
 
Optimizing Apache Spark UDFs
Databricks
 
Tuning and Monitoring Deep Learning on Apache Spark
Databricks
 
Presto in my_use_case2
wyukawa
 
Building a High-Performance Database with Scala, Akka, and Spark
Evan Chan
 
Re-Architecting Spark For Performance Understandability
Jen Aman
 
Presto changes
N Masahiro
 
Spark on YARN
Adarsh Pannu
 

Viewers also liked (20)

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

Similar to There is Javascript in my SQL (20)

PDF
Postgres-XC as a Key Value Store Compared To MongoDB
Mason Sharp
 
PDF
Json in Postgres - the Roadmap
EDB
 
PDF
9.4json
Andrew Dunstan
 
PDF
Experiences with Evangelizing Java Within the Database
Marcelo Ochoa
 
PPT
Mongodb Training Tutorial in Bangalore
rajkamaltibacademy
 
PPTX
MongoDB by Emroz sardar.
Emroz Sardar
 
PDF
No sql database and python -- Presentation done at Python Developer's meetup ...
Roshan Bhandari
 
PPT
Fast querying indexing for performance (4)
MongoDB
 
PPT
mongodb-120401144140-phpapp01 claud camputing
moeincanada007
 
PDF
GraphQL is actually rest
Jakub Riedl
 
PDF
Mongodb (1)
Deepak Kumar
 
PPT
Introduction to MongoDB
Ravi Teja
 
PDF
Jsquery - the jsonb query language with GIN indexing support
Alexander Korotkov
 
PDF
MongoDB and Web Scrapping with the Gyes Platform
MongoDB
 
PDF
MongoDB Basics
Sarang Shravagi
 
ODP
An Introduction to Postgresql
عباس بني اسدي مقدم
 
PDF
No sql way_in_pg
Vibhor Kumar
 
PDF
NoSQL on ACID - Meet Unstructured Postgres
EDB
 
PDF
OpenSearch.pdf
Abhi Jain
 
PDF
Getting started with node JS
Hamdi Hmidi
 
Postgres-XC as a Key Value Store Compared To MongoDB
Mason Sharp
 
Json in Postgres - the Roadmap
EDB
 
Experiences with Evangelizing Java Within the Database
Marcelo Ochoa
 
Mongodb Training Tutorial in Bangalore
rajkamaltibacademy
 
MongoDB by Emroz sardar.
Emroz Sardar
 
No sql database and python -- Presentation done at Python Developer's meetup ...
Roshan Bhandari
 
Fast querying indexing for performance (4)
MongoDB
 
mongodb-120401144140-phpapp01 claud camputing
moeincanada007
 
GraphQL is actually rest
Jakub Riedl
 
Mongodb (1)
Deepak Kumar
 
Introduction to MongoDB
Ravi Teja
 
Jsquery - the jsonb query language with GIN indexing support
Alexander Korotkov
 
MongoDB and Web Scrapping with the Gyes Platform
MongoDB
 
MongoDB Basics
Sarang Shravagi
 
An Introduction to Postgresql
عباس بني اسدي مقدم
 
No sql way_in_pg
Vibhor Kumar
 
NoSQL on ACID - Meet Unstructured Postgres
EDB
 
OpenSearch.pdf
Abhi Jain
 
Getting started with node JS
Hamdi Hmidi
 
Ad

More from PGConf APAC (17)

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

Recently uploaded (20)

PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 

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,...]);