|
CREATE LANGUAGENameCREATE LANGUAGE — Defines a new language for functionsCREATE [ TRUSTED ] PROCEDURAL LANGUAGE 'langname' HANDLER call_handler LANCOMPILER 'comment' Inputs
Outputs
DescriptionUsing CREATE LANGUAGE, 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. Writing PL handlersThe 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 the opaque type, a placeholder for unspecified or undefined types.. This prevents the call handler from being called directly as a function from queries. However, arguments must be supplied on the actual call when a PL function or trigger procedure in the language offered by the handler is to be executed.
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 of the procedure will be found in the prosrc attribute of the pg_proc table 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. NotesUse CREATE FUNCTION to create a function. Use DROP LANGUAGE to drop procedural languages. Refer to the table pg_language for further information: Table "pg_language" Attribute | Type | Modifier ---------------+---------+---------- lanname | name | lanispl | boolean | lanpltrusted | boolean | lanplcallfoid | oid | lancompiler | text | lanname | lanispl | lanpltrusted | lanplcallfoid | lancompiler ----------+---------+--------------+---------------+------------- internal | f | f | 0 | n/a C | f | f | 0 | /bin/cc sql | f | f | 0 | postgres Since the call handler for a procedural language must be registered with Postgres in the 'C' language, it inherits all the capabilities and restrictions of 'C' functions. At present, the definitions for a procedural language cannot be changed once they have been created. UsageThis 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 for information on 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'; CompatibilitySQL92CREATE LANGUAGE is a Postgres extension. There is no CREATE LANGUAGE statement in SQL92. |
|||||||||||||||||
With any suggestions or questions please feel free to contact us |