> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/client-interfaces/largeobjects/lo-funcs.md).

# 33.4. Server-Side Functions \#

Server-side functions tailored for manipulating large objects from SQL are listed in [Table 33.1](#LO-FUNCS-TABLE).

**Table 33.1. SQL-Oriented Large Object Functions**

| <p>Function</p><p>Description</p><p>Example(s)</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>lo\_from\_bytea</code> ( <em><code>loid</code></em> <code>oid</code>, <em><code>data</code></em> <code>bytea</code> ) → <code>oid</code></p><p>Creates a large object and stores <em><code>data</code></em> in it. If <em><code>loid</code></em> is zero then the system will choose a free OID, otherwise that OID is used (with an error if some large object already has that OID). On success, the large object's OID is returned.</p><p><code>lo\_from\_bytea(0, '\xffffff00')</code> → <code>24528</code></p> |
| <p><code>lo\_put</code> ( <em><code>loid</code></em> <code>oid</code>, <em><code>offset</code></em> <code>bigint</code>, <em><code>data</code></em> <code>bytea</code> ) → <code>void</code></p><p>Writes <em><code>data</code></em> starting at the given offset within the large object; the large object is enlarged if necessary.</p><p><code>lo\_put(24528, 1, '\xaa')</code> →</p>                                                                                                                                     |
| <p><code>lo\_get</code> ( <em><code>loid</code></em> <code>oid</code> \[, <em><code>offset</code></em> <code>bigint</code>, <em><code>length</code></em> <code>integer</code> ] ) → <code>bytea</code></p><p>Extracts the large object's contents, or a substring thereof.</p><p><code>lo\_get(24528, 0, 3)</code> → <code>\xffaaff</code></p>                                                                                                                                                                               |

<br>

There are additional server-side functions corresponding to each of the client-side functions described earlier; indeed, for the most part the client-side functions are simply interfaces to the equivalent server-side functions. The ones just as convenient to call via SQL commands are `lo_creat`, `lo_create`, `lo_unlink`, `lo_import`, and `lo_export`. Here are examples of their use:

```

CREATE TABLE image (
    name            text,
    raster          oid
);

SELECT lo_creat(-1);       -- returns OID of new, empty large object

SELECT lo_create(43213);   -- attempts to create large object with OID 43213

SELECT lo_unlink(173454);  -- deletes large object with OID 173454

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

INSERT INTO image (name, raster)  -- same as above, but specify OID to use
    VALUES ('beautiful image', lo_import('/etc/motd', 68583));

SELECT lo_export(image.raster, '/tmp/motd') FROM image
    WHERE name = 'beautiful image';
```

The server-side `lo_import` and `lo_export` functions behave considerably differently from their client-side analogs. These two functions read and write files in the server's file system, using the permissions of the database's owning user. Therefore, by default their use is restricted to superusers. In contrast, the client-side import and export functions read and write files in the client's file system, using the permissions of the client program. The client-side functions do not require any database privileges, except the privilege to read or write the large object in question.

## Caution

It is possible to [GRANT](/reference/sql-commands/sql-grant.md) use of the server-side `lo_import` and `lo_export` functions to non-superusers, but careful consideration of the security implications is required. A malicious user of such privileges could easily parlay them into becoming superuser (for example by rewriting server configuration files), or could attack the rest of the server's file system without bothering to obtain database superuser privileges as such. *Access to roles having such privilege must therefore be guarded just as carefully as access to superuser roles.* Nonetheless, if use of server-side `lo_import` or `lo_export` is needed for some routine task, it's safer to use a role with such privileges than one with full superuser privileges, as that helps to reduce the risk of damage from accidental errors.

The functionality of `lo_read` and `lo_write` is also available via server-side calls, but the names of the server-side functions differ from the client side interfaces in that they do not contain underscores. You must call these functions as `loread` and `lowrite`.

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/lo-funcs.html)（英文原文，待翻譯）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.postgresql.tw/client-interfaces/largeobjects/lo-funcs.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
