gooderp18绿色标准版
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

318 lignes
22KB

  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>34.3. Client Interfaces</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="lo-implementation.html" title="34.2. Implementation Features" /><link rel="next" href="lo-funcs.html" title="34.4. Server-Side Functions" /></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">34.3. Client Interfaces</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="lo-implementation.html" title="34.2. Implementation Features">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="largeobjects.html" title="Chapter 34. Large Objects">Up</a></td><th width="60%" align="center">Chapter 34. Large Objects</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="lo-funcs.html" title="34.4. Server-Side Functions">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="LO-INTERFACES"><div class="titlepage"><div><div><h2 class="title" style="clear: both">34.3. Client Interfaces</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="lo-interfaces.html#LO-CREATE">34.3.1. Creating a Large Object</a></span></dt><dt><span class="sect2"><a href="lo-interfaces.html#LO-IMPORT">34.3.2. Importing a Large Object</a></span></dt><dt><span class="sect2"><a href="lo-interfaces.html#LO-EXPORT">34.3.3. Exporting a Large Object</a></span></dt><dt><span class="sect2"><a href="lo-interfaces.html#LO-OPEN">34.3.4. Opening an Existing Large Object</a></span></dt><dt><span class="sect2"><a href="lo-interfaces.html#LO-WRITE">34.3.5. Writing Data to a Large Object</a></span></dt><dt><span class="sect2"><a href="lo-interfaces.html#LO-READ">34.3.6. Reading Data from a Large Object</a></span></dt><dt><span class="sect2"><a href="lo-interfaces.html#LO-SEEK">34.3.7. Seeking in a Large Object</a></span></dt><dt><span class="sect2"><a href="lo-interfaces.html#LO-TELL">34.3.8. Obtaining the Seek Position of a Large Object</a></span></dt><dt><span class="sect2"><a href="lo-interfaces.html#LO-TRUNCATE">34.3.9. Truncating a Large Object</a></span></dt><dt><span class="sect2"><a href="lo-interfaces.html#LO-CLOSE">34.3.10. Closing a Large Object Descriptor</a></span></dt><dt><span class="sect2"><a href="lo-interfaces.html#LO-UNLINK">34.3.11. Removing a Large Object</a></span></dt></dl></div><p>
  3. This section describes the facilities that
  4. <span class="productname">PostgreSQL</span>'s <span class="application">libpq</span>
  5. client interface library provides for accessing large objects.
  6. The <span class="productname">PostgreSQL</span> large object interface is
  7. modeled after the <acronym class="acronym">Unix</acronym> file-system interface, with
  8. analogues of <code class="function">open</code>, <code class="function">read</code>,
  9. <code class="function">write</code>,
  10. <code class="function">lseek</code>, etc.
  11. </p><p>
  12. All large object manipulation using these functions
  13. <span class="emphasis"><em>must</em></span> take place within an SQL transaction block,
  14. since large object file descriptors are only valid for the duration of
  15. a transaction.
  16. </p><p>
  17. If an error occurs while executing any one of these functions, the
  18. function will return an otherwise-impossible value, typically 0 or -1.
  19. A message describing the error is stored in the connection object and
  20. can be retrieved with <code class="function">PQerrorMessage</code>.
  21. </p><p>
  22. Client applications that use these functions should include the header file
  23. <code class="filename">libpq/libpq-fs.h</code> and link with the
  24. <span class="application">libpq</span> library.
  25. </p><div class="sect2" id="LO-CREATE"><div class="titlepage"><div><div><h3 class="title">34.3.1. Creating a Large Object</h3></div></div></div><p>
  26. <a id="id-1.7.4.8.6.2.1" class="indexterm"></a>
  27. The function
  28. </p><pre class="synopsis">
  29. Oid lo_creat(PGconn *conn, int mode);
  30. </pre><p>
  31. creates a new large object.
  32. The return value is the OID that was assigned to the new large object,
  33. or <code class="symbol">InvalidOid</code> (zero) on failure.
  34. <em class="replaceable"><code>mode</code></em> is unused and
  35. ignored as of <span class="productname">PostgreSQL</span> 8.1; however, for
  36. backward compatibility with earlier releases it is best to
  37. set it to <code class="symbol">INV_READ</code>, <code class="symbol">INV_WRITE</code>,
  38. or <code class="symbol">INV_READ</code> <code class="literal">|</code> <code class="symbol">INV_WRITE</code>.
  39. (These symbolic constants are defined
  40. in the header file <code class="filename">libpq/libpq-fs.h</code>.)
  41. </p><p>
  42. An example:
  43. </p><pre class="programlisting">
  44. inv_oid = lo_creat(conn, INV_READ|INV_WRITE);
  45. </pre><p>
  46. </p><p>
  47. <a id="id-1.7.4.8.6.4.1" class="indexterm"></a>
  48. The function
  49. </p><pre class="synopsis">
  50. Oid lo_create(PGconn *conn, Oid lobjId);
  51. </pre><p>
  52. also creates a new large object. The OID to be assigned can be
  53. specified by <em class="replaceable"><code>lobjId</code></em>;
  54. if so, failure occurs if that OID is already in use for some large
  55. object. If <em class="replaceable"><code>lobjId</code></em>
  56. is <code class="symbol">InvalidOid</code> (zero) then <code class="function">lo_create</code> assigns an unused
  57. OID (this is the same behavior as <code class="function">lo_creat</code>).
  58. The return value is the OID that was assigned to the new large object,
  59. or <code class="symbol">InvalidOid</code> (zero) on failure.
  60. </p><p>
  61. <code class="function">lo_create</code> is new as of <span class="productname">PostgreSQL</span>
  62. 8.1; if this function is run against an older server version, it will
  63. fail and return <code class="symbol">InvalidOid</code>.
  64. </p><p>
  65. An example:
  66. </p><pre class="programlisting">
  67. inv_oid = lo_create(conn, desired_oid);
  68. </pre><p>
  69. </p></div><div class="sect2" id="LO-IMPORT"><div class="titlepage"><div><div><h3 class="title">34.3.2. Importing a Large Object</h3></div></div></div><p>
  70. <a id="id-1.7.4.8.7.2.1" class="indexterm"></a>
  71. To import an operating system file as a large object, call
  72. </p><pre class="synopsis">
  73. Oid lo_import(PGconn *conn, const char *filename);
  74. </pre><p>
  75. <em class="replaceable"><code>filename</code></em>
  76. specifies the operating system name of
  77. the file to be imported as a large object.
  78. The return value is the OID that was assigned to the new large object,
  79. or <code class="symbol">InvalidOid</code> (zero) on failure.
  80. Note that the file is read by the client interface library, not by
  81. the server; so it must exist in the client file system and be readable
  82. by the client application.
  83. </p><p>
  84. <a id="id-1.7.4.8.7.3.1" class="indexterm"></a>
  85. The function
  86. </p><pre class="synopsis">
  87. Oid lo_import_with_oid(PGconn *conn, const char *filename, Oid lobjId);
  88. </pre><p>
  89. also imports a new large object. The OID to be assigned can be
  90. specified by <em class="replaceable"><code>lobjId</code></em>;
  91. if so, failure occurs if that OID is already in use for some large
  92. object. If <em class="replaceable"><code>lobjId</code></em>
  93. is <code class="symbol">InvalidOid</code> (zero) then <code class="function">lo_import_with_oid</code> assigns an unused
  94. OID (this is the same behavior as <code class="function">lo_import</code>).
  95. The return value is the OID that was assigned to the new large object,
  96. or <code class="symbol">InvalidOid</code> (zero) on failure.
  97. </p><p>
  98. <code class="function">lo_import_with_oid</code> is new as of <span class="productname">PostgreSQL</span>
  99. 8.4 and uses <code class="function">lo_create</code> internally which is new in 8.1; if this function is run against 8.0 or before, it will
  100. fail and return <code class="symbol">InvalidOid</code>.
  101. </p></div><div class="sect2" id="LO-EXPORT"><div class="titlepage"><div><div><h3 class="title">34.3.3. Exporting a Large Object</h3></div></div></div><p>
  102. <a id="id-1.7.4.8.8.2.1" class="indexterm"></a>
  103. To export a large object
  104. into an operating system file, call
  105. </p><pre class="synopsis">
  106. int lo_export(PGconn *conn, Oid lobjId, const char *filename);
  107. </pre><p>
  108. The <em class="parameter"><code>lobjId</code></em> argument specifies the OID of the large
  109. object to export and the <em class="parameter"><code>filename</code></em> argument
  110. specifies the operating system name of the file. Note that the file is
  111. written by the client interface library, not by the server. Returns 1
  112. on success, -1 on failure.
  113. </p></div><div class="sect2" id="LO-OPEN"><div class="titlepage"><div><div><h3 class="title">34.3.4. Opening an Existing Large Object</h3></div></div></div><p>
  114. <a id="id-1.7.4.8.9.2.1" class="indexterm"></a>
  115. To open an existing large object for reading or writing, call
  116. </p><pre class="synopsis">
  117. int lo_open(PGconn *conn, Oid lobjId, int mode);
  118. </pre><p>
  119. The <em class="parameter"><code>lobjId</code></em> argument specifies the OID of the large
  120. object to open. The <em class="parameter"><code>mode</code></em> bits control whether the
  121. object is opened for reading (<code class="symbol">INV_READ</code>), writing
  122. (<code class="symbol">INV_WRITE</code>), or both.
  123. (These symbolic constants are defined
  124. in the header file <code class="filename">libpq/libpq-fs.h</code>.)
  125. <code class="function">lo_open</code> returns a (non-negative) large object
  126. descriptor for later use in <code class="function">lo_read</code>,
  127. <code class="function">lo_write</code>, <code class="function">lo_lseek</code>,
  128. <code class="function">lo_lseek64</code>, <code class="function">lo_tell</code>,
  129. <code class="function">lo_tell64</code>, <code class="function">lo_truncate</code>,
  130. <code class="function">lo_truncate64</code>, and <code class="function">lo_close</code>.
  131. The descriptor is only valid for
  132. the duration of the current transaction.
  133. On failure, -1 is returned.
  134. </p><p>
  135. The server currently does not distinguish between modes
  136. <code class="symbol">INV_WRITE</code> and <code class="symbol">INV_READ</code> <code class="literal">|</code>
  137. <code class="symbol">INV_WRITE</code>: you are allowed to read from the descriptor
  138. in either case. However there is a significant difference between
  139. these modes and <code class="symbol">INV_READ</code> alone: with <code class="symbol">INV_READ</code>
  140. you cannot write on the descriptor, and the data read from it will
  141. reflect the contents of the large object at the time of the transaction
  142. snapshot that was active when <code class="function">lo_open</code> was executed,
  143. regardless of later writes by this or other transactions. Reading
  144. from a descriptor opened with <code class="symbol">INV_WRITE</code> returns
  145. data that reflects all writes of other committed transactions as well
  146. as writes of the current transaction. This is similar to the behavior
  147. of <code class="literal">REPEATABLE READ</code> versus <code class="literal">READ COMMITTED</code> transaction
  148. modes for ordinary SQL <code class="command">SELECT</code> commands.
  149. </p><p>
  150. <code class="function">lo_open</code> will fail if <code class="literal">SELECT</code>
  151. privilege is not available for the large object, or
  152. if <code class="symbol">INV_WRITE</code> is specified and <code class="literal">UPDATE</code>
  153. privilege is not available.
  154. (Prior to <span class="productname">PostgreSQL</span> 11, these privilege
  155. checks were instead performed at the first actual read or write call
  156. using the descriptor.)
  157. These privilege checks can be disabled with the
  158. <a class="xref" href="runtime-config-compatible.html#GUC-LO-COMPAT-PRIVILEGES">lo_compat_privileges</a> run-time parameter.
  159. </p><p>
  160. An example:
  161. </p><pre class="programlisting">
  162. inv_fd = lo_open(conn, inv_oid, INV_READ|INV_WRITE);
  163. </pre><p>
  164. </p></div><div class="sect2" id="LO-WRITE"><div class="titlepage"><div><div><h3 class="title">34.3.5. Writing Data to a Large Object</h3></div></div></div><p>
  165. <a id="id-1.7.4.8.10.2.1" class="indexterm"></a>
  166. The function
  167. </p><pre class="synopsis">
  168. int lo_write(PGconn *conn, int fd, const char *buf, size_t len);
  169. </pre><p>
  170. writes <em class="parameter"><code>len</code></em> bytes from <em class="parameter"><code>buf</code></em>
  171. (which must be of size <em class="parameter"><code>len</code></em>) to large object
  172. descriptor <em class="parameter"><code>fd</code></em>. The <em class="parameter"><code>fd</code></em> argument must
  173. have been returned by a previous <code class="function">lo_open</code>. The
  174. number of bytes actually written is returned (in the current
  175. implementation, this will always equal <em class="parameter"><code>len</code></em> unless
  176. there is an error). In the event of an error, the return value is -1.
  177. </p><p>
  178. Although the <em class="parameter"><code>len</code></em> parameter is declared as
  179. <code class="type">size_t</code>, this function will reject length values larger than
  180. <code class="literal">INT_MAX</code>. In practice, it's best to transfer data in chunks
  181. of at most a few megabytes anyway.
  182. </p></div><div class="sect2" id="LO-READ"><div class="titlepage"><div><div><h3 class="title">34.3.6. Reading Data from a Large Object</h3></div></div></div><p>
  183. <a id="id-1.7.4.8.11.2.1" class="indexterm"></a>
  184. The function
  185. </p><pre class="synopsis">
  186. int lo_read(PGconn *conn, int fd, char *buf, size_t len);
  187. </pre><p>
  188. reads up to <em class="parameter"><code>len</code></em> bytes from large object descriptor
  189. <em class="parameter"><code>fd</code></em> into <em class="parameter"><code>buf</code></em> (which must be
  190. of size <em class="parameter"><code>len</code></em>). The <em class="parameter"><code>fd</code></em>
  191. argument must have been returned by a previous
  192. <code class="function">lo_open</code>. The number of bytes actually read is
  193. returned; this will be less than <em class="parameter"><code>len</code></em> if the end of
  194. the large object is reached first. In the event of an error, the return
  195. value is -1.
  196. </p><p>
  197. Although the <em class="parameter"><code>len</code></em> parameter is declared as
  198. <code class="type">size_t</code>, this function will reject length values larger than
  199. <code class="literal">INT_MAX</code>. In practice, it's best to transfer data in chunks
  200. of at most a few megabytes anyway.
  201. </p></div><div class="sect2" id="LO-SEEK"><div class="titlepage"><div><div><h3 class="title">34.3.7. Seeking in a Large Object</h3></div></div></div><p>
  202. <a id="id-1.7.4.8.12.2.1" class="indexterm"></a>
  203. To change the current read or write location associated with a
  204. large object descriptor, call
  205. </p><pre class="synopsis">
  206. int lo_lseek(PGconn *conn, int fd, int offset, int whence);
  207. </pre><p>
  208. This function moves the
  209. current location pointer for the large object descriptor identified by
  210. <em class="parameter"><code>fd</code></em> to the new location specified by
  211. <em class="parameter"><code>offset</code></em>. The valid values for <em class="parameter"><code>whence</code></em>
  212. are <code class="symbol">SEEK_SET</code> (seek from object start),
  213. <code class="symbol">SEEK_CUR</code> (seek from current position), and
  214. <code class="symbol">SEEK_END</code> (seek from object end). The return value is
  215. the new location pointer, or -1 on error.
  216. </p><p>
  217. <a id="id-1.7.4.8.12.3.1" class="indexterm"></a>
  218. When dealing with large objects that might exceed 2GB in size,
  219. instead use
  220. </p><pre class="synopsis">
  221. pg_int64 lo_lseek64(PGconn *conn, int fd, pg_int64 offset, int whence);
  222. </pre><p>
  223. This function has the same behavior
  224. as <code class="function">lo_lseek</code>, but it can accept an
  225. <em class="parameter"><code>offset</code></em> larger than 2GB and/or deliver a result larger
  226. than 2GB.
  227. Note that <code class="function">lo_lseek</code> will fail if the new location
  228. pointer would be greater than 2GB.
  229. </p><p>
  230. <code class="function">lo_lseek64</code> is new as of <span class="productname">PostgreSQL</span>
  231. 9.3. If this function is run against an older server version, it will
  232. fail and return -1.
  233. </p></div><div class="sect2" id="LO-TELL"><div class="titlepage"><div><div><h3 class="title">34.3.8. Obtaining the Seek Position of a Large Object</h3></div></div></div><p>
  234. <a id="id-1.7.4.8.13.2.1" class="indexterm"></a>
  235. To obtain the current read or write location of a large object descriptor,
  236. call
  237. </p><pre class="synopsis">
  238. int lo_tell(PGconn *conn, int fd);
  239. </pre><p>
  240. If there is an error, the return value is -1.
  241. </p><p>
  242. <a id="id-1.7.4.8.13.3.1" class="indexterm"></a>
  243. When dealing with large objects that might exceed 2GB in size,
  244. instead use
  245. </p><pre class="synopsis">
  246. pg_int64 lo_tell64(PGconn *conn, int fd);
  247. </pre><p>
  248. This function has the same behavior
  249. as <code class="function">lo_tell</code>, but it can deliver a result larger
  250. than 2GB.
  251. Note that <code class="function">lo_tell</code> will fail if the current
  252. read/write location is greater than 2GB.
  253. </p><p>
  254. <code class="function">lo_tell64</code> is new as of <span class="productname">PostgreSQL</span>
  255. 9.3. If this function is run against an older server version, it will
  256. fail and return -1.
  257. </p></div><div class="sect2" id="LO-TRUNCATE"><div class="titlepage"><div><div><h3 class="title">34.3.9. Truncating a Large Object</h3></div></div></div><p>
  258. <a id="id-1.7.4.8.14.2.1" class="indexterm"></a>
  259. To truncate a large object to a given length, call
  260. </p><pre class="synopsis">
  261. int lo_truncate(PGcon *conn, int fd, size_t len);
  262. </pre><p>
  263. This function truncates the large object
  264. descriptor <em class="parameter"><code>fd</code></em> to length <em class="parameter"><code>len</code></em>. The
  265. <em class="parameter"><code>fd</code></em> argument must have been returned by a
  266. previous <code class="function">lo_open</code>. If <em class="parameter"><code>len</code></em> is
  267. greater than the large object's current length, the large object
  268. is extended to the specified length with null bytes ('\0').
  269. On success, <code class="function">lo_truncate</code> returns
  270. zero. On error, the return value is -1.
  271. </p><p>
  272. The read/write location associated with the descriptor
  273. <em class="parameter"><code>fd</code></em> is not changed.
  274. </p><p>
  275. Although the <em class="parameter"><code>len</code></em> parameter is declared as
  276. <code class="type">size_t</code>, <code class="function">lo_truncate</code> will reject length
  277. values larger than <code class="literal">INT_MAX</code>.
  278. </p><p>
  279. <a id="id-1.7.4.8.14.5.1" class="indexterm"></a>
  280. When dealing with large objects that might exceed 2GB in size,
  281. instead use
  282. </p><pre class="synopsis">
  283. int lo_truncate64(PGcon *conn, int fd, pg_int64 len);
  284. </pre><p>
  285. This function has the same
  286. behavior as <code class="function">lo_truncate</code>, but it can accept a
  287. <em class="parameter"><code>len</code></em> value exceeding 2GB.
  288. </p><p>
  289. <code class="function">lo_truncate</code> is new as of <span class="productname">PostgreSQL</span>
  290. 8.3; if this function is run against an older server version, it will
  291. fail and return -1.
  292. </p><p>
  293. <code class="function">lo_truncate64</code> is new as of <span class="productname">PostgreSQL</span>
  294. 9.3; if this function is run against an older server version, it will
  295. fail and return -1.
  296. </p></div><div class="sect2" id="LO-CLOSE"><div class="titlepage"><div><div><h3 class="title">34.3.10. Closing a Large Object Descriptor</h3></div></div></div><p>
  297. <a id="id-1.7.4.8.15.2.1" class="indexterm"></a>
  298. A large object descriptor can be closed by calling
  299. </p><pre class="synopsis">
  300. int lo_close(PGconn *conn, int fd);
  301. </pre><p>
  302. where <em class="parameter"><code>fd</code></em> is a
  303. large object descriptor returned by <code class="function">lo_open</code>.
  304. On success, <code class="function">lo_close</code> returns zero. On
  305. error, the return value is -1.
  306. </p><p>
  307. Any large object descriptors that remain open at the end of a
  308. transaction will be closed automatically.
  309. </p></div><div class="sect2" id="LO-UNLINK"><div class="titlepage"><div><div><h3 class="title">34.3.11. Removing a Large Object</h3></div></div></div><p>
  310. <a id="id-1.7.4.8.16.2.1" class="indexterm"></a>
  311. To remove a large object from the database, call
  312. </p><pre class="synopsis">
  313. int lo_unlink(PGconn *conn, Oid lobjId);
  314. </pre><p>
  315. The <em class="parameter"><code>lobjId</code></em> argument specifies the OID of the
  316. large object to remove. Returns 1 if successful, -1 on failure.
  317. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="lo-implementation.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="largeobjects.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="lo-funcs.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">34.2. Implementation Features </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 34.4. Server-Side Functions</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1