SlideShare a Scribd company logo
PLV8

The Postgresql web side
Lucio Grenzi
l.grenzi@gmail.com

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

1 di 27
Who I am
Delphi developer since 1999
IT Consultant
Front end web developer
Postgresql addicted
Nonantolando.blogspot.com
lucio.grenzi
lucio grenzi

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

2 di 27
Agenda
NoSQL db pros
PLV8 introduction
PLV8 implemented features
Json datatype in PostgreSQL
Why choose PostregreSQL instead of NoSQL db

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

3 di 27
NoSQL
Relations, no tables
Schema less
Json
Document based
Easy to understand
Schema flexibility (model
application-level object)

documents

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

to

reflect

4 di 27
PLV8: an introduction

plv8 is a shared library that provides a PostgreSQL
procedural language powered by V8 JavaScript
Engine. With this program you can write in JavaScript
your function that is callable from SQL.
-http://pgxn.org/dist/plv8/-

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

5 di 27
javascript
Used everywhere
Language known by lots of devs
Simplicity. JavaScript is relatively simple to learn and
implement.
Lots of extensions available

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

6 di 27
Why v8 engine
Developed and maintained by Google
open source high-performance JavaScript engine
written in C++
V8 can run standalone, or can be embedded into any C+
+ application
compiles and executes JavaScript source code
handles memory allocation for objects
garbage collects objects it no longer needs

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

7 di 27
Requirements
PG: version >= 8.4
V8: version >= 3.14.5
Step to follow in order to install it
Install Postgresql
Install v8
(PG 9.1 or newer) psql -d dbname -c "CREATE
EXTENSION plv8"
createlang -d dbname plv8
psql -d dbname -c "CREATE LANGUAGE plv8"
PGDay.IT 2013 – 25 Ottobre 2013 - Prato

8 di 27
Install plv8 on Debian/Ubuntu
apt-get install postgresql
apt-get install libv8
apt-get install postgresql-9.2-plv8

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

9 di 27
PLV8: a trusted language
non-core loadable language
interpreter is sandboxed
no way to load external processing modules from the
file system
JavaScript modules can be loaded from the database
itself

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

10 di 27
Let’s code

