从pg_size_pretty恢复值,转换为bigint

时间:2015-12-21 12:42:17

标签: sql postgresql

如何恢复到PostgreSQL中使用pg_size_pretty生成的原始值。

SELECT
 c.relname
,pg_size_pretty (pg_relation_size (c.oid))
FROM pg_class c
ORDER BY c.relpages DESC
LIMIT 1;

RESULT:
relname | pg_size_pretty
------------------------
tabla   | 928 MB

我需要:928 MB > 973078528

2 个答案:

答案 0 :(得分:1)

我不认为这有任何内置功能;你必须做很长的事情:

select split_part(pg_size_pretty, ' ', 1)::bigint *
  case split_part(pg_size_pretty, ' ', 2)
    when 'bytes' then 1
    when 'kB' then 1024
    when 'MB' then 1024*1024
    when 'GB' then 1024*1024*1024
    when 'TB' then 1024*1024*1024*1024::bigint
  end

答案 1 :(得分:0)

此功能的内置函数为pg_size_bytes(text),从9.6开始可用。

postgres=# --
select
    pg_size_pretty(973078528::bigint),
    pg_size_bytes('928 MB'),
    (pg_size_bytes('928 MB') > 973078528)::text as "is_bigger"
;
 pg_size_pretty | pg_size_bytes | is_bigger
----------------+---------------+-----------
 928 MB         |     973078528 | false
(1 row)

postgres=#

Try it on db<>fiddle