Current File : //usr/share/doc/postgresql-9.2.24/html/lo-interfaces.html |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Client Interfaces</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.2.24 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Large Objects"
HREF="largeobjects.html"><LINK
REL="PREVIOUS"
TITLE="Implementation Features"
HREF="lo-implementation.html"><LINK
REL="NEXT"
TITLE="Server-side Functions"
HREF="lo-funcs.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2017-11-06T22:43:11"></HEAD
><BODY
CLASS="SECT1"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.2.24 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="Implementation Features"
HREF="lo-implementation.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="largeobjects.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
>Chapter 32. Large Objects</TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="Server-side Functions"
HREF="lo-funcs.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="LO-INTERFACES"
>32.3. Client Interfaces</A
></H1
><P
> This section describes the facilities that
<SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> client interface libraries
provide for accessing large objects. All large object
manipulation using these functions <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>must</I
></SPAN
> take
place within an SQL transaction block.
The <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> large object interface is modeled after
the <ACRONYM
CLASS="ACRONYM"
>Unix</ACRONYM
> file-system interface, with analogues of
<CODE
CLASS="FUNCTION"
>open</CODE
>, <CODE
CLASS="FUNCTION"
>read</CODE
>,
<CODE
CLASS="FUNCTION"
>write</CODE
>,
<CODE
CLASS="FUNCTION"
>lseek</CODE
>, etc.
</P
><P
> Client applications which use the large object interface in
<SPAN
CLASS="APPLICATION"
>libpq</SPAN
> should include the header file
<TT
CLASS="FILENAME"
>libpq/libpq-fs.h</TT
> and link with the
<SPAN
CLASS="APPLICATION"
>libpq</SPAN
> library.
</P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="LO-CREATE"
>32.3.1. Creating a Large Object</A
></H2
><P
> The function
</P><PRE
CLASS="SYNOPSIS"
>Oid lo_creat(PGconn *conn, int mode);</PRE
><P>
creates a new large object.
The return value is the OID that was assigned to the new large object,
or <TT
CLASS="SYMBOL"
>InvalidOid</TT
> (zero) on failure.
<TT
CLASS="REPLACEABLE"
><I
>mode</I
></TT
> is unused and
ignored as of <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> 8.1; however, for
backward compatibility with earlier releases it is best to
set it to <TT
CLASS="SYMBOL"
>INV_READ</TT
>, <TT
CLASS="SYMBOL"
>INV_WRITE</TT
>,
or <TT
CLASS="SYMBOL"
>INV_READ</TT
> <TT
CLASS="LITERAL"
>|</TT
> <TT
CLASS="SYMBOL"
>INV_WRITE</TT
>.
(These symbolic constants are defined
in the header file <TT
CLASS="FILENAME"
>libpq/libpq-fs.h</TT
>.)
</P
><P
> An example:
</P><PRE
CLASS="PROGRAMLISTING"
>inv_oid = lo_creat(conn, INV_READ|INV_WRITE);</PRE
><P>
</P
><P
> The function
</P><PRE
CLASS="SYNOPSIS"
>Oid lo_create(PGconn *conn, Oid lobjId);</PRE
><P>
also creates a new large object. The OID to be assigned can be
specified by <TT
CLASS="REPLACEABLE"
><I
>lobjId</I
></TT
>;
if so, failure occurs if that OID is already in use for some large
object. If <TT
CLASS="REPLACEABLE"
><I
>lobjId</I
></TT
>
is <TT
CLASS="SYMBOL"
>InvalidOid</TT
> (zero) then <CODE
CLASS="FUNCTION"
>lo_create</CODE
> assigns an unused
OID (this is the same behavior as <CODE
CLASS="FUNCTION"
>lo_creat</CODE
>).
The return value is the OID that was assigned to the new large object,
or <TT
CLASS="SYMBOL"
>InvalidOid</TT
> (zero) on failure.
</P
><P
> <CODE
CLASS="FUNCTION"
>lo_create</CODE
> is new as of <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>
8.1; if this function is run against an older server version, it will
fail and return <TT
CLASS="SYMBOL"
>InvalidOid</TT
>.
</P
><P
> An example:
</P><PRE
CLASS="PROGRAMLISTING"
>inv_oid = lo_create(conn, desired_oid);</PRE
><P>
</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="LO-IMPORT"
>32.3.2. Importing a Large Object</A
></H2
><P
> To import an operating system file as a large object, call
</P><PRE
CLASS="SYNOPSIS"
>Oid lo_import(PGconn *conn, const char *filename);</PRE
><P>
<TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
>
specifies the operating system name of
the file to be imported as a large object.
The return value is the OID that was assigned to the new large object,
or <TT
CLASS="SYMBOL"
>InvalidOid</TT
> (zero) on failure.
Note that the file is read by the client interface library, not by
the server; so it must exist in the client file system and be readable
by the client application.
</P
><P
> The function
</P><PRE
CLASS="SYNOPSIS"
>Oid lo_import_with_oid(PGconn *conn, const char *filename, Oid lobjId);</PRE
><P>
also imports a new large object. The OID to be assigned can be
specified by <TT
CLASS="REPLACEABLE"
><I
>lobjId</I
></TT
>;
if so, failure occurs if that OID is already in use for some large
object. If <TT
CLASS="REPLACEABLE"
><I
>lobjId</I
></TT
>
is <TT
CLASS="SYMBOL"
>InvalidOid</TT
> (zero) then <CODE
CLASS="FUNCTION"
>lo_import_with_oid</CODE
> assigns an unused
OID (this is the same behavior as <CODE
CLASS="FUNCTION"
>lo_import</CODE
>).
The return value is the OID that was assigned to the new large object,
or <TT
CLASS="SYMBOL"
>InvalidOid</TT
> (zero) on failure.
</P
><P
> <CODE
CLASS="FUNCTION"
>lo_import_with_oid</CODE
> is new as of <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>
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
fail and return <TT
CLASS="SYMBOL"
>InvalidOid</TT
>.
</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="LO-EXPORT"
>32.3.3. Exporting a Large Object</A
></H2
><P
> To export a large object
into an operating system file, call
</P><PRE
CLASS="SYNOPSIS"
>int lo_export(PGconn *conn, Oid lobjId, const char *filename);</PRE
><P>
The <TT
CLASS="PARAMETER"
>lobjId</TT
> argument specifies the OID of the large
object to export and the <TT
CLASS="PARAMETER"
>filename</TT
> argument
specifies the operating system name of the file. Note that the file is
written by the client interface library, not by the server. Returns 1
on success, -1 on failure.
</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="LO-OPEN"
>32.3.4. Opening an Existing Large Object</A
></H2
><P
> To open an existing large object for reading or writing, call
</P><PRE
CLASS="SYNOPSIS"
>int lo_open(PGconn *conn, Oid lobjId, int mode);</PRE
><P>
The <TT
CLASS="PARAMETER"
>lobjId</TT
> argument specifies the OID of the large
object to open. The <TT
CLASS="PARAMETER"
>mode</TT
> bits control whether the
object is opened for reading (<TT
CLASS="SYMBOL"
>INV_READ</TT
>), writing
(<TT
CLASS="SYMBOL"
>INV_WRITE</TT
>), or both.
(These symbolic constants are defined
in the header file <TT
CLASS="FILENAME"
>libpq/libpq-fs.h</TT
>.)
A large object cannot be opened before it is created.
<CODE
CLASS="FUNCTION"
>lo_open</CODE
> returns a (non-negative) large object
descriptor for later use in <CODE
CLASS="FUNCTION"
>lo_read</CODE
>,
<CODE
CLASS="FUNCTION"
>lo_write</CODE
>, <CODE
CLASS="FUNCTION"
>lo_lseek</CODE
>,
<CODE
CLASS="FUNCTION"
>lo_tell</CODE
>, and <CODE
CLASS="FUNCTION"
>lo_close</CODE
>.
The descriptor is only valid for
the duration of the current transaction.
On failure, -1 is returned.
</P
><P
> The server currently does not distinguish between modes
<TT
CLASS="SYMBOL"
>INV_WRITE</TT
> and <TT
CLASS="SYMBOL"
>INV_READ</TT
> <TT
CLASS="LITERAL"
>|</TT
>
<TT
CLASS="SYMBOL"
>INV_WRITE</TT
>: you are allowed to read from the descriptor
in either case. However there is a significant difference between
these modes and <TT
CLASS="SYMBOL"
>INV_READ</TT
> alone: with <TT
CLASS="SYMBOL"
>INV_READ</TT
>
you cannot write on the descriptor, and the data read from it will
reflect the contents of the large object at the time of the transaction
snapshot that was active when <CODE
CLASS="FUNCTION"
>lo_open</CODE
> was executed,
regardless of later writes by this or other transactions. Reading
from a descriptor opened with <TT
CLASS="SYMBOL"
>INV_WRITE</TT
> returns
data that reflects all writes of other committed transactions as well
as writes of the current transaction. This is similar to the behavior
of <TT
CLASS="LITERAL"
>REPEATABLE READ</TT
> versus <TT
CLASS="LITERAL"
>READ COMMITTED</TT
> transaction
modes for ordinary SQL <TT
CLASS="COMMAND"
>SELECT</TT
> commands.
</P
><P
> An example:
</P><PRE
CLASS="PROGRAMLISTING"
>inv_fd = lo_open(conn, inv_oid, INV_READ|INV_WRITE);</PRE
><P>
</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="LO-WRITE"
>32.3.5. Writing Data to a Large Object</A
></H2
><P
> The function
</P><PRE
CLASS="SYNOPSIS"
>int lo_write(PGconn *conn, int fd, const char *buf, size_t len);</PRE
><P>
writes
<TT
CLASS="PARAMETER"
>len</TT
> bytes from <TT
CLASS="PARAMETER"
>buf</TT
>
to large object descriptor <TT
CLASS="PARAMETER"
>fd</TT
>. The <TT
CLASS="PARAMETER"
>fd</TT
>
argument must have been returned by a previous
<CODE
CLASS="FUNCTION"
>lo_open</CODE
>. The number of bytes actually
written is returned. In the event of an error, the return value
is negative.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="LO-READ"
>32.3.6. Reading Data from a Large Object</A
></H2
><P
> The function
</P><PRE
CLASS="SYNOPSIS"
>int lo_read(PGconn *conn, int fd, char *buf, size_t len);</PRE
><P>
reads
<TT
CLASS="PARAMETER"
>len</TT
> bytes from large object descriptor
<TT
CLASS="PARAMETER"
>fd</TT
> into <TT
CLASS="PARAMETER"
>buf</TT
>. The
<TT
CLASS="PARAMETER"
>fd</TT
> argument must have been returned by a
previous <CODE
CLASS="FUNCTION"
>lo_open</CODE
>. The number of bytes
actually read is returned. In the event of an error, the return
value is negative.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="LO-SEEK"
>32.3.7. Seeking in a Large Object</A
></H2
><P
> To change the current read or write location associated with a
large object descriptor, call
</P><PRE
CLASS="SYNOPSIS"
>int lo_lseek(PGconn *conn, int fd, int offset, int whence);</PRE
><P>
This function moves the
current location pointer for the large object descriptor identified by
<TT
CLASS="PARAMETER"
>fd</TT
> to the new location specified by
<TT
CLASS="PARAMETER"
>offset</TT
>. The valid values for <TT
CLASS="PARAMETER"
>whence</TT
>
are <TT
CLASS="SYMBOL"
>SEEK_SET</TT
> (seek from object start),
<TT
CLASS="SYMBOL"
>SEEK_CUR</TT
> (seek from current position), and
<TT
CLASS="SYMBOL"
>SEEK_END</TT
> (seek from object end). The return value is
the new location pointer, or -1 on error.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="LO-TELL"
>32.3.8. Obtaining the Seek Position of a Large Object</A
></H2
><P
> To obtain the current read or write location of a large object descriptor,
call
</P><PRE
CLASS="SYNOPSIS"
>int lo_tell(PGconn *conn, int fd);</PRE
><P>
If there is an error, the
return value is negative.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="LO-TRUNCATE"
>32.3.9. Truncating a Large Object</A
></H2
><P
> To truncate a large object to a given length, call
</P><PRE
CLASS="SYNOPSIS"
>int lo_truncate(PGcon *conn, int fd, size_t len);</PRE
><P>
truncates the large object
descriptor <TT
CLASS="PARAMETER"
>fd</TT
> to length <TT
CLASS="PARAMETER"
>len</TT
>. The
<TT
CLASS="PARAMETER"
>fd</TT
> argument must have been returned by a
previous <CODE
CLASS="FUNCTION"
>lo_open</CODE
>. If <TT
CLASS="PARAMETER"
>len</TT
> is
greater than the current large object length, the large object
is extended with null bytes ('\0').</P
><P
> The file offset is not changed.</P
><P
> On success <CODE
CLASS="FUNCTION"
>lo_truncate</CODE
> returns
zero. On error, the return value is negative.</P
><P
> <CODE
CLASS="FUNCTION"
>lo_truncate</CODE
> is new as of <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>
8.3; if this function is run against an older server version, it will
fail and return a negative value.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="LO-CLOSE"
>32.3.10. Closing a Large Object Descriptor</A
></H2
><P
> A large object descriptor can be closed by calling
</P><PRE
CLASS="SYNOPSIS"
>int lo_close(PGconn *conn, int fd);</PRE
><P>
where <TT
CLASS="PARAMETER"
>fd</TT
> is a
large object descriptor returned by <CODE
CLASS="FUNCTION"
>lo_open</CODE
>.
On success, <CODE
CLASS="FUNCTION"
>lo_close</CODE
> returns zero. On
error, the return value is negative.</P
><P
> Any large object descriptors that remain open at the end of a
transaction will be closed automatically.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="LO-UNLINK"
>32.3.11. Removing a Large Object</A
></H2
><P
> To remove a large object from the database, call
</P><PRE
CLASS="SYNOPSIS"
>int lo_unlink(PGconn *conn, Oid lobjId);</PRE
><P>
The
<TT
CLASS="PARAMETER"
>lobjId</TT
> argument specifies the OID of the
large object to remove. Returns 1 if successful, -1 on failure.
</P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="lo-implementation.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="lo-funcs.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Implementation Features</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="largeobjects.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Server-side Functions</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>