'\" t .\" Title: CREATE LANGUAGE .\" Author: The PostgreSQL Global Development Group .\" Generator: DocBook XSL Stylesheets v1.75.2 .\" Date: 2016 .\" Manual: PostgreSQL 9.5.2 Documentation .\" Source: PostgreSQL 9.5.2 .\" Language: English .\" .TH "CREATE LANGUAGE" "5sql" "2016" "PostgreSQL 9.5.2" "PostgreSQL 9.5.2 Documentation" .\" ----------------------------------------------------------------- .\" * set default formatting .\" ----------------------------------------------------------------- .\" disable hyphenation .nh .\" disable justification (adjust text to left margin only) .ad l .\" ----------------------------------------------------------------- .\" * MAIN CONTENT STARTS HERE * .\" ----------------------------------------------------------------- .SH "NAME" CREATE_LANGUAGE \- define a new procedural language .SH "SYNOPSIS" .sp .nf CREATE [ OR REPLACE ] [ PROCEDURAL ] LANGUAGE \fIname\fR CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE \fIname\fR HANDLER \fIcall_handler\fR [ INLINE \fIinline_handler\fR ] [ VALIDATOR \fIvalfunction\fR ] .fi .SH "DESCRIPTION" .PP \fBCREATE LANGUAGE\fR registers a new procedural language with a PostgreSQL database\&. Subsequently, functions and trigger procedures can be defined in this new language\&. .if n \{\ .sp .\} .RS 4 .it 1 an-trap .nr an-no-space-flag 1 .nr an-break-flag 1 .br .ps +1 \fBNote\fR .ps -1 .br .PP As of PostgreSQL 9\&.1, most procedural languages have been made into \(lqextensions\(rq, and should therefore be installed with CREATE EXTENSION (\fBCREATE_EXTENSION\fR(5)) not \fBCREATE LANGUAGE\fR\&. Direct use of \fBCREATE LANGUAGE\fR should now be confined to extension installation scripts\&. If you have a \(lqbare\(rq language in your database, perhaps as a result of an upgrade, you can convert it to an extension using CREATE EXTENSION \fIlangname\fR FROM unpackaged\&. .sp .5v .RE .PP \fBCREATE LANGUAGE\fR effectively associates the language name with handler function(s) that are responsible for executing functions written in the language\&. Refer to Chapter 53, Writing A Procedural Language Handler, in the documentation for more information about language handlers\&. .PP There are two forms of the \fBCREATE LANGUAGE\fR command\&. In the first form, the user supplies just the name of the desired language, and the PostgreSQL server consults the pg_pltemplate system catalog to determine the correct parameters\&. In the second form, the user supplies the language parameters along with the language name\&. The second form can be used to create a language that is not defined in pg_pltemplate, but this approach is considered obsolescent\&. .PP When the server finds an entry in the pg_pltemplate catalog for the given language name, it will use the catalog data even if the command includes language parameters\&. This behavior simplifies loading of old dump files, which are likely to contain out\-of\-date information about language support functions\&. .PP Ordinarily, the user must have the PostgreSQL superuser privilege to register a new language\&. However, the owner of a database can register a new language within that database if the language is listed in the pg_pltemplate catalog and is marked as allowed to be created by database owners (tmpldbacreate is true)\&. The default is that trusted languages can be created by database owners, but this can be adjusted by superusers by modifying the contents of pg_pltemplate\&. The creator of a language becomes its owner and can later drop it, rename it, or assign it to a new owner\&. .PP \fBCREATE OR REPLACE LANGUAGE\fR will either create a new language, or replace an existing definition\&. If the language already exists, its parameters are updated according to the values specified or taken from pg_pltemplate, but the language\(aqs ownership and permissions settings do not change, and any existing functions written in the language are assumed to still be valid\&. In addition to the normal privilege requirements for creating a language, the user must be superuser or owner of the existing language\&. The REPLACE case is mainly meant to be used to ensure that the language exists\&. If the language has a pg_pltemplate entry then REPLACE will not actually change anything about an existing definition, except in the unusual case where the pg_pltemplate entry has been modified since the language was created\&. .SH "PARAMETERS" .PP TRUSTED .RS 4 TRUSTED specifies that the language does not grant access to data that the user would not otherwise have\&. If this key word is omitted when registering the language, only users with the PostgreSQL superuser privilege can use this language to create new functions\&. .RE .PP PROCEDURAL .RS 4 This is a noise word\&. .RE .PP \fIname\fR .RS 4 The name of the new procedural language\&. The name must be unique among the languages in the database\&. .sp For backward compatibility, the name can be enclosed by single quotes\&. .RE .PP HANDLER \fIcall_handler\fR .RS 4 \fIcall_handler\fR is the name of a previously registered function that will be called to execute the procedural language\(aqs functions\&. The call handler for a procedural language must be written in a compiled language such as C with version 1 call convention and registered with PostgreSQL as a function taking no arguments and returning the language_handler type, a placeholder type that is simply used to identify the function as a call handler\&. .RE .PP INLINE \fIinline_handler\fR .RS 4 \fIinline_handler\fR is the name of a previously registered function that will be called to execute an anonymous code block (\fBDO\fR(5) command) in this language\&. If no \fIinline_handler\fR function is specified, the language does not support anonymous code blocks\&. The handler function must take one argument of type internal, which will be the \fBDO\fR command\(aqs internal representation, and it will typically return void\&. The return value of the handler is ignored\&. .RE .PP VALIDATOR \fIvalfunction\fR .RS 4 \fIvalfunction\fR is the name of a previously registered function that will be called when a new function in the language is created, to validate the new function\&. If no validator function is specified, then a new function will not be checked when it is created\&. The validator function must take one argument of type oid, which will be the OID of the to\-be\-created function, and will typically return void\&. .sp 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 \fBereport()\fR function\&. The return value of the function is ignored\&. .RE .PP The TRUSTED option and the support function name(s) are ignored if the server has an entry for the specified language name in pg_pltemplate\&. .SH "NOTES" .PP The \fBcreatelang\fR(1) program is a simple wrapper around the \fBCREATE LANGUAGE\fR command\&. It eases installation of procedural languages from the shell command line\&. .PP Use DROP LANGUAGE (\fBDROP_LANGUAGE\fR(5)), or better yet the \fBdroplang\fR(1) program, to drop procedural languages\&. .PP The system catalog pg_language (see Section 49.28, \(lqpg_language\(rq, in the documentation) records information about the currently installed languages\&. Also, \fBcreatelang\fR has an option to list the installed languages\&. .PP To create functions in a procedural language, a user must have the USAGE privilege for the language\&. By default, USAGE is granted to PUBLIC (i\&.e\&., everyone) for trusted languages\&. This can be revoked if desired\&. .PP Procedural languages are local to individual databases\&. However, a language can be installed into the template1 database, which will cause it to be available automatically in all subsequently\-created databases\&. .PP The call handler function, the inline handler function (if any), and the validator function (if any) must already exist if the server does not have an entry for the language in pg_pltemplate\&. But when there is an entry, the functions need not already exist; they will be automatically defined if not present in the database\&. (This might result in \fBCREATE LANGUAGE\fR failing, if the shared library that implements the language is not available in the installation\&.) .PP 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, \fBCREATE LANGUAGE\fR will accept a function declared as returning opaque, but it will issue a notice and change the function\(aqs declared return type to language_handler\&. .SH "EXAMPLES" .PP The preferred way of creating any of the standard procedural languages is just: .sp .if n \{\ .RS 4 .\} .nf CREATE LANGUAGE plperl; .fi .if n \{\ .RE .\} .PP For a language not known in the pg_pltemplate catalog, a sequence such as this is needed: .sp .if n \{\ .RS 4 .\} .nf CREATE FUNCTION plsample_call_handler() RETURNS language_handler AS \(aq$libdir/plsample\(aq LANGUAGE C; CREATE LANGUAGE plsample HANDLER plsample_call_handler; .fi .if n \{\ .RE .\} .SH "COMPATIBILITY" .PP \fBCREATE LANGUAGE\fR is a PostgreSQL extension\&. .SH "SEE ALSO" ALTER LANGUAGE (\fBALTER_LANGUAGE\fR(5)), CREATE FUNCTION (\fBCREATE_FUNCTION\fR(5)), DROP LANGUAGE (\fBDROP_LANGUAGE\fR(5)), \fBGRANT\fR(5), \fBREVOKE\fR(5), \fBcreatelang\fR(1), \fBdroplang\fR(1)