Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
summaryrefslogtreecommitdiff
blob: 8e72b9318988df93b777d3d0f9ea6bac0e738b7c (plain)
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
<!--
$Header: /cvsroot/pgsql/doc/src/sgml/xplang.sgml,v 1.12 2001/02/09 02:20:52 tgl Exp $
-->

 <chapter id="xplang">
  <title id="xplang-title">Procedural Languages</title>

  <para>
   <productname>Postgres</productname> allows users to add new
   programming languages to be available for writing functions and
   procedures.  These are called <firstterm>procedural
   languages</firstterm> (PL).  In the case of a function or trigger
   procedure 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 <quote>glue</quote> between
   <productname>Postgres</productname> and an existing implementation
   of a programming language.  The handler itself is a special
   programming language function compiled into a shared object and
   loaded on demand.
  </para>

  <para>
   Writing a handler for a new procedural language is outside the
   scope of this manual, although some information is provided in
   the CREATE LANGUAGE reference page.  Several procedural languages are
   available in the standard <productname>Postgres</productname> distribution.
  </para>

  <sect1 id="xplang-install">
   <title>Installing Procedural Languages</title>

   <para>
    A procedural language must be <quote>installed</quote> into each
    database where it is to be used.  But procedural languages installed in
    the template1 database are automatically available in all
    subsequently created databases. So the database administrator can
    decide which languages are available in which databases, and can make
    some languages available by default if he chooses.
   </para>

   <para>
    For the languages supplied with the standard distribution, the
    shell script <filename>createlang</filename> may be used instead
    of carrying out the details by hand.  For example, to install PL/pgSQL
    into the template1 database, use
<programlisting>
createlang plpgsql template1
</programlisting>
    The manual procedure described below is only recommended for
    installing custom languages that <filename>createlang</filename>
    does not know about.
   </para>

   <procedure>
    <title>
     Manual Procedural Language Installation
    </title>

    <para>
     A procedural language is installed in the database in three
     steps, which must be carried out by a database superuser.
    </para>

    <step performance="required">
     <para>
      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 <xref linkend="dfunc">.
     </para>
    </step>

    <step performance="required">
     <para>
      The handler must be declared with the command
<synopsis>
CREATE FUNCTION <replaceable>handler_function_name</replaceable> ()
    RETURNS OPAQUE AS
    '<replaceable>path-to-shared-object</replaceable>' LANGUAGE 'C';
</synopsis>
      The special return type of <type>OPAQUE</type> tells
      the database that this function does not return one of
      the defined <acronym>SQL</acronym> data types and is not directly usable
      in <acronym>SQL</acronym> statements.
     </para>
    </step>

    <step performance="required">
     <para>
      The PL must be declared with the command
<synopsis>
CREATE <optional>TRUSTED</optional> <optional>PROCEDURAL</optional> LANGUAGE '<replaceable>language-name</replaceable>'
    HANDLER <replaceable>handler_function_name</replaceable>
    LANCOMPILER '<replaceable>description</replaceable>';
</synopsis>
      The optional key word <token>TRUSTED</token> tells
      whether 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 backend, the <acronym>TRUSTED</acronym>
      flag should only be given for
      languages that do not allow access to database backends
      internals or the filesystem. The languages PL/pgSQL,
      PL/Tcl, and PL/Perl are known to be trusted; the language PL/TclU
      should <emphasis>not</emphasis> be marked trusted.
     </para>
    </step>
   </procedure>

   <para>
    In a default <productname>Postgres</productname> installation, the
    handler for the PL/pgSQL language is built and installed into the
    <quote>library</quote> directory. If Tcl/Tk 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 handler is built and installed
    if Perl support is configured.  The <filename>createlang</filename>
    script automates the two CREATE steps described above.
   </para>

   <procedure>
    <title>Example</title>

    <step performance="required">
     <para>
      The following command tells the database where to find the 
      shared object for the PL/pgSQL language's call handler function.

<programlisting>
CREATE FUNCTION plpgsql_call_handler () RETURNS OPAQUE AS
    '/usr/local/pgsql/lib/plpgsql.so' LANGUAGE 'C';
</programlisting>
     </para>
    </step>

    <step performance="Required">
    <para>
      The command
<programlisting>
CREATE TRUSTED PROCEDURAL LANGUAGE 'plpgsql'
    HANDLER plpgsql_call_handler
    LANCOMPILER 'PL/pgSQL';
</programlisting>
      then defines that the previously declared call handler function
      should be invoked for functions and trigger procedures where the
      language attribute is 'plpgsql'.
     </para>
    </step>
   </procedure>

  </sect1>

</chapter>

<!-- Keep this comment at the end of the file
Local variables:
mode:sgml
sgml-omittag:nil
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"./reference.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:("/usr/lib/sgml/catalog")
sgml-local-ecat-files:nil
End:
-->