lo_get()

A function for extracting data from a large object

lo_get() is a system function for extracting all or parts of a large object.

lo_get() was added in PostgreSQL 9.4.

Usage

lo_get ( loid oid [, offset bigint, length integer ] ) → bytea

An error will be raised if an invalid large object OID is provided. An empty bytea value will be returned if the offset value is beyond the end of the large object.

Change history

Examples

Given the following large object:

postgres=# SELECT lo_from_bytea(0, '\x48656c6c6f20776f726c64210a');
 lo_from_bytea 
---------------
         16414

postgres=# SELECT *, encode(data, 'escape') FROM pg_largeobject WHERE loid = 16414;
 loid  | pageno |             data             |    encode    
-------+--------+------------------------------+--------------
 16414 |      0 | \x48656c6c6f20776f726c64210a | Hello world!+
       |     

lo_get() can be used like this:

postgres=# SELECT lo_get(16414);
            lo_get            
------------------------------
 \x48656c6c6f20776f726c64210a
(1 row)

postgres=# SELECT encode(lo_get(16414, 6, 5), 'escape');
 encode 
--------
 world
(1 row)

Providing an offset beyond the end of the large object:

postgres=# SELECT lo_get(16414, 999, 5);
 lo_get 
--------
 \x
(1 row)

The returned value will not be padded to the specified length if that exceeds the actual length of the large object:

postgres=# SELECT lo_get(16414, 0, 99);
            lo_get            
------------------------------
 \x48656c6c6f20776f726c64210a
(1 row)

Negative offsets and lengths are not permitted:

postgres=# SELECT lo_get(16414, -1, 5);
ERROR:  invalid large object seek target: -1

postgres=# SELECT lo_get(16414, 0, -1);
ERROR:  requested length cannot be negative

Providing the OID of a non-existent large object:

postgres=# SELECT lo_get(9999);
ERROR:  large object 9999 does not exist

Categories

Large object, System function