0

我有多租户应用程序轨道,并试图为每个帐户获取模式 posgresql 的大小磁盘。最初,我在视图中进行了尝试,大致如下:

<% for account in @accounts %>
<td>
    <%= number_to_human_size(@schemasize = (ActiveRecord::Base.connection.select_rows(%q{select sum(pg_total_relation_size(table_schema || '.' || table_name)) from information_schema.tables where table_schema in ('} +account.schema+ %q{') group by table_schema}).to_s.gsub(/\D/, '').to_f + account.size_logo.to_f), :locale=> 'en') %>
</td>
<% end %>

浏览器输出

| schema  | usage  |
|_________|________|
| schema1 | 259 KB |
| schema2 | 294 KB |

它没有插入数据库(在公共模式上)。现在,我希望它存储在数据库中。

这是每个帐户的模式处理大小磁盘的控制器:

@accounts.each do |t|
     select_size = "select sum(pg_total_relation_size(table_schema || '.' || table_name)) from information_schema.tables where table_schema = " + t.schema.to_s + " group by table_schema"
     @a =  ActiveRecord::Base.connection.select_rows(select_size)  
     @b = @a.to_s.gsub(/\D/, '').to_f
     @c = t.size_logo.to_f
     @d = @b + @c
     @cek_schema = Usage.find_by_account_id(t.id)
     if @cek_schema.nil?
       Usage.create(:account_id => t.id, :size => @d)
     else
       @cek_schema.destroy
       Usage.create(:account_id => t.id, :size => @d)
     end
end

我收到一个错误

ActiveRecord::StatementInvalid in AccountsController#kapasitas

PG::Error: ERROR:  column "schema1" does not exist
LINE 1: ... from information_schema.tables where table_schema = schema1
                                                                ^
: select sum(pg_total_relation_size(table_schema || '.' || table_name)) from information_schema.tables where table_schema = schema1 group by table_schema

谁能告诉我是什么错误?

  • 获取模式的大小磁盘并存储在数据库中

帮我。

谢谢

4

1 回答 1

0

感谢@DondiMichaelStroma

t.schema.to_s 没有被引用。Pg 认为这是一个表名。

解决方案:在 t.schema.to_s 周围加上单引号

select_size = "select sum(pg_total_relation_size(table_schema || '.' || table_name)) from information_schema.tables where table_schema = '" + t.schema.to_s + "' group by table_schema"
于 2013-04-16T17:10:58.657 回答