【问题标题】:Disk Size Of Schema PostgreSQL on RailsSchema PostgreSQL on Rails 的磁盘大小
【发布时间】:2013-04-06 19:53:55
【问题描述】:

我有多租户应用程序轨道,并尝试为每个帐户获取架构 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

谁能告诉我是什么错误?

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

帮帮我。

谢谢

【问题讨论】:

  • t.schema.to_s 没有被引用。 Pg 认为这是一个表名。用一个 ?占位符或在其周围加上单引号。

标签: ruby-on-rails postgresql activerecord ruby-on-rails-3.2


【解决方案1】:

感谢@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"

【讨论】:

    猜你喜欢
    • 2012-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 2013-02-17
    相关资源
    最近更新 更多