Re: Remove redundant NULL check in clause_selectivity_ext() function

From: Ilia Evdokimov <ilya(dot)evdokimov(at)tantorlabs(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Remove redundant NULL check in clause_selectivity_ext() function
Date: 2024-08-19 15:48:19
Message-ID: 7ad4f7c5-fedc-44d4-adda-ea6f243163bb@tantorlabs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


On 19.8.24 18:02, Tom Lane wrote:
> Ilia Evdokimov<ilya(dot)evdokimov(at)tantorlabs(dot)com> writes:
>> In the `clause_selectivity_ext()` function, there’s a comment regarding
>> a NULL clause check that asks, "can this still happen?". I decided to
>> investigate whether this scenario is still valid.
>> Here’s what I found after reviewing the relevant cases:
>> - approx_tuple_count(): The function iterates over a loop of other clauses.
>> - get_foreign_key_join_selectivity(): The function is invoked within an
>> `if (clause)` condition.
>> - consider_new_or_clause(): The clause is generated by
>> `make_restrictinfo()`, which never returns NULL.
>> - clauselist_selectivity_ext(): This function either checks
>> `list_length(clauses) == 1` before being called, or it is called within
>> a loop of clauses.
> That list_length check doesn't prove anything about whether the list
> element is NULL, though.
>
> While I suspect that you may be right that the case doesn't occur
> anymore (if it ever did), I'm inclined to leave this test in place.
> It's cheap enough compared to what the rest of the function will do,
> and more importantly we can't assume that all interesting call sites
> are within Postgres core. There are definitely extensions calling
> clauselist_selectivity and related functions. It's possible that
> some of them rely on clause_selectivity not crashing on a NULL.
> Certainly, such an assumption could be argued to be a bug they
> should fix; but I'm disinclined to make them jump through that
> hoop for a vanishingly small performance improvement.
>
> Also, there are boatloads of other places where the planner has
> possibly-redundant checks for null clause pointers. It's likely
> that some of the other ones are more performance-critical than
> this. But I wouldn't be in favor of retail removal of the others
> either. Maybe with a more holistic approach we could remove a
> whole lot of them and make a measurable improvement; but it
> would require some careful thought about what invariants we
> want to assume. There's not really any design principles
> about this right now, and where we've ended up is that most
> functions operating on expression trees assume they've got to
> defend against NULL inputs. To remove those checks, we'd
> need a clear understanding of where caller-side checks need
> to be added instead.
>
> regards, tom lane

Let's assume that this check needs to remain, and the length check
doesn't guarantee anything. However, I'm a bit concerned that there's a
NULL check here, but it's missing in the |clauselist_selectivity_ext()|
function. For the reasons mentioned above, I would suggest the
following: either we perform the NULL check in both places, or we don't
perform it in either.

--
Regards,
Ilia Evdokimov,
Tantor Labs LCC.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2024-08-19 15:51:55 Re: gitmaster server problem?
Previous Message Bertrand Drouvot 2024-08-19 15:35:14 Re: Avoid orphaned objects dependencies, take 3