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

[YSQL] Support wait-on-conflict concurrency control #5680

Closed
rkarthik007 opened this issue Sep 14, 2020 · 13 comments
Closed

[YSQL] Support wait-on-conflict concurrency control #5680

rkarthik007 opened this issue Sep 14, 2020 · 13 comments
Assignees
Labels
area/ysql Yugabyte SQL (YSQL) kind/enhancement This is an enhancement of an existing feature priority/medium Medium priority issue roadmap-tracking-issue This issue tracks a major roadmap item, and usually appears in the roadmap list.

Comments

@rkarthik007
Copy link
Collaborator

rkarthik007 commented Sep 14, 2020

Jira Link: DB-1529

In many scenarios, wait-on-conflict behavior is desirable, wherein the DB will wait for existing transactions (that might otherwise conflict) to complete before making progress.

Wait-on-conflict semantics are already part of READ COMMITTED isolation level. This is done via internal statement retries (in the query layer) with exponential backoff when a transaction detects conflicting transactions. A wait-queue based implementation will ensure lower latencies and be more resource efficient since it would eliminate statement retries.

Also, the wait queue based implementation should be agnostic to the isolation level i.e., it will allow wait-on-conflict semantics for all isolation levels.

Design doc - https://docs.google.com/document/d/1DCg4FjGlYZNBBIPcADk7z2B2TGznNHWtr8hU-0dUXyo/edit

Tracking Project: https://github.com/yugabyte/yugabyte-db/projects/67

TODO: Convert this doc to a .md file once the implementation has been completed.

Functional requirements

Nice-to-have

Analytics

@rkarthik007 rkarthik007 added area/ysql Yugabyte SQL (YSQL) roadmap-tracking-issue This issue tracks a major roadmap item, and usually appears in the roadmap list. labels Sep 14, 2020
@rkarthik007 rkarthik007 added this to Backlog in YSQL via automation Sep 14, 2020
@bmatican bmatican added this to Backlog in YBase features Oct 7, 2020
@triump2020
Copy link
Contributor

@rkarthik007 Have a plan or scheme to support pessimistic locking ?

@jaki
Copy link
Contributor

jaki commented Apr 13, 2021

Simple test:

CREATE TABLE t (i int);
INSERT INTO t VALUES (1);
bin/psql   --command 'update t set i = 2' & bin/psql   --command 'update t set i = 3' # both succeed
bin/ysqlsh --command 'update t set i = 2' & bin/ysqlsh --command 'update t set i = 3' # one fails

@salarali
Copy link

What is the expected timeline for this feature to be implemented? Is it still supposed to be shipped in April with 2.7 release?

We really need this feature in place, before trying out yugabyte.

@bmatican
Copy link
Contributor

hey @salarali , thanks for the interest in Yugabyte!

can you describe a bit your use case, to better understand why this is a blocker for you? cc @rkarthik007

@salarali
Copy link

Thanks for the quick reply.

We have two applications that are touching the same record at the same time. We want the first transaction to finish and then handle the second transaction.

@bmatican
Copy link
Contributor

@salarali is this a relatively isolated transaction, that you could retry from the client side? say, if you knew that the error code you got on the client was specifically a conflict?

we've actually not seen this be a significant blocker, so would really like to understand better if there's some easy workaround you could be ok with, to still try out Yugabyte, or if there's something more fundamental, that could be great feedback for us and the product

@salarali
Copy link

Yes, it is a relatively isolated transaction and we could retry from the client side after looking at the error code from the client.

Unfortunately, updating our code to support retries would require some engineering effort to update the pipelines.

@kulminaator
Copy link

as agreed on the yugabyte slack i will duplicate the text here so it is marked down.

while trying to port over an existing application that runs on postgresql i run into the issue that i need the pessimistic locking to be supported in order to avoid rewriting the whole application.

i will describe a few scenarios here:

Example on the select for update use case: i have to guarantee that a customer X does not get more than 3 subscriptions in activate state on his/her name. As uncommited transactions do not see each other's data what i do currently with postgres to guarantee this: i have separate tables for customers and subscriptions, when ever i do a status change on any of the subscriptions i at first grab the consumer's record lock with select for update (with no real intention to update it at all, just to lock it), do my operations & checks on the other records as necessary, release the lock with commit at transaction end. By doing this my application layer can remain very simple and very understandable to developers, and since i don't have a programmatic pattern to access subscriptions without the lock i do not have to lock or worry about locks on any of the subscription records. I also don't have to retry possible complex computations over and over again.

Another example on the parallel updates to the same record, postgresql has atomic increment support which is very useful for rapid parallel updates of numbers in records

whatever=# create table increment_test(any_number int);
CREATE TABLE
whatever=# insert into increment_test values(1);
INSERT 0 1

now when i have a small script like this (pg_sleep added for ease of testing)

begin;
update increment_test set any_number = any_number + 1;
select pg_sleep(5);
commit;

when thes update scripts are executed in parallel on postgresql, numbers get incremented by all script instances that were run (e.g. 2 parallel executions wait for each other due to row being locked by update and the result after 2 increments is 3). when i execute the script with a parallelism of 2 on yugabytedb i get the 40001 error on one of the transactions and i would have to retry this myself. not an issue if i do it on one record and seldom, but a huge problem if you run this at big concurrency against a small set of records, i could be almost stuck in a while loop for retries from the application side.

i am sure more compatibility with postgresql behavior would be a huge factor to win over customers from postgresql.

@RunningJon
Copy link

Supporting READ COMMITTED (PostgreSQL default) has also been a request so that existing applications have an easier time migrating from PostgreSQL to Yugabyte.

@pkj415 pkj415 self-assigned this Jul 10, 2021
@pkj415
Copy link
Contributor

pkj415 commented Jul 14, 2021

@salarali @kulminaator in your use cases, what isolation level are you using?

@dbuenor
Copy link

dbuenor commented Jul 14, 2021

@salarali @kulminaator in your use cases, what isolation level are you using?

Sorry for taking part in it, but in our case, we usually use READ COMMITED (Postgres default) like @RunningJon said before

@robertsami robertsami changed the title [YSQL] Improved wait queue based pessimistic locking [YSQL] Support wait-on-conflict concurrency control Jan 5, 2023
@robertsami
Copy link
Contributor

FYI -- we have moved towards the nomenclature of "wait-on-conflict" (FKA pessimistic locking) vs. "fail-on-conflict" (FKA optimistic locking). Issue details have been updated accordingly

@robertsami robertsami moved this from Needs Triage to Backlog in Wait-Queue Based Locking Jan 17, 2023
@robertsami robertsami removed this from Backlog in Wait-Queue Based Locking Jan 17, 2023
@rthallamko3
Copy link
Contributor

Closing this as we are using the board https://github.com/orgs/yugabyte/projects/19 to track the open items. All the necessary items have landed. cc @robertsami

YBase features automation moved this from Backlog to Done Mar 19, 2024
YSQL automation moved this from Backlog to Done Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/ysql Yugabyte SQL (YSQL) kind/enhancement This is an enhancement of an existing feature priority/medium Medium priority issue roadmap-tracking-issue This issue tracks a major roadmap item, and usually appears in the roadmap list.
Projects
Status: Done
YSQL
  
Done
Development

No branches or pull requests