我想获得一个blob的字节大小。在PostgreSQL查询中获取大对象的大小?
我正在使用Postgresql并希望使用SQL查询获取大小。这样的事情:
SELECT sizeof(field) FROM table;
这是可能在Postgresql?
更新:我已阅读postgresql手册,并找不到合适的函数来计算文件大小。另外,blob被存储为一个大对象。
我想获得一个blob的字节大小。在PostgreSQL查询中获取大对象的大小?
我正在使用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, 0, 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 where relname LIKE 'lo%'
让自己开始。 C编程的模糊记忆以及其他模式需要的模式x'40000'::int
和SEEK_END = 2
值得研究!
尝试length()
或octet_length()
您可以在创建大对象时更改应用程序以存储大小。否则,你可以使用查询,如:
select sum(length(lo.data)) from pg_largeobject lo
where lo.loid=XXXXXX
您还可以使用大对象的API函数,如在以前的文章中提出,他们的工作好了,但幅度慢于选择方法上面建议的顺序。
select pg_column_size(lo_get(lo_oid)) from table;
以字节为单位给出您的大小。
如果你想漂亮的印刷:
select pg_size_pretty(pg_column_size(lo_get(lo_oid))::numeric) from table;
请张贴这样的问题之前阅读手册:http://www.postgresql.org/docs/current/static/functions.html – 2012-04-16 06:51:38
定义“BLOB数据”。一个bytea列?或者存储在'pg_largeobject'中的大对象?显示你的表格定义。 – 2012-04-16 06:55:55
的重复【获得lobject的大小在PostgreSQL的](http://stackoverflow.com/questions/9248738/obtaining-the-size-of-lobject-in-postgressql)? – 2012-04-17 01:19:24