Supported Versions: Current (14) / 13 / 12 / 11 / 10
Development Versions: devel
Unsupported versions: 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3 / 7.2 / 7.1
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for the current version, or one of the other supported versions listed above instead.

Chapter 34. Procedural Languages

PostgreSQL allows user-defined functions to be written in other languages besides SQL and C. 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 PostgreSQL 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 four procedural languages available in the standard PostgreSQL distribution: PL/pgSQL (Chapter 35), PL/Tcl (Chapter 36), PL/Perl (Chapter 37), and PL/Python (Chapter 38). Other languages can be defined by users. The basics of developing a new procedural language are covered in Chapter 45.

There are additional procedural languages available that are not included in the core distribution. Appendix H has information about finding them.

34.1. 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 he chooses.

For the languages supplied with the standard distribution, the program createlang may be used to install the language instead of carrying out the details by hand. For example, to install the language PL/pgSQL into the database template1, 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 a database in four steps, which must be carried out by a database superuser. The createlang program automates all but step 1.

  1. 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 Section 31.9.6. 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.

  2. 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.

  3. Optionally, the language handler may 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 handler, declare it with a command like

    CREATE FUNCTION validator_function_name(oid)
        RETURNS void
        AS 'path-to-shared-object'
        LANGUAGE C;
  4. The PL must be declared with the command

        HANDLER handler_function_name
        [VALIDATOR validator_function_name] ;

    The optional key word TRUSTED specifies that 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, and PL/Perl are considered trusted; the languages PL/TclU, PL/PerlU, and PL/PythonU are designed to provide unlimited functionality and should not be marked trusted.

Example 34-1 shows how the manual installation procedure would work with the language PL/pgSQL.

Example 34-1. Manual Installation of PL/pgSQL

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;

PL/pgSQL has a validator function, so we declare that too:

CREATE FUNCTION plpgsql_validator(oid) RETURNS void AS
    '$libdir/plpgsql' LANGUAGE C;

The command

    HANDLER plpgsql_call_handler
    VALIDATOR plpgsql_validator;

then defines that the previously declared functions should be invoked for functions and trigger procedures where the language attribute is plpgsql.

In a default PostgreSQL installation, the handler for the PL/pgSQL language is built and installed into the "library" directory. If Tcl 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/PythonU is installed if Python support is configured.