Re: limits, indexes, views and query planner - Mailing list pgsql-general

From David Rowley
Subject Re: limits, indexes, views and query planner
Date
Msg-id CAApHDvraXCuERV_Ov3ZR4UMc-iUVsFM=N1dFoWMM+pA5WLU3Nw@mail.gmail.com
Whole thread Raw
In response to limits, indexes, views and query planner  (Marc Mamin <M.Mamin@intershop.de>)
List pgsql-general
On Mon, 15 Aug 2022 at 20:30, Marc Mamin <M.Mamin@intershop.de> wrote:
> in the example below, we can see that the view test_ab prevents the usage of the index to retrieve the top last
rows.
> This is a special case, as the where clause excludes data from the second table, and the explain output do not
referencesit at all.
 
> I wonder if the planner could be able to exclude the table_b earlier in its plan and to possibly fallback to a plan
equivalentto the first one.
 
> with a view on a single table (test_av), the index is used.

I think you might be confusing UNION and UNION ALL.   PostgreSQL is
under no obligation to uniquify the final result in your version of
the view with a single table, but it is when you UNION both tables
together, regardless of if one of the scans causes one of the union
branches to be removed or not.

Having said that, the planner still could do a better job of this as
it could have done Limit -> Unique -> Merge Append -> Index Scan.  I
do have some work in progress code to rewrite the union planner so
that it able to request pre-sorted input to allow Merge Append to be
used rather than Unique -> Sort. I'm not sure if it'll help this case
or not.

David



pgsql-general by date:

Previous
From: Marc Mamin
Date:
Subject: limits, indexes, views and query planner
Next
From: "Lahnov, Igor"
Date:
Subject: RE: Unable to start replica after failover