Can someone please let me know how to convert the below Oracle Code to Postgresql
IF prodNum = 1 THEN
DBMS_LOB.WRITEAPPEND(pkgFilterNode, LENGTH(pkgFil
It depends on a size of your large object. When your large objects are less about 500MB, then you don't need to use LOBs (PostgreSQL uses term LO), and you can use a text
or varchar
type - the work is similar like with varchar
. After this size you should to use LO API.
CREATE OR REPLACE FUNCTION writeappend(oid, text)
RETURNS void AS $$
DECLARE
content bytea;
fd int;
BEGIN
content := convert_to($2, getdatabaseencoding());
fd := lo_open($1, 131072);
PERFORM lo_lseek(fd, 0, 2);
IF length(content) <> lowrite(fd, content) THEN
RAISE EXCEPTION 'not all content was written';
END IF;
PERFORM lo_close(fd);
END;
$$ LANGUAGE plpgsql;
postgres=> select lo_creat(-1);
┌──────────┐
│ lo_creat │
╞══════════╡
│ 20653 │
└──────────┘
(1 row)
postgres=> select writeappend(20653, e'Hello\r\n');
┌─────────────┐
│ writeappend │
╞═════════════╡
│ │
└─────────────┘
(1 row)
postgres=> select writeappend(20653, e'Hello\r\n');
...
postgres=> select convert_from(lo_get(20653),getdatabaseencoding());
┌──────────────┐
│ convert_from │
╞══════════════╡
│ Hello\r ↵│
│ Hello\r ↵│
│ │
└──────────────┘
(1 row)
So you can use LO API, but basic types should be preferred. The limits for these types is usually good enough - and work with basic types are much more comfortable - with some possibilities like fulltext.