Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TM3sB-0004LA-N8 for pgsql-hackers@postgresql.org; Wed, 10 Oct 2012 21:31:23 +0000 Received: from moutng.kundenserver.de ([212.227.17.9]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TM3s9-0002Li-EG for pgsql-hackers@postgresql.org; Wed, 10 Oct 2012 21:31:23 +0000 Received: from awork2.localnet (p5DDC7B21.dip.t-dialin.net [93.220.123.33]) by mrelayeu.kundenserver.de (node=mrbap1) with ESMTP (Nemesis) id 0LZlbI-1TnNrl44Ro-00lQw6; Wed, 10 Oct 2012 23:31:18 +0200 From: Andres Freund Organization: 2ndquadrant.com To: pgsql-hackers@postgresql.org Subject: Re: change in LOCK behavior Date: Wed, 10 Oct 2012 23:31:16 +0200 User-Agent: KMail/1.13.7 (Linux/3.6.0-rc7-andres-00118-g66266e1-dirty; KDE/4.8.4; x86_64; ; ) Cc: Tomas Vondra References: <5075D8DF.6050500@fuzzy.cz> <201210102305.26511.andres@2ndquadrant.com> <5075E73E.8040608@fuzzy.cz> In-Reply-To: <5075E73E.8040608@fuzzy.cz> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201210102331.16382.andres@2ndquadrant.com> X-Provags-ID: V02:K0:4Nw7KyahgqdnVPekXk3vbGeT3frJtwUfAPGNEAoEITP 6AMgwNijegjyjI76tbTy+yM4Q64MTdAlFMdiVBgb/2qPBjd9t4 2ZQcHElxCkAQitbYMiH/gUfaXoT8ti3/GJWb7QQY7DMEK1TcA5 Lie8DZ0B2fm9qbuUrwsT31cYVTXVKP4h7+sPso4lhXFh2JoyZO DR5evYodKOkHWK0/ciH4N8QfrfegxhfHPUyA+v1plYog7KIFcJ H8t5AnKUypZk7BXXE5kmxx1+VFey6P7nrb5nc4XOg0Fq++fEy/ affH3KwLgTnS8LC/jNJnK3SrGFWk1SZfWvUICsaj/P2eExN22L 1fCAKCTtIayrkp73lLs0wAxo+MmQfUWouhReNwn+mCnARlsTHZ Erf9iulU4Zp4w== X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201210/526 X-Sequence-Number: 214762 On Wednesday, October 10, 2012 11:23:10 PM Tomas Vondra wrote: > On 10.10.2012 23:05, Andres Freund wrote: > > On Wednesday, October 10, 2012 10:43:57 PM Thom Brown wrote: > >> On 10 October 2012 21:21, Tomas Vondra wrote: > >>> Hi, > >>> > >>> I've just noticed a change of LOCK command behavior between 9.1 and > >>> 9.2, and I'm not sure whether this is expected or not. > >>> > >>> Let's use a very simple table > >>> > >>> CREATE TABLE x (id INT); > >>> > >>> Say there are two sessions - A and B, where A performs some operations > >>> on "x" and needs to protect them with an "ACCESS EXCLUSIVE" lock (e.g. > >>> it might be a pg_bulkload that acquires such locks, and we need to do > >>> that explicitly on one or two places). > >>> > >>> Session B is attempting to read the data, but is blocked and waits. On > >>> 9.1 it sees the commited data (which is what we need) but on 9.2 it > >>> sees only data commited at the time of the lock attemt. > >>> > >>> Example: > >>> > >>> A: BEGIN; > >>> A: LOCK x IN ACCESS EXCLUSIVE MODE; > >>> A: INSERT INTO x VALUES (100); > >>> B: SELECT * FROM x; > >>> A: COMMIT; > >>> > >>> Now on 9.1, B receives the value "100" while on 9.2 it gets no rows. > >>> > >>> Is this expected? I suspect the snapshot is read at different time or > >>> something, but I've checked release notes but I haven't seen anything > >>> relevant. > >>> > >>> Without getting the commited version of data, the locking is somehow > >>> pointless for us (unless using a different lock, not the table itself). > >> > >> I suspect it's this commit: d573e239f03506920938bf0be56c868d9c3416da > >> > >> http://archives.postgresql.org/pgsql-committers/2011-12/msg00167.php > > > > Very likely, yes. In fact you get the same beaviour in 9.1 if you modify > > the example slightly: > > > > B: PREPARE foo AS SELECT * FROM x; > > A: BEGIN; > > A: LOCK x IN ACCESS EXCLUSIVE MODE; > > A: INSERT INTO x VALUES (100); > > B: EXECUTE foo; > > A: COMMIT; > > > > If you think about it for a second its not that surprising anymore. We > > start to execute a query, acquire a snapshot for that, and then wait for > > the locks on the target relations. We continue executing in the same > > snapshot for the duration of the statement and thus cannot see any of > > the new rows which committed *after* we assembled our snapshot. > > Yes, that was my guess too (that the snapshot is acquired before asking > for the lock and not re-acquired after getting the lock). > > > The easy workaround is acquiring a AccessShareLock in the B transaction > > separately. > > I know - I've mentioned explicit locking as a possible solution in my > first message, although it would make the whole process more complex. I read your original statement as if you would want to use a separate lock (advisory?) which you don't need. > The question is whether that should be necessary or whether the 9.2 > should behave the same as 9.1. Given that 9.1 behaves the same as 9.2 with prepared statements I don't really see a convincing argument for changing this from the status quo. You can hit the same/similar behaviour in 9.1 even if youre not using PREPARE although the window isn't too big and you need DML + only an EXCLUSIVE (not access exlusive) lock for it. Greetings, Andres -- Andres Freund http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services