在PostgreSQL中获取lobject的大小 [英] Obtaining the size of lobject in PostgreSQL

查看:143
本文介绍了在PostgreSQL中获取lobject的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么没有函数可以直接获取PostgreSQL中大对象的大小。我相信可以 seek()对象的末尾,然后 tell()的位置,但不是吗太贵了吗我在Google中找不到有关它的任何信息吗?那么获得lobject大小的正确方法是什么?如果要填充 Content-Size http标头?

I wonder why is there no function to directly obtain the size of large object in PostgreSQL. I believe one can seek() the end of object and then tell() the position, but isn't it too expensive? I can't find any info about it in google? So what is the proper way to obtain the size of lobject e.g. if you want to fill the Content-Size http header?

推荐答案

此功能对我来说足够有效,您可能需要对数据进行尝试:

This function has been efficient enough for me, you may want to try it with you data:

CREATE OR REPLACE FUNCTION lo_size(oid) RETURNS integer 
AS $$ 
declare 
 fd integer; 
 sz integer; 
begin 
 fd = lo_open($1, 262144);
 if (fd<0) then
   raise exception 'Failed to open large object %', $1;
 end if;
 sz=lo_lseek(fd,0,2);
 if (lo_close(fd)!=0) then
   raise exception 'Failed to close large object %', $1;
 end if;
 return sz;
end; 
$$ LANGUAGE 'plpgsql'; 

另一个选项是从pg_largeobject中选择sum(length(data)),其中倍体= the_oid ,但是它需要对pg_largeobject的读访问权限,我认为在pg 9.0+中,对于非超级用户,该访问权限已被禁止

Another option is select sum(length(data)) from pg_largeobject where loid=the_oid but it requires read access to pg_largeobject which I think has been suppressed in pg 9.0+ for non-superusers

这篇关于在PostgreSQL中获取lobject的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