lobj.sgml 17.1 KB
Newer Older
1
<!--
P
Peter Eisentraut 已提交
2
$Header: /cvsroot/pgsql/doc/src/sgml/lobj.sgml,v 1.21 2001/09/15 16:08:59 petere Exp $
3 4
-->

T
Thomas G. Lockhart 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 <chapter id="largeObjects">
  <title id="largeObjects-title">Large Objects</title>

  <para>
   In <productname>Postgres</productname>,
   data values are stored in tuples and 
   individual tuples cannot span data pages. Since the size of
   a data page is 8192 bytes, the upper limit on the  size
   of a data value is relatively low. To support the storage 
   of larger atomic values, 
   <productname>Postgres</productname> provides a  large
   object   interface.    This  interface  provides  file
   oriented access to user data that has been declared  to
   be a large type.
   This  section describes the implementation and the 
20
   programming and query  language  interfaces  to 
T
Thomas G. Lockhart 已提交
21 22 23 24
   <productname>Postgres</productname>
   large object data.
  </para>

25
  <sect1 id="lo-history">
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
   <title>Historical Note</title>

   <para>
    Originally, <productname>Postgres 4.2</productname> supported three standard 
    implementations of large objects: as files external 
    to <productname>Postgres</productname>,  as
    external files managed by <productname>Postgres</productname>, and as data
    stored within the <productname>Postgres</productname> database. It causes  
    considerable confusion among users. As a result, we only 
    support large objects as data stored within  the  <productname>Postgres</productname>
    database  in  <productname>PostgreSQL</productname>.  Even  though it is slower to
    access, it provides stricter data  integrity.
    For historical reasons, this storage scheme is referred to as 
    Inversion large objects. (We will use  Inversion  and  large
    objects  interchangeably to mean the same thing in this
    section.)
B
Bruce Momjian 已提交
42
    Since <productname>PostgreSQL 7.1</productname> all large objects are placed in
43
    one system table called <classname>pg_largeobject</classname>.
44 45 46
   </para>
  </sect1>

47
  <sect1 id="lo-implementation">
48 49 50 51
   <title>Implementation Features</title>

   <para>
    The Inversion large object implementation breaks  large
52
    objects  up  into  <quote>chunks</quote>  and  stores  the chunks in
53 54 55 56 57 58
    tuples in the database.  A B-tree index guarantees fast
    searches for the correct chunk number when doing random
    access reads and writes.
   </para>
  </sect1>

59
  <sect1 id="lo-interfaces">
60 61 62 63 64 65 66 67
   <title>Interfaces</title>

   <para>
    The  facilities  <productname>Postgres</productname> provides  to
    access large objects,  both  in  the backend as part of user-defined
    functions or the front end as part  of  an  application
    using  the   interface, are described below. For users
    familiar with <productname>Postgres 4.2</productname>,
T
Thomas G. Lockhart 已提交
68
    <productname>PostgreSQL</productname> has a new set of
69 70 71 72 73 74
    functions  providing  a  more  coherent  interface.

    <note>
     <para>
      All large object manipulation <emphasis>must</emphasis> take
      place within an SQL transaction. This requirement is strictly
75
      enforced as of Postgres 6.5, though it has been an
76 77 78 79 80 81 82 83 84 85 86 87 88 89
      implicit requirement in previous versions, resulting in
      misbehavior if ignored.
     </para>
    </note>
   </para>

   <para>
    The  <productname>Postgres</productname>  large  object interface is modeled after
    the <acronym>Unix</acronym>  file  system  interface,  with  analogues  of
    <function>open(2)</function>,  <function>read(2)</function>,
    <function>write(2)</function>,
    <function>lseek(2)</function>, etc.  User 
    functions call these routines to retrieve only the data  of
    interest  from a large object.  For example, if a large
90
    object type called <type>mugshot</type>  existed  that  stored  
91
    photographs  of  faces, then a function called beard could
92
    be declared on <type>mugshot</type> data.  Beard could look  at  the
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    lower third of a photograph, and determine the color of
    the beard that appeared  there,  if  any.   The  entire
    large  object value need not be buffered, or even 
    examined, by the beard function.
    Large objects may be accessed from dynamically-loaded <acronym>C</acronym>
    functions  or  database  client  programs that link the
    library.  <productname>Postgres</productname> provides a set of routines that 
    support opening, reading, writing, closing, and seeking on
    large objects.
   </para>

   <sect2>
    <title>Creating a Large Object</title>

    <para>
