gooderp18绿色标准版
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

268 lines
18KB

  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>33.11. Miscellaneous Functions</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="libpq-control.html" title="33.10. Control Functions" /><link rel="next" href="libpq-notice-processing.html" title="33.12. Notice Processing" /></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">33.11. Miscellaneous Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="libpq-control.html" title="33.10. Control Functions">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="libpq.html" title="Chapter 33. libpq - C Library">Up</a></td><th width="60%" align="center">Chapter 33. <span xmlns="http://www.w3.org/1999/xhtml" class="application">libpq</span> - C Library</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="libpq-notice-processing.html" title="33.12. Notice Processing">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="LIBPQ-MISC"><div class="titlepage"><div><div><h2 class="title" style="clear: both">33.11. Miscellaneous Functions</h2></div></div></div><p>
  3. As always, there are some functions that just don't fit anywhere.
  4. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PQFREEMEM"><span class="term">
  5. <code class="function">PQfreemem</code>
  6. <a id="id-1.7.3.18.3.1.1.2" class="indexterm"></a>
  7. </span></dt><dd><p>
  8. Frees memory allocated by <span class="application">libpq</span>.
  9. </p><pre class="synopsis">
  10. void PQfreemem(void *ptr);
  11. </pre><p>
  12. </p><p>
  13. Frees memory allocated by <span class="application">libpq</span>, particularly
  14. <code class="function">PQescapeByteaConn</code>,
  15. <code class="function">PQescapeBytea</code>,
  16. <code class="function">PQunescapeBytea</code>,
  17. and <code class="function">PQnotifies</code>.
  18. It is particularly important that this function, rather than
  19. <code class="function">free()</code>, be used on Microsoft Windows. This is because
  20. allocating memory in a DLL and releasing it in the application works
  21. only if multithreaded/single-threaded, release/debug, and static/dynamic
  22. flags are the same for the DLL and the application. On non-Microsoft
  23. Windows platforms, this function is the same as the standard library
  24. function <code class="function">free()</code>.
  25. </p></dd><dt id="LIBPQ-PQCONNINFOFREE"><span class="term">
  26. <code class="function">PQconninfoFree</code>
  27. <a id="id-1.7.3.18.3.2.1.2" class="indexterm"></a>
  28. </span></dt><dd><p>
  29. Frees the data structures allocated by
  30. <code class="function">PQconndefaults</code> or <code class="function">PQconninfoParse</code>.
  31. </p><pre class="synopsis">
  32. void PQconninfoFree(PQconninfoOption *connOptions);
  33. </pre><p>
  34. </p><p>
  35. A simple <code class="function">PQfreemem</code> will not do for this, since
  36. the array contains references to subsidiary strings.
  37. </p></dd><dt id="LIBPQ-PQENCRYPTPASSWORDCONN"><span class="term">
  38. <code class="function">PQencryptPasswordConn</code>
  39. <a id="id-1.7.3.18.3.3.1.2" class="indexterm"></a>
  40. </span></dt><dd><p>
  41. Prepares the encrypted form of a <span class="productname">PostgreSQL</span> password.
  42. </p><pre class="synopsis">
  43. char *PQencryptPasswordConn(PGconn *conn, const char *passwd, const char *user, const char *algorithm);
  44. </pre><p>
  45. This function is intended to be used by client applications that
  46. wish to send commands like <code class="literal">ALTER USER joe PASSWORD
  47. 'pwd'</code>. It is good practice not to send the original cleartext
  48. password in such a command, because it might be exposed in command
  49. logs, activity displays, and so on. Instead, use this function to
  50. convert the password to encrypted form before it is sent.
  51. </p><p>
  52. The <em class="parameter"><code>passwd</code></em> and <em class="parameter"><code>user</code></em> arguments
  53. are the cleartext password, and the SQL name of the user it is for.
  54. <em class="parameter"><code>algorithm</code></em> specifies the encryption algorithm
  55. to use to encrypt the password. Currently supported algorithms are
  56. <code class="literal">md5</code> and <code class="literal">scram-sha-256</code> (<code class="literal">on</code> and
  57. <code class="literal">off</code> are also accepted as aliases for <code class="literal">md5</code>, for
  58. compatibility with older server versions). Note that support for
  59. <code class="literal">scram-sha-256</code> was introduced in <span class="productname">PostgreSQL</span>
  60. version 10, and will not work correctly with older server versions. If
  61. <em class="parameter"><code>algorithm</code></em> is <code class="symbol">NULL</code>, this function will query
  62. the server for the current value of the
  63. <a class="xref" href="runtime-config-connection.html#GUC-PASSWORD-ENCRYPTION">password_encryption</a> setting. That can block, and
  64. will fail if the current transaction is aborted, or if the connection
  65. is busy executing another query. If you wish to use the default
  66. algorithm for the server but want to avoid blocking, query
  67. <code class="varname">password_encryption</code> yourself before calling
  68. <code class="function">PQencryptPasswordConn</code>, and pass that value as the
  69. <em class="parameter"><code>algorithm</code></em>.
  70. </p><p>
  71. The return value is a string allocated by <code class="function">malloc</code>.
  72. The caller can assume the string doesn't contain any special characters
  73. that would require escaping. Use <code class="function">PQfreemem</code> to free the
  74. result when done with it. On error, returns <code class="symbol">NULL</code>, and
  75. a suitable message is stored in the connection object.
  76. </p></dd><dt id="LIBPQ-PQENCRYPTPASSWORD"><span class="term">
  77. <code class="function">PQencryptPassword</code>
  78. <a id="id-1.7.3.18.3.4.1.2" class="indexterm"></a>
  79. </span></dt><dd><p>
  80. Prepares the md5-encrypted form of a <span class="productname">PostgreSQL</span> password.
  81. </p><pre class="synopsis">
  82. char *PQencryptPassword(const char *passwd, const char *user);
  83. </pre><p>
  84. <code class="function">PQencryptPassword</code> is an older, deprecated version of
  85. <code class="function">PQencryptPasswordConn</code>. The difference is that
  86. <code class="function">PQencryptPassword</code> does not
  87. require a connection object, and <code class="literal">md5</code> is always used as the
  88. encryption algorithm.
  89. </p></dd><dt id="LIBPQ-PQMAKEEMPTYPGRESULT"><span class="term">
  90. <code class="function">PQmakeEmptyPGresult</code>
  91. <a id="id-1.7.3.18.3.5.1.2" class="indexterm"></a>
  92. </span></dt><dd><p>
  93. Constructs an empty <code class="structname">PGresult</code> object with the given status.
  94. </p><pre class="synopsis">
  95. PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
  96. </pre><p>
  97. </p><p>
  98. This is <span class="application">libpq</span>'s internal function to allocate and
  99. initialize an empty <code class="structname">PGresult</code> object. This
  100. function returns <code class="symbol">NULL</code> if memory could not be allocated. It is
  101. exported because some applications find it useful to generate result
  102. objects (particularly objects with error status) themselves. If
  103. <em class="parameter"><code>conn</code></em> is not null and <em class="parameter"><code>status</code></em>
  104. indicates an error, the current error message of the specified
  105. connection is copied into the <code class="structname">PGresult</code>.
  106. Also, if <em class="parameter"><code>conn</code></em> is not null, any event procedures
  107. registered in the connection are copied into the
  108. <code class="structname">PGresult</code>. (They do not get
  109. <code class="literal">PGEVT_RESULTCREATE</code> calls, but see
  110. <code class="function">PQfireResultCreateEvents</code>.)
  111. Note that <code class="function">PQclear</code> should eventually be called
  112. on the object, just as with a <code class="structname">PGresult</code>
  113. returned by <span class="application">libpq</span> itself.
  114. </p></dd><dt id="LIBPQ-PQFIRERESULTCREATEEVENTS"><span class="term">
  115. <code class="function">PQfireResultCreateEvents</code>
  116. <a id="id-1.7.3.18.3.6.1.2" class="indexterm"></a>
  117. </span></dt><dd><p>
  118. Fires a <code class="literal">PGEVT_RESULTCREATE</code> event (see <a class="xref" href="libpq-events.html" title="33.13. Event System">Section 33.13</a>) for each event procedure registered in the
  119. <code class="structname">PGresult</code> object. Returns non-zero for success,
  120. zero if any event procedure fails.
  121. </p><pre class="synopsis">
  122. int PQfireResultCreateEvents(PGconn *conn, PGresult *res);
  123. </pre><p>
  124. </p><p>
  125. The <code class="literal">conn</code> argument is passed through to event procedures
  126. but not used directly. It can be <code class="symbol">NULL</code> if the event
  127. procedures won't use it.
  128. </p><p>
  129. Event procedures that have already received a
  130. <code class="literal">PGEVT_RESULTCREATE</code> or <code class="literal">PGEVT_RESULTCOPY</code> event
  131. for this object are not fired again.
  132. </p><p>
  133. The main reason that this function is separate from
  134. <code class="function">PQmakeEmptyPGresult</code> is that it is often appropriate
  135. to create a <code class="structname">PGresult</code> and fill it with data
  136. before invoking the event procedures.
  137. </p></dd><dt id="LIBPQ-PQCOPYRESULT"><span class="term">
  138. <code class="function">PQcopyResult</code>
  139. <a id="id-1.7.3.18.3.7.1.2" class="indexterm"></a>
  140. </span></dt><dd><p>
  141. Makes a copy of a <code class="structname">PGresult</code> object. The copy is
  142. not linked to the source result in any way and
  143. <code class="function">PQclear</code> must be called when the copy is no longer
  144. needed. If the function fails, <code class="symbol">NULL</code> is returned.
  145. </p><pre class="synopsis">
  146. PGresult *PQcopyResult(const PGresult *src, int flags);
  147. </pre><p>
  148. </p><p>
  149. This is not intended to make an exact copy. The returned result is
  150. always put into <code class="literal">PGRES_TUPLES_OK</code> status, and does not
  151. copy any error message in the source. (It does copy the command status
  152. string, however.) The <em class="parameter"><code>flags</code></em> argument determines
  153. what else is copied. It is a bitwise OR of several flags.
  154. <code class="literal">PG_COPYRES_ATTRS</code> specifies copying the source
  155. result's attributes (column definitions).
  156. <code class="literal">PG_COPYRES_TUPLES</code> specifies copying the source
  157. result's tuples. (This implies copying the attributes, too.)
  158. <code class="literal">PG_COPYRES_NOTICEHOOKS</code> specifies
  159. copying the source result's notify hooks.
  160. <code class="literal">PG_COPYRES_EVENTS</code> specifies copying the source
  161. result's events. (But any instance data associated with the source
  162. is not copied.)
  163. </p></dd><dt id="LIBPQ-PQSETRESULTATTRS"><span class="term">
  164. <code class="function">PQsetResultAttrs</code>
  165. <a id="id-1.7.3.18.3.8.1.2" class="indexterm"></a>
  166. </span></dt><dd><p>
  167. Sets the attributes of a <code class="structname">PGresult</code> object.
  168. </p><pre class="synopsis">
  169. int PQsetResultAttrs(PGresult *res, int numAttributes, PGresAttDesc *attDescs);
  170. </pre><p>
  171. </p><p>
  172. The provided <em class="parameter"><code>attDescs</code></em> are copied into the result.
  173. If the <em class="parameter"><code>attDescs</code></em> pointer is <code class="symbol">NULL</code> or
  174. <em class="parameter"><code>numAttributes</code></em> is less than one, the request is
  175. ignored and the function succeeds. If <em class="parameter"><code>res</code></em>
  176. already contains attributes, the function will fail. If the function
  177. fails, the return value is zero. If the function succeeds, the return
  178. value is non-zero.
  179. </p></dd><dt id="LIBPQ-PQSETVALUE"><span class="term">
  180. <code class="function">PQsetvalue</code>
  181. <a id="id-1.7.3.18.3.9.1.2" class="indexterm"></a>
  182. </span></dt><dd><p>
  183. Sets a tuple field value of a <code class="structname">PGresult</code> object.
  184. </p><pre class="synopsis">
  185. int PQsetvalue(PGresult *res, int tup_num, int field_num, char *value, int len);
  186. </pre><p>
  187. </p><p>
  188. The function will automatically grow the result's internal tuples array
  189. as needed. However, the <em class="parameter"><code>tup_num</code></em> argument must be
  190. less than or equal to <code class="function">PQntuples</code>, meaning this
  191. function can only grow the tuples array one tuple at a time. But any
  192. field of any existing tuple can be modified in any order. If a value at
  193. <em class="parameter"><code>field_num</code></em> already exists, it will be overwritten.
  194. If <em class="parameter"><code>len</code></em> is -1 or
  195. <em class="parameter"><code>value</code></em> is <code class="symbol">NULL</code>, the field value
  196. will be set to an SQL null value. The
  197. <em class="parameter"><code>value</code></em> is copied into the result's private storage,
  198. thus is no longer needed after the function
  199. returns. If the function fails, the return value is zero. If the
  200. function succeeds, the return value is non-zero.
  201. </p></dd><dt id="LIBPQ-PQRESULTALLOC"><span class="term">
  202. <code class="function">PQresultAlloc</code>
  203. <a id="id-1.7.3.18.3.10.1.2" class="indexterm"></a>
  204. </span></dt><dd><p>
  205. Allocate subsidiary storage for a <code class="structname">PGresult</code> object.
  206. </p><pre class="synopsis">
  207. void *PQresultAlloc(PGresult *res, size_t nBytes);
  208. </pre><p>
  209. </p><p>
  210. Any memory allocated with this function will be freed when
  211. <em class="parameter"><code>res</code></em> is cleared. If the function fails,
  212. the return value is <code class="symbol">NULL</code>. The result is
  213. guaranteed to be adequately aligned for any type of data,
  214. just as for <code class="function">malloc</code>.
  215. </p></dd><dt id="LIBPQ-PQRESULTMEMORYSIZE"><span class="term">
  216. <code class="function">PQresultMemorySize</code>
  217. <a id="id-1.7.3.18.3.11.1.2" class="indexterm"></a>
  218. </span></dt><dd><p>
  219. Retrieves the number of bytes allocated for
  220. a <code class="structname">PGresult</code> object.
  221. </p><pre class="synopsis">
  222. size_t PQresultMemorySize(const PGresult *res);
  223. </pre><p>
  224. </p><p>
  225. This value is the sum of all <code class="function">malloc</code> requests
  226. associated with the <code class="structname">PGresult</code> object, that is,
  227. all the space that will be freed by <code class="function">PQclear</code>.
  228. This information can be useful for managing memory consumption.
  229. </p></dd><dt id="LIBPQ-PQLIBVERSION"><span class="term">
  230. <code class="function">PQlibVersion</code>
  231. <a id="id-1.7.3.18.3.12.1.2" class="indexterm"></a>
  232. </span></dt><dd><p>
  233. Return the version of <span class="productname">libpq</span> that is being used.
  234. </p><pre class="synopsis">
  235. int PQlibVersion(void);
  236. </pre><p>
  237. </p><p>
  238. The result of this function can be used to determine, at
  239. run time, whether specific functionality is available in the currently
  240. loaded version of libpq. The function can be used, for example,
  241. to determine which connection options are available in
  242. <code class="function">PQconnectdb</code>.
  243. </p><p>
  244. The result is formed by multiplying the library's major version
  245. number by 10000 and adding the minor version number. For example,
  246. version 10.1 will be returned as 100001, and version 11.0 will be
  247. returned as 110000.
  248. </p><p>
  249. Prior to major version 10, <span class="productname">PostgreSQL</span> used
  250. three-part version numbers in which the first two parts together
  251. represented the major version. For those
  252. versions, <code class="function">PQlibVersion</code> uses two digits for each
  253. part; for example version 9.1.5 will be returned as 90105, and
  254. version 9.2.0 will be returned as 90200.
  255. </p><p>
  256. Therefore, for purposes of determining feature compatibility,
  257. applications should divide the result of <code class="function">PQlibVersion</code>
  258. by 100 not 10000 to determine a logical major version number.
  259. In all release series, only the last two digits differ between
  260. minor releases (bug-fix releases).
  261. </p><div class="note"><h3 class="title">Note</h3><p>
  262. This function appeared in <span class="productname">PostgreSQL</span> version 9.1, so
  263. it cannot be used to detect required functionality in earlier
  264. versions, since calling it will create a link dependency
  265. on version 9.1 or later.
  266. </p></div></dd></dl></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="libpq-control.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="libpq.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="libpq-notice-processing.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">33.10. Control Functions </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 33.12. Notice Processing</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1