gpt4 book ai didi

postgresql - pg_relation_size 告诉我列不存在

转载 作者:行者123 更新时间:2023-12-01 23:44:46 25 4
gpt4 key购买 nike

http://www.postgresql.org/docs/8.4/static/functions-admin.html说:

pg_relation_size
accepts the OID or name of a table, index or toast table, and returns the size in bytes

但是,当我将它与有效的表名称一起使用时,我收到错误:

column [table] does not exist...

我知道我的 table 存在,因为这样做

SELECT count(*) FROM [table]

返回一个有效的数字。有什么想法吗?

最佳答案

尽管原因不同,但我遇到了相同的错误。 pg_relation_size 不区分大小写,因此如果您有小写以外的任何内容,它将无法开箱即用:

postgres=> SELECT pg_size_pretty(pg_total_relation_size('MyTable'));
ERROR: relation "mytable" does not exist
LINE 1: SELECT pg_size_pretty(pg_total_relation_size('mytable...
^
postgres=> SELECT pg_size_pretty(pg_total_relation_size('"MyTable"'));
pg_size_pretty
----------------
225 MB
(1 row)

因此,为了使其在 SELECT 语句中起作用,您需要将表名称括在引号中:

postgres=> SELECT relname, nspname, pg_size_pretty(pg_relation_size('"' || relname || '"')) 
FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast') AND pg_table_is_visible(c.oid)
ORDER BY c.relpages DESC;

关于postgresql - pg_relation_size 告诉我列不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1633688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com