Re: BUG #16251: ::text type casting of a constant breaks query performance

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: imyfess(at)gmail(dot)com, PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #16251: ::text type casting of a constant breaks query performance
Date: 2020-02-08 16:49:13
Message-ID: 10694.1581180553@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> writes:
> so 8. 2. 2020 v 7:44 odesílatel PG Bug reporting form <
> noreply(at)postgresql(dot)org> napsal:
>> With '::text' type casting of '2020-02-08' (which is already text) query
>> permofance is very low

> The '2020-02-08' is not text type - it is 'unknown' type - and then is just
> directly transformed to date. I think so cast from text to date is not
> maybe immutable, and it can stops some optimizations.

Yeah. There actually isn't any cast from text to date, if you look into
pg_cast. So "('2020-02-08'::text)::date" is implemented as a text Const
that's fed through a CoerceViaIO node that applies date_in(), and
date_in() is only stable not immutable. (That must be so because its
behavior depends on the DateStyle setting, and maybe TimeZone too; not
sure about the latter but definitely the former.) So the planner is
unable to reduce the IS NULL test to constant-false and thereby get
rid of the OR, and that means it can't usefully apply the index.

If you can't rearrange things so that the IS NULL argument is seen
as a constant, the UNION trick that Pavel mentioned might be a useful
workaround. But I'm inclined to think that you need to take two steps
back and figure out whether this query logic is really sane or not.
You do realize that the query is asking to retrieve the entire table,
if whatever-it-is is NULL? Why would that be what you want?

regards, tom lane

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Pavel Stehule 2020-02-08 17:03:55 Re: BUG #16251: ::text type casting of a constant breaks query performance
Previous Message Marco Lechner privat 2020-02-08 09:00:58 Re: BUG #16245: proj63 from rhel-7 repo depends on sqlite33, but is not available