gooderp18绿色标准版
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

152 linhas
6.0KB

  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>46.6. Examples</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-visibility.html" title="46.5. Visibility of Data Changes" /><link rel="next" href="bgworker.html" title="Chapter 47. Background Worker Processes" /></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">46.6. Examples</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="spi-visibility.html" title="46.5. Visibility of Data Changes">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="spi.html" title="Chapter 46. Server Programming Interface">Up</a></td><th width="60%" align="center">Chapter 46. Server Programming Interface</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="bgworker.html" title="Chapter 47. Background Worker Processes">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="SPI-EXAMPLES"><div class="titlepage"><div><div><h2 class="title" style="clear: both">46.6. Examples</h2></div></div></div><p>
  3. This section contains a very simple example of SPI usage. The
  4. C function <code class="function">execq</code> takes an SQL command as its
  5. first argument and a row count as its second, executes the command
  6. using <code class="function">SPI_exec</code> and returns the number of rows
  7. that were processed by the command. You can find more complex
  8. examples for SPI in the source tree in
  9. <code class="filename">src/test/regress/regress.c</code> and in the
  10. <a class="xref" href="contrib-spi.html" title="F.36. spi">spi</a> module.
  11. </p><pre class="programlisting">
  12. #include "postgres.h"
  13. #include "executor/spi.h"
  14. #include "utils/builtins.h"
  15. PG_MODULE_MAGIC;
  16. PG_FUNCTION_INFO_V1(execq);
  17. Datum
  18. execq(PG_FUNCTION_ARGS)
  19. {
  20. char *command;
  21. int cnt;
  22. int ret;
  23. uint64 proc;
  24. /* Convert given text object to a C string */
  25. command = text_to_cstring(PG_GETARG_TEXT_PP(0));
  26. cnt = PG_GETARG_INT32(1);
  27. SPI_connect();
  28. ret = SPI_exec(command, cnt);
  29. proc = SPI_processed;
  30. /*
  31. * If some rows were fetched, print them via elog(INFO).
  32. */
  33. if (ret &gt; 0 &amp;&amp; SPI_tuptable != NULL)
  34. {
  35. TupleDesc tupdesc = SPI_tuptable-&gt;tupdesc;
  36. SPITupleTable *tuptable = SPI_tuptable;
  37. char buf[8192];
  38. uint64 j;
  39. for (j = 0; j &lt; proc; j++)
  40. {
  41. HeapTuple tuple = tuptable-&gt;vals[j];
  42. int i;
  43. for (i = 1, buf[0] = 0; i &lt;= tupdesc-&gt;natts; i++)
  44. snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %s%s",
  45. SPI_getvalue(tuple, tupdesc, i),
  46. (i == tupdesc-&gt;natts) ? " " : " |");
  47. elog(INFO, "EXECQ: %s", buf);
  48. }
  49. }
  50. SPI_finish();
  51. pfree(command);
  52. PG_RETURN_INT64(proc);
  53. }
  54. </pre><p>
  55. This is how you declare the function after having compiled it into
  56. a shared library (details are in <a class="xref" href="xfunc-c.html#DFUNC" title="37.10.5. Compiling and Linking Dynamically-Loaded Functions">Section 37.10.5</a>.):
  57. </p><pre class="programlisting">
  58. CREATE FUNCTION execq(text, integer) RETURNS int8
  59. AS '<em class="replaceable"><code>filename</code></em>'
  60. LANGUAGE C STRICT;
  61. </pre><p>
  62. </p><p>
  63. Here is a sample session:
  64. </p><pre class="programlisting">
  65. =&gt; SELECT execq('CREATE TABLE a (x integer)', 0);
  66. execq
  67. -------
  68. 0
  69. (1 row)
  70. =&gt; INSERT INTO a VALUES (execq('INSERT INTO a VALUES (0)', 0));
  71. INSERT 0 1
  72. =&gt; SELECT execq('SELECT * FROM a', 0);
  73. INFO: EXECQ: 0 -- inserted by execq
  74. INFO: EXECQ: 1 -- returned by execq and inserted by upper INSERT
  75. execq
  76. -------
  77. 2
  78. (1 row)
  79. =&gt; SELECT execq('INSERT INTO a SELECT x + 2 FROM a', 1);
  80. execq
  81. -------
  82. 1
  83. (1 row)
  84. =&gt; SELECT execq('SELECT * FROM a', 10);
  85. INFO: EXECQ: 0
  86. INFO: EXECQ: 1
  87. INFO: EXECQ: 2 -- 0 + 2, only one row inserted - as specified
  88. execq
  89. -------
  90. 3 -- 10 is the max value only, 3 is the real number of rows
  91. (1 row)
  92. =&gt; DELETE FROM a;
  93. DELETE 3
  94. =&gt; INSERT INTO a VALUES (execq('SELECT * FROM a', 0) + 1);
  95. INSERT 0 1
  96. =&gt; SELECT * FROM a;
  97. x
  98. ---
  99. 1 -- no rows in a (0) + 1
  100. (1 row)
  101. =&gt; INSERT INTO a VALUES (execq('SELECT * FROM a', 0) + 1);
  102. INFO: EXECQ: 1
  103. INSERT 0 1
  104. =&gt; SELECT * FROM a;
  105. x
  106. ---
  107. 1
  108. 2 -- there was one row in a + 1
  109. (2 rows)
  110. -- This demonstrates the data changes visibility rule:
  111. =&gt; INSERT INTO a SELECT execq('SELECT * FROM a', 0) * x FROM a;
  112. INFO: EXECQ: 1
  113. INFO: EXECQ: 2
  114. INFO: EXECQ: 1
  115. INFO: EXECQ: 2
  116. INFO: EXECQ: 2
  117. INSERT 0 2
  118. =&gt; SELECT * FROM a;
  119. x
  120. ---
  121. 1
  122. 2
  123. 2 -- 2 rows * 1 (x in first row)
  124. 6 -- 3 rows (2 + 1 just inserted) * 2 (x in second row)
  125. (4 rows) ^^^^^^
  126. rows visible to execq() in different invocations
  127. </pre><p>
  128. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="spi-visibility.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="spi.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="bgworker.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">46.5. Visibility of Data Changes </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 47. Background Worker Processes</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1