Obtaining the size of lobject in PostgreSQL

前端 未结 1 1303
谎友^
谎友^ 2021-01-16 13:19

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()

1条回答
  •  一向
    一向 (楼主)
    2021-01-16 14:09

    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'; 
    

    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

    0 讨论(0)
提交回复
热议问题