lo-funcs
Server-side functions
PolarDB for Oracle provides server-side SQL functions for reading, writing, and managing large objects directly from SQL. The three primary functions form a complete create-write-read workflow around a single OID:
SQL-oriented large object functions
| Function | Description | Example |
|---|---|---|
lo_from_bytea ( loid oid, data bytea ) → oid | Creates a large object and stores data in it. If loid is zero, the system chooses a free OID; otherwise, that OID is used (with an error if a large object with that OID already exists). Returns the OID of the large object on success. | lo_from_bytea(0, '\xffffff00') → 24528 |
lo_put ( loid oid, offset bigint, data bytea ) → void | Writes data starting at the given offset within the large object. The large object is enlarged if necessary. | lo_put(24528, 1, '\xaa') → |
lo_get ( loid oid [, offset bigint, length integer ] ) → bytea | Extracts the large object's contents, or a substring starting at offset with at most length bytes. Omit offset and length to retrieve the full contents. | lo_get(24528, 0, 3) → \xffaaff |
The examples in the table share OID 24528: lo_from_bytea creates the large object and returns 24528, lo_put writes a byte at offset 1, and lo_get reads back 3 bytes from offset 0.
Additional server-side functions are callable via SQL commands: lo_creat, lo_create, lo_unlink, lo_import, and lo_export. The following examples show how to use them:
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 differently from their client-side counterparts. Server-side functions read and write files in the server's file system using the permissions of the database's owning user, and are restricted to superusers by default. Client-side import and export functions read and write files in the client's file system using the permissions of the client program, and require no database privileges beyond the privilege to read or write the large object itself.
It is possible to GRANT use of the server-side lo_import and lo_export functions to non-superusers, but this requires careful consideration of the security implications. A malicious user with such privileges could parlay them into becoming a superuser (for example, by rewriting server configuration files), or could attack the rest of the server's file system without obtaining full superuser privileges. Access to roles having such privilege must therefore be guarded just as carefully as access to superuser roles. If server-side lo_import or lo_export is needed for a routine task, a role with only those specific privileges is safer than one with full superuser privileges.
The lo_read and lo_write client-side functions also have server-side equivalents, but the server-side names have no underscores. Call them as loread and lowrite.