Oracle DBMS_LOB.WRITEAPPEND to Postgres Conversion

人走茶凉 提交于 2019-12-20 05:45:26

问题


Can someone please let me know how to convert the below Oracle Code to Postgresql

IF prodNum = 1 THEN
          DBMS_LOB.WRITEAPPEND(pkgFilterNode, LENGTH(pkgFilter_tab || '<PackageFilters isNewFormat="Y" > '||l_crlf), pkgFilter_tab || '<PackageFilters isNewFormat="Y" > '||l_crlf);
END IF;

Appreciate your time!


回答1:


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.



来源:https://stackoverflow.com/questions/53164524/oracle-dbms-lob-writeappend-to-postgres-conversion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!