X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id CBD36D1B4ED for ; Tue, 14 Oct 2003 19:12:46 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 74048-01 for ; Tue, 14 Oct 2003 16:12:17 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by svr1.postgresql.org (Postfix) with ESMTP id D29CFD1B543 for ; Tue, 14 Oct 2003 16:12:15 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.12.9/8.12.9) with ESMTP id h9EJCFO0030395 for ; Tue, 14 Oct 2003 19:12:15 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id h9EJ4TVL029062 for pgsql-admin@postgresql.org; Tue, 14 Oct 2003 19:04:29 GMT From: Christopher Browne X-Newsgroups: comp.databases.postgresql.admin Subject: Re: Lock! Date: Tue, 14 Oct 2003 15:00:11 -0400 Organization: cbbrowne Computing Inc Lines: 67 Message-ID: References: <004c01c39175$0f0b3620$2702a8c0@WorkGroup> <1066148873.5581.67.camel@linda.lfix.co.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@news.hub.org X-message-flag: Outlook is rather hackable, isn't it? X-Home-Page: http://www.cbbrowne.com/info/ X-Affero: http://svcs.affero.net/rm.php?r=cbbrowne User-Agent: Gnus/5.1003 (Gnus v5.10.3) XEmacs/21.4 (Reasonable Discussion, linux) Cancel-Lock: sha1:RBtWiIGjRiJft5bBL+7RgT772xg= To: pgsql-admin@postgresql.org X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/143 X-Sequence-Number: 10704 In an attempt to throw the authorities off his trail, olly@lfix.co.uk (Oliver Elphick) transmitted: > On Mon, 2003-10-13 at 11:30, Szabó Péter wrote: >> Hi! >> >> I have a lock problem. If i lock a record with SELECT FOR UPDATE, than >> i try to lock again, the process just wait until the record free. But >> i have to send a message to the user like 'The record is locked, try >> later.'. But i can't. How can i do this? > > You can't. > > Quote from the manual: > So long as no deadlock situation is detected, a transaction > seeking either a table-level or row-level lock will wait > indefinitely for conflicting locks to be released. This means it > is a bad idea for applications to hold transactions open for > long periods of time (e.g., while waiting for user input). > > You need to rethink your application. > > What I have done is to read the data with SELECT. Just before changing > it I do SELECT FOR UPDATE and tell the user if anything he is changing > has changed since he read it in. If there has been no change, I go > ahead with the UPDATE. This means that records are only locked for a > very short time. Now, instead of the possibility of being locked out > for a long time, there is a possibility of having to throw away some > editing, but I estimate that to be less of a cost overall. Another strategy that some of our folks have been trying out is that of "optimistic locking." It's an in-the-application scheme, which is arguably not totally ideal, but it has the not-inconsiderable merit that its cost is _very_ low for the common case where there is no conflict. General idea: You start by SELECTing a lock field on the data you want to update. SELECT STUFF, LOCK_FIELD FROM SOME_RELATION; When you actually do the update, you do it as: UPDATE SOME_RELATION SET THIS='This', THAT='That', LOCK_FIELD=txn_id WHERE [normal criteria] AND LOCK_FIELD='Value_I_Found_Earlier'; If the record has been updated, then LOCK_FIELD will have a different value, and this transaction fails; you'll have to do something to recover, probably a ROLLBACK, and perhaps a retry. On the other hand, if the record _hasn't_ been touched by anyone else, then this change will go through, and there wasn't any costly locking done in the DBMS. It's not new; it was presented in the IEEE Transactions on Software Engineering back in '91, and that might not be its genesis... There's discussion of it in a Squeak Wiki... It seems to have become publicly popular in the Java world, presumably due to them finding it expensive to do pessimistic locking (e.g. - as in starting out with the SELECT FOR UPDATE). -- output = ("aa454" "@" "freenet.carleton.ca") http://cbbrowne.com/info/linux.html debugging, v: Removing the needles from the haystack.