select-for-update

select “for update” with JDBC?

感情迁移 提交于 2019-12-14 03:59:19
问题 I want to create a for update select statement in Java using JDBC, but not sure how it would be done. If you are unfamiliar with for update you can read about it here https://www.postgresql.org/docs/9.0/static/sql-select.html#SQL-FOR-UPDATE-SHARE For example, I have the following select statements My select statement select email from email_accounts where already_linked = false order by random() limit 1 My update statement UPDATE email_accounts set already_linked = true, account_link

Postgres SELECT … FOR UPDATE in functions

不打扰是莪最后的温柔 提交于 2019-11-30 04:23:34
I have two questions about using SELECT … FOR UPDATE row-level locking in a Postgres function: Does it matter which columns I select? Do they have any relation to what data I need to lock and then update? SELECT * FROM table WHERE x=y FOR UPDATE; vs SELECT 1 FROM table WHERE x=y FOR UPDATE; I can't do a select in a function without saving the data somewhere, so I save to a dummy variable. This seems hacky; is it the right way to do things? Here is my function: CREATE OR REPLACE FUNCTION update_message(v_1 INTEGER, v_timestamp INTEGER, v_version INTEGER) RETURNS void AS $$ DECLARE v_timestamp

SELECT FOR UPDATE [duplicate]

独自空忆成欢 提交于 2019-11-29 12:52:10
Possible Duplicate: SELECT … FOR UPDATE and MAX() Which rows in this query lock ? select max(id) from table where id like '9%' for update what occur if another user run this query ? this is related question. If you have no index on id, this will lock all of the records. But I guess you have such index. So this will lock all records that are matching, including some records in between (if you are locking 3 and 5, 4 is also being locked) A SELECT ... FOR UPDATE reads the latest available data, setting exclusive locks on each row it reads. Thus, it sets the same locks a searched SQL UPDATE would

SQLAlchemy - select for update example

℡╲_俬逩灬. 提交于 2019-11-29 03:00:01
I'm looking for a complete example of using select for update in SQLAlchemy, but haven't found one googling. I need to lock a single row and update a column, the following code doesn't work (blocks forever): s = table.select(table.c.user=="test",for_update=True) # Do update or not depending on the row u = table.update().where(table.c.user=="test") u.execute(email="foo") Do I need a commit? How do I do that? As far as I know you need to: begin transaction select ... for update update commit If you are using the ORM, try the with_for_update function: foo = session.query(Foo).filter(Foo.id==1234)

Postgres SELECT … FOR UPDATE in functions

流过昼夜 提交于 2019-11-29 01:37:23
问题 I have two questions about using SELECT … FOR UPDATE row-level locking in a Postgres function: Does it matter which columns I select? Do they have any relation to what data I need to lock and then update? SELECT * FROM table WHERE x=y FOR UPDATE; vs SELECT 1 FROM table WHERE x=y FOR UPDATE; I can't do a select in a function without saving the data somewhere, so I save to a dummy variable. This seems hacky; is it the right way to do things? Here is my function: CREATE OR REPLACE FUNCTION

SELECT FOR UPDATE [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 06:07:12
问题 Possible Duplicate: SELECT … FOR UPDATE and MAX() Which rows in this query lock ? select max(id) from table where id like '9%' for update what occur if another user run this query ? this is related question. 回答1: If you have no index on id, this will lock all of the records. But I guess you have such index. So this will lock all records that are matching, including some records in between (if you are locking 3 and 5, 4 is also being locked) A SELECT ... FOR UPDATE reads the latest available

SQLAlchemy - select for update example

懵懂的女人 提交于 2019-11-27 15:51:25
问题 I'm looking for a complete example of using select for update in SQLAlchemy, but haven't found one googling. I need to lock a single row and update a column, the following code doesn't work (blocks forever): s = table.select(table.c.user=="test",for_update=True) # Do update or not depending on the row u = table.update().where(table.c.user=="test") u.execute(email="foo") Do I need a commit? How do I do that? As far as I know you need to: begin transaction select ... for update update commit

When to use SELECT … FOR UPDATE?

狂风中的少年 提交于 2019-11-27 02:27:35
Please help me understand the use-case behind SELECT ... FOR UPDATE . Question 1 : Is the following a good example of when SELECT ... FOR UPDATE should be used? Given: rooms[id] tags[id, name] room_tags[room_id, tag_id] room_id and tag_id are foreign keys The application wants to list all rooms and their tags, but needs to differentiate between rooms with no tags versus rooms that have been removed. If SELECT ... FOR UPDATE is not used, what could happen is: Initially: rooms contains [id = 1] tags contains [id = 1, name = 'cats'] room_tags contains [room_id = 1, tag_id = 1] Thread 1: SELECT id