Re: pgsql: Add better handling of redundant IS [NOT] NULL quals

From: David Rowley <dgrowley(at)gmail(dot)com>
To: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
Cc: David Rowley <drowley(at)postgresql(dot)org>, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: pgsql: Add better handling of redundant IS [NOT] NULL quals
Date: 2024-01-23 21:02:16
Message-ID: CAHoyFK9uhdyBeOwL5nCqL_96P0JJnpjoTZPVRYLJmBzs-pXnKA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers pgsql-hackers

On Wed, 24 Jan 2024 at 08:15, Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org> wrote:
> (Similarly, allowing GROUP BY to ignore columns not in the GROUP BY,
> when a UNIQUE constraint exists and all columns are NOT NULL; currently
> we allow that for PRIMARY KEY, but if you have the NOT NULL constraint
> OIDs to cue the plan invalidation would let that case to be implemented
> as well.)

I recall some discussion about the GROUP BY case. I think at the time
there might have been some confusion with plan cache invalidation and
invalidating views that have been created with columns in the target
list which are functionally dependent on columns in the GROUP BY.

i.e, given:

create table ab (a int primary key, b int not null unique);

the following works:

create view v_ab1 as select a,b from ab group by a; -- works

but this one does not:

create view v_ab2 as select a,b from ab group by b; -- does not work
ERROR: column "ab.a" must appear in the GROUP BY clause or be used in
an aggregate function
LINE 1: create view v_ab2 as select a,b from ab group by b;

I think thanks to your work on adding pg_constraint records for NOT
NULL conditions, the latter case could now be made to work.

As for the plan optimisation, I agree with Tom about the relcache
invalidation triggering a replan. Maybe it's worth adding a test to
ensure the replan is done after a ALTER TABLE ... DROP NOT NULL,
however.

David

In response to

Browse pgsql-committers by date

  From Date Subject
Next Message Michael Paquier 2024-01-24 05:24:00 pgsql: Fix ALTER TABLE .. ADD COLUMN with complex inheritance trees
Previous Message Nathan Bossart 2024-01-23 20:22:14 pgsql: Fix crash in autoprewarm.

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavel Stehule 2024-01-23 21:10:22 Re: Schema variables - new implementation for Postgres 15
Previous Message Robert Haas 2024-01-23 20:47:24 Re: logical decoding and replication of sequences, take 2