How to use SQL group clause with FOR UPDATE SKIP LOCKED in Rails?
NickName:kskickislk Ask DateTime:2022-10-16T13:01:37

How to use SQL group clause with FOR UPDATE SKIP LOCKED in Rails?

How to use SQL group clause with FOR UPDATE SKIP LOCKED in Rails? I'd prefer to use ActiveRecord as opposed to raw queries. I have a generic model called Job, which has an attribute called client_class, which I'd like to group by. I also need to remove clients who exceed the limits set in the class variable CLIENT_LIMITS. My setup is as follows:

CLIENT_LIMITS = { CLIENT_1: 2, CLIENT_2: 3, ... }.freeze

unavailable_clients = Job.running
  .group(:client_class)
  .count
  .select { |k, v| v >= CLIENT_LIMITS[k] }
  .keys

job = Job.lock("FOR UPDATE SKIP LOCKED")
  .queued
  .where.not(client_class: unavailable_clients)
  .sample

I get the following error:

.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/postgresql_adapter.rb:672:in `exec_params': PG::FeatureNotSupported: ERROR:  FOR UPDATE is not allowed with GROUP BY clause (ActiveRecord::StatementInvalid)

.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/postgresql_adapter.rb:672:in `exec_params': ERROR:  FOR UPDATE is not allowed with GROUP BY clause (PG::FeatureNotSupported)

Copyright Notice:Content Author:「kskickislk」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/74084714/how-to-use-sql-group-clause-with-for-update-skip-locked-in-rails

More about “How to use SQL group clause with FOR UPDATE SKIP LOCKED in Rails?” related questions

How to use SQL group clause with FOR UPDATE SKIP LOCKED in Rails?

How to use SQL group clause with FOR UPDATE SKIP LOCKED in Rails? I'd prefer to use ActiveRecord as opposed to raw queries. I have a generic model called Job, which has an attribute called client_c...

Show Detail

update skip locked

I have two SQL queries that I would like to combine into 1. The first one selects rows for the update and skips those that are locked. The second one updates the rows selected in the first quer...

Show Detail

Select for update skip locked with row limit

I have a program written in Java that creates 5 threads which select data from Oracle. The select is like this: select * from queue_requests where request_status = 0 and date_requested <= sysdat...

Show Detail

Execute Oracle 'for update skip locked' clause with Tomcat DataSource not working

I am working on a spring application which select rows from an Oracle8i DB in locked mode. Am using Tomcat7.0.57. I have configure my data source in context.xml as below. <Resource name="jdbc/

Show Detail

How to work around Restriction on Distinct in for update clause

select distinct t._id from terra.wl_log t where t.in_queue = 'Y' for update skip locked " Restrictions on the FOR UPDATE Clause You cannot specify this clause with the follow...

Show Detail

Oracle Select For Update Skip Locked not locking data

We have a main table that will be access by a few concurrent thread trying to grab data from the table. We are thinking that using SELECT FOR UPDATE SKIP LOCKED would be beneficial in this scenario...

Show Detail

Select for update for skip locked is not working

I have a use case where multiple instances read data from DB and call an external API. Now I have to make sure that each instance read unique data. For that, I am using select for the update using ...

Show Detail

postgres SKIP LOCKED not working

Below the steps I followed to test the SKIP LOCKED: open one sql console of some Postgres UI client Connect to Postgres DB execute the queries CREATE TABLE t_demo AS SELECT * FROM ...

Show Detail

Is there any alternative of skip locked for update in oracle

I have 5 rows in table. And some of the rows are locked in some sessions . I don't want to generate any error, Just want to wait until any row will become free for further processing I tired with ...

Show Detail

SKIP LOCKED unless all rows are locked

Is there a way to do a query with PostgreSQL using SKIP LOCKED to avoid getting a row already locked but in the meantime if everything is already locked to wait for the first unlocked row ? In my...

Show Detail