From matyee@westel900.net Mon Oct 13 10:37:07 2003 X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 15F30D1B4E9 for ; Mon, 13 Oct 2003 10:37:07 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 74937-03 for ; Mon, 13 Oct 2003 07:36:19 -0300 (ADT) Received: from darmachakra.axelero.hu (fe03.axelero.hu [195.228.240.91]) by svr1.postgresql.org (Postfix) with ESMTP id 0C607D1B4E1 for ; Mon, 13 Oct 2003 07:36:16 -0300 (ADT) Received: from localhost (localhost-02 [127.0.2.1]) by darmachakra.axelero.hu (8.12.10/8.12.10) with SMTP id h9DAaCKH035064 for ; Mon, 13 Oct 2003 12:36:12 +0200 (CEST) Received: from fe03.axelero.hu [127.0.2.1] via SMTP gateway by darmachakra.axelero.hu [195.228.240.91]; id A088B65268C at Mon, 13 Oct 2003 12:36:11 +0200 Received: from pter (202.1-182-adsl-pool.axelero.hu [81.182.1.202]) by fe03.axelero.hu (8.12.10/8.12.10) with SMTP id h9DAa94t034923 for ; Mon, 13 Oct 2003 12:36:10 +0200 (CEST) Message-ID: <004c01c39175$0f0b3620$2702a8c0@WorkGroup> From: "=?iso-8859-2?B?U3phYvMgUOl0ZXI=?=" To: Subject: Lock! Date: Mon, 13 Oct 2003 12:30:43 +0200 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0049_01C39185.D1F55520" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.00.2919.6600 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600 X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.7 tagged_above=0.0 required=5.0 tests=HTML_40_50 X-Spam-Level: X-Archive-Number: 200310/138 X-Sequence-Number: 10699 This is a multi-part message in MIME format. ------=_NextPart_000_0049_01C39185.D1F55520 Content-Type: text/plain; charset="iso-8859-2" Content-Transfer-Encoding: quoted-printable Hi! I have a lock problem. If i lock a record with SELECT FOR UPDATE, than i tr= y 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? Matyee ------=_NextPart_000_0049_01C39185.D1F55520 Content-Type: text/html; charset="iso-8859-2" Content-Transfer-Encoding: quoted-printable
Hi!
 
I have a lock problem. If i lock a record = with=20 SELECT FOR UPDATE, than i try to lock again, the process just wait until th= e=20 record free. But i have to send a message to the user like 'The record is= =20 locked, try later.'. But i can't. How can i do this?
 
Matyee
------=_NextPart_000_0049_01C39185.D1F55520-- From matyee@mail.alba.hu Mon Oct 13 10:39:37 2003 X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id DDCDFD1B503 for ; Mon, 13 Oct 2003 10:39:37 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 65656-07 for ; Mon, 13 Oct 2003 07:38:50 -0300 (ADT) Received: from fe05.axelero.hu (fe05.axelero.hu [195.228.240.93]) by svr1.postgresql.org (Postfix) with ESMTP id E3503D1B517 for ; Mon, 13 Oct 2003 07:38:46 -0300 (ADT) Received: from localhost (localhost-02 [127.0.2.1]) by fe05.axelero.hu (8.12.10/8.12.10) with SMTP id h9DAcnfN002484 for ; Mon, 13 Oct 2003 12:38:49 +0200 (CEST) Received: from fe05.axelero.hu [127.0.2.1] via SMTP gateway by fe05 [195.228.240.93]; id A009ACF3201 at Mon, 13 Oct 2003 12:38:48 +0200 Received: from pter (202.1-182-adsl-pool.axelero.hu [81.182.1.202]) by fe05.axelero.hu (8.12.10/8.12.10) with SMTP id h9DAcmnP002466 for ; Mon, 13 Oct 2003 12:38:48 +0200 (CEST) Message-ID: <006001c39175$6d8771a0$2702a8c0@WorkGroup> From: "=?iso-8859-2?B?U3phYvMgUOl0ZXI=?=" To: Subject: Lock! Date: Mon, 13 Oct 2003 12:33:22 +0200 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_005D_01C39186.30DDE700" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.00.2919.6600 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600 X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.7 tagged_above=0.0 required=5.0 tests=HTML_40_50 X-Spam-Level: X-Archive-Number: 200310/130 X-Sequence-Number: 10691 This is a multi-part message in MIME format. ------=_NextPart_000_005D_01C39186.30DDE700 Content-Type: text/plain; charset="iso-8859-2" Content-Transfer-Encoding: quoted-printable Hi! I have a lock problem. If i lock a record with SELECT FOR UPDATE, than i tr= y 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? Matyee ------=_NextPart_000_005D_01C39186.30DDE700 Content-Type: text/html; charset="iso-8859-2" Content-Transfer-Encoding: quoted-printable
Hi!
 
