Re: How to hint 2 coulms IS NOT DISTINCT FROM each other - Mailing list pgsql-general

From Kim Rose Carlsen
Subject Re: How to hint 2 coulms IS NOT DISTINCT FROM each other
Date
Msg-id AM4PR0501MB26100F718260A4425280825EC7AC0@AM4PR0501MB2610.eurprd05.prod.outlook.com
Whole thread Raw
In response to Re: How to hint 2 coulms IS NOT DISTINCT FROM each other  (Scott Marlowe <scott.marlowe@gmail.com>)
Responses Re: How to hint 2 coulms IS NOT DISTINCT FROM each other
List pgsql-general

>> This doesn't do much good. This doesn't tell the planner that the 3

>> customer_ids are actually of same value, and it therefore can't filter them
>> as it sees fit.

> You do know you can index on a function, and the planner then keeps
> stats on it when you run analyze right?

Yes, but I don't think it will make any difference. I don't think I can solve this with
an index lookup. I think my savior is the inference that the 2 columns are of 
same value and the planner are free to choose which order to do the filter and join
with this extra information.

I have tried creating a function called 
zero_if_null(int) : int that just select COALESCE($1, 0)
and adding a index on (zero_if_null(customer_id)) on table that contains customer_id. The only thing I get from is the planner now only knows how to compare customer_id, but it still doesn't know that they are of same value, only I know that and I want to declare it for the planner. 

I could probably rewrite the whole view in one query, and then fix it with a proper index. But I think I will loose alot of readability. 

I could also change the structure to save an explicit state, instead of a calculated state. But then I get some redundancy I need to make sure always stays the same.

In the end one of these will probably be the solution.

I guess the question is more or less,

why doesn't IS NOT DISTINCT FROM behave the same way as = operator, are there any alternatives? And a plausible use case for when it would be useful.

pgsql-general by date:

Previous
From: Chris Mair
Date:
Subject: Re: Best way to return Random rows from a table with non-repeatability of rows
Next
From: Adrian Klaver
Date:
Subject: Re: initdb createuser commands