File: xplang.html

package info (click to toggle)
pgadmin3 1.4.3-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 29,796 kB
  • ctags: 10,758
  • sloc: cpp: 55,356; sh: 6,164; ansic: 1,520; makefile: 576; sql: 482; xml: 100; perl: 18
file content (196 lines) | stat: -rw-r--r-- 11,578 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Chapter35.Procedural Languages</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="server-programming.html" title="PartV.Server Programming">
<link rel="prev" href="rules-triggers.html" title="34.6.Rules versus Triggers">
<link rel="next" href="plpgsql.html" title="Chapter36.PL/pgSQL - SQL Procedural Language">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter" lang="en" id="xplang">
<div class="titlepage"><div><div><h2 class="title">
<a name="xplang"></a>Chapter35.Procedural Languages</h2></div></div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl><dt><span class="sect1"><a href="xplang.html#xplang-install">35.1. Installing Procedural Languages</a></span></dt></dl>
</div>
<a name="id719856"></a><p>   <span class="productname">PostgreSQL</span> allows user-defined functions
   to be written in other languages besides SQL and C.  These other
   languages are generically called <em class="firstterm">procedural
   languages</em> (<acronym class="acronym">PL</acronym>s).  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 &#8220;<span class="quote">glue</span>&#8221; between
   <span class="productname">PostgreSQL</span> 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.
  </p>
<p>   There are currently four procedural languages available in the
   standard <span class="productname">PostgreSQL</span> distribution:
   <span class="application">PL/pgSQL</span> (<a href="plpgsql.html" title="Chapter36.PL/pgSQL - SQL Procedural Language">Chapter36, <i><span class="application">PL/pgSQL</span> - <acronym class="acronym">SQL</acronym> Procedural Language</i></a>),
   <span class="application">PL/Tcl</span> (<a href="pltcl.html" title="Chapter37.PL/Tcl - Tcl Procedural Language">Chapter37, <i>PL/Tcl - Tcl Procedural Language</i></a>),
   <span class="application">PL/Perl</span> (<a href="plperl.html" title="Chapter38.PL/Perl - Perl Procedural Language">Chapter38, <i>PL/Perl - Perl Procedural Language</i></a>), and
   <span class="application">PL/Python</span> (<a href="plpython.html" title="Chapter39.PL/Python - Python Procedural Language">Chapter39, <i>PL/Python - Python Procedural Language</i></a>).
   Other languages can be defined by users.
   The basics of developing a new procedural language are covered in <a href="plhandler.html" title="Chapter46.Writing A Procedural Language Handler">Chapter46, <i>Writing A Procedural Language Handler</i></a>.
  </p>
<p>   There are additional procedural languages available that are not
   included in the core distribution. <a href="external-projects.html" title="AppendixH.External Projects">AppendixH, <i>External Projects</i></a>
   has information about finding them.
  </p>
<div class="sect1" lang="en">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="xplang-install"></a>35.1.Installing Procedural Languages</h2></div></div></div>
<p>    A procedural language must be &#8220;<span class="quote">installed</span>&#8221; into each
    database where it is to be used.  But procedural languages installed in
    the database <code class="literal">template1</code> are automatically available in all
    subsequently created databases, since their entries in
    <code class="literal">template1</code> will be copied by <code class="command">CREATE DATABASE</code>.
    So the database administrator can
    decide which languages are available in which databases and can make
    some languages available by default if he chooses.
   </p>
<p>    For the languages supplied with the standard distribution, it is
    only necessary to execute <code class="command">CREATE LANGUAGE</code>
    <em class="replaceable"><code>language_name</code></em> to install the language into the
    current database.  Alternatively, the program <a href="app-createlang.html" title="createlang"><span class="refentrytitle"><a name="app-createlang-title"></a><span class="application">createlang</span></span></a> may be used to do this from the shell
    command line.  For example, to install the language
    <span class="application">PL/pgSQL</span> into the database
    <code class="literal">template1</code>, use
</p>
<pre class="programlisting">createlang plpgsql template1</pre>
<p>
    The manual procedure described below is only recommended for
    installing custom languages that <code class="command">CREATE LANGUAGE</code>
    does not know about.
   </p>
<div class="procedure">
<a name="id720085"></a><p class="title"><b>     Manual Procedural Language Installation
    </b></p>
<p>     A procedural language is installed in a database in four steps,
     which must be carried out by a database superuser.  (For languages
     known to <code class="command">CREATE LANGUAGE</code>, the second and third steps
     can be omitted, because they will be carried out automatically
     if needed.)
    </p>
<ol type="1">
<li>
<a name="xplang-install-cr1"></a><p>      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 <a href="xfunc-c.html#dfunc" title="32.9.6.Compiling and Linking Dynamically-Loaded Functions">Section32.9.6, &#8220;Compiling and Linking Dynamically-Loaded Functions&#8221;</a>.  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.
     </p>
