gooderp18绿色标准版
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

84 lines
8.3KB

  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>SPI_prepare</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="spi-spi-execute-with-args.html" title="SPI_execute_with_args" /><link rel="next" href="spi-spi-prepare-cursor.html" title="SPI_prepare_cursor" /></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">SPI_prepare</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="spi-spi-execute-with-args.html" title="SPI_execute_with_args">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="spi-interface.html" title="46.1. Interface Functions">Up</a></td><th width="60%" align="center">46.1. Interface Functions</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="spi-spi-prepare-cursor.html" title="SPI_prepare_cursor">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SPI-SPI-PREPARE"><div class="titlepage"></div><a id="id-1.8.12.8.7.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">SPI_prepare</span></h2><p>SPI_prepare — prepare a statement, without executing it yet</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. SPIPlanPtr SPI_prepare(const char * <em class="parameter"><code>command</code></em>, int <em class="parameter"><code>nargs</code></em>, Oid * <em class="parameter"><code>argtypes</code></em>)
  4. </pre></div><div class="refsect1" id="id-1.8.12.8.7.5"><h2>Description</h2><p>
  5. <code class="function">SPI_prepare</code> creates and returns a prepared
  6. statement for the specified command, but doesn't execute the command.
  7. The prepared statement can later be executed repeatedly using
  8. <code class="function">SPI_execute_plan</code>.
  9. </p><p>
  10. When the same or a similar command is to be executed repeatedly, it
  11. is generally advantageous to perform parse analysis only once, and
  12. might furthermore be advantageous to re-use an execution plan for the
  13. command.
  14. <code class="function">SPI_prepare</code> converts a command string into a
  15. prepared statement that encapsulates the results of parse analysis.
  16. The prepared statement also provides a place for caching an execution plan
  17. if it is found that generating a custom plan for each execution is not
  18. helpful.
  19. </p><p>
  20. A prepared command can be generalized by writing parameters
  21. (<code class="literal">$1</code>, <code class="literal">$2</code>, etc.) in place of what would be
  22. constants in a normal command. The actual values of the parameters
  23. are then specified when <code class="function">SPI_execute_plan</code> is called.
  24. This allows the prepared command to be used over a wider range of
  25. situations than would be possible without parameters.
  26. </p><p>
  27. The statement returned by <code class="function">SPI_prepare</code> can be used
  28. only in the current invocation of the C function, since
  29. <code class="function">SPI_finish</code> frees memory allocated for such a
  30. statement. But the statement can be saved for longer using the functions
  31. <code class="function">SPI_keepplan</code> or <code class="function">SPI_saveplan</code>.
  32. </p></div><div class="refsect1" id="id-1.8.12.8.7.6"><h2>Arguments</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">const char * <em class="parameter"><code>command</code></em></code></span></dt><dd><p>
  33. command string
  34. </p></dd><dt><span class="term"><code class="literal">int <em class="parameter"><code>nargs</code></em></code></span></dt><dd><p>
  35. number of input parameters (<code class="literal">$1</code>, <code class="literal">$2</code>, etc.)
  36. </p></dd><dt><span class="term"><code class="literal">Oid * <em class="parameter"><code>argtypes</code></em></code></span></dt><dd><p>
  37. pointer to an array containing the <acronym class="acronym">OID</acronym>s of
  38. the data types of the parameters
  39. </p></dd></dl></div></div><div class="refsect1" id="id-1.8.12.8.7.7"><h2>Return Value</h2><p>
  40. <code class="function">SPI_prepare</code> returns a non-null pointer to an
  41. <code class="type">SPIPlan</code>, which is an opaque struct representing a prepared
  42. statement. On error, <code class="symbol">NULL</code> will be returned,
  43. and <code class="varname">SPI_result</code> will be set to one of the same
  44. error codes used by <code class="function">SPI_execute</code>, except that
  45. it is set to <code class="symbol">SPI_ERROR_ARGUMENT</code> if
  46. <em class="parameter"><code>command</code></em> is <code class="symbol">NULL</code>, or if
  47. <em class="parameter"><code>nargs</code></em> is less than 0, or if <em class="parameter"><code>nargs</code></em> is
  48. greater than 0 and <em class="parameter"><code>argtypes</code></em> is <code class="symbol">NULL</code>.
  49. </p></div><div class="refsect1" id="id-1.8.12.8.7.8"><h2>Notes</h2><p>
  50. If no parameters are defined, a generic plan will be created at the
  51. first use of <code class="function">SPI_execute_plan</code>, and used for all
  52. subsequent executions as well. If there are parameters, the first few uses
  53. of <code class="function">SPI_execute_plan</code> will generate custom plans
  54. that are specific to the supplied parameter values. After enough uses
  55. of the same prepared statement, <code class="function">SPI_execute_plan</code> will
  56. build a generic plan, and if that is not too much more expensive than the
  57. custom plans, it will start using the generic plan instead of re-planning
  58. each time. If this default behavior is unsuitable, you can alter it by
  59. passing the <code class="literal">CURSOR_OPT_GENERIC_PLAN</code> or
  60. <code class="literal">CURSOR_OPT_CUSTOM_PLAN</code> flag to
  61. <code class="function">SPI_prepare_cursor</code>, to force use of generic or custom
  62. plans respectively.
  63. </p><p>
  64. Although the main point of a prepared statement is to avoid repeated parse
  65. analysis and planning of the statement, <span class="productname">PostgreSQL</span> will
  66. force re-analysis and re-planning of the statement before using it
  67. whenever database objects used in the statement have undergone
  68. definitional (DDL) changes since the previous use of the prepared
  69. statement. Also, if the value of <a class="xref" href="runtime-config-client.html#GUC-SEARCH-PATH">search_path</a> changes
  70. from one use to the next, the statement will be re-parsed using the new
  71. <code class="varname">search_path</code>. (This latter behavior is new as of
  72. <span class="productname">PostgreSQL</span> 9.3.) See <a class="xref" href="sql-prepare.html" title="PREPARE"><span class="refentrytitle">PREPARE</span></a> for more information about the behavior of prepared
  73. statements.
  74. </p><p>
  75. This function should only be called from a connected C function.
  76. </p><p>
  77. <code class="type">SPIPlanPtr</code> is declared as a pointer to an opaque struct type in
  78. <code class="filename">spi.h</code>. It is unwise to try to access its contents
  79. directly, as that makes your code much more likely to break in
  80. future revisions of <span class="productname">PostgreSQL</span>.
  81. </p><p>
  82. The name <code class="type">SPIPlanPtr</code> is somewhat historical, since the data
  83. structure no longer necessarily contains an execution plan.
  84. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="spi-spi-execute-with-args.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="spi-interface.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="spi-spi-prepare-cursor.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">SPI_execute_with_args </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> SPI_prepare_cursor</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1