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 1s4Nzr-003NKQ-JB for pgsql-general@arkaria.postgresql.org; Tue, 07 May 2024 16:48:55 +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 1s4Nzp-003xox-3O for pgsql-general@arkaria.postgresql.org; Tue, 07 May 2024 16:48:54 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s4Nzo-003xop-NW for pgsql-general@lists.postgresql.org; Tue, 07 May 2024 16:48:53 +0000 Received: from mail-yb1-xb2a.google.com ([2607:f8b0:4864:20::b2a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s4Nzm-00221Y-OH for pgsql-general@lists.postgresql.org; Tue, 07 May 2024 16:48:52 +0000 Received: by mail-yb1-xb2a.google.com with SMTP id 3f1490d57ef6-de462979e00so3713259276.3 for ; Tue, 07 May 2024 09:48:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715100529; x=1715705329; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=TDDrH5BoIoJVyKwe1ODc6pNs0tPdKuubJN3TxeXJS3s=; b=GeaUj9ckpvj/7Y+w2lu/yb4lCvCQ/taQK49tj0whHHG6V3m2TsC+1wPaZwsM2+yvuh 9YtCa6sg7Zs8rxwfT9SjbnGKaD8SQ6Atr2RsTEGn6sHtIhw591LCicGezeDBkWbM9iQ7 0xCJLKWnO4zFtjX1CLxRp9KX4TADeWcdgaVHNbtG6rDZc2ni1kK2LlBuULqC6krWcyBa tQ8jts7MMYIy9oR57+zNHPM/tHdNUhWIFtWAFtD1I1Qkx+A0ASF7Pa/8MvJszxtCHv3U bbhZd4Cwq3SEy1I+nI5Dvfq5SIh6VPMuno9o0vcug8WW97p66gUv2MkwI5GnuDen0oJ4 ztIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715100529; x=1715705329; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=TDDrH5BoIoJVyKwe1ODc6pNs0tPdKuubJN3TxeXJS3s=; b=bNJ2t6nfSg6gYdfDozUiy+pHwfWYb4aCv/NSPEQHiKvBbjSwqiJSaSom365US+DJAs jARvIurAY0L5ZfEwO5G5PZnYnpBuWTpQY/hBK6UCB1i0NHhu/HYhYH9Kwvq7Lk7ES3/X YOdEcgmAPAbZTwa1DeEABSLoV7EQQty+W+PJYk4HvID/zlPpUme02nsRDXf1z2trLIbN Gx8wBrWBvAaychGNse4fT0QtAhvkcrNGf0cxh93BAa7AMPrDk5GSjVe3CDq2MKvIi0lp zjRm4LdElSfCoMCXpfC4wZd/ruTk0GzsqqylHr2Bw3y1pthmJQBTeuQqpE8sDL+TccSw TumA== X-Forwarded-Encrypted: i=1; AJvYcCVugFHAYQwp/RjsQEhPrai/S37IUobcmyoWtYnXaBI3WZPr5LeY1h29DFrEquxFmsRz1hTcyd2KSn8KaF/zjZEZdiKaLWJvMGNLH65NL9EXaX4D X-Gm-Message-State: AOJu0YxNC+ashUIhekBfxo9Lvz5c7obfP9mEncxdsO3XBWW0VxMCNyOK xDCsENHPUeIsy6XkYl6abpQNpcFRBnj9GfTh4izpevS9PS7zuUXl7fBi0gmDveLQt2WamJeHtQV AhkXoOrccetqtwUJj6fVBacPJwqM= X-Google-Smtp-Source: AGHT+IEcZE9b3CIIf2k8lzM1gGO3wD4LFx8L29DrO+dNtJPO+pi9UFPwcaMSIaCnxQ7wKu5JL5ymXO9H8II/QRokGJo= X-Received: by 2002:a25:108b:0:b0:dc7:4b0a:589 with SMTP id 3f1490d57ef6-debb9e70401mr214577276.55.1715100529012; Tue, 07 May 2024 09:48:49 -0700 (PDT) MIME-Version: 1.0 References: <2805642.1714793392@sss.pgh.pa.us> In-Reply-To: <2805642.1714793392@sss.pgh.pa.us> From: Siddharth Jain Date: Tue, 7 May 2024 09:48:38 -0700 Message-ID: Subject: Re: Question regarding how databases support atomicity To: Tom Lane Cc: "David G. Johnston" , "pgsql-general@lists.postgresql.org" Content-Type: multipart/alternative; boundary="000000000000c93aa00617dff538" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000c93aa00617dff538 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Thanks All for the kind responses. I understand how MVCC takes care of atomicity for updates to rows. I was developing a project where lets say data for each table is stored in its own folder together with metadata (we are not talking postgres now). So if I have two tables A and B I have a folder structure like: A \_ metadata.json B \_ metadata.json Now if I want to rename a table, I need to move the folder and also update metadata accordingly. These are two separate operations but need to be done atomically - all or none. in this case it is possible that we succeed in renaming the folder but fail to update metadata for whatever reason. then if we try to undo the folder rename we get another failure for whatever reason. how to deal with such scenarios? are there no such scenarios in postgres? On Fri, May 3, 2024 at 8:29=E2=80=AFPM Tom Lane wrote: > "David G. Johnston" writes: > > On Friday, May 3, 2024, Siddharth Jain wrote: > >> The way I understand this is that if there is a failure in-between, we > >>> start undoing and reverting the previous operations one by one. > > > Not in PostgreSQL. All work performed is considered provisional until = a > > commit succeeds. At which point all provisional work, which had been > > tagged with the same transaction identifier, becomes reality to the res= t > of > > the system, by virtue of marking the transaction live. > > Right. We don't use UNDO; instead, we use multiple versions of > database rows (MVCC). A transaction doesn't modify the contents > of existing rows, but just marks them as provisionally outdated, and > then inserts new row versions that are marked provisionally inserted. > Other transactions ignore the outdate markings and the uncommitted new > rows, until the transaction commits, at which time the new versions > become live and the old ones become dead. If the transaction never > does commit -- either through ROLLBACK or a crash -- then the old row > versions remain live and the new ones are dead. In either case, we > don't have a consistency or correctness problem, but we do have dead > rows that must eventually get vacuumed away to prevent indefinite > storage bloat. That can be done by background housekeeping processes > though (a/k/a autovacuum). > > I believe Oracle, for one, actually does use UNDO. I don't know > what they do about failure-to-UNDO. > > regards, tom lane > --000000000000c93aa00617dff538 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Thanks All for the kind responses. I understand how MVCC t= akes care of atomicity for updates to rows. I was developing a project wher= e lets say data for each table is stored in its own folder together with me= tadata (we are not talking postgres now). So if I have two tables A and B I= have a folder structure like:
A
\_ metadata.json
B=
\_ metadata.json
Now if I want to rename a table, I ne= ed to move the folder and also update metadata accordingly. These are two s= eparate operations but need to be done atomically - all or none. in this ca= se it is possible that we succeed in renaming the folder but fail to update= metadata for whatever reason. then if we try to undo the folder rename we = get another failure for whatever reason. how to deal with such scenarios? a= re there no such scenarios in postgres?


On Fri, May 3, = 2024 at 8:29=E2=80=AFPM Tom Lane <t= gl@sss.pgh.pa.us> wrote:
"David G. Johnston" <david.g.johnston@gmail.com> w= rites:
> On Friday, May 3, 2024, Siddharth Jain <siddhsql@gmail.com> wrote:
>> The way I understand this is that if there is a failure in-between= , we
>>> start undoing and reverting the previous operations one by one= .

> Not in PostgreSQL.=C2=A0 All work performed is considered provisional = until a
> commit succeeds.=C2=A0 At which point all provisional work, which had = been
> tagged with the same transaction identifier, becomes reality to the re= st of
> the system, by virtue of marking the transaction live.

Right.=C2=A0 We don't use UNDO; instead, we use multiple versions of database rows (MVCC).=C2=A0 A transaction doesn't modify the contents of existing rows, but just marks them as provisionally outdated, and
then inserts new row versions that are marked provisionally inserted.
Other transactions ignore the outdate markings and the uncommitted new
rows, until the transaction commits, at which time the new versions
become live and the old ones become dead.=C2=A0 If the transaction never does commit -- either through ROLLBACK or a crash -- then the old row
versions remain live and the new ones are dead.=C2=A0 In either case, we don't have a consistency or correctness problem, but we do have dead rows that must eventually get vacuumed away to prevent indefinite
storage bloat.=C2=A0 That can be done by background housekeeping processes<= br> though (a/k/a autovacuum).

I believe Oracle, for one, actually does use UNDO.=C2=A0 I don't know what they do about failure-to-UNDO.

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 regards, tom lane
--000000000000c93aa00617dff538--