I have a lock problem. If i lock a record = with=20 SELECT FOR UPDATE, than i try to lock again, the process just wait until th= e=20 record free. But i have to send a message to the user like 'The record is= =20 locked, try later.'. But i can't. How can i do this?
 
Matyee
------=_NextPart_000_005D_01C39186.30DDE700-- From olly@lfix.co.uk Tue Oct 14 16:30:11 2003 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 D612DD1B57E for ; Tue, 14 Oct 2003 16:30:11 +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 35238-08 for ; Tue, 14 Oct 2003 13:29:41 -0300 (ADT) Received: from anchor-post-37.mail.demon.net (anchor-post-37.mail.demon.net [194.217.242.87]) by svr1.postgresql.org (Postfix) with ESMTP id BD2D0D1B533 for ; Tue, 14 Oct 2003 13:27:57 -0300 (ADT) Received: from lfix.demon.co.uk ([80.177.205.209] helo=cerberus.lfix.co.uk) by anchor-post-37.mail.demon.net with esmtp (Exim 3.35 #1) id 1A9S1d-000PEB-0b; Tue, 14 Oct 2003 17:27:57 +0100 Received: from linda.lfix.co.uk ([192.168.1.1]) by cerberus.lfix.co.uk with esmtp (Exim 4.22) id 1A9S1Z-00058I-Jg; Tue, 14 Oct 2003 17:27:53 +0100 Subject: Re: Lock! From: Oliver Elphick To: =?ISO-8859-1?Q?Szab=F3_P=E9ter?= Cc: pgsql-admin@postgresql.org In-Reply-To: <004c01c39175$0f0b3620$2702a8c0@WorkGroup> References: <004c01c39175$0f0b3620$2702a8c0@WorkGroup> Content-Type: text/plain; charset=iso-8859-15 Organization: LFIX Limited Message-Id: <1066148873.5581.67.camel@linda.lfix.co.uk> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.5 Date: Tue, 14 Oct 2003 17:27:53 +0100 Content-Transfer-Encoding: quoted-printable X-LFIX-MailScanner: Found to be clean X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/140 X-Sequence-Number: 10701 On Mon, 2003-10-13 at 11:30, Szab=F3 P=E9ter wrote: > Hi! >=20=20 > 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). =20=20=20=20=20=20=20=20 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. --=20 Oliver Elphick Oliver.Elphick@lfix.co.uk Isle of Wight, UK http://www.lfix.co.uk/oliver GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839 932A 614D 4C34 3E1D 0C1C =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D "I delight to do thy will, O my God; yea, thy law is=20 within my heart." Psalms 40:8=20 From cbbrowne@acm.org Tue Oct 14 19:12:46 2003 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. From angch@bytecraft.com.my Wed Oct 15 04:30:06 2003 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 36288D1B524 for ; Wed, 15 Oct 2003 04:30:06 +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 69547-04 for ; Wed, 15 Oct 2003 01:29:35 -0300 (ADT) Received: from gateway.vault-id.com (unknown [203.115.192.108]) by svr1.postgresql.org (Postfix) with SMTP id 13E1FD1B4F4 for ; Wed, 15 Oct 2003 01:29:28 -0300 (ADT) Received: (qmail 19572 invoked from network); 15 Oct 2003 04:10:15 -0000 Received: from unknown (HELO bytecraft.com.my) (219.93.229.26) by mail.bytecraft.com.my with SMTP; 15 Oct 2003 04:10:15 -0000 Message-ID: <3F8CCC7F.6@bytecraft.com.my> Date: Wed, 15 Oct 2003 12:26:39 +0800 From: Ang Chin Han User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20030225 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Oliver Elphick Cc: =?ISO-8859-15?Q?Szab=F3_P=E9ter?= , pgsql-admin@postgresql.org Subject: Re: Lock! References: <004c01c39175$0f0b3620$2702a8c0@WorkGroup> <1066148873.5581.67.camel@linda.lfix.co.uk> In-Reply-To: <1066148873.5581.67.camel@linda.lfix.co.uk> X-Enigmail-Version: 0.71.0.0 X-Enigmail-Supports: pgp-inline, pgp-mime Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enig7451EC7D9C91AC049309A543" X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/150 X-Sequence-Number: 10711 This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig7451EC7D9C91AC049309A543 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Oliver Elphick wrote: > 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). Unless you set STATEMENT_TIMEOUT: http://www.postgresql.org/docs/7.3/static/runtime-config.html#RUNTIME-CONFIG-GENERAL In other words, try: SET STATEMENT_TIMEOUT=50; SELECT * FROM foo WHERE foo_id = 1 FOR UPDATE; That means if another process is locking foo_id = 1 row for more than 50 millisecond after you try to SELECT it FOR UPDATE, you'll get an error/rollback, and you can then assume it's being used. Just remember to reset STATEMENT_TIMEOUT to zero or your default before any slow queries in that transaction. There was some discussion about emulating (IIRC) Oracle's SELECT .. FOR UPDATE NOWAIT somewhere in the Postgresql archives. Look them up for more details. --------------enig7451EC7D9C91AC049309A543 Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQE/jMyCNYbTUIgzwfARAm7UAKCP8QuGDKhWA5WeTCtEW1PkrhqY4gCfSv5p obAe6j8Hpc1OESXl5be9/2E= =MaNL -----END PGP SIGNATURE----- --------------enig7451EC7D9C91AC049309A543-- From tgl@sss.pgh.pa.us Wed Oct 15 15:27:44 2003 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 7608CD1B54F for ; Wed, 15 Oct 2003 15:27:44 +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 68264-10 for ; Wed, 15 Oct 2003 12:27:13 -0300 (ADT) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by svr1.postgresql.org (Postfix) with ESMTP id 64CCAD1B547 for ; Wed, 15 Oct 2003 12:27:12 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.10/8.12.10) with ESMTP id h9FFQrhn015775; Wed, 15 Oct 2003 11:26:53 -0400 (EDT) To: Ang Chin Han Cc: Oliver Elphick , =?ISO-8859-15?Q?Szab=F3_P=E9ter?= , pgsql-admin@postgresql.org Subject: Re: Lock! In-reply-to: <3F8CCC7F.6@bytecraft.com.my> References: <004c01c39175$0f0b3620$2702a8c0@WorkGroup> <1066148873.5581.67.camel@linda.lfix.co.uk> <3F8CCC7F.6@bytecraft.com.my> Comments: In-reply-to Ang Chin Han message dated "Wed, 15 Oct 2003 12:26:39 +0800" Date: Wed, 15 Oct 2003 11:26:52 -0400 Message-ID: <15774.1066231612@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/155 X-Sequence-Number: 10716 Ang Chin Han writes: > Just remember to reset STATEMENT_TIMEOUT to zero or your default before > any slow queries in that transaction. Also, you can use "SET LOCAL ..." to constrain the effects of the SET command; this is probably safer than trying to remember to reset the variable when you're done. regards, tom lane