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 1npOnG-0006BB-Eu for pgsql-general@arkaria.postgresql.org; Fri, 13 May 2022 06:28:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1npOnE-0004xg-Ik for pgsql-general@arkaria.postgresql.org; Fri, 13 May 2022 06:28: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 1npOnE-0004vC-4r for pgsql-general@lists.postgresql.org; Fri, 13 May 2022 06:28:52 +0000 Received: from mail-ej1-x629.google.com ([2a00:1450:4864:20::629]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1npOnB-0004Tm-5d for pgsql-general@lists.postgresql.org; Fri, 13 May 2022 06:28:51 +0000 Received: by mail-ej1-x629.google.com with SMTP id kq17so14304204ejb.4 for ; Thu, 12 May 2022 23:28:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; h=message-id:subject:from:to:cc:date:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=/+SSOxAptwHXBE/V4dKuYoAbPGWE4eY0KPXLdarrTKA=; b=CAExxFE4T5jDSMM5OyIytXuF89gtmKI5RSaBccXDryX4u/kXmf8JM86iqxlivxglrc K/Cqm4hhQ5ds9WMByUcRO7EnhKVSBB9aHW+qclrWYeppxcMoXybfRsjMkduVRxecu9X8 1+nLq4jAhumFgNK1Go6tw/Skr0QCosHhlYDj7nsXtC6K4Ya90aswkbVKEkaXb+fRFZlK q4bNFIW1HlUVsekvsQI68lAGGSKPcWGeQFCxlEG/s6KqsbtJTlJ/fHROcNl0waHlQu/A Z1xp+joQniXsuQopXi5YrHdzEJb1TbmZc9YNCO+GnmXXwdzHWFuEhJV3xFwWGs83E6EO Us+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=/+SSOxAptwHXBE/V4dKuYoAbPGWE4eY0KPXLdarrTKA=; b=OUYqpKl4k/Kg1xOUepVmsipbTyuOXRMGklC2m4yPNYfMHBV/HKzDeDAOIENOGmDSDY rZVrMh3Uh/l0dPqM+JElwyX+2gIW+mi0dezfxS72dJp3bow0U8eSnO5NBmuMUGWzKgTC YXX6Jp1vplXtKQEji5xk836+fhOmP3bBHP6VioCw3eKG2ffYlyMdlzZ3mEzesF4pjo/t zAaYqpFdlb3I8cgvOqxKTPSscNZwgzAZIyRwYbkfSiJoY3giqeEmHeER4PcvZDFHgi55 aTPeyeXYTu64Ya2fd041a41hpxACXIK+K772cJaLUrB3lGJ2NR62zrCVIkjF536NJQ75 32og== X-Gm-Message-State: AOAM531DKSqoQSIWrpDxjOyB/v5m+SWlMtwt3LdvpUMvBdlMSAaGFkmu 3Er8tk9YjnbHqAQ2/f+exakxAWaHMYKVwHbW X-Google-Smtp-Source: ABdhPJzKIE/sRwNiqWIkotiUlDulvWyUoXEUDYqi+mIcS2h1mMR3DBlKDsACuesyy73NyfcDWSOPMw== X-Received: by 2002:a17:907:1b05:b0:6f0:18d8:7be0 with SMTP id mp5-20020a1709071b0500b006f018d87be0mr2839404ejc.561.1652423327790; Thu, 12 May 2022 23:28:47 -0700 (PDT) Received: from [10.1.32.63] (i68970612.versanet.de. [104.151.6.18]) by smtp.gmail.com with ESMTPSA id ek27-20020a056402371b00b0042617ba63c0sm510690edb.74.2022.05.12.23.28.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 May 2022 23:28:47 -0700 (PDT) Message-ID: <4e2de8ec77447a8347ea1923171afb6bcf77397b.camel@cybertec.at> Subject: Re: Deferred constraint trigger semantics From: Laurenz Albe To: Bryn Llewellyn Cc: pgsql-general list Date: Fri, 13 May 2022 08:28:46 +0200 In-Reply-To: References: <5C29B392-6215-4791-8DD8-7B8977A637B7@yugabyte.com> <987A57EE-C893-43EE-BFF0-FAF1422526D4@yugabyte.com> <356843a91b48918d8e78d2accb932a46b7889161.camel@cybertec.at> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.42.4 (3.42.4-2.fc35) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 2022-05-12 at 22:06 -0700, Bryn Llewellyn wrote: > > > In the case of constraint triggers, yes. But there is no race condition for primary key, > > unique and foreign key constraints, because they also "see" uncommitted data. > > I can't follow you here, sorry. I tried this: > > create table t( >   k serial primary key, >   v int not null, >   constraint t_v_unq unique(v) initially deferred); > > -- RED > start transaction isolation level read committed; > insert into t(v) values (1), (2); > select k, v from t order by k; > > -- BLUE > start transaction isolation level read committed; > insert into t(v) values (1), (3); > select k, v from t order by k; > > -- RED > commit; > select k, v from t order by k; > > -- BLUE > select k, v from t order by k; > commit; > > select k, v from t order by k; > > The first "select" from the "BLUE" session at the very end produces this: > >  k | v  > ---+--- >  1 | 1 >  2 | 2 >  3 | 1 >  4 | 3 > > This doesn't surprise me. It's ultimately illegal. But not yet. (Before "RED" committed, "BLUE" > didn't see the rows with "k = 1" and "k = 2". So it isn't seeing any other sessions uncommitted > data—but only it's own uncommitted data.) Be "seeing" I didn't mean "show to the user". I mean that the code that implements PostgreSQL constraints takes uncommitted data into account. The documentation describes that for the case of uniqueness in some detail: https://www.postgresql.org/docs/current/index-unique-checks.html > Then, when "BLUE" commits, it (of course) gets this: > > ERROR:  duplicate key value violates unique constraint "t_v_unq" > DETAIL:  Key (v)=(1) already exists. > > Then it sees (of course, again) only the rows with "k = 1" and "k = 2"—the same as what "RED" saw. > > It seems to be impossible to do a test in slow motion where "RED" and "BLUE" each issues "commit" > at the exact same moment. So thinking about this scenario doesn't tell me if: > > (a) Each session runs its constraint check and the rest of what "commit" entails in a genuinely serialized fashion. > > OR > > (b) Each session first runs its constraint check (and some other stuff) non-serializedly—and only > then runs the small part of the total "commit" action (the WAL part) serializedly. > (This would result in bad data in the database at rest—just as my contrived misuse of > "set constraints all immediate" left things in my "one or two admins" scenario.) I'd say that (b) is a more accurate description. > The (a) scheme sounds correct. And the (b) scheme sounds wrong. Why would PG prefer to implement (b) rather than (a)? > > I'm clearly missing something. Because (a) would result in terrible performance if there are many concurrent transactions. I don't see why (b) is wrong - as your example shows, the behavior is correct. Perhaps you have to understand what a PostgreSQL "snapshot" is and that the exact moment at which a row was created is not important - it is the transaction numbers in "xmin" and "xmax" that count. > > > Where, in the PG doc, can I read the account of the proper mental model for the application programmer? > It seems to be impossible to conduct an experiment that would disprove the hypothesis that one, > or the other, of these mental models is correct. I'd say that the proper mental model is that you don't need to care. The ACID properties are guarantees that the database makes, and these guarantees are usually negative: "no sequence of actions can result in the violation of a unique constraint" or "you don't get to see uncommitted data". The exact sequence of what happens during COMMIT is interesting, but irrelevant to the programmer. All that counts is "a deferred constraint is checked between the time that COMMIT starts processing and the time that it returns". If you want to know more, you have to start reading the code. It is open source and well documented. > > > > > Your post's testcase used the condition "at least one guard on duty" [...] > > My testcase used a stricter rule: the table of staff must have exactly one or two rows where > the job is "Admin". So, here, concurrent sessions can break the rule (when the txn starts > with one "Admin") by updating different rows to make them "Admin" or by inserting different > new "Admin" rows. I've convinced myself by experiment that an ordinary trigger can enforce > this rule when contending sessions use "serializable" isolation. Am I right that you'd say > that no pessimistic locking scheme can enforce the rule at lower isolation levels except the > brute-force "lock table"? If the transaction can insert two rows, I'd agree. If the transaction only inserts a single row, than it could SELECT ... FOR NO KEY UPDATE the one existing row, thus serializing the concurrent transactions. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com