lo_put()

A function for writing data to a large object

lo_put() is a system function for writing data to a large object.

lo_put() was added in PostgreSQL 9.4.

Usage

lo_put ( loid oid, offset bigint, data bytea ) → void

Data will be added at the given offset within the large object; the large object will be enlarged if necessary.

Change history

Examples

Given the following large object:

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

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

lo_put() can be used like this:

postgres=# SELECT lo_put(16394, 6, '\x4561727468');
 lo_put 
--------
 
(1 row)

postgres=# SELECT *, encode(data, 'escape') FROM pg_largeobject WHERE loid = 16394;
 loid  | pageno |             data             |    encode    
-------+--------+------------------------------+--------------
 16394 |      0 | \x48656c6c6f204561727468210a | Hello Earth!+
       |        |                              | 
(1 row)

The large object will be enlarged if required:

postgres=# SELECT lo_put(16394, 13, '\x54616b65206d6520746f20796f7572206c656164657221');
 lo_put 
--------
 
(1 row)

postgres=# SELECT loid, pageno, encode(data, 'escape') FROM pg_largeobject WHERE loid = 16394;
 loid  | pageno |         encode          
-------+--------+-------------------------
 16394 |      0 | Hello world!           +
       |        | Take me to your leader!
(1 row)

If the provided offset value is beyond the end of the current data, the intermediate space will be padded with NUL bytes (\000):

postgres=# SELECT lo_put(16394, 40, '\x424545502042454550');
 lo_put 
--------
 
(1 row)

Time: 1.262 ms
postgres=# SELECT loid, pageno, encode(data, 'escape') FROM pg_largeobject WHERE loid = 16394;
 loid  | pageno |                      encode                      
-------+--------+--------------------------------------------------
 16394 |      0 | Hello world!                                    +
       |        | Take me to your leader!\000\000\000\000BEEP BEEP
(1 row)

Categories

Large object, System function

See also

lo_get(), lo_from_bytea()