108
     The routine
109
<synopsis>
110
Oid lo_creat(PGconn *<replaceable class="parameter">conn</replaceable>, int <replaceable class="parameter">mode</replaceable>)
111
</synopsis>
112
     creates a new large  object.  
113
     <replaceable class="parameter">mode</replaceable>  is  a  bit mask
114 115 116
     describing  several  different  attributes  of  the new
     object.  The symbolic constants listed here are defined
     in
117
     <filename>$<envar>PGROOT</envar>/src/backend/libpq/libpq-fs.h</filename>
118
     The access type (read, write, or both) is controlled by
119
     OR'ing together the bits <acronym>INV_READ</acronym>  and
B
Bruce Momjian 已提交
120
     <acronym>INV_WRITE</acronym>.  The low-order sixteen bits of mask  are
121 122 123 124
     the  storage  manager  number on which the large object
     should reside.  For sites other  than  Berkeley,  these
     bits should always be zero.
     The commands below create an (Inversion) large object:
125
     <programlisting>
B
Bruce Momjian 已提交
126
inv_oid = lo_creat(INV_READ|INV_WRITE);
127 128 129 130 131 132 133 134
     </programlisting>
    </para>
   </sect2>

   <sect2>
    <title>Importing a Large Object</title>

    <para>
135 136 137 138
     To import a <acronym>UNIX</acronym> file as a large object, call
<synopsis>
Oid lo_import(PGconn *<replaceable class="parameter">conn</replaceable>, const char *<replaceable class="parameter">filename</replaceable>)
</synopsis>
139
    <replaceable class="parameter">filename</replaceable> 
140
     specifies the  <acronym>Unix</acronym>  path name  of
141
     the file to be imported as a large object.
142 143 144 145 146 147 148 149
    </para>
   </sect2>

   <sect2>
    <title>Exporting a Large Object</title>

    <para>
     To export a large object
150 151 152 153
     into <acronym>UNIX</acronym> file, call
<synopsis>
int lo_export(PGconn *<replaceable class="parameter">conn</replaceable>, Oid <replaceable class="parameter">lobjId</replaceable>, const char *<replaceable class="parameter">filename</replaceable>)
</synopsis>
154 155 156
     The <parameter>lobjId</parameter> argument specifies  the  Oid  of  the  large
     object  to  export  and the <parameter>filename</parameter> argument specifies
     the <acronym>UNIX</acronym> path name of the file.
157 158
    </para>
   </sect2>
159

160 161
   <sect2>
    <title>Opening an Existing Large Object</title>
162

163
    <para>
164
     To open an existing large object, call
165 166 167
<synopsis>
int lo_open(PGconn *conn, Oid lobjId, int mode)
</synopsis>
168 169 170
     The <parameter>lobjId</parameter> argument specifies  the  Oid  of  the  large
     object  to  open.   The  <parameter>mode</parameter>  bits control whether the
     object is opened  for  reading  (<symbol>INV_READ</>),  writing  or
171 172
     both.
     A  large  object cannot be opened before it is created.
173 174 175 176
     <function>lo_open</function> returns a large object descriptor
     for later use in <function>lo_read</function>, <function>lo_write</function>,
     <function>lo_lseek</function>, <function>lo_tell</function>, and
     <function>lo_close</function>.
T
Thomas G. Lockhart 已提交
177 178
</para>
</sect2>
179

T
Thomas G. Lockhart 已提交
180 181
<sect2>
<title>Writing Data to a Large Object</title>
182

T
Thomas G. Lockhart 已提交
183
<para>
184
     The routine
T
Thomas G. Lockhart 已提交
185
<programlisting>
186
int lo_write(PGconn *conn, int fd, const char *buf, size_t len)
T
Thomas G. Lockhart 已提交
187
</programlisting>
188
     writes <parameter>len</parameter> bytes from <parameter>buf</parameter> to large object <parameter>fd</>.   The <parameter>fd</parameter>
189
     argument must have been returned by a previous <function>lo_open</function>.
190 191
     The number of bytes actually written is  returned.   In
     the event of an error, the return value is negative.
T
Thomas G. Lockhart 已提交
192 193
</para>
</sect2>
194

195 196 197 198 199 200 201 202
<sect2>
<title>Reading Data from a Large Object</title>

<para>
     The routine
