Quantcast
Channel: Planet PostgreSQL
Viewing all articles
Browse latest Browse all 9965

Andrew Dunstan: More PLV8 work

$
0
0
Yesterday I committed preliminary cursor support and support for anonymous records, fully recursive with arrays so you can have arrays of records with array fields and so on, for PLV8, and they will be represented as arrays and objects in the V8 engine. so that we get results like this (minus line breaks added for clarity):

andrew=# select q2json('
  select $$a$$ || x as b, 
         y as c, 
         array[row(x.*,array[1,2,3]),
               row(y.*,array[4,5,6])] as z 
  from generate_series(1,2) x, 
       generate_series(4,5) y');

 [{"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]},
  {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]},
  {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]},
  {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}]

Cursor and plan support is very similar to what is in PLPerl: we have createPlan(), freePlan(), createCursor(), fetchCursor(), and closeCursor(). Soon there will also be executePlan(), which will return a set of rows all at once back to the V8 engine, done too. Here's an example:
andrew=# CREATE OR REPLACE FUNCTION public.myfunc(n integer)
 RETURNS SETOF record
 LANGUAGE plv8
AS $function$
  var pln = createPlan("select x from generate_series(1,$1) as x",'int');
  var cursor = createCursor(pln,n);
  var row;
  while (row = fetchCursor(cursor))
        yield(row);
$function$
;
CREATE FUNCTION
andrew=# select x from myfunc(3) as n(x int);
 x 
---
 1
 2
 3
(3 rows)


Plans can outlive transactions, and be saved in PLV8 global space for later reuse, as in this example:
andrew=# do language plv8 'myplan = createPlan("select * from generate_series(1,5) as x"); ';
DO
andrew=# do language plv8 'mycursor = createCursor(myplan); 
          var row; 
          while (row=fetchCursor(mycursor)) {  print(NOTICE,row.x);} ';
NOTICE:  1
NOTICE:  2
NOTICE:  3
NOTICE:  4
NOTICE:  5
DO

One of the things I'd like to be able to do is to have support for some non-builtin types if they happen to be installed - hstore and citext are cases that come readily to mind.

Viewing all articles
Browse latest Browse all 9965

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>