在PostgreSQL中获取lobject的大小

zef*_*ciu 1 database api postgresql

我想知道为什么在PostgreSQL中没有直接获取大对象大小的函数.我相信一个人可以seek()结束对象,然后tell()是位置,但是不是太贵了吗?我在谷歌找不到任何关于它的信息?那么获取lobject大小的正确方法是什么,例如,如果你想填充Content-Sizehttp标题?

Dan*_*ité 7

这个功能对我来说足够有效,你可能想和你一起试试数据:

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'; 
Run Code Online (Sandbox Code Playgroud)

另一种选择是,select sum(length(data)) from pg_largeobject where loid=the_oid但它需要对pg_largeobject的读访问权限,我认为在非超级用户的pg 9.0+中已被抑制

  • 我还要证明,使用length(data)方法比查找和告知要慢得多。至少在我的8.4上,在370条记录上,使用sum()-30秒,使用open / seek-1秒。谢谢你,丹尼尔。 (2认同)