Re: Column reset all values

From: Olivier Gautherot <ogautherot(at)gautherot(dot)net>
To: otar shavadze <oshavadze(at)gmail(dot)com>
Cc: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Column reset all values
Date: 2020-05-13 09:15:31
Message-ID: CAJ7S9TWpJRmxg10kxJu4-DKLQXy7BvZEDFd_Ec0bkWF16CTa7g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi Otar,

On Wed, May 13, 2020 at 10:15 AM otar shavadze <oshavadze(at)gmail(dot)com> wrote:

> postgres version 12
> I have very simple update query, like this:
>
> update my_table
> set
> col = NULL
> where
> col IS NOT NULL;
>
> my_table contains few million rows, col is indexed column
>
> Fastest way would be alter table, drop column and then add column again,
> but I can't do this just because on this column depends bunch of views
> and materialized views.
>
> No much hope, but still asking, Is there some another way to just reset
> column all values? ( with NULL in my case)
>

If views depend on this column, you may need to drop them (in the right
order...) and then recreate them. Now, if they depend on a column that will
not contain significant data, you may wish to remove the column, or declare
it as null if you need to maintain compatibility.

Now, if you have time and down time of the database is an issue, you may
run the UPDATE on lots of 1000 rows (or whatever that number fits you).
UPDATE is typically a INSERT/DELETE/VACUUM sequence and this copying around
is the killer - doing it in one go can temporarily increase the disk usage.
I've had success with the following pseudo code:

SELECT rowid FROM mytable WHERE col IS NOT NULL

and fed the result to something like:

FOR chunk IN chunk_in_1000_rows(query_result)
DO
BEGIN
UPDATE my_table SET col = NULL WHERE rowid IN chunk
COMMIT
SLEEP(5)
DONE

You may wish to run a VACUUM FULL manually at the end.

In my case, I had to compute individual numbers so the processing was a bit
more complex but it happily processed over 60 millions rows in a few days.

Hope it helps
--
Olivier Gautherot

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Matthias Apitz 2020-05-13 10:13:01 Re: ESQL/C: a ROLLBACK rolls back a COMMITED transaction
Previous Message Peter J. Holzer 2020-05-13 08:35:49 Re: Column reset all values