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
l...
Agenda
NoSQL db pros
PLV8 introduction
PLV8 implemented features
Json datatype in PostgreSQL
Why choose PostregreSQL inste...
NoSQL
Relations, no tables
Schema less
Json
Document based
Easy to understand
Schema flexibility (model
application-level ...
PLV8: an introduction

plv8 is a shared library that provides a PostgreSQL
procedural language powered by V8 JavaScript
En...
javascript
Used everywhere
Language known by lots of devs
Simplicity. JavaScript is relatively simple to learn and
impleme...
Why v8 engine
Developed and maintained by Google
open source high-performance JavaScript engine
written in C++
V8 can run ...
Requirements
PG: version >= 8.4
V8: version >= 3.14.5
Step to follow in order to install it
Install Postgresql
Install v8
...
Install plv8 on Debian/Ubuntu
apt-get install postgresql
apt-get install libv8
apt-get install postgresql-9.2-plv8

PGDay....
PLV8: a trusted language
non-core loadable language
interpreter is sandboxed
no way to load external processing modules fr...
Let’s code

set underscore `cat underscore­min.js`
set underscore `cat underscore­min.js`
reate table plv8_modules(modname...
Implemented features
Scalar function calls
Set returing function calls
Trigger function calls
Inline statement calls
Auto ...
Auto mapping between JS and database built-in types
Database types and JS types are mapped automatically
oid
bool
int2
int...
Subtransaction
plv8.subtransaction( func )
plv8.execute() creates a subtransaction every time. If you need an atomic
opera...
Utility functions
PL/v8 provides the following utility built-in functions.
plv8.elog(elevel, msg1[, msg2, ...])
plv8.quote...
Typed array
Allow fast access to native memory, mainly for the purpose of their
canvas support in browsers. PL/v8 uses thi...
Remote debugger
PL/v8 supports v8 remote debugger.
To enable it at the compile time pass
ENABLE_DEBUGGER_SUPPORT.
Once PL/...
Runtime environment separation
In PL/v8, each session has one global JS runtime context. For the security
reasons, if the ...
Dialects
Currently two dialects are supported
CoffeeScript (plcoffee)
LiveScript (plls)

Dialects can be loaded via CREATE...
Json datatype
JSON is the lingua franca of the web
Introduced on Postgresql 9.2
Currently this is nothing more than a vali...
A basic approach

CREATE or REPLACE FUNCTION 
CREATE or REPLACE FUNCTION 
  json_string(data json, key text) RETURNS TEXT ...
Web development stack
Client side: backbone.js – jquery.js
Server side: node.js
Database: PostgreSQL + PLV8
Pros:
Rapid pr...
Table like NOSql

CREATE EXTENSION plv8;
CREATE EXTENSION plv8;
CREATE TABLE things (
CREATE TABLE things (
   Id uuid PRI...
At the end ...
All very easy
All very fast
All very simple
Plv8 is stable for production
Constanly updated
Nosql db? No th...
Risorse
http://code.google.com/p/plv8js/wiki/PLV8
http://adpgtech.blogspot.it/2013/03/loading-useful-modulesin-plv8.html
h...
Questions?

PGDay.IT 2013 – 25 Ottobre 2013 - Prato

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

27 di 27
Upcoming SlideShare
Loading in …5
×

PLV8 - The PostgreSQL web side

4,134 views

Published on

Talk presented at the 2013 pgday.it. It talk about how PLV8 is integrated with PostgreSQL.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,134
On SlideShare
0
From Embeds
0
Number of Embeds
828
Actions
Shares
0
Downloads
30
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • {}
  • PLV8 - The PostgreSQL web side

    1. 1. PLV8 The Postgresql web side Lucio Grenzi l.grenzi@gmail.com PGDay.IT 2013 – 25 Ottobre 2013 - Prato 1 di 27
    2. 2. 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
    3. 3. 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
    4. 4. 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
    5. 5. 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
    6. 6. 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
    7. 7. 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
    8. 8. 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
    9. 9. 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
    10. 10. 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
    11. 11. 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
    12. 12. 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
    13. 13. 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
    14. 14. 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
    15. 15. 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
    16. 16. 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
    17. 17. 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
    18. 18. 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
    19. 19. 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
    20. 20. 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
    21. 21. 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
    22. 22. 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
    23. 23. 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
    24. 24. 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
    25. 25. 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
    26. 26. Questions? PGDay.IT 2013 – 25 Ottobre 2013 - Prato 26 di 27
    27. 27. PGDay.IT 2013 – 25 Ottobre 2013 - Prato 27 di 27

    ×