2

Is there a way in postgres to lock the rows until the next select query execution from the same system.And one more thing is there will be no update process on locked rows. scenario is something like this

If the table1 contains data like

id |    txt
-------------------
1 |    World 
2 |    Text
3 |    Crawler
4 |    Solution
5 |    Nation
6 |    Under
7 |    Padding
8 |    Settle
9 |    Begin
10 |    Large
11 |    Someone
12 |    Dance

If sys1 executes

select * from table1 order by id limit 5;

then it should lock row from id 1 to 5 for other system which are executing select statement concurrently.

Later if sys1 again execute another select query like

select * from table1 where id>10 order by id limit 5;

then pereviously locked rows should be released.

4
  • A design that needs to prevent reading of valid data that are not going to be updated probably has some design flaw. I'd try to rethink the process so that it doesn't need this bizarre behavior.
    – 9000
    Commented Jan 1, 2011 at 10:58
  • @9000 The process is like. A table called english_all contains huge list of words which are just words without any spell check. Next these words should be fetched to different system as I mention above. Then each user will sort the words it by just checking spell and store the word into another table like if it starts with a then table_a, if it is b then table_b and so on. I think this clarifies why I need locking mechanism so that same words would not processed by diffrent users.
    – kiranking
    Commented Jan 1, 2011 at 13:32
  • Instead of table_a,... table_z I'd humbly suggest to create index by_first_letter on table1(substring(txt from 1 to 1)) and make sure your "select by first letter" uses the same expression.
    – 9000
    Commented Jan 1, 2011 at 17:43
  • @9000 can you suggest any book or link which covers the advantages of index by_first_letter implementation.
    – kiranking
    Commented Jan 2, 2011 at 3:26

3 Answers 3

4

I don't think this is possible. You cannot block a read only access to a table (unless that select is done FOR UPDATE)

As far as I can tell, the only chance you have is to use the pg_advisory_lock() function.
http://www.postgresql.org/docs/current/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS

But this requires a "manual" release of the locks obtained through it. You won't get an automatic unlocking with that.

To lock the rows you would need something like this:

select pg_advisory_lock(id), * 
from 
( 
  select * table1 order by id limit 5
) t

(Note the use of the derived table for the LIMIT part. See the manual link I posted for an explanation)

Then you need to store the retrieved IDs and later call pg_advisory_unlock() for each ID.

If each process is always releasing all IDs at once, you could simply use pg_advisory_unlock_all() instead. Then you will not need to store the retrieved IDs.

Note that this will not prevent others from reading the rows using "normal" selects. It will only work if every process that accesses that table uses the same pattern of obtaining the locks.

4
  • @ahwnn Excellent, manual locking and unlocking wont be any problem for me.The link is useful for the required solution but I don't find anything about unlocking can be done by the same system as one which locks it,especially pg_advisory_unlock_all.If it works I will call that before calling pg_try_advisory_lock.
    – kiranking
    Commented Jan 1, 2011 at 13:27
  • @ahwnn "select * from table1 where pg_try_advisory_lock(id) order by id limit 5;" this worked and locks the rows from sys1 and sys2 is not getting that rows.But I couldn't unlock the rows by pg_advisory_unlock_all() from sys1.But when browser tab(opera and mozilla) is closed it is releasing the locked rows. If I make pg_advisory_unlock_all() before selecting the rows ie in the beginging of post process in php will it unlock the rows previously locked.
    – kiranking
    Commented Jan 1, 2011 at 20:08
  • 1
    Just a warning: if you combine this with database transactions, things can get a little funky. For example, if thread A acquires this lock in a transaction and thread B comes along to acquire the same lock, when A finally lets go (after committing the transaction) thread B gets the data from before thread A's transaction. The issue is the "SELECT pg_advisory_lock(id), *" syntax where you're trying to grab the lock and the data at the same time. Much safer to keep it as two calls. Select the lock, then in another query select the data. (Postgres 9.1 here, not sure about other versions.)
    – JavadocMD
    Commented Apr 6, 2012 at 0:20
  • a question, does select for update locks the row just for the select statement or batch? or session? what if it's inside a function? would you check my question too sir? stackoverflow.com/questions/26694382/…
    – user2889419
    Commented Nov 2, 2014 at 22:02
0

It looks like you really have a transaction which transcends the borders of your database, and all the change happens in an another system.

My idea is select ... for update no wait to lock the relevant rows, then offload the data into another system, then rollback to unlock the rows. No two select ... for update queries will select the same row, and the second select will fail immediately rather than wait and proceed.

But you don't seem to mark offloaded records in any way; I don't see why two non-consecutive selects won't happily select overlapping range. So I'd still update the records with a flag and/or a target user name and would only select records with the flag unset.

4
  • (1) for select...for update to work properly is it should be in the transaction compulsorily. I executed two different queries in pgadmin's two query panel, but getting same rows. (2) once the rows are selected should I have to apply update query to row compulsorily to unlock the row.
    – kiranking
    Commented Jan 1, 2011 at 19:24
  • (1) You have to begin transaction for this to work, and run two different sessions (I used 2 instances of psql), then it works as advertised. (2) No, you don't have to actually update anything; you can rollback right after select for update just fine. No update or select for update will touch these rows until rollback or commit is issued.
    – 9000
    Commented Jan 1, 2011 at 20:14
  • Within transaction it works fine.But problem is with rollback now.Php file do_search.php is for query execution which calls the function get_all_words function and locks the rows using for update in transaction and echos the result to get_words.php(this file is one which calls the do_search.php though ajax). Now until all the fetched words are processed it should not be unlocked.Then where should I put rollback or is there a way to get transaction id and rollback that transaction after all words are processed.
    – kiranking
    Commented Jan 1, 2011 at 20:45
  • If you can't fit the entire action into one transaction, you'll need to go for explicit, "hand-made" locking. (With pooled connections, you'll not even be able to rollback as you may get different connection the next page load.) This returns us to the idea of a flag in each row that you update every time you 'lock' your rows for operation, and then update (reset) again once you're done.
    – 9000
    Commented Jan 1, 2011 at 20:52
0

I tried both select...for update and pg_try_advisory_lock and managed to get near my requirement.

/*rows are locking but limit is the problem*/
select * from table1 where  pg_try_advisory_lock( id) limit 5;
.
.
$_SESSION['rows'] = $rowcount; // no of row to process
.
.
/*afer each process of word*/
$_SESSION['rows'] -=1;
.
.
/*and finally unlock locked rows*/
if ($_SESSION['rows']===0)
select pg_advisory_unlock_all() from table1

But there are two problem in this
1. As Limit will apply before lock, every time the same rows are trying to lock in different instance.
2. Not sure whether pg_advisory_unlock_all will unlock the rows locked by current instance or all the instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.