F.16. dbms_lob — operate on large objects #
dbms_lob is a Postgres Pro extension that allows operating on LOBs: BLOB, CLOB, BFILE, and temporary LOBs. The extension can be used to access and manipulate specific parts of a LOB or complete LOBs. The functionality provided by this module overlaps substantially with the functionality of Oracle's DBMS_LOB package.
Note
Note that the dbms_lob extension depends on both pgpro_bfile and pgpro_sfile. You must install these extensions before creating dbms_lob, or you can install all dependencies automatically with:
CREATE EXTENSION dbms_lob CASCADE;
F.16.1. Installation #
The dbms_lob extension is a built-in extension included into Postgres Pro Enterprise. To enable dbms_lob, create the extension using the following query:
CREATE EXTENSION dbms_lob;
F.16.2. Data Types #
The dbms_lob extension works with several data types:
The
bfiletype is provided by pgpro_bfile.CREATE TYPE BFILE AS ( dir_id int, file_name text );Table F.8.
bfileParametersParameter Description dir_idThe ID of the directory where the bfileis stored.file_nameThe name of the file to read the bfilefrom.The
blobtype stores binary data and has the same interface as Oracle'sBLOB. It is provided by pgpro_sfile.CREATE TYPE dbms_lob.blob AS ( temp_data bytea, mime text, sf @extschema:pgpro_sfile@.sfile );Table F.9.
blobParametersParameter Description sfsfileobject on disk, which contains BLOB.temp_dataTemporary BLOB data stored in memory. mimeAuxiliary data defining the type of data stored in BLOB. The
clobtype is the equivalent of Oracle'sCLOBandNCLOB. Only the UTF-8 encoding is supported.CREATE TYPE CLOB AS ( t text, istemp bool, mime text );Table F.10.
clobParametersParameter Description ttextobject on disk, which stores data.istempDefines if the object is temporary. mimeAuxiliary data defining the type of data stored in CLOB.
F.16.3. Utility Functions #
-
bfilename(#dirnametext,filenametext) returnsbfile Creates a
bfileobject associated with a physical file in the file system. Heredirnameis the name of the directory object created withbfile_directory_create()where the filefilenameis located.-
empty_blob() returns#blob Creates an empty
blobobject, which contains ansfilewithout data. It can be populated with data using write functions.-
empty_clob() returns#clob Creates an empty
clobobject, which contains an empty string. It can be populated with data using write functions.-
to_blob(bbytea) returnsblob
to_blob(#fbfile,mime_typetext) returnsblob Converts a
byteaobject to ablob. If the original file isbfile, themimedata type can be specified.-
to_clob(ttext) returnsclob
to_clob(tvarchar) returnsclob
to_clob(#bbfile,intcsid,mimetext) returnsclob Converts text objects to
clobobjects. If the original file isbfile, its data is read and converted intoclob. Only the UTF-8 encoding is supported.-
to_raw(bblob) returnsclob
to_raw(#bbfile) returnsclob Copies the data from a
bloborbfilefile into abytea. Only the first GB of data is processed.
F.16.4. dbms_lob Functions and Procedures #
F.16.4.1. Opening and Closing LOBs #
-
open(file_locIN OUTbfile,open_modeINint)
open(lob_locIN OUTblob,open_modeINint)
open(#lob_locIN OUTclob,open_modeINint) open(opens abfile)bfileobject. Theopen_modeparameter specifies if the file is to be open in read/write or read-only mode. Forbfile, only read-only mode is supported (0). The functionsopen(andblob)open(do nothing and exist only for syntax compatibility.clob)-
isopen(file_locINbfile)
isopen(lob_locINblob)
isopen(#lob_locINclob) isopen(checks if abfile)bfileobject is open. Returns 1 if the LOB is open, otherwise 0. The functionsisopen(andblob)isopen(always return 1 and exist only for syntax compatibility.clob)-
close(file_locIN OUTbfile)
close(lob_locIN OUTblob)
close(#lob_locIN OUTclob) close(checks if abfile)bfileobject is open, and if it is, closes it. The functionsclose(andblob)close(do nothing and exist only for syntax compatibility.clob)-
createtemporary(lob_locIN OUTblob,cacheINbool,durINintdefault 10)
createtemporary(#lob_locIN OUTclob,cacheINbool,durINintdefault 10) Creates a temporary LOB, with
blobdata stored asbyteaandclobdata stored astext.-
freetemporary(lob_locIN OUTblob)
freetemporary(#lob_locIN OUTclob) Releases resources associated with the temporary LOB.
F.16.4.2. Reading LOBs #
-
getlength(file_locINbfile)
getlength(lob_locINblob)
getlength(#lob_locINclob) Returns the length of a
bloborbfilein bytes or aclobin characters.-
read(file_locINbfile,amountIN OUTint,offsetINint,bufferOUTbytea)
read(lob_locINblob,amountIN OUTint,offsetINint,bufferOUTbytea)
read(#lob_locINclob,amountIN OUTint,offsetINint,bufferOUTtext) Reads a piece of a LOB, and writes the specified amount of bytes (
blob/bfile) or characters (clob) into thebufferparameter, starting from an absoluteoffsetfrom the beginning of the LOB. Note thatoffset1 should be specified to read from the beginning.-
get_storage_limit(lob_locINblob)
get_storage_limit(#lob_locINclob) Returns the LOB storage limit for the specified LOB.
-
substr(file_locINbfile,amountINint,offsetINint)
substr(lob_locINblob,amountINint,offsetINint)
substr(#lob_locINclob,amountINint,offsetINint) Returns
amountof bytes (blob/bfile) or characters (clob) of a LOB, starting from an absoluteoffsetfrom the beginning of the LOB.-
instr(file_locINbfile,patternINint,offsetINbigintdefault 1,nthINbigintdefault 1)
instr(lob_locINblob,patternINint,offsetINbigintdefault 1,nthINbigintdefault 1)
instr(#lob_locINclob,patternINint,offsetINbigintdefault 1,nthINbigintdefault 1) Returns the matching position of the
nthoccurrence of thepatternin the LOB, starting from the specifiedoffset. Returns 0 if thepatternis not found. Only the first GB of data is searched.
F.16.4.3. Updating LOBs #
-
write(lob_locIN OUTblob,amountINint,offsetINbigint,bufferINbytea)
write(#lob_locIN OUTclob,amountINint,offsetINint,bufferINtext) Writes a specified
amountof data into the internal LOB, starting from the absoluteoffsetfrom the beginning of the LOB. The data is taken from thebuffer. If the specifiedoffsetexceeds the current size of the LOB, the value is padded with zero bytes (forblob) or spaces (forclob).If
bufferis longer thanamount, only the specified number of bytes (forblob) or characters (forclob) is written. This ensures that exactlyamountof data is written into the internal LOB.-
writeappend(lob_locIN OUTblob,amountINint,bufferINbytea)
writeappend(#lob_locIN OUTclob,amountINint,bufferINtext) Writes a specified
amountof data to the end of an internal LOB. The data is written from thebufferparameter.-
erase(lob_locIN OUTblob,amountIN OUTint,offsetINbigintdefault 1)
erase(#lob_locIN OUTclob,amountIN OUTint,offsetINintdefault 1) Erases an entire internal LOB or part of an internal LOB. When data is erased from the middle of a LOB, zero-byte fillers (temporary
blob) or spaces (clob) are written. Non-temporaryblobobjects can only be deleted as a whole.-
trim(lob_locIN OUTblob,newlenINbigint)
trim(#lob_locIN OUTclob,newlenINint) Trims the value of the internal LOB to the length you specify in the
newlenparameter. Specify the length in bytes for temporaryblob, and specify the length in characters forclob. For non-temporaryblob, works only if the new length is 0, which means erasing the object.
F.16.4.4. Operations with Multiple LOBs #
-
compare(lob_1INbfile,lob_2INbfile,amountINbigint,offset_1INbigintdefault 1,offset_2INbigintdefault 1) returns int
compare(lob_1INblob,lob_2INblob,amountINintdefault 1024*1024*1024-8,offset_1INbigintdefault 1,offset_2INbigintdefault 1) returns int
compare(#lob_1INclob,lob_2INclob,amountINintdefault (1024*1024*1024-8)/2,offset_1INintdefault 1,offset_2INintdefault 1) returns int Compares two entire LOBs or parts of two LOBs. You can only compare LOBs of the same datatype. For
bfileandblob, binary comparison is performed. Forclob, files are compared according to the current database collation.-
append(lob_1IN OUTblob,lob_2INblob)
append(#lob_1IN OUTclob,lob_2INclob) Appends the contents of a source internal LOB to a destination LOB. It appends the complete source LOB.
-
copy(dest_lobIN OUTblob,src_lobINblob,amountINbigint,dest_offsetINbigintdefault 1,src_offsetINbigintdefault 1) returnsint
copy(#dest_lobIN OUTclob,src_lobINclob,amountINint,dest_offsetINintdefault 1,src_offsetINintdefault 1) returnsint Copies all, or a part of, a source internal LOB to a destination internal LOB. You can specify the offsets for both the source and destination LOBs, and the number of bytes or characters to copy.
-
converttoblob(#dest_lobIN OUTblob,src_clobINclob,amountINint,dest_offsetIN OUTbigint,src_offsetIN OUTint,blob_csidINint,lang_contextIN OUTint,warningOUTint) Reads character data from a source
clob, converts the character data to the specified character set, writes the converted data to a destinationblobin binary format, and returns the new offsets. Only the UTF-8 encoding is supported.-
converttoclob(#dest_lobIN OUTclob,src_blobINblob,amountINint,dest_offsetIN OUTint,src_offsetIN OUTbigint,blob_csidINint,lang_contextIN OUTint,warningOUTint) Reads binary data from a source
blob, converts it into UTF-8 encoding, and writes the converted character data to a destinationclob.
F.16.4.5. Legacy API #
-
fileexists(#file_locINbfile) returnsint Finds out if a specified
bfilelocator points to a file that actually exists on the server file system. Implemented asbfile_fileexists.-
fileopen(#file_locIN OUTbfile,open_modeINint) returnsint Opens the specified
bfilefor read-only access. Implemented asbfile_open.-
fileisopen(#file_locINbfile) returnsint Finds out whether the specified
bfilewas opened.-
loadfromfile(#dest_lobIN OUTblob,src_bfileINbfile,amountINintdefault 1024*1024*1024-8,dest_offsetINbigintdefault 1,src_offsetINbigintdefault 1) Converts data from the specified
bfiletoblob.-
fileclose(#file_locIN OUTbfile) Closes the previously opened
bfile. Implemented asbfile_close.-
filecloseall()# Closes all
bfilefiles opened in the session. Implemented asbfile_close_all.-
filegetname(#file_locINbfile,dir_aliasOUTtext,filenameOUTtext) Determines the directory object and filename. This function only indicates the directory object name and filename assigned to the locator, not if the physical file or directory actually exists. Implemented as
bfile_directory_get_alias_by_id.
F.16.4.6. Other Operations #
-
loadblobfromfile(#dest_lobIN OUTblob,src_bfileINbfile,amountINintdefault 1024*1024*1024-8,dest_offsetINbigintdefault 1,src_offsetINbigintdefault 1) returnsint Synonym for
loadfromfile().-
loadclobfromfile(#dest_lobIN OUTclob,src_bfileINbfile,amountINint,dest_offsetIN OUTint,src_offsetIN OUTbigint,bfile_csidINint,lang_contextIN OUTint,warningOUTint) Loads data from a
bfileto an internalclob.-
setcontenttype(lob_locIN OUTblob,contenttypeINtext)
setcontenttype(#lob_locIN OUTclob,contenttypeINtext) Sets the content type string associated with the LOB.
-
getcontenttype(lob_locINblob) returnstext
getcontenttype(#lob_locINclob) returnstext Returns the content type string associated with the LOB.
-
getchunksize(lob_locINblob) returnstext
getchunksize(#lob_locINclob) returnstext Returns the amount of space used in the LOB chunk to store the LOB value.
F.16.5. Example #
DO $$ DECLARE cur_clob dbms_lob.clob; buffer text; amount int := 3000; BEGIN cur_clob := dbms_lob.empty_clob(); cur_clob.t := 'just some sample text'; raise notice 'clob length: %', dbms_lob.getlength(cur_clob); call dbms_lob.read(cur_clob, amount, 1, buffer); raise notice 'all clob read: %', buffer; amount := 6; call dbms_lob.read(cur_clob, amount, 4, buffer); raise notice 'clob read from 4 position for 6 symbols: %', buffer; raise notice 'storage limit: %', dbms_lob.get_storage_limit(cur_clob); raise notice 'clob substr from 6 position for 8 symbols: %', dbms_lob.substr(cur_clob, 8, 6); raise notice 'third postion of letter s in clob: %', dbms_lob.instr(cur_clob, 's', 1, 3); call dbms_lob.write(cur_clob, 6, 4, 'foobar'); raise notice 'new clob contents: %', cur_clob.t; call dbms_lob.write(cur_clob, 3, 25, 'baz'); raise notice 'new clob contents: %', cur_clob.t; call dbms_lob.writeappend(cur_clob, 4, 'test'); raise notice 'new clob contents: %', cur_clob.t; amount := 3; call dbms_lob.erase(cur_clob, amount, 2); raise notice 'amount of symbols deleted: %', amount; raise notice 'new clob contents: %', cur_clob.t; call dbms_lob.erase(cur_clob, amount, 30); raise notice 'amount of symbols deleted: %', amount; raise notice 'new clob contents: %', cur_clob.t; call dbms_lob.trim_(cur_clob, 22); raise notice 'new clob contents: %', cur_clob.t; END; $$; --output NOTICE: clob length: 21 NOTICE: all clob read: just some sample text NOTICE: clob read from 4 position for 6 symbols: t some NOTICE: storage limit: 536870908 NOTICE: clob substr from 6 position for 8 symbols: some sam NOTICE: third postion of letter s in clob: 11 NOTICE: new clob contents: jusfoobar sample text NOTICE: new clob contents: jusfoobar sample text baz NOTICE: new clob contents: jusfoobar sample text baztest NOTICE: amount of symbols deleted: 3 NOTICE: new clob contents: j oobar sample text baztest NOTICE: amount of symbols deleted: 2 NOTICE: new clob contents: j oobar sample text bazte NOTICE: new clob contents: j oobar sample text