PostgreSQL:从 OID 到 Bytea

发布于 2024-10-18 01:40:16 字数 517 浏览 2 评论 0原文

我们决定放弃 PostgreSQL 9.0 数据库中的 OID 并使用 bytea 列。我正在尝试将数据从一列复制到另一列,但我无法找出正确的查询。这是我最接近的:

update user as thistable set pkcs_as_bytea = (select array_agg(mylargeobject.data) from 
  (select * from pg_largeobject where loid = thistable.pkcs12_as_oid order by pageno) as mylargeobject) where thistable.pkcs12 is not null

这给了我以下错误消息:

ERROR:  column "pkcs_as_bytea" is of type bytea but expression is of type bytea[]

那么正确的查询是什么?

We have decided to move from OIDs in our PostgreSQL 9.0 database and use bytea columns instead. I'm trying to copy the data from one column to the other, but I can't figure out the right query. This is the closest I've gotten to:

update user as thistable set pkcs_as_bytea = (select array_agg(mylargeobject.data) from 
  (select * from pg_largeobject where loid = thistable.pkcs12_as_oid order by pageno) as mylargeobject) where thistable.pkcs12 is not null

And that gives me the following error message:

ERROR:  column "pkcs_as_bytea" is of type bytea but expression is of type bytea[]

What would be the right query then?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦情居士 2024-10-25 01:40:16

另一种不需要自定义函数的方法是使用 loread(lo_open(...)) 组合,例如:

UPDATE user SET pkcs_as_bytea = loread(lo_open(pkcs12_as_oid, 262144), 1000000) WHERE thistable.pkcs12 IS NOT NULL

此代码有问题,loread 函数需要读取的最大字节数作为第二个参数(我上面使用的 1000000 参数),因此如果您的数据很大,您应该在此处使用一个非常大的数字。否则,内容将在这么多字节后被修剪,并且您将无法将所有数据放回到 bytea 字段中。

如果要将 OID 转换为文本字段,还应该使用转换函数,如下所示:(

UPDATE user SET pkcs_as_text = convert_from(loread(lo_open(pkcs12_as_oid, 262144), 1000000), 'UTF8')

262144 是打开模式的标志,40000 为十六进制,这意味着“以只读方式打开”)

Another way which doesn't require a custom function is to use the loread(lo_open(...)) combination, like:

UPDATE user SET pkcs_as_bytea = loread(lo_open(pkcs12_as_oid, 262144), 1000000) WHERE thistable.pkcs12 IS NOT NULL

There is a problem with this code, the loread function requires as the second parameter the maximum number of bytes to read (the 1000000 parameter I used above), so you should use a really big number here if your data is big. Otherwise, the content will be trimmed after this many bytes, and you won't get all the data back into the bytea field.

If you want to convert from OID to a text field, you should also use a conversion function, as in:

UPDATE user SET pkcs_as_text = convert_from(loread(lo_open(pkcs12_as_oid, 262144), 1000000), 'UTF8')

(262144 is a flag for the open mode, 40000 in hexa, which means "open read-only")

相思碎 2024-10-25 01:40:16

这是一个神奇的存储过程:

CREATE OR REPLACE FUNCTION merge_oid(val oid) 
returns bytea as $
declare merged bytea;
declare arr bytea;
 BEGIN  
   FOR arr IN SELECT data from pg_largeobject WHERE loid = val ORDER BY pageno LOOP
     IF merged IS NULL THEN
       merged := arr;
     ELSE
       merged := merged || arr;
     END IF;
   END LOOP;
  RETURN merged;

END  
$ LANGUAGE plpgsql;

Here is a stored procedure that does the magic:

CREATE OR REPLACE FUNCTION merge_oid(val oid) 
returns bytea as $
declare merged bytea;
declare arr bytea;
 BEGIN  
   FOR arr IN SELECT data from pg_largeobject WHERE loid = val ORDER BY pageno LOOP
     IF merged IS NULL THEN
       merged := arr;
     ELSE
       merged := merged || arr;
     END IF;
   END LOOP;
  RETURN merged;

END  
$ LANGUAGE plpgsql;
等待圉鍢 2024-10-25 01:40:16

好吧,我做了这样的事情。我有附件表和内容列,其中包含 oid 类型的数据。我通过四个操作进行了迁移:

ALTER TABLE attachment add column content_bytea bytea
UPDATE attachment SET content_bytea = lo_get(content)
ALTER TABLE attachment drop column content
ALTER TABLE attachment rename column content_bytea to content

well, i did something like this. I have attachment table and content column with data in oid type. I migrated with four actions:

ALTER TABLE attachment add column content_bytea bytea
UPDATE attachment SET content_bytea = lo_get(content)
ALTER TABLE attachment drop column content
ALTER TABLE attachment rename column content_bytea to content
回眸一遍 2024-10-25 01:40:16

您需要类似 array_to_string(anyarray, text) 的文本数组,但在本例中需要 array_to_bytea(largeobjectarray) 来连接所有部分。您必须自己创建此函数,或在应用程序逻辑中处理此函数。

You need something like array_to_string(anyarray, text) for text arrays, but in this case an array_to_bytea(largeobjectarray) to concat all sections. You have to create this function yourself, or handle this in application logic.

成熟的代价 2024-10-25 01:40:16
This is what you can do.

--table thistable --
ALTER TABLE thistable add column se_signed_bytea bytea;
UPDATE thistable SET se_signed_bytea = lo_get(pkcs_as_bytea);
ALTER TABLE thistable drop column pkc`enter code here`s_as_bytea;
ALTER TABLE thistable rename column se_signed_bytea to pkcs_as_bytea;
This is what you can do.

--table thistable --
ALTER TABLE thistable add column se_signed_bytea bytea;
UPDATE thistable SET se_signed_bytea = lo_get(pkcs_as_bytea);
ALTER TABLE thistable drop column pkc`enter code here`s_as_bytea;
ALTER TABLE thistable rename column se_signed_bytea to pkcs_as_bytea;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文