﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
30488	SearchVector lookup is generating redundent Coalesce wrapper.	T Lee	T Lee	"In Django 2.2.1, in the SearchVector if we do:

{{{
>>> from django.contrib.postgres.search import SearchVector
>>> Entry.objects.annotate(
...     search=SearchVector('body_text', 'summary_text'),
... ).filter(search='Cheese')
}}}

The generated SQL currently looks something like
{{{
SELECT id /*......*/
FROM ""app_entry"" WHERE
to_tsvector(COALESCE(COALESCE(""app_entry"".""body_text"", ''), '') || ' '
|| COALESCE(COALESCE(""app_entry"".""summary_text"", ''), '')) @@
(plainto_tsquery('Cheese')) = true
}}}

i.e. in the {{{WHERE}}} clause, the fields is wrapped twice with {{{COALESCE}}}, .e.g. {{{COALESCE(COALESCE(""app_entry"".""body_text"", ), )}}}

While it is still possible to create a functional index with this, the generated SQL does not feel optimal at the very least.

It will be great if the generated SQL will keep to one level of  {{{COALESCE}}}, .e.g. {{{COALESCE(""app_entry"".""body_text"", ), )}}}"	Cleanup/optimization	closed	contrib.postgres	2.2	Normal	fixed		Simon Charette	Accepted	1	0	0	0	0	0
