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

query.empty() shouldn't add LIMIT 1 if query is marked as forUpdate() #684

Closed
abishai opened this issue Nov 10, 2019 · 0 comments
Closed
Labels

Comments

@abishai
Copy link

abishai commented Nov 10, 2019

query.empty() check adds LIMIT 1 to query, so information about row locking is lost (only 1 pseudo-random row will be locked).
For example,

            val query = Securities.select { Securities.lot eq 2 }.forUpdate()
            if (!query.empty()) {
                Securities.update({ query.where!! }) {
                    it[lot] = 1
                }
            }

produces

SELECT securities.id, securities."class", securities.ticker, securities.lot, securities.up_limit, securities.low_limit, securities.step FROM securities WHERE securities.lot = 2 LIMIT 1 FOR UPDATE
UPDATE securities SET lot=1 WHERE securities.lot = 2

So, we can expect that server will lock only 1 row, while it is possible that query affects more that one.
https://www.postgresql.org/docs/12/sql-select.html#SQL-FOR-UPDATE-SHARE

If a LIMIT is used, locking stops once enough rows have been returned to satisfy the limit

This can lead to hardly reproducible errors.

Suggestion: check if query has forUpdate() flag and leave query unaltered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants