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.

87 satır
8.2KB

  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.4. Server-Side 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="lo-interfaces.html" title="34.3. Client Interfaces" /><link rel="next" href="lo-examplesect.html" title="34.5. Example Program" /></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.4. Server-Side Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="lo-interfaces.html" title="34.3. Client Interfaces">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-examplesect.html" title="34.5. Example Program">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="LO-FUNCS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">34.4. Server-Side Functions</h2></div></div></div><p>
  3. Server-side functions tailored for manipulating large objects from SQL are
  4. listed in <a class="xref" href="lo-funcs.html#LO-FUNCS-TABLE" title="Table 34.1. SQL-Oriented Large Object Functions">Table 34.1</a>.
  5. </p><div class="table" id="LO-FUNCS-TABLE"><p class="title"><strong>Table 34.1. SQL-Oriented Large Object Functions</strong></p><div class="table-contents"><table class="table" summary="SQL-Oriented Large Object Functions" border="1"><colgroup><col /><col /><col /><col /><col /></colgroup><thead><tr><th>Function</th><th>Return Type</th><th>Description</th><th>Example</th><th>Result</th></tr></thead><tbody><tr><td>
  6. <a id="id-1.7.4.9.3.2.2.1.1.1" class="indexterm"></a>
  7. <code class="literal"><code class="function">lo_from_bytea(<em class="parameter"><code>loid</code></em> <code class="type">oid</code>, <em class="parameter"><code>string</code></em> <code class="type">bytea</code>)</code></code>
  8. </td><td><code class="type">oid</code></td><td>
  9. Create a large object and store data there, returning its OID.
  10. Pass <code class="literal">0</code> to have the system choose an OID.
  11. </td><td><code class="literal">lo_from_bytea(0, '\xffffff00')</code></td><td><code class="literal">24528</code></td></tr><tr><td>
  12. <a id="id-1.7.4.9.3.2.2.2.1.1" class="indexterm"></a>
  13. <code class="literal"><code class="function">lo_put(<em class="parameter"><code>loid</code></em> <code class="type">oid</code>, <em class="parameter"><code>offset</code></em> <code class="type">bigint</code>, <em class="parameter"><code>str</code></em> <code class="type">bytea</code>)</code></code>
  14. </td><td><code class="type">void</code></td><td>
  15. Write data at the given offset.
  16. </td><td><code class="literal">lo_put(24528, 1, '\xaa')</code></td><td> </td></tr><tr><td>
  17. <a id="id-1.7.4.9.3.2.2.3.1.1" class="indexterm"></a>
  18. <code class="literal"><code class="function">lo_get(<em class="parameter"><code>loid</code></em> <code class="type">oid</code> [<span class="optional">, <em class="parameter"><code>from</code></em> <code class="type">bigint</code>, <em class="parameter"><code>for</code></em> <code class="type">int</code></span>])</code></code>
  19. </td><td><code class="type">bytea</code></td><td>
  20. Extract contents or a substring thereof.
  21. </td><td><code class="literal">lo_get(24528, 0, 3)</code></td><td><code class="literal">\xffaaff</code></td></tr></tbody></table></div></div><br class="table-break" /><p>
  22. There are additional server-side functions corresponding to each of the
  23. client-side functions described earlier; indeed, for the most part the
  24. client-side functions are simply interfaces to the equivalent server-side
  25. functions. The ones just as convenient to call via SQL commands are
  26. <code class="function">lo_creat</code><a id="id-1.7.4.9.4.2" class="indexterm"></a>,
  27. <code class="function">lo_create</code>,
  28. <code class="function">lo_unlink</code><a id="id-1.7.4.9.4.5" class="indexterm"></a>,
  29. <code class="function">lo_import</code><a id="id-1.7.4.9.4.7" class="indexterm"></a>, and
  30. <code class="function">lo_export</code><a id="id-1.7.4.9.4.9" class="indexterm"></a>.
  31. Here are examples of their use:
  32. </p><pre class="programlisting">
  33. CREATE TABLE image (
  34. name text,
  35. raster oid
  36. );
  37. SELECT lo_creat(-1); -- returns OID of new, empty large object
  38. SELECT lo_create(43213); -- attempts to create large object with OID 43213
  39. SELECT lo_unlink(173454); -- deletes large object with OID 173454
  40. INSERT INTO image (name, raster)
  41. VALUES ('beautiful image', lo_import('/etc/motd'));
  42. INSERT INTO image (name, raster) -- same as above, but specify OID to use
  43. VALUES ('beautiful image', lo_import('/etc/motd', 68583));
  44. SELECT lo_export(image.raster, '/tmp/motd') FROM image
  45. WHERE name = 'beautiful image';
  46. </pre><p>
  47. </p><p>
  48. The server-side <code class="function">lo_import</code> and
  49. <code class="function">lo_export</code> functions behave considerably differently
  50. from their client-side analogs. These two functions read and write files
  51. in the server's file system, using the permissions of the database's
  52. owning user. Therefore, by default their use is restricted to superusers.
  53. In contrast, the client-side import and export functions read and write
  54. files in the client's file system, using the permissions of the client
  55. program. The client-side functions do not require any database
  56. privileges, except the privilege to read or write the large object in
  57. question.
  58. </p><div class="caution"><h3 class="title">Caution</h3><p>
  59. It is possible to <a class="xref" href="sql-grant.html" title="GRANT"><span class="refentrytitle">GRANT</span></a> use of the
  60. server-side <code class="function">lo_import</code>
  61. and <code class="function">lo_export</code> functions to non-superusers, but
  62. careful consideration of the security implications is required. A
  63. malicious user of such privileges could easily parlay them into becoming
  64. superuser (for example by rewriting server configuration files), or could
  65. attack the rest of the server's file system without bothering to obtain
  66. database superuser privileges as such. <span class="emphasis"><em>Access to roles having
  67. such privilege must therefore be guarded just as carefully as access to
  68. superuser roles.</em></span> Nonetheless, if use of
  69. server-side <code class="function">lo_import</code>
  70. or <code class="function">lo_export</code> is needed for some routine task, it's
  71. safer to use a role with such privileges than one with full superuser
  72. privileges, as that helps to reduce the risk of damage from accidental
  73. errors.
  74. </p></div><p>
  75. The functionality of <code class="function">lo_read</code> and
  76. <code class="function">lo_write</code> is also available via server-side calls,
  77. but the names of the server-side functions differ from the client side
  78. interfaces in that they do not contain underscores. You must call
  79. these functions as <code class="function">loread</code> and <code class="function">lowrite</code>.
  80. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="lo-interfaces.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-examplesect.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">34.3. Client Interfaces </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 34.5. Example Program</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1