-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
PostgreSQL lock table not supported yet #5384
Comments
Hi @mckeeh3, thanks for opening this issue, this is something we've been discussing as well. Just wanted to understand your usage scenario for table level locks - could you please describe why/when you're looking to lock some tables? This is more for us to understand. |
@rkarthik007 run in the same issue. I've same intent like described here: -- prevent concurrent inserts from multiple transactions
LOCK TABLE photos IN EXCLUSIVE MODE; I'm exploring hasura with yugabyte and the example use case is to limit the count of wheels an every car can have. I've put transactions in serializable isolation level, but not sure that helps to avoid need of exclusive lock mode. |
I'm not sure what happens here 8o |
@jaki @m-iancu - can @Bessonov achieve the above with something like:
Haha no issues, we've changed the process a bit to leave issues we're not actively working on unassigned. |
@rkarthik007, @Bessonov, the link you had uses Since we don't support it, what you can do instead is I have come up with this. It takes advantage of the fact that we select count CREATE TABLE photos (data int, owner oid); -- data is int for demo convenience
CREATE FUNCTION check_photos() RETURNS trigger AS $$
DECLARE
max_num_photos int := 10;
must_check boolean := false;
num_photos int;
BEGIN
IF TG_OP = 'INSERT' THEN
must_check := true;
END IF;
IF TG_OP = 'UPDATE' THEN
IF (NEW.owner != OLD.owner) THEN
must_check := true;
END IF;
END IF;
IF must_check THEN
SELECT COUNT(*) FROM (SELECT *
FROM photos
WHERE owner = NEW.owner
FOR UPDATE NOWAIT) AS owner_photos INTO num_photos;
IF (num_photos > max_num_photos) THEN
RAISE EXCEPTION 'Too many photos % for owner %', num_photos, NEW.owner;
END IF;
END IF;
-- PERFORM pg_sleep(4); -- debug concurrency
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER check_photos_trigger
AFTER INSERT OR UPDATE ON photos
FOR EACH ROW EXECUTE FUNCTION check_photos();
INSERT INTO photos VALUES (generate_series(1, 9), 13);
INSERT INTO photos VALUES (generate_series(10, 11), 13); -- error
INSERT INTO photos VALUES (100, 13);
INSERT INTO photos VALUES (101, 13); -- error You should be able to modify this to fit you cars + wheels example. The other idea is to take this into your own hands and manually check the
But you'd need to find every insert and wrap it like this. |
Wow @jaki @rkarthik007 thank you very much for the great support! You're right, BTW, it was a great event today. |
|
Any news about when SHARE UPDATE EXCLUSIVE mode for lock will be supported? Without this yugabyte can not be used with Phoenix/Ecto (Elixir), it would be sad to not use Yugabyte just because of this. Thanks for all your work. |
The issue still exists for Elixir + Phoenix. Presently, as a workaround we have to use the |
Jira Link: DB-2468
Execute statement lock table blah in exclusive mode fails: Exception "ERROR: EXCLUSIVE not supported yet"
The text was updated successfully, but these errors were encountered: