Procedural Languages
QHB allows user-defined functions to be written in other languages besides SQL and C/RUST. These other languages are generically called procedural languages (PLs). For a function 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 QHB and an existing implementation of a programming language. The handler itself is a C language function compiled into a shared object and loaded on demand, just like any other C function.
There are currently PL/pgSQL procedural language (Chapter PL/pgSQL) available in the standard QHB distribution. There are additional procedural languages available that are not included in the core distribution. In addition other languages can be defined by users; the basics of developing a new procedural language are covered in Chapter Writing a Procedural Language Handler.
Installing Procedural Languages
A procedural language must be “installed” into each database where it is to be used. But procedural languages installed in the database template1 are automatically available in all subsequently created databases, since their entries in template1 will be copied by CREATE DATABASE. So the database administrator can decide which languages are available in which databases and can make some languages available by default if desired.
For the languages supplied with the standard distribution, it is only necessary
to execute CREATE EXTENSION language_name to install the language into
the current database. The manual procedure described below is only recommended
for installing languages that have not been packaged as extensions.
Manual Procedural Language Installation
A procedural language is installed in a database in five steps, which must be carried out by a database superuser. In most cases the required SQL commands should be packaged as the installation script of an “extension”, so that CREATE EXTENSION can be used to execute them.
-
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/RUST functions does; see Section Compiling and Linking Dynamically-Loaded Functions. Often, the language handler will depend on an external library that provides the actual programming language engine; if so, that must be installed as well.
-
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 system that this function does not return one of the defined SQL data types and is not directly usable in SQL statements.
- Optionally, the language handler can provide an “inline” handler function that executes anonymous code blocks (DO commands) written in this language. If an inline handler function is provided by the language, declare it with a command like
CREATE FUNCTION inline_function_name(internal)
RETURNS void
AS 'path-to-shared-object'
LANGUAGE C;
- Optionally, the language handler can provide a “validator” function that checks a function definition for correctness without actually executing it. The validator function is called by CREATE FUNCTION if it exists. If a validator function is provided by the language, declare it with a command like
CREATE FUNCTION validator_function_name(oid)
RETURNS void
AS 'path-to-shared-object'
LANGUAGE C STRICT;
- Finally, the PL must be declared with the command
CREATE [TRUSTED] LANGUAGE language_name
HANDLER handler_function_name
[INLINE inline_function_name]
[VALIDATOR validator_function_name] ;
The optional key word TRUSTED specifies that the language does not grant access to data that the user would not otherwise have. Trusted languages are designed for ordinary database users (those without superuser privilege) and allows them to safely create functions and 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 language PL/pgSQL is considered trusted.
In a default QHB installation, the handler for the PL/pgSQL language is built and installed into the “library” directory; furthermore, the PL/pgSQL language itself is installed in all databases.