Tricking the optimizer

From: Vitaliy Garnashevich <vgarnashevich(at)gmail(dot)com>
To: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Tricking the optimizer
Date: 2018-04-18 12:15:50
Message-ID: 612dae0f-16b3-e5f8-7458-1ec38aea163c@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

In order to implement some security features in our application, we
sometimes append additional WHERE filters to our queries, so the filer
in the end looks like:

SELECT ... FROM ... WHERE securityFilter1 AND securityFilter2 AND
securityFilter3

In the EXPLAIN the filters look something like:

(((SubPlan 1) = 'foo') AND ((SubPlan 2) = 'bar') AND ((SubPlan 3) = 'baz'))

There are no applicable indexes and each filter clause looks opaque to
the optimizer, so the optimizer is using some generic rules and assumes
that each AND clause would reduce the total number of returned rows by
some factor. The problem is that this is not usually the case, and
usually the clauses would not filter out any rows at all. This leads to
poor plans, when such query is a part of another bigger query.

Is there any trick to craft the query in such a way, so that to make the
optimizer believe that the filters would not remove any rows, and all
rows will likely be returned by the query?

Regards,
Vitaliy

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Pavan Deolasee 2018-04-18 12:16:07 Re: pg_locks.relation question
Previous Message Daniel Westermann 2018-04-18 12:05:37 pg_locks.relation question