Thread: support for atomic multi-table updates?

support for atomic multi-table updates?

From
Steve Aulenbach
Date:
Hi,

Does postgreSQL support atomic multi-table updates? Something like:

update items,month set items.price=month.price where items.id=month.id;

We are considering postgreSQL for a project where we would like to be
able to do this.

Thanks,
Steve Aulenbach

Re: support for atomic multi-table updates?

From
"Joshua b. Jore"
Date:
-----BEGIN PGP MESSAGE-----
Comment: For info see http://www.gnupg.org

owGbwMvMyCRY+8Z7U4/OdnPG0+tKGGyO/ZALLi1K1VHIKi0uUSgtTlVwC/L31Q/3
cA1yVUjOSQSKFPNy8XKFBrg4hrgqZJak5hYrBLuGKBQUZSanKtgq5ObnlWToQXgg
rRABBYgBYOV6mSlwdZkpIMN4ubzyizNKExWS9BS88otSebkySkoKrPT1y8vL9dKL
UlPzSlKTM/Lyc/LTM4tL9PKL0kF6OuyZWRlBDob5QJCJiY1hnhHfgmUVbKE5mT8m
bFxnnzXxxQStBIb5IQu3OfI5mfRPmSP6yi+p9JrED5GtAA==
=iI9V
-----END PGP MESSAGE-----


Re: support for atomic multi-table updates?

From
"Henshall, Stuart - WCP"
Date:
You can use transactions to ensure that either both happen or neither happen
over an arbitrary number of commands.
eg:
BEGIN;
UPDATE x SET a=1;
UPDATE y SET b=2;
COMMIT;
Neither update will be visible to another transaction until the commit and
if there is a crash before the commit it will rollback on restart.
However in your specific case you should just be able to do something like
this:
UPDATE items SET items.price=(SELECT month.price FROM month WHERE
month.id=item.id);
Hope this helps,
- Stuart
(As a side note this looks like a candidate for normalisation, just joining
like so:
SELECT items.*,month.price FROM items INNER JOIN month ON items.id=month.id;

or
SELECT items.*,month.price FROM items,month WHERE items.id=month.id;
(the back end will try and optimise the table join order for this one))

> From: Steve Aulenbach [mailto:aulenbac@ucar.edu]
>
>
> Hi,
>
> Does postgreSQL support atomic multi-table updates? Something like:
>
> update items,month set items.price=month.price where
> items.id=month.id;
>
> We are considering postgreSQL for a project where we would like to be
> able to do this.
>
> Thanks,
> Steve Aulenbach