<programlisting>
int lo_read(PGconn *conn, int fd, char *buf, size_t len)
</programlisting>
203
     reads <parameter>len</parameter> bytes from large object <parameter>fd</parameter> into <parameter>buf</parameter>. The  <parameter>fd</parameter>
204 205 206 207 208 209
     argument must have been returned by a previous <function>lo_open</function>.
     The number of bytes actually read is returned. In
     the event of an error, the return value is negative.
</para>
</sect2>

T
Thomas G. Lockhart 已提交
210 211
<sect2>
<title>Seeking on a Large Object</title>
212

T
Thomas G. Lockhart 已提交
213
<para>
214 215
     To change the current read or write location on a large
     object, call
T
Thomas G. Lockhart 已提交
216
<programlisting>
217
int lo_lseek(PGconn *conn, int fd, int offset, int whence)
T
Thomas G. Lockhart 已提交
218
</programlisting>
219 220
     This routine moves the current location pointer for the
     large object described by fd to the new location specified 
221 222
     by offset.  The valid values for whence are
     SEEK_SET, SEEK_CUR, and SEEK_END.
T
Thomas G. Lockhart 已提交
223 224
</para>
</sect2>
225

T
Thomas G. Lockhart 已提交
226 227
<sect2>
<title>Closing a Large Object Descriptor</title>
228

T
Thomas G. Lockhart 已提交
229
<para>
230
     A large object may be closed by calling
T
Thomas G. Lockhart 已提交
231
<programlisting>
232
int lo_close(PGconn *conn, int fd)
T
Thomas G. Lockhart 已提交
233
</programlisting>
234
     where  fd  is  a  large  object  descriptor returned by
235 236
     <function>lo_open</function>.  On success, <function>lo_close</function>
      returns zero.  On error, the return value is negative.
T
Thomas G. Lockhart 已提交
237
</para>
238
</sect2>
239 240 241 242 243 244 245 246 247

   <sect2>
    <title>Removing a Large Object</title>

    <para>
     To remove a large object from the database, call
<synopsis>
Oid lo_unlink(PGconn *<replaceable class="parameter">conn</replaceable>, Oid lobjId)
</synopsis>
248
     The <parameter>lobjId</parameter> argument specifies  the  Oid  of  the  large
249 250 251 252 253
     object  to  remove.
    </para>
   </sect2>


T
Thomas G. Lockhart 已提交
254
</sect1>
255

256
<sect1 id="lo-funcs">
P
Peter Eisentraut 已提交
257
<title>Server-side Built-in Functions</title>
258

T
Thomas G. Lockhart 已提交
259 260 261 262
<para>
     There  are two built-in registered functions, <acronym>lo_import</acronym>
     and <acronym>lo_export</acronym> which  are  convenient  for  use
    in  <acronym>SQL</acronym>
263 264
     queries.
     Here is an example of their use
T
Thomas G. Lockhart 已提交
265
<programlisting>
266 267 268 269 270 271 272 273
CREATE TABLE image (
    name            text,
    raster          oid
);

INSERT INTO image (name, raster)
    VALUES ('beautiful image', lo_import('/etc/motd'));

B
Bruce Momjian 已提交
274
SELECT lo_export(image.raster, '/tmp/motd') from image
275
    WHERE name = 'beautiful image';
T
Thomas G. Lockhart 已提交
276 277 278
</programlisting>
</para>
</sect1>
279

280
<sect1 id="lo-libpq">
281
<title>Accessing Large Objects from <application>Libpq</application></title>
282

T
Thomas G. Lockhart 已提交
283
<para>
284 285
     Below is a sample program which shows how the large object  
     interface
286
     in  <application>libpq</>  can  be used.  Parts of the program are 
287 288
     commented out but are left in the source for  the  readers
     benefit.  This program can be found in
T
Thomas G. Lockhart 已提交
289
<filename>
290
../src/test/examples
T
Thomas G. Lockhart 已提交
291
</filename>
292
     Frontend applications which use the large object interface  
293 294
     in  <application>libpq</application>  should   include   the   header   file
     <filename>libpq/libpq-fs.h</filename> and link with the <application>libpq</application> library.
T
Thomas G. Lockhart 已提交
295 296
</para>
</sect1>
297

P
Peter Eisentraut 已提交
298 299
<sect1 id="lo-example">
<title>Example Program</title>
300

P
Peter Eisentraut 已提交
301 302
  <example>
   <title>Large Objects with <application>Libpq</application> Example Program</title>
