Procedural Languages Introduction PostgreSQL allows users to add new programming languages to be available for writing functions and procedures. These are called procedural languages (PL). In the case of a function or trigger procedure written in a procedural language, the database server has no built-in knowledge about how to interpret the function's source text. Instead, the task is passed to a special handler that knows the details of the language. The handler could either do all the work of parsing, syntax analysis, execution, etc. itself, or it could serve as glue between PostgreSQL and an existing implementation of a programming language. The handler itself is a special programming language function compiled into a shared object and loaded on demand. Writing a handler for a new procedural language is described in . Several procedural languages are available in the standard PostgreSQL distribution, which can serve as examples. Installing Procedural Languages A procedural language must be installed into each database where it is to be used. But procedural languages installed in the template1 database are automatically available in all subsequently created databases. So the database administrator can decide which languages are available in which databases, and can make some languages available by default if he chooses. For the languages supplied with the standard distribution, the shell script createlang may be used instead of carrying out the details by hand. For example, to install PL/pgSQL into the template1 database, use createlang plpgsql template1 The manual procedure described below is only recommended for installing custom languages that createlang does not know about. Manual Procedural Language Installation A procedural language is installed in the database in three steps, which must be carried out by a database superuser. The shared object for the language handler must be compiled and installed into an appropriate library directory. This works in the same way as building and installing modules with regular user-defined C functions does; see . The handler must be declared with the command CREATE FUNCTION handler_function_name () RETURNS LANGUAGE_HANDLER AS 'path-to-shared-object' LANGUAGE C; The special return type of LANGUAGE_HANDLER tells the database that this function does not return one of the defined SQL data types and is not directly usable in SQL statements. The PL must be declared with the command CREATE TRUSTED PROCEDURAL LANGUAGE language-name HANDLER handler_function_name; The optional key word TRUSTED tells whether ordinary database users that have no superuser privileges should be allowed to use this language to create functions and trigger procedures. Since PL functions are executed inside the database server, the TRUSTED flag should only be given for languages that do not allow access to database server internals or the file system. The languages PL/pgSQL, PL/Tcl, PL/Perl, and PL/Python are known to be trusted; the languages PL/TclU and PL/PerlU are designed to provide unlimited functionality should not be marked trusted. In a default PostgreSQL installation, the handler for the PL/pgSQL language is built and installed into the library directory. If Tcl/Tk support is configured in, the handlers for PL/Tcl and PL/TclU are also built and installed in the same location. Likewise, the PL/Perl and PL/PerlU handlers are built and installed if Perl support is configured, and PL/Python is installed if Python support is configured. The createlang script automates and described above. Manual Installation of <application>PL/pgSQL</application> The following command tells the database server where to find the shared object for the PL/pgSQL language's call handler function. CREATE FUNCTION plpgsql_call_handler () RETURNS LANGUAGE_HANDLER AS '$libdir/plpgsql' LANGUAGE C; The command CREATE TRUSTED PROCEDURAL LANGUAGE plpgsql HANDLER plpgsql_call_handler; then defines that the previously declared call handler function should be invoked for functions and trigger procedures where the language attribute is plpgsql.