gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
14KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Chapter 55. Writing a Procedural Language Handler</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="nls-programmer.html" title="54.2. For the Programmer" /><link rel="next" href="fdwhandler.html" title="Chapter 56. Writing a Foreign Data Wrapper" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">Chapter 55. Writing a Procedural Language Handler</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="nls-programmer.html" title="54.2. For the Programmer">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="internals.html" title="Part VII. Internals">Up</a></td><th width="60%" align="center">Part VII. Internals</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="fdwhandler.html" title="Chapter 56. Writing a Foreign Data Wrapper">Next</a></td></tr></table><hr></hr></div><div class="chapter" id="PLHANDLER"><div class="titlepage"><div><div><h2 class="title">Chapter 55. Writing a Procedural Language Handler</h2></div></div></div><a id="id-1.10.8.2" class="indexterm"></a><p>
  3. All calls to functions that are written in a language other than
  4. the current <span class="quote">“<span class="quote">version 1</span>”</span> interface for compiled
  5. languages (this includes functions in user-defined procedural languages
  6. and functions written in SQL) go through a <em class="firstterm">call handler</em>
  7. function for the specific language. It is the responsibility of
  8. the call handler to execute the function in a meaningful way, such
  9. as by interpreting the supplied source text. This chapter outlines
  10. how a new procedural language's call handler can be written.
  11. </p><p>
  12. The call handler for a procedural language is a
  13. <span class="quote">“<span class="quote">normal</span>”</span> function that must be written in a compiled
  14. language such as C, using the version-1 interface, and registered
  15. with <span class="productname">PostgreSQL</span> as taking no arguments
  16. and returning the type <code class="type">language_handler</code>. This
  17. special pseudo-type identifies the function as a call handler and
  18. prevents it from being called directly in SQL commands.
  19. For more details on C language calling conventions and dynamic loading,
  20. see <a class="xref" href="xfunc-c.html" title="37.10. C-Language Functions">Section 37.10</a>.
  21. </p><p>
  22. The call handler is called in the same way as any other function:
  23. It receives a pointer to a
  24. <code class="structname">FunctionCallInfoBaseData</code> <code class="type">struct</code> containing
  25. argument values and information about the called function, and it
  26. is expected to return a <code class="type">Datum</code> result (and possibly
  27. set the <code class="structfield">isnull</code> field of the
  28. <code class="structname">FunctionCallInfoBaseData</code> structure, if it wishes
  29. to return an SQL null result). The difference between a call
  30. handler and an ordinary callee function is that the
  31. <code class="structfield">flinfo-&gt;fn_oid</code> field of the
  32. <code class="structname">FunctionCallInfoBaseData</code> structure will contain
  33. the OID of the actual function to be called, not of the call
  34. handler itself. The call handler must use this field to determine
  35. which function to execute. Also, the passed argument list has
  36. been set up according to the declaration of the target function,
  37. not of the call handler.
  38. </p><p>
  39. It's up to the call handler to fetch the entry of the function from the
  40. <code class="classname">pg_proc</code> system catalog and to analyze the argument
  41. and return types of the called function. The <code class="literal">AS</code> clause from the
  42. <code class="command">CREATE FUNCTION</code> command for the function will be found
  43. in the <code class="literal">prosrc</code> column of the
  44. <code class="classname">pg_proc</code> row. This is commonly source
  45. text in the procedural language, but in theory it could be something else,
  46. such as a path name to a file, or anything else that tells the call handler
  47. what to do in detail.
  48. </p><p>
  49. Often, the same function is called many times per SQL statement.
  50. A call handler can avoid repeated lookups of information about the
  51. called function by using the
  52. <code class="structfield">flinfo-&gt;fn_extra</code> field. This will
  53. initially be <code class="symbol">NULL</code>, but can be set by the call handler to point at
  54. information about the called function. On subsequent calls, if
  55. <code class="structfield">flinfo-&gt;fn_extra</code> is already non-<code class="symbol">NULL</code>
  56. then it can be used and the information lookup step skipped. The
  57. call handler must make sure that
  58. <code class="structfield">flinfo-&gt;fn_extra</code> is made to point at
  59. memory that will live at least until the end of the current query,
  60. since an <code class="structname">FmgrInfo</code> data structure could be
  61. kept that long. One way to do this is to allocate the extra data
  62. in the memory context specified by
  63. <code class="structfield">flinfo-&gt;fn_mcxt</code>; such data will
  64. normally have the same lifespan as the
  65. <code class="structname">FmgrInfo</code> itself. But the handler could
  66. also choose to use a longer-lived memory context so that it can cache
  67. function definition information across queries.
  68. </p><p>
  69. When a procedural-language function is invoked as a trigger, no arguments
  70. are passed in the usual way, but the
  71. <code class="structname">FunctionCallInfoBaseData</code>'s
  72. <code class="structfield">context</code> field points at a
  73. <code class="structname">TriggerData</code> structure, rather than being <code class="symbol">NULL</code>
  74. as it is in a plain function call. A language handler should
  75. provide mechanisms for procedural-language functions to get at the trigger
  76. information.
  77. </p><p>
  78. This is a template for a procedural-language handler written in C:
  79. </p><pre class="programlisting">
  80. #include "postgres.h"
  81. #include "executor/spi.h"
  82. #include "commands/trigger.h"
  83. #include "fmgr.h"
  84. #include "access/heapam.h"
  85. #include "utils/syscache.h"
  86. #include "catalog/pg_proc.h"
  87. #include "catalog/pg_type.h"
  88. PG_MODULE_MAGIC;
  89. PG_FUNCTION_INFO_V1(plsample_call_handler);
  90. Datum
  91. plsample_call_handler(PG_FUNCTION_ARGS)
  92. {
  93. Datum retval;
  94. if (CALLED_AS_TRIGGER(fcinfo))
  95. {
  96. /*
  97. * Called as a trigger function
  98. */
  99. TriggerData *trigdata = (TriggerData *) fcinfo-&gt;context;
  100. retval = ...
  101. }
  102. else
  103. {
  104. /*
  105. * Called as a function
  106. */
  107. retval = ...
  108. }
  109. return retval;
  110. }
  111. </pre><p>
  112. Only a few thousand lines of code have to be added instead of the
  113. dots to complete the call handler.
  114. </p><p>
  115. After having compiled the handler function into a loadable module
  116. (see <a class="xref" href="xfunc-c.html#DFUNC" title="37.10.5. Compiling and Linking Dynamically-Loaded Functions">Section 37.10.5</a>), the following commands then
  117. register the sample procedural language:
  118. </p><pre class="programlisting">
  119. CREATE FUNCTION plsample_call_handler() RETURNS language_handler
  120. AS '<em class="replaceable"><code>filename</code></em>'
  121. LANGUAGE C;
  122. CREATE LANGUAGE plsample
  123. HANDLER plsample_call_handler;
  124. </pre><p>
  125. </p><p>
  126. Although providing a call handler is sufficient to create a minimal
  127. procedural language, there are two other functions that can optionally
  128. be provided to make the language more convenient to use. These
  129. are a <em class="firstterm">validator</em> and an
  130. <em class="firstterm">inline handler</em>. A validator can be provided
  131. to allow language-specific checking to be done during
  132. <a class="xref" href="sql-createfunction.html" title="CREATE FUNCTION"><span class="refentrytitle">CREATE FUNCTION</span></a>.
  133. An inline handler can be provided to allow the language to support
  134. anonymous code blocks executed via the <a class="xref" href="sql-do.html" title="DO"><span class="refentrytitle">DO</span></a> command.
  135. </p><p>
  136. If a validator is provided by a procedural language, it
  137. must be declared as a function taking a single parameter of type
  138. <code class="type">oid</code>. The validator's result is ignored, so it is customarily
  139. declared to return <code class="type">void</code>. The validator will be called at
  140. the end of a <code class="command">CREATE FUNCTION</code> command that has created
  141. or updated a function written in the procedural language.
  142. The passed-in OID is the OID of the function's <code class="classname">pg_proc</code>
  143. row. The validator must fetch this row in the usual way, and do
  144. whatever checking is appropriate.
  145. First, call <code class="function">CheckFunctionValidatorAccess()</code> to diagnose
  146. explicit calls to the validator that the user could not achieve through
  147. <code class="command">CREATE FUNCTION</code>. Typical checks then include verifying
  148. that the function's argument and result types are supported by the
  149. language, and that the function's body is syntactically correct
  150. in the language. If the validator finds the function to be okay,
  151. it should just return. If it finds an error, it should report that
  152. via the normal <code class="function">ereport()</code> error reporting mechanism.
  153. Throwing an error will force a transaction rollback and thus prevent
  154. the incorrect function definition from being committed.
  155. </p><p>
  156. Validator functions should typically honor the <a class="xref" href="runtime-config-client.html#GUC-CHECK-FUNCTION-BODIES">check_function_bodies</a> parameter: if it is turned off then
  157. any expensive or context-sensitive checking should be skipped. If the
  158. language provides for code execution at compilation time, the validator
  159. must suppress checks that would induce such execution. In particular,
  160. this parameter is turned off by <span class="application">pg_dump</span> so that it can
  161. load procedural language functions without worrying about side effects or
  162. dependencies of the function bodies on other database objects.
  163. (Because of this requirement, the call handler should avoid
  164. assuming that the validator has fully checked the function. The point
  165. of having a validator is not to let the call handler omit checks, but
  166. to notify the user immediately if there are obvious errors in a
  167. <code class="command">CREATE FUNCTION</code> command.)
  168. While the choice of exactly what to check is mostly left to the
  169. discretion of the validator function, note that the core
  170. <code class="command">CREATE FUNCTION</code> code only executes <code class="literal">SET</code> clauses
  171. attached to a function when <code class="varname">check_function_bodies</code> is on.
  172. Therefore, checks whose results might be affected by GUC parameters
  173. definitely should be skipped when <code class="varname">check_function_bodies</code> is
  174. off, to avoid false failures when reloading a dump.
  175. </p><p>
  176. If an inline handler is provided by a procedural language, it
  177. must be declared as a function taking a single parameter of type
  178. <code class="type">internal</code>. The inline handler's result is ignored, so it is
  179. customarily declared to return <code class="type">void</code>. The inline handler
  180. will be called when a <code class="command">DO</code> statement is executed specifying
  181. the procedural language. The parameter actually passed is a pointer
  182. to an <code class="structname">InlineCodeBlock</code> struct, which contains information
  183. about the <code class="command">DO</code> statement's parameters, in particular the
  184. text of the anonymous code block to be executed. The inline handler
  185. should execute this code and return.
  186. </p><p>
  187. It's recommended that you wrap all these function declarations,
  188. as well as the <code class="command">CREATE LANGUAGE</code> command itself, into
  189. an <em class="firstterm">extension</em> so that a simple <code class="command">CREATE EXTENSION</code>
  190. command is sufficient to install the language. See
  191. <a class="xref" href="extend-extensions.html" title="37.17. Packaging Related Objects into an Extension">Section 37.17</a> for information about writing
  192. extensions.
  193. </p><p>
  194. The procedural languages included in the standard distribution
  195. are good references when trying to write your own language handler.
  196. Look into the <code class="filename">src/pl</code> subdirectory of the source tree.
  197. The <a class="xref" href="sql-createlanguage.html" title="CREATE LANGUAGE"><span class="refentrytitle">CREATE LANGUAGE</span></a>
  198. reference page also has some useful details.
  199. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="nls-programmer.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="internals.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="fdwhandler.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">54.2. For the Programmer </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 56. Writing a Foreign Data Wrapper</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1