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

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

问题描述

我不知道为什么没有函数直接获得PostgreSQL大对象的大小。我相信人能求()对象的末尾,然后告诉()的位置,而不是它太贵了?我找不到关于它的谷歌的任何信息?那么,什么是获得lobject例如大小的正确方法如果你想以填补内容的大小 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选择总和(长(数据)),其中二倍体= the_oid ,但它需要读取访问pg_largeobject,我认为已经燮pressed在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

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

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