postgresql - 尝试在 pgadmin4 中使用 pg_size_pretty 时出错

标签 postgresql pgadmin-4

我对 postgres sql 有点陌生,所以这可能很简单,但是在调用系统函数时我在 pgadmin4 中遇到错误:

    select c.relname,
           pg_size_pretty(t.table_name)
    from pg_class c
    inner join information_schema.tables t
        on c.relname = t.table_name
    where table_schema = 'public'

结果:

ERROR:  function pg_size_pretty(information_schema.sql_identifier) does not exist
LINE 2:     pg_size_pretty(t.table_name)
            ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 23

是否有某种特定的方法我需要完全限定函数名称,pgadmin4 文档没有太大帮助,并且大多数 postgres 文档假设这将按照上面写的那样工作...谢谢!

最佳答案

文档 Admin functions会告诉你问题:

pg_size_pretty ( bigint ) → text

pg_size_pretty ( numeric ) → text

Converts a size in bytes into a more easily human-readable format with size units (bytes, kB, MB, GB or TB as appropriate). Note that the units are powers of 2 rather than powers of 10, so 1kB is 1024 bytes, 1MB is 10242 = 1048576 bytes, and so on.

您需要处理 intnumeric 值,而不是表名称。

所以:

pg_table_size ( regclass ) → bigint

Computes the disk space used by the specified table, excluding indexes (but including its TOAST table if any, free space map, and visibility map).

这导致:

select c.relname,
           pg_size_pretty(pg_table_size(t.table_name::regclass))
    from pg_class c
    inner join information_schema.tables t
        on c.relname = t.table_name
    where table_schema = 'public'

regclass 用于正确 CAST information_schema.sql_identifier

如果您想在大小中包含索引,请使用:

pg_total_relation_size ( regclass ) → bigint

Computes the total disk space used by the specified table, including all indexes and TOAST data. The result is equivalent to pg_table_size + pg_indexes_size.

为了简化事情:

select 
    relname, pg_size_pretty(pg_table_size(relname::regclass))
from 
    pg_class c
where 
     relnamespace = 'public'::regnamespace;

然后您将从系统目录中获取所有信息,而不必加入information_schema.tables

关于postgresql - 尝试在 pgadmin4 中使用 pg_size_pretty 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71755734/

相关文章:

postgresql - elasticsearch ngram 和 postgresql trigram 搜索结果不匹配

postgresql - 如何在 PostgreSQL 中将子查询作为 bool 列反馈?

django - Django 项目的最低服务器要求

pgadmin4 执行 pgscript 选项不可用

sql - 如何在 postgresql 中创建角色组

postgresql - 将结果存储在数组中时为 "cannot assign non-composite value to a record variable "

postgresql - 将渲染数据库连接到 pgAdmin

postgresql - 如何在 pgadmin 4 中重命名当前数据库?

docker - 在 docker 文件中添加到 pgadmin 的 postgres 连接