Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qtV90-00CbyU-IL for pgsql-general@arkaria.postgresql.org; Thu, 19 Oct 2023 15:41:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qtV8y-003Xzb-7X for pgsql-general@arkaria.postgresql.org; Thu, 19 Oct 2023 15:41:04 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qtV8w-003Xx6-0f for pgsql-general@lists.postgresql.org; Thu, 19 Oct 2023 15:41:04 +0000 Received: from wout1-smtp.messagingengine.com ([64.147.123.24]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qtV8t-001HRE-Cr for pgsql-general@lists.postgresql.org; Thu, 19 Oct 2023 15:41:01 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.west.internal (Postfix) with ESMTP id 4A62E3200997; Thu, 19 Oct 2023 11:40:56 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Thu, 19 Oct 2023 11:40:56 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to; s=fm2; t= 1697730055; x=1697816455; bh=EZQya8RSUNxk7C9JBHLx2nx1jdGGNFgg4oF Vq+G123Y=; b=WBTZpvBvmUd/E4Qq6jP1dhfpWnI+hdBQ4YFyRTyMrZK/uIB030d /2Nycy05YmtY1YAJMlZKDHLzNFGMUdy2uhd7Q5/6UE3bhjR8VxpOw0ifs+djhTc2 GP+cembKQqakZSl1EFhMIZWU+z3xQX2Fqtj+rNqflmu3kHLi1LHkoIVPRPgXDpCZ Y8euy/mabpSRygG6otfsGG/YJpEK6Pr7Gb3hj/aH+zh/cTyikyxd9eGDJotXHYwd kdIiK1pVjqtA81cO8gwmgIFb7ASGe05idCJr/qdZhu1t2O0jr8/K10ofbfSso9ke hCeiJdwey7rZCg8pltN476AuRlFjYWjJrmg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1697730055; x= 1697816455; bh=EZQya8RSUNxk7C9JBHLx2nx1jdGGNFgg4oFVq+G123Y=; b=H 4SeDeZcb4p0SQueK2y2Ija363mYjdhizF9SGaBRs3IjfLMkwzlctl+JbzZt1XAU3 SI0THmVBIMAA5KDGoWFwHBc26ut/3kp6EJeZEevwbxh8bl9guwMEhIegz/k+LxM6 ePZCD/UWj09Wh4Ove9/usjwcqMEItQw+SwmygmU5vkJ0lI+onAhPTJ+RIkA1IvGD jdg7Dh962YOE0t7Y77l8J3oz9WDizbCsNocubjg/6yZMKoblftNNVgM+kTBxoZPc oN4++4NCanfDgiQpTTlDEA6hjqTUcKCiBvNt7WAh+J2FjicqdfZaQqwKaZ1JfOus IcfbOoGuTn8BbJsSPFjXA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrjeeigdeltdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefkffggfgfuvfhfhfgjtgfgsehtkeertddtvdejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuggftrfgrthhtvghrnhepleegveekkeekueeigfdtveeileeuhfefudefteekjeff keejueejheegheegkedtnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghenuc evlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgr nhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 19 Oct 2023 11:40:55 -0400 (EDT) Message-ID: <269d1ab8-afea-45e6-8774-45ca569b0312@aklaver.com> Date: Thu, 19 Oct 2023 08:40:54 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: My question about the transaction To: Wen Yi , pgsql-general References: Content-Language: en-US From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 10/19/23 04:36, Wen Yi wrote: > Hi community, > I am learning the transaction of the postgresql, and I try to test using > these: > > ###### > > terminal 1: > > postgres=# select * from t; >  number > -------- >       1 > (1 row) > > postgres=# update t set number = 2; > UPDATE 1 > postgres=# select * from t; >  number > -------- >       2 > (1 row) > > postgres=# select * from t; >  number > -------- >       2 > (1 row) > > postgres=# > > ###### > > terminal 2: > > postgres=# create table t (number integer); > CREATE TABLE > postgres=# insert into t values (1); > INSERT 0 1 > postgres=# begin; > BEGIN > postgres=*# insert into t values (2); > INSERT 0 1 > postgres=*# select * from t; >  number > -------- >       2 >       2 > (2 rows) > > postgres=*# rollback; > ROLLBACK > > My question is, in my view, the transaction model should make sure that > when one on-process transaction don't commit itself, the data on this > transaction shouldn't can be modified by other transaction(I the > single-statement also be treated as a simple transaction), but why the > update works?(I think terminal 1 will block until the terminal 2's > transaction commit or rollback). > Can someone share you opinion to me? Assuming you did in order, where terminal 1 = t1 and terminal 2 = t2: t2 create table t (number integer); t2 insert into t values (1); t1 select * from t; t1 update t set number = 2; t1 select * from t; t2 begin; t2 insert into t values (2); t2 select * from t; t1 select * from t; t2 rollback; Then it is as David said, the commands in t1 see the inserted value of 1 in table t and updates it as they are running in autocommit as where the commands in t2 before the begin;. Autocommit commits on each successful completion of a command. You then start am explicit transaction is t2 that sees the updated row and then adds a new row, both of which are seen in the t2 transaction but not in the t1 session. My suggestion would be to read through this: https://www.postgresql.org/docs/current/transaction-iso.html several times. There is a lot going on there. > Thanks in advance! > > Yours, > Wen Yi > > -- Adrian Klaver adrian.klaver@aklaver.com