加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在PostgreSQL查询中获取大对象的大小?

发布时间:2020-12-13 16:34:50 所属栏目:百科 来源:网络整理
导读:我想获得blob的字节大小. 我正在使用Postgresql,并希望使用SQL查询获取大小.像这样的东西: SELECT sizeof(field) FROM table; 这在Postgresql中可行吗? 更新:我已阅读postgresql手册,但找不到合适的函数来计算文件大小.此外,blob存储为大对象. 不是说我使
我想获得blob的字节大小.

我正在使用Postgresql,并希望使用SQL查询获取大小.像这样的东西:

SELECT sizeof(field) FROM table;

这在Postgresql中可行吗?

更新:我已阅读postgresql手册,但找不到合适的函数来计算文件大小.此外,blob存储为大对象.

不是说我使用过大型对象,而是查看文档: http://www.postgresql.org/docs/current/interactive/lo-interfaces.html#LO-TELL

我认为你必须使用与某些文件系统API要求相同的技术:寻找到最后,然后告诉位置. PostgreSQL具有似乎包含内部C函数的SQL函数.我找不到太多文档,但这有效:

CREATE OR REPLACE FUNCTION get_lo_size(oid) RETURNS bigint
VOLATILE STRICT
LANGUAGE 'plpgsql'
AS $$
DECLARE
    fd integer;
    sz bigint;
BEGIN
    -- Open the LO; N.B. it needs to be in a transaction otherwise it will close immediately.
    -- Luckily a function invocation makes its own transaction if necessary.
    -- The mode x'40000'::int corresponds to the PostgreSQL LO mode INV_READ = 0x40000.
    fd := lo_open($1,x'40000'::int);
    -- Seek to the end.  2 = SEEK_END.
    PERFORM lo_lseek(fd,2);
    -- Fetch the current file position; since we're at the end,this is the size.
    sz := lo_tell(fd);
    -- Remember to close it,since the function may be called as part of a larger transaction.
    PERFORM lo_close(fd);
    -- Return the size.
    RETURN sz;
END;
$$;

测试它:

-- Make a new LO,returns an OID e.g. 1234567
SELECT lo_create(0);

-- Populate it with data somehow
...

-- Get the length.
SELECT get_lo_size(1234567);

似乎LO功能主要是通过客户端或通过低级服务器编程来使用,但至少它们为它提供了一些SQL可见功能,这使得上述功能成为可能.我对SELECT relname FROM pg_proc进行了查询,其中relname LIKE’lo%’让我自己开始.对于C编程的模糊记忆以及对模式x’40000′:: int和SEEK_END = 2值的一些研究是其余的需要!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读