使用Rails和Postgres获取原子计数器(增量)的值

tbu*_*ehl 5 postgresql ruby-on-rails atomic sidekiq rails-activerecord

我需要以原子方式递增模型计数器并使用其新值(由Sidekiq作业处理).

目前,我使用

Group.increment_counter :tasks_count, @task.id
Run Code Online (Sandbox Code Playgroud)

在我的模型中,原子地递增计数器.

但是,如果计数器具有例如值,我还需要其新值来发送通知50.有任何想法吗?锁定桌子/行还是有更简单的方法?

编辑/解决方案

基于mu太短的答案和Rails的update_counters方法,我实现了一个实例方法(用PostgreSQL测试).

def self.increment_counter_and_return_value(counter_name, id)
  quoted_column = connection.quote_column_name(counter_name)
  quoted_table = connection.quote_table_name(table_name)
  quoted_primary_key = connection.quote_column_name(primary_key)
  quoted_primary_key_value = connection.quote(id)

  sql = "UPDATE #{quoted_table} SET #{quoted_column} = COALESCE(#{quoted_column}, 0) + 1 WHERE #{quoted_table}.#{quoted_primary_key} = #{quoted_primary_key_value} RETURNING #{quoted_column}"
  connection.select_value(sql).to_i
end
Run Code Online (Sandbox Code Playgroud)

使用它像:

Group.increment_counter_and_return_value(:tasks_count, @task.id)
Run Code Online (Sandbox Code Playgroud)

它用于RETURNING在同一查询中获取新值.

mu *_*ort 5

您的Group.increment_counter调用将这样的SQL发送到数据库:

update groups
set tasks_count = coalesce(tasks_counter, 0) + 1
where id = X
Run Code Online (Sandbox Code Playgroud)

哪里X@task.id.获取新tasks_counter值的SQL方法是包含RETURNING子句:

update groups
set tasks_count = coalesce(tasks_counter, 0) + 1
where id = X
returning tasks_count
Run Code Online (Sandbox Code Playgroud)

我不知道有什么方便的Railsy方法来获取SQL到数据库.通常的Rails方法是要么做一堆锁定和重新加载,@task要么跳过锁定并希望最好:

Group.increment_counter :tasks_count, @task.id
@task.reload
# and now look at @task.tasks_count to get the new value
Run Code Online (Sandbox Code Playgroud)

你可以像这样使用RETURNING:

new_count = Group.connection.execute(%Q{
    update groups
    set tasks_count = coalesce(tasks_counter, 0) + 1
    where id = #{Group.connection.quote(@task.id)}
    returning tasks_count
}).first['tasks_count'].to_i
Run Code Online (Sandbox Code Playgroud)

你可能想隐藏一个方法背后的混乱,Group这样你就可以说:

n = Group.increment_tasks_count_for(@task)
# or
n = @task.increment_tasks_count
Run Code Online (Sandbox Code Playgroud)