Re: feature idea: use index when checking for NULLs before SET NOT NULL

From: Oleksandr Shulgin <oleksandr(dot)shulgin(at)zalando(dot)de>
To: Sergei Kornilov <sk(at)zsrv(dot)org>
Cc: John Bachir <j(at)jjb(dot)cc>, "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: feature idea: use index when checking for NULLs before SET NOT NULL
Date: 2020-05-29 07:26:03
Message-ID: CACACo5QyLWdu042k9yfEMXNcXU08dzLXLRgdY2CbdY9jv4qRtg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, May 29, 2020 at 8:56 AM Sergei Kornilov <sk(at)zsrv(dot)org> wrote:

> Hello
>
> Correct index lookup is a difficult task. I tried to implement this
> previously...
>
> But the answer in SO is a bit incomplete for recent postgresql releases.
> Seqscan is not the only possible way to set not null in pg12+. My patch was
> commited ( https://commitfest.postgresql.org/22/1389/ ) and now it's
> possible to do this way:
>
> alter table foos
> add constraint foos_not_null
> check (bar1 is not null) not valid; -- short-time exclusive lock
>
> alter table foos validate constraint foos_not_null; -- still seqscan
> entire table but without exclusive lock
>
> An then another short lock:
> alter table foos alter column bar1 set not null;
> alter table foos drop constraint foos_not_null;
>

That's really good to know, Sergei!

John, I think it's worth pointing out that Postgres most likely does a full
table scan to validate a constraint by design and not in optimization
oversight. Think of what's gonna happen if the index used for checking is
corrupted?

Cheers,
--
Alex

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2020-05-29 07:33:07 Re: password_encryption default
Previous Message Fujii Masao 2020-05-29 07:24:12 Re: race condition when writing pg_control