Re: Will UPDATE lock if FROM refers to target table?

From: "Carlo Stonebanks" <stonec(dot)register(at)sympatico(dot)ca>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Will UPDATE lock if FROM refers to target table?
Date: 2007-10-15 19:23:59
Message-ID: ff0elv$n67$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>> You cannot block yourself with a lock, if that's what you mean.
>> The locks your session takes out will only block other sessions.

Well, that's the GOOD news! The bad news is - I'm not sure whether there's
somethign wrong with the logic of this query. The sub-query inside the FROM
is correct, is this logic consistent with how UPDAT.. FROM should work (all
of the samples I see are incredibly simplistic and show assignment and
reference to literals, not SET or WHERE clauses that are dependent on
anything in the FROM clause).

Would someone be kind enough to tell me if there is somethign wrong with
this apporach:

UPDATE mdx_core.provider_practice
SET default_postal_code = def.postal_code,
default_state_code = def.state_code,
default_country_code = 'US'
FROM
(SELECT provider_id,
provider_practice_id,
substr(coalesce(a.postal_code, f.default_postal_code), 1, 5) as
postal_code,
coalesce(a.state_code, f.default_state_code) as state_code
FROM mdx_core.provider_practice as pp
JOIN mdx_core.facility as f
ON f.facility_id = pp.facility_id
LEFT JOIN mdx_core.facility_address as fa
ON fa.facility_address_id = pp.facility_address_id
LEFT JOIN mdx_core.address as a
ON a.address_id = fa.address_id
WHERE coalesce(a.country_code, f.default_country_code) = 'US'
) as def
WHERE provider_practice.provider_practice_id = def.provider_practice_id

Thanks,

Carlo

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Benjamin Arai 2007-10-15 19:34:36 Re: Restore v. Running COPY/INDEX seperatly
Previous Message Kris Jurka 2007-10-15 19:06:37 Re: 8.2.3: Server crashes on Windows using Eclipse/Junit