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.

168 lines
13KB

  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_execute</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-finish.html" title="SPI_finish" /><link rel="next" href="spi-spi-exec.html" title="SPI_exec" /></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_execute</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="spi-spi-finish.html" title="SPI_finish">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-exec.html" title="SPI_exec">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SPI-SPI-EXECUTE"><div class="titlepage"></div><a id="id-1.8.12.8.4.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">SPI_execute</span></h2><p>SPI_execute — execute a command</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. int SPI_execute(const char * <em class="parameter"><code>command</code></em>, bool <em class="parameter"><code>read_only</code></em>, long <em class="parameter"><code>count</code></em>)
  4. </pre></div><div class="refsect1" id="id-1.8.12.8.4.5"><h2>Description</h2><p>
  5. <code class="function">SPI_execute</code> executes the specified SQL command
  6. for <em class="parameter"><code>count</code></em> rows. If <em class="parameter"><code>read_only</code></em>
  7. is <code class="literal">true</code>, the command must be read-only, and execution overhead
  8. is somewhat reduced.
  9. </p><p>
  10. This function can only be called from a connected C function.
  11. </p><p>
  12. If <em class="parameter"><code>count</code></em> is zero then the command is executed
  13. for all rows that it applies to. If <em class="parameter"><code>count</code></em>
  14. is greater than zero, then no more than <em class="parameter"><code>count</code></em> rows
  15. will be retrieved; execution stops when the count is reached, much like
  16. adding a <code class="literal">LIMIT</code> clause to the query. For example,
  17. </p><pre class="programlisting">
  18. SPI_execute("SELECT * FROM foo", true, 5);
  19. </pre><p>
  20. will retrieve at most 5 rows from the table. Note that such a limit
  21. is only effective when the command actually returns rows. For example,
  22. </p><pre class="programlisting">
  23. SPI_execute("INSERT INTO foo SELECT * FROM bar", false, 5);
  24. </pre><p>
  25. inserts all rows from <code class="structname">bar</code>, ignoring the
  26. <em class="parameter"><code>count</code></em> parameter. However, with
  27. </p><pre class="programlisting">
  28. SPI_execute("INSERT INTO foo SELECT * FROM bar RETURNING *", false, 5);
  29. </pre><p>
  30. at most 5 rows would be inserted, since execution would stop after the
  31. fifth <code class="literal">RETURNING</code> result row is retrieved.
  32. </p><p>
  33. You can pass multiple commands in one string;
  34. <code class="function">SPI_execute</code> returns the
  35. result for the command executed last. The <em class="parameter"><code>count</code></em>
  36. limit applies to each command separately (even though only the last
  37. result will actually be returned). The limit is not applied to any
  38. hidden commands generated by rules.
  39. </p><p>
  40. When <em class="parameter"><code>read_only</code></em> is <code class="literal">false</code>,
  41. <code class="function">SPI_execute</code> increments the command
  42. counter and computes a new <em class="firstterm">snapshot</em> before executing each
  43. command in the string. The snapshot does not actually change if the
  44. current transaction isolation level is <code class="literal">SERIALIZABLE</code> or <code class="literal">REPEATABLE READ</code>, but in
  45. <code class="literal">READ COMMITTED</code> mode the snapshot update allows each command to
  46. see the results of newly committed transactions from other sessions.
  47. This is essential for consistent behavior when the commands are modifying
  48. the database.
  49. </p><p>
  50. When <em class="parameter"><code>read_only</code></em> is <code class="literal">true</code>,
  51. <code class="function">SPI_execute</code> does not update either the snapshot
  52. or the command counter, and it allows only plain <code class="command">SELECT</code>
  53. commands to appear in the command string. The commands are executed
  54. using the snapshot previously established for the surrounding query.
  55. This execution mode is somewhat faster than the read/write mode due
  56. to eliminating per-command overhead. It also allows genuinely
  57. <em class="firstterm">stable</em> functions to be built: since successive executions
  58. will all use the same snapshot, there will be no change in the results.
  59. </p><p>
  60. It is generally unwise to mix read-only and read-write commands within
  61. a single function using SPI; that could result in very confusing behavior,
  62. since the read-only queries would not see the results of any database
  63. updates done by the read-write queries.
  64. </p><p>
  65. The actual number of rows for which the (last) command was executed
  66. is returned in the global variable <code class="varname">SPI_processed</code>.
  67. If the return value of the function is <code class="symbol">SPI_OK_SELECT</code>,
  68. <code class="symbol">SPI_OK_INSERT_RETURNING</code>,
  69. <code class="symbol">SPI_OK_DELETE_RETURNING</code>, or
  70. <code class="symbol">SPI_OK_UPDATE_RETURNING</code>,
  71. then you can use the
  72. global pointer <code class="literal">SPITupleTable *SPI_tuptable</code> to
  73. access the result rows. Some utility commands (such as
  74. <code class="command">EXPLAIN</code>) also return row sets, and <code class="literal">SPI_tuptable</code>
  75. will contain the result in these cases too. Some utility commands
  76. (<code class="command">COPY</code>, <code class="command">CREATE TABLE AS</code>) don't return a row set, so
  77. <code class="literal">SPI_tuptable</code> is NULL, but they still return the number of
  78. rows processed in <code class="varname">SPI_processed</code>.
  79. </p><p>
  80. The structure <code class="structname">SPITupleTable</code> is defined
  81. thus:
  82. </p><pre class="programlisting">
  83. typedef struct
  84. {
  85. MemoryContext tuptabcxt; /* memory context of result table */
  86. uint64 alloced; /* number of alloced vals */
  87. uint64 free; /* number of free vals */
  88. TupleDesc tupdesc; /* row descriptor */
  89. HeapTuple *vals; /* rows */
  90. } SPITupleTable;
  91. </pre><p>
  92. <code class="structfield">vals</code> is an array of pointers to rows. (The number
  93. of valid entries is given by <code class="varname">SPI_processed</code>.)
  94. <code class="structfield">tupdesc</code> is a row descriptor which you can pass to
  95. SPI functions dealing with rows. <code class="structfield">tuptabcxt</code>,
  96. <code class="structfield">alloced</code>, and <code class="structfield">free</code> are internal
  97. fields not intended for use by SPI callers.
  98. </p><p>
  99. <code class="function">SPI_finish</code> frees all
  100. <code class="structname">SPITupleTable</code>s allocated during the current
  101. C function. You can free a particular result table earlier, if you
  102. are done with it, by calling <code class="function">SPI_freetuptable</code>.
  103. </p></div><div class="refsect1" id="id-1.8.12.8.4.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>
  104. string containing command to execute
  105. </p></dd><dt><span class="term"><code class="literal">bool <em class="parameter"><code>read_only</code></em></code></span></dt><dd><p><code class="literal">true</code> for read-only execution</p></dd><dt><span class="term"><code class="literal">long <em class="parameter"><code>count</code></em></code></span></dt><dd><p>
  106. maximum number of rows to return,
  107. or <code class="literal">0</code> for no limit
  108. </p></dd></dl></div></div><div class="refsect1" id="id-1.8.12.8.4.7"><h2>Return Value</h2><p>
  109. If the execution of the command was successful then one of the
  110. following (nonnegative) values will be returned:
  111. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="symbol">SPI_OK_SELECT</code></span></dt><dd><p>
  112. if a <code class="command">SELECT</code> (but not <code class="command">SELECT
  113. INTO</code>) was executed
  114. </p></dd><dt><span class="term"><code class="symbol">SPI_OK_SELINTO</code></span></dt><dd><p>
  115. if a <code class="command">SELECT INTO</code> was executed
  116. </p></dd><dt><span class="term"><code class="symbol">SPI_OK_INSERT</code></span></dt><dd><p>
  117. if an <code class="command">INSERT</code> was executed
  118. </p></dd><dt><span class="term"><code class="symbol">SPI_OK_DELETE</code></span></dt><dd><p>
  119. if a <code class="command">DELETE</code> was executed
  120. </p></dd><dt><span class="term"><code class="symbol">SPI_OK_UPDATE</code></span></dt><dd><p>
  121. if an <code class="command">UPDATE</code> was executed
  122. </p></dd><dt><span class="term"><code class="symbol">SPI_OK_INSERT_RETURNING</code></span></dt><dd><p>
  123. if an <code class="command">INSERT RETURNING</code> was executed
  124. </p></dd><dt><span class="term"><code class="symbol">SPI_OK_DELETE_RETURNING</code></span></dt><dd><p>
  125. if a <code class="command">DELETE RETURNING</code> was executed
  126. </p></dd><dt><span class="term"><code class="symbol">SPI_OK_UPDATE_RETURNING</code></span></dt><dd><p>
  127. if an <code class="command">UPDATE RETURNING</code> was executed
  128. </p></dd><dt><span class="term"><code class="symbol">SPI_OK_UTILITY</code></span></dt><dd><p>
  129. if a utility command (e.g., <code class="command">CREATE TABLE</code>)
  130. was executed
  131. </p></dd><dt><span class="term"><code class="symbol">SPI_OK_REWRITTEN</code></span></dt><dd><p>
  132. if the command was rewritten into another kind of command (e.g.,
  133. <code class="command">UPDATE</code> became an <code class="command">INSERT</code>) by a <a class="link" href="rules.html" title="Chapter 40. The Rule System">rule</a>.
  134. </p></dd></dl></div><p>
  135. </p><p>
  136. On error, one of the following negative values is returned:
  137. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="symbol">SPI_ERROR_ARGUMENT</code></span></dt><dd><p>
  138. if <em class="parameter"><code>command</code></em> is <code class="symbol">NULL</code> or
  139. <em class="parameter"><code>count</code></em> is less than 0
  140. </p></dd><dt><span class="term"><code class="symbol">SPI_ERROR_COPY</code></span></dt><dd><p>
  141. if <code class="command">COPY TO stdout</code> or <code class="command">COPY FROM stdin</code>
  142. was attempted
  143. </p></dd><dt><span class="term"><code class="symbol">SPI_ERROR_TRANSACTION</code></span></dt><dd><p>
  144. if a transaction manipulation command was attempted
  145. (<code class="command">BEGIN</code>,
  146. <code class="command">COMMIT</code>,
  147. <code class="command">ROLLBACK</code>,
  148. <code class="command">SAVEPOINT</code>,
  149. <code class="command">PREPARE TRANSACTION</code>,
  150. <code class="command">COMMIT PREPARED</code>,
  151. <code class="command">ROLLBACK PREPARED</code>,
  152. or any variant thereof)
  153. </p></dd><dt><span class="term"><code class="symbol">SPI_ERROR_OPUNKNOWN</code></span></dt><dd><p>
  154. if the command type is unknown (shouldn't happen)
  155. </p></dd><dt><span class="term"><code class="symbol">SPI_ERROR_UNCONNECTED</code></span></dt><dd><p>
  156. if called from an unconnected C function
  157. </p></dd></dl></div><p>
  158. </p></div><div class="refsect1" id="id-1.8.12.8.4.8"><h2>Notes</h2><p>
  159. All SPI query-execution functions set both
  160. <code class="varname">SPI_processed</code> and
  161. <code class="varname">SPI_tuptable</code> (just the pointer, not the contents
  162. of the structure). Save these two global variables into local
  163. C function variables if you need to access the result table of
  164. <code class="function">SPI_execute</code> or another query-execution function
  165. across later calls.
  166. </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-finish.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-exec.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">SPI_finish </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> SPI_exec</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1