Re: Mistake in statement example

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: marlene(dot)brandstaetter(at)cargonet(dot)software, pgsql-docs(at)lists(dot)postgresql(dot)org
Subject: Re: Mistake in statement example
Date: 2023-03-01 16:45:00
Message-ID: CAKFQuwadh0Q9WgQjRL=Uq8Fdm8pi6gfVpjX=ZWHNNTVkRpnvkg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs

On Wed, Mar 1, 2023 at 9:34 AM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> PG Doc comments form <noreply(at)postgresql(dot)org> writes:
> > I believe there is a mistake in an example on
> > https://www.postgresql.org/docs/current/transaction-iso.html section
> > 13.2.1:
> > BEGIN;
> > UPDATE accounts SET balance = balance + 100.00 WHERE acctnum = 12345;
> > UPDATE accounts SET balance = balance - 100.00 WHERE acctnum = 7534;
> > COMMIT;
>
> > The acctnum is expected to be 12345 in both cases.
>
> No, I think that's intentional: the example depicts transferring
> $100 from account 7534 to account 12345.
>
>
That may be, but the descriptive text and point of the example (which isn't
atomicity, but concurrency) doesn't even require the second update command
to be present. What the example could use is a more traditional
two-session depiction of the commands instead of having a single
transaction and letting the user envision the correct concurrency.

Something like:

S1: SELECT balance FROM accounts WHERE acctnum = 12345; //100
S1: BEGIN;
S2: BEGIN;
S1: UPDATE accounts SET balance = balance + 100.00 WHERE acctnum = 12345;
//200
S2: UPDATE accounts SET balance = balance + 100.00 WHERE acctnum = 12345;
//WAITING ON S1
S1: COMMIT;
S2: UPDATED; balance = 300
S2: COMMIT;

Though maybe "balance" isn't a good example domain, the incrementing
example used just after this one seems more appropriate along with the
added benefit of consistency.

David J.

In response to

Responses

Browse pgsql-docs by date

  From Date Subject
Next Message PG Doc comments form 2023-03-02 13:55:31 I think that the transaction tutorial document (3.4) should mention transaction isolation
Previous Message Tom Lane 2023-03-01 16:34:11 Re: Mistake in statement example