微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我想获得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值的一些研究是其余的需要!

原文地址:https://www.jb51.cc/postgresql/192843.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