PostgreSQL:从 OID 到 Bytea
我们决定放弃 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 OID
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
另一种不需要自定义函数的方法是使用
loread(lo_open(...))
组合,例如:此代码有问题,
loread
函数需要读取的最大字节数作为第二个参数(我上面使用的1000000
参数),因此如果您的数据很大,您应该在此处使用一个非常大的数字。否则,内容将在这么多字节后被修剪,并且您将无法将所有数据放回到bytea
字段中。如果要将 OID 转换为文本字段,还应该使用转换函数,如下所示:(
262144
是打开模式的标志,40000
为十六进制,这意味着“以只读方式打开”)Another way which doesn't require a custom function is to use the
loread(lo_open(...))
combination, like:There is a problem with this code, the
loread
function requires as the second parameter the maximum number of bytes to read (the1000000
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 thebytea
field.If you want to convert from OID to a text field, you should also use a conversion function, as in:
(
262144
is a flag for the open mode,40000
in hexa, which means "open read-only")这是一个神奇的存储过程:
Here is a stored procedure that does the magic:
好吧,我做了这样的事情。我有附件表和内容列,其中包含 oid 类型的数据。我通过四个操作进行了迁移:
well, i did something like this. I have attachment table and content column with data in oid type. I migrated with four actions:
您需要类似
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 anarray_to_bytea(largeobjectarray)
to concat all sections. You have to create this function yourself, or handle this in application logic.