Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mO2yG-0006ac-Rn for pgsql-docs@arkaria.postgresql.org; Wed, 08 Sep 2021 19:10:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mO2yC-0002QP-V4 for pgsql-docs@arkaria.postgresql.org; Wed, 08 Sep 2021 19:10:52 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mO1GI-0004H2-Ji for pgsql-docs@lists.postgresql.org; Wed, 08 Sep 2021 17:21:26 +0000 Received: from mout-p-101.mailbox.org ([80.241.56.151]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mO1GG-0008GA-B8 for pgsql-docs@lists.postgresql.org; Wed, 08 Sep 2021 17:21:26 +0000 Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:105:465:1:2:0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4H4TSf53hlzQkBY; Wed, 8 Sep 2021 19:21:22 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jix.one; s=MBO0001; t=1631121681; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=aE+dcK4g3NAkLTTUjFg/3SX1jLo7ABpLzixNelPti1Q=; b=uLXYO+jOzesbr5LGBlXOYHsuJ4Pq7cMZz5/dzDBUOGcOcM5xWWjXyNYsvYpBkq6OvwqjKW GryxhINiWd9Ksh+K3pKv108PsjhE82+7/GDS4ZuG5lA8+fOeoiZm7W73jcquFUlbgg+LRm bTinbPB9aR7j3GX0ers7j99XBTTdDPREfBJa9CWQyN+y9CqxFICCVczIh+f8xGuYpeRV9W IYWnSHJxFx9ty82BG9Z2yYJjqREOhERed0T8d2kGzWCmyd6ZgaDTPjTFgQRlAQy6F+URPe EGokNFHU4tgwusPu83nuJgxiTcgmmCrpcjuq7HSfdSNNycuuXRb6pZffU204CA== To: Tom Lane References: <163108943900.696.7027791431029866787@wrigleys.postgresql.org> <3433006.1631119060@sss.pgh.pa.us> From: Jannis Harder Cc: pgsql-docs@lists.postgresql.org Subject: Re: Warn against transaction level advisory locks for emulating "flat file" DBMS locking strategies Message-ID: Date: Wed, 8 Sep 2021 19:21:16 +0200 MIME-Version: 1.0 In-Reply-To: <3433006.1631119060@sss.pgh.pa.us> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 036CE1813 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 08.09.21 18:37, Tom Lane wrote: > PG Doc comments form writes: >> The docs mention "For example, a common use of advisory locks is to emulate >> pessimistic locking strategies typical of so-called “flat file” data >> management systems" which is exactly what I wanted to use to port some code >> from using SQLite to using PostgreSQL. (The code in question requires >> serializable transactions and cannot not handle retries.) > > Hmm. I'm afraid you're out of luck on that combination of requirements: If it's not possible even with conservative locking using advisory locks, the current phrasing of emulating "flat file" databases certainly seems a bit misleading to me, as these are exactly the guarantees those offer (or at least several of them do). > The approximation is conservative in the sense > that it won't let any actual failures get by; but it may produce false > positives. We haven't felt this is a problem, because if you're using > this stuff in the first place, you likely have *actual* anomaly hazards > and thus need the retry logic anyway. Is the approximation so conservative that it can fail even when there is just a single transaction at a time? What about multiple "SERIALIZABLE READ ONLY DEFERRABLE" transactions concurrent to at most a single writing transaction? Those are question I now have and would love to see answered by the documentation. My testing with session level advisory locks indicates that this works fine without serialization failures, but of course that testing is far from exhaustive. > The advice in the manual is thinking about READ COMMITTED mode, where > I think this should work fine. It is a bit problematic in serializable > mode, because when you do "SELECT pg_advisory_lock...", the SELECT will > acquire the transaction snapshot before getting the lock. Yes that's roughly what I assumed was happening (modulo not knowing any details of the implementation) given the behavior I saw, and how those failures did not happen when taking a session level lock before beginning a transaction. > What I'm inclined to think here is that maybe the docs are not > sufficiently vocal about the fact that you can't avoid serialization > failures altogether. If even taking a session level lock before any writing transaction, combined with "SERIALIZABLE READ ONLY DEFERRABLE" for all reading transaction is not sufficient to avoid this, I would certainly agree. Again, especially because of the comparison to "flat file" databases where this can be avoided. Thank you for the clarifications! Best Regards, Jannis