CREATE LANGUAGESection: SQL Commands (7)Updated: 2003-11-02 |
CREATE LANGUAGESection: SQL Commands (7)Updated: 2003-11-02 |
CREATE [ TRUSTED ] [ PROCEDURAL ] LANGUAGE name
HANDLER call_handler [ VALIDATOR valfunction ]
Using CREATE LANGUAGE, a PostgreSQL user can register a new procedural language with a PostgreSQL database. Subsequently, functions and trigger procedures can be defined in this new language. The user must have the PostgreSQL superuser privilege to register a new language.
CREATE LANGUAGE effectively associates the language name with a call handler that is responsible for executing functions written in the language. Refer to the section called ``User-Defined Functions'' in the documentation for more information about language call handlers.
Note that procedural languages are local to individual databases. To make a language available in all databases by default, it should be installed into the template1 database.
For backward compatibility, the name may be enclosed by single quotes.
A validator function would typically inspect the function body for syntactical correctness, but it can also look at other properties of the function, for example if the language cannot handle certain argument types. To signal an error, the validator function should use the ereport() function. The return value of the function is ignored.
This command normally should not be executed directly by users. For the procedural languages supplied in the PostgreSQL distribution, the createlang(1) program should be used, which will also install the correct call handler. (createlang will call CREATE LANGUAGE internally.)
In PostgreSQL versions before 7.3, it was necessary to declare handler functions as returning the placeholder type opaque, rather than language_handler. To support loading of old dump files, CREATE LANGUAGE will accept a function declared as returning opaque, but it will issue a notice and change the function's declared return type to language_handler.
Use the CREATE FUNCTION [create_function(7)] command to create a new function.
Use DROP LANGUAGE [drop_language(7)], or better yet the droplang(1) program, to drop procedural languages.
The system catalog pg_language (see the chapter called ``System Catalogs'' in the documentation) records information about the currently installed languages. Also createlang has an option to list the installed languages.
The definition of a procedural language cannot be changed once it has been created, with the exception of the privileges.
To be able to use a procedural language, a user must be granted the USAGE privilege. The createlang program automatically grants permissions to everyone if the language is known to be trusted.
The following two commands executed in sequence will register a new procedural language and the associated call handler.
CREATE FUNCTION plsample_call_handler() RETURNS language_handler
AS '$libdir/plsample'
LANGUAGE C;
CREATE LANGUAGE plsample
HANDLER plsample_call_handler;
CREATE LANGUAGE is a PostgreSQL extension.