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

From: Justin Pryzby <pryzby(at)telsasoft(dot)com>
To: John Bachir <j(at)jjb(dot)cc>
Cc: Sergei Kornilov <sk(at)zsrv(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-30 02:10:02
Message-ID: 20200530021002.GX17850@telsasoft.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, May 29, 2020 at 09:53:14PM -0400, John Bachir wrote:
> Hi Sergei - I just used the recipe on my production database. I didn't
> observe all the expected benefits, I wonder if there were confounding factors
> or if I did something wrong. If you have time, I'd love to get your feedback.
> Let me know if you need more info. I'd love to write a blog post informing
> the world about this potentially game-changing feature!

If you do it right, you can see a DEBUG:

postgres=# CREATE TABLE tn (i int);
postgres=# ALTER TABLE tn ADD CONSTRAINT nn CHECK (i IS NOT NULL) NOT VALID;
postgres=# ALTER TABLE tn VALIDATE CONSTRAINT nn;
postgres=# SET client_min_messages=debug;
postgres=# ALTER TABLE tn ALTER i SET NOT NULL ;
DEBUG: existing constraints on column "tn"."i" are sufficient to prove that it does not contain nulls

> SLOW (table scan speed) - didn't have timing on, but I think about same time as the next one.
> ALTER TABLE my_table ALTER COLUMN column1 SET NOT NULL;
>
> 01:39 SLOW (table scan speed)
> ALTER TABLE my_table ALTER COLUMN column2 SET NOT NULL;
>
> 00:22 - 1/4 time of table scan but still not instant like expected
> ALTER TABLE my_table ALTER COLUMN column3 SET NOT NULL;
>
> 20.403 ms - instant, like expected
> ALTER TABLE my_table ALTER COLUMN column4 SET NOT NULL;

That the duration decreased every time may have been due to caching?
How big is the table vs RAM ?
Do you know if the SET NOT NULL blocked or not ?

Maybe something else had a nontrivial lock on the table, and those commands
were waiting on lock. If you "SET deadlock_timeout='1'; SET
log_lock_waits=on;", then you could see that.

--
Justin

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Khandekar 2020-05-30 05:28:35 Re: Inlining of couple of functions in pl_exec.c improves performance
Previous Message John Bachir 2020-05-30 01:53:14 Re: feature idea: use index when checking for NULLs before SET NOT NULL