Re: limits, indexes, views and query planner

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: Marc Mamin <M(dot)Mamin(at)intershop(dot)de>
Cc: "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: limits, indexes, views and query planner
Date: 2022-08-15 11:14:55
Message-ID: CAApHDvraXCuERV_Ov3ZR4UMc-iUVsFM=N1dFoWMM+pA5WLU3Nw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, 15 Aug 2022 at 20:30, Marc Mamin <M(dot)Mamin(at)intershop(dot)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 references it 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 equivalent to 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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Lahnov, Igor 2022-08-15 12:09:56 RE: Unable to start replica after failover
Previous Message Marc Mamin 2022-08-15 08:30:08 limits, indexes, views and query planner