pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedNot Null Constraint vs Query Planning
4+ messages / 2 participants
[nested] [flat]
* Not Null Constraint vs Query Planning
@ 2020-03-02 17:09 Don Seiler <don@seiler.us>
0 siblings, 1 reply; 4+ messages in thread
From: Don Seiler @ 2020-03-02 17:09 UTC (permalink / raw)
To: pgsql-general
If I have a NOT NULL constraint on a column, and then run a query where
that column IS NULL, does the optimizer "short-circuit" the query to return
0 rows right away?
If so, is there a way to see that it is doing so? I've been running a few
explain plans this morning and they all look the same.
Here I create a table in PG10 with 10 million dummy rows, no indexes,
vacuum/analyzed. I then query before and after and don't notice much
difference. Wondering if there's any way to see an indication that the
constraint was used in the query planning. My sample runs are at the end of
this email.
The REAL reason for this is that I'm wondering if I created a NOT NULL
check constraint with "NOT VALID" would that then NOT be considered in such
a "short-circuit" case until I ran the VALIDATE CONSTRAINT on it? Perhaps I
should have just asked this in its own thread but I started diving into the
query plan thing.
postgres=# explain (analyze, buffers) select name from people where
created_at is null;
QUERY PLAN
------------------------------------------------------------------------------------------------------------
Seq Scan on people (cost=0.00..203093.21 rows=1 width=33) (actual
time=5365.886..5365.886 rows=0 loops=1)
Filter: (created_at IS NULL)
Rows Removed by Filter: 10000000
Buffers: shared hit=12828 read=90265
Planning time: 2.558 ms
Execution time: 5379.862 ms
(6 rows)
postgres=# alter table people alter column created_at set not null;
ALTER TABLE
postgres=# vacuum analyze people;
VACUUM
postgres=# explain (analyze, buffers) select name from people where
created_at is null;
QUERY PLAN
------------------------------------------------------------------------------------------------------------
Seq Scan on people (cost=0.00..203092.49 rows=1 width=33) (actual
time=2339.254..2339.254 rows=0 loops=1)
Filter: (created_at IS NULL)
Rows Removed by Filter: 10000000
Buffers: shared hit=12938 read=90155
Planning time: 0.390 ms
Execution time: 2339.274 ms
(6 rows)
--
Don Seiler
www.seiler.us
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Not Null Constraint vs Query Planning
@ 2020-03-02 18:30 Vik Fearing <vik@postgresfriends.org>
parent: Don Seiler <don@seiler.us>
0 siblings, 1 reply; 4+ messages in thread
From: Vik Fearing @ 2020-03-02 18:30 UTC (permalink / raw)
To: Don Seiler <don@seiler.us>; pgsql-general
On 02/03/2020 18:09, Don Seiler wrote:
> The REAL reason for this is that I'm wondering if I created a NOT NULL
> check constraint with "NOT VALID" would that then NOT be considered in such
> a "short-circuit" case until I ran the VALIDATE CONSTRAINT on it? Perhaps I
> should have just asked this in its own thread but I started diving into the
> query plan thing.
You cannot do this because NOT NULL isn't a real constraint (meaning it
does not appear in pg_constraint). There have been several attempts to
make it a real constraint over the years but so far nothing has come of
them.
--
Vik Fearing
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Not Null Constraint vs Query Planning
@ 2020-03-02 23:02 Don Seiler <don@seiler.us>
parent: Vik Fearing <vik@postgresfriends.org>
0 siblings, 1 reply; 4+ messages in thread
From: Don Seiler @ 2020-03-02 23:02 UTC (permalink / raw)
To: Vik Fearing <vik@postgresfriends.org>; +Cc: pgsql-general
On Mon, Mar 2, 2020, 12:30 Vik Fearing <vik@postgresfriends.org> wrote:
> On 02/03/2020 18:09, Don Seiler wrote:
> > The REAL reason for this is that I'm wondering if I created a NOT NULL
> > check constraint with "NOT VALID" would that then NOT be considered in
> such
> > a "short-circuit" case until I ran the VALIDATE CONSTRAINT on it?
> Perhaps I
> > should have just asked this in its own thread but I started diving into
> the
> > query plan thing.
>
> You cannot do this because NOT NULL isn't a real constraint (meaning it
> does not appear in pg_constraint). There have been several attempts to
> make it a real constraint over the years but so far nothing has come of
> them
Using the check constraint method seemed to allow for the "not valid" step.
I'm curious what the difference is between a NOT NULL check constraint
versus setting the column to NOT NULL (assuming both are validated).
Don.
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Not Null Constraint vs Query Planning
@ 2020-03-02 23:13 Vik Fearing <vik@postgresfriends.org>
parent: Don Seiler <don@seiler.us>
0 siblings, 0 replies; 4+ messages in thread
From: Vik Fearing @ 2020-03-02 23:13 UTC (permalink / raw)
To: Don Seiler <don@seiler.us>; +Cc: pgsql-general
On 03/03/2020 00:02, Don Seiler wrote:
> On Mon, Mar 2, 2020, 12:30 Vik Fearing <vik@postgresfriends.org> wrote:
>
>> On 02/03/2020 18:09, Don Seiler wrote:
>>> The REAL reason for this is that I'm wondering if I created a NOT NULL
>>> check constraint with "NOT VALID" would that then NOT be considered in
>> such
>>> a "short-circuit" case until I ran the VALIDATE CONSTRAINT on it?
>> Perhaps I
>>> should have just asked this in its own thread but I started diving into
>> the
>>> query plan thing.
>>
>> You cannot do this because NOT NULL isn't a real constraint (meaning it
>> does not appear in pg_constraint). There have been several attempts to
>> make it a real constraint over the years but so far nothing has come of
>> them
>
>
> Using the check constraint method seemed to allow for the "not valid" step.
> I'm curious what the difference is between a NOT NULL check constraint
> versus setting the column to NOT NULL (assuming both are validated).
You kind of answered your own question there.
--
Vik Fearing
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2020-03-02 23:13 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-03-02 17:09 Not Null Constraint vs Query Planning Don Seiler <don@seiler.us>
2020-03-02 18:30 ` Vik Fearing <vik@postgresfriends.org>
2020-03-02 23:02 ` Don Seiler <don@seiler.us>
2020-03-02 23:13 ` Vik Fearing <vik@postgresfriends.org>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox