2 doc/src/sgml/ref/create_language.sgml
3 PostgreSQL documentation
6 <refentry id=
"sql-createlanguage">
7 <indexterm zone=
"sql-createlanguage">
8 <primary>CREATE LANGUAGE
</primary>
12 <refentrytitle>CREATE LANGUAGE
</refentrytitle>
13 <manvolnum>7</manvolnum>
14 <refmiscinfo>SQL - Language Statements
</refmiscinfo>
18 <refname>CREATE LANGUAGE
</refname>
19 <refpurpose>define a new procedural language
</refpurpose>
24 CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE
<replaceable class=
"parameter">name
</replaceable>
25 HANDLER
<replaceable class=
"parameter">call_handler
</replaceable> [ INLINE
<replaceable class=
"parameter">inline_handler
</replaceable> ] [ VALIDATOR
<replaceable>valfunction
</replaceable> ]
26 CREATE [ OR REPLACE ] [ TRUSTED ] [ PROCEDURAL ] LANGUAGE
<replaceable class=
"parameter">name
</replaceable>
30 <refsect1 id=
"sql-createlanguage-description">
31 <title>Description
</title>
34 <command>CREATE LANGUAGE
</command> registers a new
35 procedural language with a
<productname>PostgreSQL
</productname>
36 database. Subsequently, functions and procedures can be
37 defined in this new language.
41 <command>CREATE LANGUAGE
</command> effectively associates the
42 language name with handler function(s) that are responsible for executing
43 functions written in the language. Refer to
<xref linkend=
"plhandler"/>
44 for more information about language handlers.
48 <command>CREATE OR REPLACE LANGUAGE
</command> will either create a
49 new language, or replace an existing definition. If the language
50 already exists, its parameters are updated according to the command,
51 but the language's ownership and permissions settings do not change,
52 and any existing functions written in the language are assumed to still
58 <productname>PostgreSQL
</productname> superuser privilege to
59 register a new language or change an existing language's parameters.
60 However, once the language is created it is valid to assign ownership of
61 it to a non-superuser, who may then drop it, change its permissions,
62 rename it, or assign it to a new owner. (Do not, however, assign
63 ownership of the underlying C functions to a non-superuser; that would
64 create a privilege escalation path for that user.)
68 The form of
<command>CREATE LANGUAGE
</command> that does not supply
69 any handler function is obsolete. For backwards compatibility with
70 old dump files, it is interpreted as
<command>CREATE EXTENSION
</command>.
71 That will work if the language has been packaged into an extension of
72 the same name, which is the conventional way to set up procedural
77 <refsect1 id=
"sql-createlanguage-parameters">
78 <title>Parameters
</title>
82 <term><literal>TRUSTED
</literal></term>
85 <para><literal>TRUSTED
</literal> specifies that the language does
86 not grant access to data that the user would not otherwise
87 have. If this key word is omitted
88 when registering the language, only users with the
89 <productname>PostgreSQL
</productname> superuser privilege can
90 use this language to create new functions.
96 <term><literal>PROCEDURAL
</literal></term>
100 This is a noise word.
106 <term><replaceable class=
"parameter">name
</replaceable></term>
110 The name of the new procedural language.
111 The name must be unique among the languages in the database.
117 <term><literal>HANDLER
</literal> <replaceable class=
"parameter">call_handler
</replaceable></term>
120 <para><replaceable class=
"parameter">call_handler
</replaceable> is
121 the name of a previously registered function that will be
122 called to execute the procedural language's functions. The call
123 handler for a procedural language must be written in a compiled
124 language such as C with version
1 call convention and
125 registered with
<productname>PostgreSQL
</productname> as a
126 function taking no arguments and returning the
127 <type>language_handler
</type> type, a placeholder type that is
128 simply used to identify the function as a call handler.
134 <term><literal>INLINE
</literal> <replaceable class=
"parameter">inline_handler
</replaceable></term>
137 <para><replaceable class=
"parameter">inline_handler
</replaceable> is the
138 name of a previously registered function that will be called
139 to execute an anonymous code block
140 (
<link linkend=
"sql-do"><command>DO
</command></link> command)
142 If no
<replaceable class=
"parameter">inline_handler
</replaceable>
143 function is specified, the language does not support anonymous code
145 The handler function must take one argument of
146 type
<type>internal
</type>, which will be the
<command>DO
</command> command's
147 internal representation, and it will typically return
148 <type>void
</type>. The return value of the handler is ignored.
154 <term><literal>VALIDATOR
</literal> <replaceable class=
"parameter">valfunction
</replaceable></term>
157 <para><replaceable class=
"parameter">valfunction
</replaceable> is the
158 name of a previously registered function that will be called
159 when a new function in the language is created, to validate the
162 validator function is specified, then a new function will not
163 be checked when it is created.
164 The validator function must take one argument of
165 type
<type>oid
</type>, which will be the OID of the
166 to-be-created function, and will typically return
<type>void
</type>.
170 A validator function would typically inspect the function body
171 for syntactical correctness, but it can also look at other
172 properties of the function, for example if the language cannot
173 handle certain argument types. To signal an error, the
174 validator function should use the
<function>ereport()
</function>
175 function. The return value of the function is ignored.
182 <refsect1 id=
"sql-createlanguage-notes">
186 Use
<link linkend=
"sql-droplanguage"><command>DROP LANGUAGE
</command></link> to drop procedural languages.
190 The system catalog
<classname>pg_language
</classname> (see
<xref
191 linkend=
"catalog-pg-language"/>) records information about the
192 currently installed languages. Also, the
<application>psql
</application>
193 command
<command>\dL
</command> lists the installed languages.
197 To create functions in a procedural language, a user must have the
198 <literal>USAGE
</literal> privilege for the language. By default,
199 <literal>USAGE
</literal> is granted to
<literal>PUBLIC
</literal> (i.e., everyone)
200 for trusted languages. This can be revoked if desired.
204 Procedural languages are local to individual databases.
205 However, a language can be installed into the
<literal>template1
</literal>
206 database, which will cause it to be available automatically in
207 all subsequently-created databases.
211 <refsect1 id=
"sql-createlanguage-examples">
212 <title>Examples
</title>
215 A minimal sequence for creating a new procedural language is:
217 CREATE FUNCTION plsample_call_handler() RETURNS language_handler
218 AS '$libdir/plsample'
220 CREATE LANGUAGE plsample
221 HANDLER plsample_call_handler;
223 Typically that would be written in an extension's creation script,
224 and users would do this to install the extension:
226 CREATE EXTENSION plsample;
227 </programlisting></para>
230 <refsect1 id=
"sql-createlanguage-compat">
231 <title>Compatibility
</title>
234 <command>CREATE LANGUAGE
</command> is a
235 <productname>PostgreSQL
</productname> extension.
240 <title>See Also
</title>
242 <simplelist type=
"inline">
243 <member><xref linkend=
"sql-alterlanguage"/></member>
244 <member><xref linkend=
"sql-createfunction"/></member>
245 <member><xref linkend=
"sql-droplanguage"/></member>
246 <member><xref linkend=
"sql-grant"/></member>
247 <member><xref linkend=
"sql-revoke"/></member>