T
Thomas G. Lockhart 已提交
303
<programlisting>
304
/*--------------------------------------------------------------
305 306 307 308 309 310 311 312 313 314 315 316 317
 *
 * testlo.c--
 *    test using large objects with libpq
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *    /usr/local/devel/pglite/cvs/src/doc/manual.me,v 1.16 1995/09/01 23:55:00 jolly Exp
 *
 *--------------------------------------------------------------
 */
#include &lt;stdio.h&gt;
318 319
#include &quot;libpq-fe.h&quot;
#include &quot;libpq/libpq-fs.h&quot;
320 321 322 323

#define BUFSIZE          1024

/*
P
Peter Eisentraut 已提交
324 325
 * importFile
 *    import file &quot;in_filename&quot; into database as large object &quot;lobjOid&quot;
326 327
 *
 */
328 329
Oid
importFile(PGconn *conn, char *filename)
330
{
331 332 333 334 335 336
    Oid         lobjId;
    int         lobj_fd;
    char        buf[BUFSIZE];
    int         nbytes,
                tmp;
    int         fd;
337 338 339 340 341

    /*
     * open the file to be read in
     */
    fd = open(filename, O_RDONLY, 0666);
342 343 344
    if (fd &lt; 0)
    {                           /* error */
        fprintf(stderr, &quot;can't open unix file %s\n&quot;, filename);
345 346 347 348 349
    }

    /*
     * create the large object
     */
350 351 352
    lobjId = lo_creat(conn, INV_READ | INV_WRITE);
    if (lobjId == 0)
        fprintf(stderr, &quot;can't create large object\n&quot;);
353 354

    lobj_fd = lo_open(conn, lobjId, INV_WRITE);
355

356 357 358
    /*
     * read in from the Unix file and write to the inversion file
     */
359 360 361 362 363
    while ((nbytes = read(fd, buf, BUFSIZE)) &gt; 0)
    {
        tmp = lo_write(conn, lobj_fd, buf, nbytes);
        if (tmp &lt; nbytes)
            fprintf(stderr, &quot;error while reading large object\n&quot;);
364 365 366 367 368 369 370 371
    }

    (void) close(fd);
    (void) lo_close(conn, lobj_fd);

    return lobjId;
}

372 373
void
pickout(PGconn *conn, Oid lobjId, int start, int len)
374
{
375 376 377 378
    int         lobj_fd;
    char       *buf;
    int         nbytes;
    int         nread;
379 380

    lobj_fd = lo_open(conn, lobjId, INV_READ);
381 382 383 384
    if (lobj_fd &lt; 0)
    {
        fprintf(stderr, &quot;can't open large object %d\n&quot;,
                lobjId);
385 386 387
    }

    lo_lseek(conn, lobj_fd, start, SEEK_SET);
388
    buf = malloc(len + 1);
389 390

    nread = 0;
391 392 393 394 395 396
    while (len - nread &gt; 0)
    {
        nbytes = lo_read(conn, lobj_fd, buf, len - nread);
        buf[nbytes] = ' ';
        fprintf(stderr, &quot;&gt;&gt;&gt; %s&quot;, buf);
        nread += nbytes;
397
    }
398 399
    free(buf);
    fprintf(stderr, &quot;\n&quot;);
400 401 402
    lo_close(conn, lobj_fd);
}

403 404
void
overwrite(PGconn *conn, Oid lobjId, int start, int len)
405
{
406 407 408 409 410
    int         lobj_fd;
    char       *buf;
    int         nbytes;
    int         nwritten;
    int         i;
411 412

    lobj_fd = lo_open(conn, lobjId, INV_READ);
413 414 415 416
    if (lobj_fd &lt; 0)
    {
        fprintf(stderr, &quot;can't open large object %d\n&quot;,
                lobjId);
417 418 419
    }

    lo_lseek(conn, lobj_fd, start, SEEK_SET);
420
    buf = malloc(len + 1);
421

422 423
    for (i = 0; i &lt; len; i++)
        buf[i] = 'X';
424 425 426
    buf[i] = ' ';

    nwritten = 0;
427 428 429 430
    while (len - nwritten &gt; 0)
    {
        nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
        nwritten += nbytes;
431
    }
432 433
    free(buf);
    fprintf(stderr, &quot;\n&quot;);
434 435 436 437
    lo_close(conn, lobj_fd);
}

/*
438
 * exportFile *    export large object &quot;lobjOid&quot; to file &quot;out_filename&quot;
439 440
 *
 */
441 442
void
exportFile(PGconn *conn, Oid lobjId, char *filename)
443
{
444 445 446 447 448
    int         lobj_fd;
    char        buf[BUFSIZE];
    int         nbytes,
                tmp;
    int         fd;
449 450

    /*
451
     * create an inversion &quot;object&quot;
452 453
     */
    lobj_fd = lo_open(conn, lobjId, INV_READ);
454 455 456 457
    if (lobj_fd &lt; 0)
    {
        fprintf(stderr, &quot;can't open large object %d\n&quot;,
                lobjId);
458 459 460 461 462
    }

    /*
     * open the file to be written to
     */
463 464 465 466 467
    fd = open(filename, O_CREAT | O_WRONLY, 0666);
    if (fd &lt; 0)
    {                           /* error */
        fprintf(stderr, &quot;can't open unix file %s\n&quot;,
                filename);
468 469 470 471 472
    }

    /*
     * read in from the Unix file and write to the inversion file
     */
473 474 475 476 477 478 479 480
    while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) &gt; 0)
    {
        tmp = write(fd, buf, nbytes);
        if (tmp &lt; nbytes)
        {
            fprintf(stderr, &quot;error while writing %s\n&quot;,
                    filename);
        }
481 482 483 484 485 486 487 488 489
    }

    (void) lo_close(conn, lobj_fd);
    (void) close(fd);

    return;
}

