Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove FOR UPDATE SKIP LOCKED from job locking sql statement #288

Merged
merged 1 commit into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions lib/good_job/lockable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,9 @@ module Lockable
end

composed_cte = Arel::Nodes::As.new(cte_table, Arel::Nodes::SqlLiteral.new([cte_type, "(", cte_query.to_sql, ")"].join(' ')))

# In addition to an advisory lock, there is also a FOR UPDATE SKIP LOCKED
# because this causes the query to skip jobs that were completed (and deleted)
# by another session in the time since the table snapshot was taken.
# In rare cases under high concurrency levels, leaving this out can result in double executions.
query = cte_table.project(cte_table[:id])
.with(composed_cte)
.where(Arel.sql(sanitize_sql_for_conditions(["#{function}(('x' || substr(md5(:table_name || #{connection.quote_table_name(cte_table.name)}.#{connection.quote_column_name(column)}::text), 1, 16))::bit(64)::bigint)", { table_name: table_name }])))
.lock(Arel.sql("FOR UPDATE SKIP LOCKED"))

limit = original_query.arel.ast.limit
query.limit = limit.value if limit.present?
Expand Down
2 changes: 0 additions & 2 deletions spec/lib/good_job/lockable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
FROM "rows"
WHERE pg_try_advisory_lock(('x' || substr(md5('good_jobs' || "rows"."id"::text), 1, 16))::bit(64)::bigint)
LIMIT 2
FOR UPDATE SKIP LOCKED
)
ORDER BY "good_jobs"."priority" DESC
SQL
Expand All @@ -58,7 +57,6 @@
FROM "rows"
WHERE pg_try_advisory_lock(('x' || substr(md5('good_jobs' || "rows"."queue_name"::text), 1, 16))::bit(64)::bigint)
LIMIT 2
FOR UPDATE SKIP LOCKED
)
ORDER BY "good_jobs"."priority" DESC
SQL
Expand Down