where $funcbody$ is the script source you specify in the CREATE FUNCTION AS
clause. The argument names are inherited from the CREATE FUNCTION statement,
or they will be named as $1, $2 if the names are omitted.

If the function is declared as RETURNS SETOF, PL/v8 prepares a tuplestore every
time called. You can call plv8.return_next() function to add as many results as
you like to return rows from this function. Alternatively, you can just return
a JS array to add set of records, a JS object to add a record, or a scalar value
to add a scalar to the tuplestore. Unlike other PLs, PL/v8 does not support
the per-value return strategy, but it always uses the tuplestore strategy.
If the argument object has extra properties that are not defined by the argument,
return_next raises an error.

Inline statement calls

Auto mapping between JS and database built-in types

For the result and arguments, database types and JS types are mapped
automatically. If the desired database type is one of

oid

bool

int2

int4

int8

float4

float8

numeric

date

timestamp

timestamptz

and 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. A JS object will be mapped to
a tuple when applicable.

Database access via SPI including prepared statements and cursors

plv8.execute( sql [, args] )

Executes SQL statements and retrieve the result. The args is an optional
argument that replaces $n placeholders in sql. For SELECT queries, the
returned value is an array of objects. Each hash represents each record.
Column names are mapped to object properties. For non-SELECT commands, the
returned value is an integer that represents number of affected rows.

plv8.prepare( sql, [, typenames] )

Opens a prepared statement. The typename parameter is an array where
each element is a string to indicate database type name for bind parameters.
Returned value is an object of PreparedPlan. This object must be freed by
plan.free() before leaving the function.

PreparedPlan.execute( [args] )

Executes the prepared statement. The args parameter is as plv8.execute(), and
can be omitted if the statement does not have parameters at all. The result
of this method is also as described in plv8.execute().

PreparedPlan.cursor( [args] )

Opens a cursor from the prepared statement. The args parameter is as
plv8.execute(), and can be omitted if the statement does not have parameters
at all. The returned object is of Cursor. This must be closed by Cursor.close()
before leaving the function.

If one of the SQL execution in the subtransaction block fails, all of operation
within the block is rolled back. If the process in the block throws JS
exception, it is transported to the outside. So use try ... catch block to
capture it and do alternative operations when it happens.

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.

In addition, PL/v8 provides a function to access other plv8 functions that have
been registered in the database.

With plv8.find_function(), you can look up other plv8 functions. If they are
not the plv8 function, it errors out. The function signature parameter to
plv8.find_function() is either of regproc (function name only) or regprocedure
(function name with argument types).

The plv8 object provides version string as plv8.version. This string
corresponds to plv8 module version. Note this is not the extension version.

Runtime environment separation across users in the same session

In PL/v8, each session has one global JS runtime context. This enables function
invocations at low cost, and sharing common object among the functions. However,
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.

Start-up procedure

PL/v8 provides a start up facility, which allows to call initialization plv8
function specified in the GUC variable.

SET plv8.start_proc = 'plv8_init';
SELECT plv8_test(10);

If this variable is set when the runtime is initialized, before the function
call of plv8_test() another plv8 function plv8_init() is invoked. In such
initialization function, you can add any properties to plv8 object to expose
common values or assign them to "this" property. In the initialization
function, the receiver "this" is specially pointing to the global object, so
the variables that is assigned to "this" property in this initialization are
visible from any subsequent function as global variables.

Remember CREATE FUNCTION also starts the plv8 runtime environment, so make sure
to SET this GUC before any plv8 actions including CREATE FUNCTION.