void
490
exit_nicely(PGconn *conn)
491
{
492 493
    PQfinish(conn);
    exit(1);
494 495 496 497 498
}

int
main(int argc, char **argv)
{
499 500 501 502 503 504 505 506 507 508 509 510
    char       *in_filename,
               *out_filename;
    char       *database;
    Oid         lobjOid;
    PGconn     *conn;
    PGresult   *res;

    if (argc != 4)
    {
        fprintf(stderr, &quot;Usage: %s database_name in_filename out_filename\n&quot;,
                argv[0]);
        exit(1);
511 512 513 514 515 516 517 518 519 520 521 522
    }

    database = argv[1];
    in_filename = argv[2];
    out_filename = argv[3];

    /*
     * set up the connection
     */
    conn = PQsetdb(NULL, NULL, NULL, NULL, database);

    /* check to see that the backend connection was successfully made */
523 524 525 526 527
    if (PQstatus(conn) == CONNECTION_BAD)
    {
        fprintf(stderr, &quot;Connection to database '%s' failed.\n&quot;, database);
        fprintf(stderr, &quot;%s&quot;, PQerrorMessage(conn));
        exit_nicely(conn);
528 529
    }

530
    res = PQexec(conn, &quot;begin&quot;);
531 532
    PQclear(res);

533
    printf(&quot;importing file %s\n&quot;, in_filename);
534 535 536
/*  lobjOid = importFile(conn, in_filename); */
    lobjOid = lo_import(conn, in_filename);
/*
537
    printf(&quot;as large object %d.\n&quot;, lobjOid);
538

539
    printf(&quot;picking out bytes 1000-2000 of the large object\n&quot;);
540 541
    pickout(conn, lobjOid, 1000, 1000);

542
    printf(&quot;overwriting bytes 1000-2000 of the large object with X's\n&quot;);
543 544 545
    overwrite(conn, lobjOid, 1000, 1000);
*/

546
    printf(&quot;exporting large object to file %s\n&quot;, out_filename);
547
/*    exportFile(conn, lobjOid, out_filename); */
548
    lo_export(conn, lobjOid, out_filename);
549

550
    res = PQexec(conn, &quot;end&quot;);
551 552 553 554
    PQclear(res);
    PQfinish(conn);
    exit(0);
}
T
Thomas G. Lockhart 已提交
555
</programlisting>
P
Peter Eisentraut 已提交
556
</example>
T
Thomas G. Lockhart 已提交
557 558 559 560 561 562

</sect1>
</chapter>

<!-- Keep this comment at the end of the file
Local variables:
563
mode:sgml
T
Thomas G. Lockhart 已提交
564 565 566 567 568 569 570 571 572
sgml-omittag:nil
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"./reference.ced"
sgml-exposed-tags:nil
573
sgml-local-catalogs:("/usr/lib/sgml/catalog")
T
Thomas G. Lockhart 已提交
574 575 576
sgml-local-ecat-files:nil
End:
-->