pg_relation_size告诉我列不存在

时间:2009-10-27 21:09:05

标签: postgresql

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

pg_relation_size

接受表,索引或toast表的OID或名称,并返回以字节为单位的大小

然而,当我使用有效的表名时,我收到错误:

[表格]不存在......”

我知道我的桌子存在,因为做了

SELECT count(*)FROM [table]

返回有效数字。

任何想法?

2 个答案:

答案 0 :(得分:1)

尝试明确地在pg_relation_size调用中添加表所在的模式(例如“public”)。

像这样(未经测试):

select pg_relation_size(public.mytablename) from pg_tables

答案 1 :(得分:1)

虽然原因不同但我得到了同样的错误。 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;