Re: looping through query to update column

From: "Albe Laurenz" <all(at)adv(dot)magwien(dot)gv(dot)at>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Re: looping through query to update column
Date: 2006-10-13 10:24:16
Message-ID: 52EF20B2E3209443BC37736D00C3C1380AD60DA9@EXADV1.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Rafal Pietrak wrote:
>> You might use 'ctid' to identify the row if you have no suitable
>
> How should I use 'ctid'? Like in the case, when I've selected
> something by means of SELECT ... FOR UPDATE?

You lock the table (with LOCK) or the row you're working on
(with SELECT FOR UPDATE) so that nobody else can change it while
you are working on it.

You need something like ctid if your table has the fundamental flaw
of lacking a primary key.

Sample:

FOR row IN SELECT ctid, * FROM table FOR UPDATE LOOP
UPDATE table SET column=value WHERE ctid=row.ctid;
...
END LOOP;

If your table has a primary key, use that instead and please
forget about the ctid.

Yours,
Laurenz Albe

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Richard Broersma Jr 2006-10-13 12:09:30 Re: Performance Problem
Previous Message Harald Armin Massa 2006-10-13 10:15:08 Re: Partitioning vs. View of a UNION ALL