Flashnux

GNU/Linux man pages

Livre :
Expressions régulières,
Syntaxe et mise en oeuvre :

ISBN : 978-2-7460-9712-4
EAN : 9782746097124
(Editions ENI)

GNU/Linux

RedHat 5.2

(Apollo)

create_language(l)


CREATE LANGUAGE

CREATE LANGUAGE

NAME
SYNOPSIS
DESCRIPTION
WRITING PL HANDLERS
EXAMPLE
SEE ALSO
RESTRICTIONS
BUGS

NAME

create language - define a new language for functions

SYNOPSIS

create [trusted] procedural language ’lanname’

handler call_handler

lancompiler ’comment’

DESCRIPTION

With this command, a Postgres user can register a new language with Postgres. Subsequently, functions and trigger procedures can be defined in this new language. The user must have the Postgres superuser privilege to register a new language.

The lanname is the name of the new procedural language. It is converted to lower case before the new entry in the pg_language system catalog is inserted. Note that this case translation is also done on create function(l) and drop language(l). Thus, the language name is case insensitive. A procedural language cannot override one of the builtin languages of Postgres.

The argument for handler is the name of a previously registered function that will be called to execute the PL procedures.

The lancompiler argument is the string that will be inserted in the lancompiler attribute of the new pg_language entry. Up to now, Postgres doesn’t use this attribute in any way.

The trusted keyword specifies, that the call handler for the language is safe - i.e. it offers an unprivileged user no functionality to get around access restrictions. If this keyword is omitted when registering the language, only users with the Postgres superuser privilege can use this language to create new functions (like the ’C’ language).

WRITING PL HANDLERS

The call handler for a procedural language must be written in a compiler language such as ’C’ and registered with Postgres as a function taking no arguments and returning opaque type. This prevents the call handler from beeing called directly as a function from queries. But there are arguments on the actual call when a PL function or trigger procedure in the language offered by the handler is to be executed.

When called from the trigger manager, the only argument is the object ID from the procedures pg_proc entry. All other information from the trigger manager is found in the global CurrentTriggerData pointer.

When called from the function manager, the arguments are the object ID of the procedures pg_proc entry, the number of arguments given to the PL function, the arguments in a FmgrValues structure and a pointer to a boolean where the function tells the caller if the return value is the SQL NULL value.

It’s up to the call handler to fetch the pg_proc entry and to analyze the argument and return types of the called procedure. the as clause from the create function(l) of the procedure will be found in the prosrc attribute of the pg_proc entry. This may be the source text in the procedural language itself (like for PL/Tcl), a pathname to a file or anything else that tells the call handler what to do in detail.

EXAMPLE

Following is a template for a PL handler written in ’C’:

#include "executor/spi.h"
#include "commands/trigger.h"
#include "utils/elog.h"

#include "fmgr.h"

/* for FmgrValues struct */

#include "access/heapam.h"
#include "utils/syscache.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"

Datum
plsample_call_handler(

Oid

prooid,

int

pronargs,

FmgrValues

*proargs,

bool

*isNull)

{

Datum

retval;

TriggerData

*trigdata;

if (CurrentTriggerData == NULL) {

/*

* Called as a function

*/

retval = ...

} else {

/*

* Called as a trigger procedure

*/

trigdata = CurrentTriggerData;

CurrentTriggerData = NULL;

retval = ...

}

*isNull = false;

return retval;

}

Only a few thousand lines of code have to be added instead of the dots to complete the PL call handler. See create function(l) how to compile it into a loadable module. The following commands then register the sample procedural language.

create function plsample_call_handler () returns opaque

as ’/usr/local/pgsql/lib/plsample.so’

language ’C’;

create procedural language ’plsample’

handler plsample_call_handler

lancompiler ’PL/Sample’;

SEE ALSO

create function(l), drop language(l).

RESTRICTIONS

Since the call handler for a procedural language must be registered with Postgres in the ’C’ language, it inherits all the restrictions of ’C’ functions.

BUGS

Currently, the definitions for a procedural language once created cannot be changed.



create_language(l)