</li>
<li>
<a name="xplang-install-cr2"></a><p>      The handler must be declared with the command
</p>
<pre class="synopsis">CREATE FUNCTION <em class="replaceable"><code>handler_function_name</code></em>()
    RETURNS language_handler
    AS '<em class="replaceable"><code>path-to-shared-object</code></em>'
    LANGUAGE C;</pre>
<p>
      The special return type of <code class="type">language_handler</code> tells
      the database system that this function does not return one of
      the defined <acronym class="acronym">SQL</acronym> data types and is not directly usable
      in <acronym class="acronym">SQL</acronym> statements.
     </p>
</li>
<li>
<a name="xplang-install-cr3"></a><p>      Optionally, the language handler may provide a &#8220;<span class="quote">validator</span>&#8221;
      function that checks a function definition for correctness without
      actually executing it.  The validator function is called by
      <code class="command">CREATE FUNCTION</code> if it exists.  If a validator function
      is provided by the handler, declare it with a command like
</p>
<pre class="synopsis">CREATE FUNCTION <em class="replaceable"><code>validator_function_name</code></em>(oid)
    RETURNS void
    AS '<em class="replaceable"><code>path-to-shared-object</code></em>'
    LANGUAGE C;</pre>
<p>
     </p>
</li>
<li>
<a name="xplang-install-cr4"></a><p>      The PL must be declared with the command
</p>
<pre class="synopsis">CREATE [<span class="optional">TRUSTED</span>] [<span class="optional">PROCEDURAL</span>] LANGUAGE <em class="replaceable"><code>language-name</code></em>
    HANDLER <em class="replaceable"><code>handler_function_name</code></em>
    [<span class="optional">VALIDATOR <em class="replaceable"><code>validator_function_name</code></em></span>] ;</pre>
<p>
      The optional key word <code class="literal">TRUSTED</code> 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 <code class="literal">TRUSTED</code> flag should only be given
      for languages that do not allow access to database server
      internals or the file system. The languages
      <span class="application">PL/pgSQL</span>,
      <span class="application">PL/Tcl</span>, and
      <span class="application">PL/Perl</span>
      are considered trusted; the languages
      <span class="application">PL/TclU</span>,
      <span class="application">PL/PerlU</span>, and
      <span class="application">PL/PythonU</span>
      are designed to provide unlimited functionality and should
      <span class="emphasis"><em>not</em></span> be marked trusted.
     </p>
</li>
</ol>
</div>
<p>    <a href="xplang.html#xplang-install-example" title="Example35.1.Manual Installation of PL/pgSQL">Example35.1, &#8220;Manual Installation of <span class="application">PL/pgSQL</span>&#8221;</a> shows how the manual
    installation procedure would work with the language
    <span class="application">PL/pgSQL</span>.
   </p>
<div class="example">
<a name="xplang-install-example"></a><p class="title"><b>Example35.1.Manual Installation of <span class="application">PL/pgSQL</span></b></p>
<div class="example-contents">
<p>      The following command tells the database server where to find the 
      shared object for the <span class="application">PL/pgSQL</span> language's call handler function.

</p>
<pre class="programlisting">CREATE FUNCTION plpgsql_call_handler() RETURNS language_handler AS
    '$libdir/plpgsql' LANGUAGE C;</pre>
<p>
     </p>
<p>      <span class="application">PL/pgSQL</span> has a validator function,
      so we declare that too:

</p>
<pre class="programlisting">CREATE FUNCTION plpgsql_validator(oid) RETURNS void AS
    '$libdir/plpgsql' LANGUAGE C;</pre>
<p>
     </p>
<p>      The command
</p>
<pre class="programlisting">CREATE TRUSTED PROCEDURAL LANGUAGE plpgsql
    HANDLER plpgsql_call_handler
    VALIDATOR plpgsql_validator;</pre>
<p>
      then defines that the previously declared functions
      should be invoked for functions and trigger procedures where the
      language attribute is <code class="literal">plpgsql</code>.
     </p>
</div>
</div>
<br class="example-break"><p>    In a default <span class="productname">PostgreSQL</span> installation,
    the handler for the <span class="application">PL/pgSQL</span> language
    is built and installed into the &#8220;<span class="quote">library</span>&#8221;
    directory. If <span class="application">Tcl</span> support is configured in, the handlers
    for <span class="application">PL/Tcl</span> and <span class="application">PL/TclU</span> are also built and
    installed in the same location.  Likewise, the <span class="application">PL/Perl</span> and
    <span class="application">PL/PerlU</span> handlers are built and installed if Perl support
    is configured, and the <span class="application">PL/PythonU</span> handler is
    installed if Python support is configured.
   </p>
</div>
</div></body>
</html>