set underscore `cat underscore­min.js`
set underscore `cat underscore­min.js`
reate table plv8_modules(modname text primary key, load_on_start boolean, code text
create table plv8_modules(modname text primary key, load_on_start boolean, code text

nsert into plv8_modules values ('underscore',true,:'underscore'),
insert into plv8_modules values ('underscore',true,:'underscore'),

reate or replace function plv8_startup()
create or replace function plv8_startup()
eturns void
returns void
anguage plv8
language plv8
s
as
$
$$
oad_module = function(modname)
load_module = function(modname)
{
   var rows = plv8.execute("SELECT code from plv8_modules " +
    var rows = plv8.execute("SELECT code from plv8_modules " +
                           " where modname = $1", [modname]);
                            " where modname = $1", [modname]);
   for (var r = 0; r < rows.length; r++)
    for (var r = 0; r < rows.length; r++)
   {
    {
       var code = rows[r].code;
        var code = rows[r].code;
       eval("(function() { " + code + "})")();
        eval("(function() { " + code + "})")();
   }      
    }      
;
};
$;
$$;

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

11 di 27
Implemented features
Scalar function calls
Set returing function calls
Trigger function calls
Inline statement calls
Auto mapping between JS and database built-in types
Database access via SPI including prepared statements and cursors
Subtransaction
Utility functions
Window function API
Typed array
Remote debugger
Runtime environment separation across users in the same session
Start-up procedure
Dialects
PGDay.IT 2013 – 25 Ottobre 2013 - Prato

12 di 27
Auto mapping between JS and database built-in types
Database types and JS types are mapped automatically
oid
bool
int2
int4
int8
float4
float8
numeric
date
timestamp
timestamptz
bytea
Json (>= 9.2)
If the JS value looks compatible, then the conversion succeeds. Otherwise, PL/v8 tries to convert
them via cstring representation. An array type is supported only if the dimention is one.

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

13 di 27
Subtransaction
plv8.subtransaction( func )
plv8.execute() creates a subtransaction every time. If you need an atomic
operation, you will need to call plv8.subtransaction() to create a subtransaction
block.
try{
try{
  plv8.subtransaction(function(){
  plv8.subtransaction(function(){
    plv8.execute("INSERT INTO table1 VALUES(1)"); 
    plv8.execute("INSERT INTO table1 VALUES(1)"); 
    plv8.execute("INSERT INTO table1 VALUES(‘test’)"); ­­ exception is raised
    plv8.execute("INSERT INTO table1 VALUES(‘test’)"); ­­ exception is raised
  });
  });
} catch(e) {
} catch(e) {
  ... rollback is executed and do alternate operation ...
  ... rollback is executed and do alternate operation ...
}
}

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

14 di 27
Utility functions
PL/v8 provides the following utility built-in functions.
plv8.elog(elevel, msg1[, msg2, ...])
plv8.quote_literal(str)
plv8.nullable(str)
plv8.quote_ident(str)
plv8.elog emits message to the client or the log file. The elevel is one of
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1, LOG, INFO, NOTICE
WARNING, ERROR
See the PostgreSQL manual for each error level.
Each functionality for quote family is identical to the built-in SQL function with the same
name.
PGDay.IT 2013 – 25 Ottobre 2013 - Prato

15 di 27
Typed array
Allow fast access to native memory, mainly for the purpose of their
canvas support in browsers. PL/v8 uses this to map bytea (unsigned
byte) and various array types to JavaScript array. IFor
int2/int4/float4/float8 array type, PL/v8 provides direct access to each
element by using PL/v8 domain types.
Currently there is no way to create such typed array inside PL/v8
functions. Only arguments can be typed array. You can modify the
element and return the value.
CREATE FUNCTION int4sum(ary plv8_int4array) RETURNS int8
AS $$ var sum = 0; for (var i = 0; i < ary.length; i++) { sum +=
ary[i]; } return sum; $$ LANGUAGE plv8 IMMUTABLE STRICT;
SELECT int4sum(ARRAY[1, 2, 3, 4, 5]); int4sum

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

16 di 27
Remote debugger
PL/v8 supports v8 remote debugger.
To enable it at the compile time pass
ENABLE_DEBUGGER_SUPPORT.
Once PL/v8 module is loaded (and the execution engine
is initialized), PL/v8 accepts a remote debugger
connection. If you have d8 from v8 package, run with
--remote-debug --debug-port=35432 to attach the
functions.
It is possible debug statement inside functions to set a
breakpoint.

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

17 di 27
Runtime environment separation
In PL/v8, each session has one global JS runtime context. For the security
reasons, if the user switches to another with SET ROLE command, a new
JS runtime context is initialized and used separately. This prevents
unexpected information leak risk.
Each plv8 function is invoked as if the function is the property of other
object. This means "this" in each function is a JS object that is created
every time the function is executed in a query. In other words, the life time
and the visibility of "this" object in a function is only a series of function
calls in a query. If you need to share some value among different functions,
keep it in plv8 object because each function invocation has different "this"
object.

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

18 di 27
Dialects
Currently two dialects are supported
CoffeeScript (plcoffee)
LiveScript (plls)

Dialects can be loaded via CREATE EXTENSION
command ( On PostgreSQL >= 9.1 )

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

19 di 27
Json datatype
JSON is the lingua franca of the web
Introduced on Postgresql 9.2
Currently this is nothing more than a validating data
type
PLV8 + Json = amazing new possibilities
Working directly with JSON and JavaScript has been all the
rage in many of the NoSQL databases

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

20 di 27
A basic approach

CREATE or REPLACE FUNCTION 
CREATE or REPLACE FUNCTION 
  json_string(data json, key text) RETURNS TEXT AS $$
  json_string(data json, key text) RETURNS TEXT AS $$
  var data = JSON.parse(data); 
  var data = JSON.parse(data); 
  return JSON.stringify(data[key]);
  return JSON.stringify(data[key]);
$$ LANGUAGE plv8 IMMUTABLE STRICT;
$$ LANGUAGE plv8 IMMUTABLE STRICT;
SELECT id, data FROM things WHERE json_string(data,'name') LIKE 'Z%';
SELECT id, data FROM things WHERE json_string(data,'name') LIKE 'Z%';
CREATE INDEX json_idx ON things (json_string(field,'name'));
CREATE INDEX json_idx ON things (json_string(field,'name'));

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

21 di 27
Web development stack
Client side: backbone.js – jquery.js
Server side: node.js
Database: PostgreSQL + PLV8
Pros:
Rapid prototyping
Easy to change
Model database as in-store memory
One table, key/data
No costs of switching language
PGDay.IT 2013 – 25 Ottobre 2013 - Prato

22 di 27
Table like NOSql

CREATE EXTENSION plv8;
CREATE EXTENSION plv8;
CREATE TABLE things (
CREATE TABLE things (
   Id uuid PRIMARY KEY DEFAULT (uuid_generate_v4()),
   Id uuid PRIMARY KEY DEFAULT (uuid_generate_v4()),
   data json);
   data json);

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

23 di 27
At the end ...
All very easy
All very fast
All very simple
Plv8 is stable for production
Constanly updated
Nosql db? No thanks

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

24 di 27
Risorse
http://code.google.com/p/plv8js/wiki/PLV8
http://adpgtech.blogspot.it/2013/03/loading-useful-modulesin-plv8.html
http://pgxn.org/dist/plv8/doc/plv8.html
http://interlinked.org/tutorials/postgresql.html
http://plv8-pgconfeu.herokuapp.com

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

25 di 27
Questions?

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

26 di 27
PGDay.IT 2013 – 25 Ottobre 2013 - Prato

27 di 27

More Related Content

PDF
Not Your Fathers C - C Application Development In 2016
PDF
Qt for beginners
PDF
Better Code: Concurrency
PPTX
Async await in C++
PPTX
C++ Coroutines
ODP
Cross Platform Qt
PPTX
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
 
PDF
Necessitas - Qt on Android - from FSCONS 2011
Not Your Fathers C - C Application Development In 2016
Qt for beginners
Better Code: Concurrency
Async await in C++
C++ Coroutines
Cross Platform Qt
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
 
Necessitas - Qt on Android - from FSCONS 2011

What's hot (20)

PDF
State of the Art OpenGL and Qt
 
PDF
Translating Qt Applications
PDF
TensorFlow Lite (r1.5) & Android 8.1 Neural Network API
PDF
Qt Internationalization
 
PPT
What's New in Groovy 1.6?
PPTX
JSX Optimizer
PDF
Gradle in a Polyglot World
PDF
A Brief Introduction to the Qt Application Framework
PPTX
Qt for beginners part 1 overview and key concepts
 
PDF
Flutter 是什麼?用 Flutter 會省到時間嗎? @ GDG Devfest2020
PDF
Idiomatic gradle plugin writing
ODP
The why and how of moving to php 7.x
PDF
QThreads: Are You Using Them Wrong?
 
ODP
The why and how of moving to php 7.x
PDF
Migrating from Photon to Qt
PDF
Clojure and The Robot Apocalypse
PPTX
Web technology slideshare
PDF
Integrazione QML / C++
PDF
WebRTC ... GWT & in-browser computation
PDF
Qt for Beginners Part 3 - QML and Qt Quick
 
State of the Art OpenGL and Qt
 
Translating Qt Applications
TensorFlow Lite (r1.5) & Android 8.1 Neural Network API
Qt Internationalization
 
What's New in Groovy 1.6?
JSX Optimizer
Gradle in a Polyglot World
A Brief Introduction to the Qt Application Framework
Qt for beginners part 1 overview and key concepts
 
Flutter 是什麼?用 Flutter 會省到時間嗎? @ GDG Devfest2020
Idiomatic gradle plugin writing
The why and how of moving to php 7.x
QThreads: Are You Using Them Wrong?
 
The why and how of moving to php 7.x
Migrating from Photon to Qt
Clojure and The Robot Apocalypse
Web technology slideshare
Integrazione QML / C++
WebRTC ... GWT & in-browser computation
Qt for Beginners Part 3 - QML and Qt Quick
 
Ad

Similar to PLV8 - The PostgreSQL web side (20)

PDF
There is Javascript in my SQL
PDF
Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type
PPTX
Why Node, Express and Postgres - presented 23 Feb 15, Talkjs, Microsoft Audit...
PDF
Mathias test
PDF
PostgreSQL 9.4
PDF
No sql way_in_pg
PPT
The NoSQL Way in Postgres
 
PDF
Writing infinite scalability web applications with PHP and PostgreSQL
PDF
Don't panic! - Postgres introduction
PDF
JavaScript: Past, Present, Future
PDF
NoSQL and Spatial Database Capabilities using PostgreSQL
 
PDF
NoSQL on ACID - Meet Unstructured Postgres
 
PDF
PostgreSQL Prologue
PDF
NoSQL Now: Postgres - The NoSQL Cake You Can Eat
PPTX
Getting started with postgresql
PDF
Plpgsql russia-pgconf
PPT
Do More with Postgres- NoSQL Applications for the Enterprise
 
PDF
PostgreSQL Server Programming 2nd Edition Usama Dar
PDF
Beyond Postgres: Interesting Projects, Tools and forks
PDF
EDB NoSQL German Webinar 2015
 
There is Javascript in my SQL
Postgrtesql as a NoSQL Document Store - The JSON/JSONB data type
Why Node, Express and Postgres - presented 23 Feb 15, Talkjs, Microsoft Audit...
Mathias test
PostgreSQL 9.4
No sql way_in_pg
The NoSQL Way in Postgres
 
Writing infinite scalability web applications with PHP and PostgreSQL
Don't panic! - Postgres introduction
JavaScript: Past, Present, Future
NoSQL and Spatial Database Capabilities using PostgreSQL
 
NoSQL on ACID - Meet Unstructured Postgres
 
PostgreSQL Prologue
NoSQL Now: Postgres - The NoSQL Cake You Can Eat
Getting started with postgresql
Plpgsql russia-pgconf
Do More with Postgres- NoSQL Applications for the Enterprise
 
PostgreSQL Server Programming 2nd Edition Usama Dar
Beyond Postgres: Interesting Projects, Tools and forks
EDB NoSQL German Webinar 2015
 
Ad

More from Lucio Grenzi (13)

ODP
How to use Postgresql in order to handle Prometheus metrics storage
ODP
Building serverless application on the Apache Openwhisk platform
ODP
Patroni: PostgreSQL HA in the cloud
ODP
Postgrest: the REST API for PostgreSQL databases
PPTX
Full slidescr16
ODP
Use Ionic Framework to develop mobile application
ODP
Rabbitmq & Postgresql
ODP
Jenkins djangovillage
ODP
Geodjango and HTML 5
ODP
Pg tap
PPT
Geodjango
PPT
Yui app-framework
PPT
node.js e Postgresql
How to use Postgresql in order to handle Prometheus metrics storage
Building serverless application on the Apache Openwhisk platform
Patroni: PostgreSQL HA in the cloud
Postgrest: the REST API for PostgreSQL databases
Full slidescr16
Use Ionic Framework to develop mobile application
Rabbitmq & Postgresql
Jenkins djangovillage
Geodjango and HTML 5
Pg tap
Geodjango
Yui app-framework
node.js e Postgresql

Recently uploaded (20)

PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
cuic standard and advanced reporting.pdf
PDF
Encapsulation theory and applications.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Machine learning based COVID-19 study performance prediction
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Big Data Technologies - Introduction.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Spectroscopy.pptx food analysis technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Cloud computing and distributed systems.
PDF
KodekX | Application Modernization Development
Review of recent advances in non-invasive hemoglobin estimation
Per capita expenditure prediction using model stacking based on satellite ima...
cuic standard and advanced reporting.pdf
Encapsulation theory and applications.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
NewMind AI Weekly Chronicles - August'25 Week I
Reach Out and Touch Someone: Haptics and Empathic Computing
Machine learning based COVID-19 study performance prediction
Diabetes mellitus diagnosis method based random forest with bat algorithm
Big Data Technologies - Introduction.pptx
Unlocking AI with Model Context Protocol (MCP)
MYSQL Presentation for SQL database connectivity
Spectroscopy.pptx food analysis technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Dropbox Q2 2025 Financial Results & Investor Presentation
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Chapter 3 Spatial Domain Image Processing.pdf
Cloud computing and distributed systems.
KodekX | Application Modernization Development

PLV8 - The PostgreSQL web side

Editor's Notes