From: Vitaliy Garnashevich <vgarnashevich@gmail.com>
To: Postgres General <pgsql-general@postgresql.org>
Subject: Tricking the optimizer
Date: Wed, 18 Apr 2018 15:15:50 +0300
Message-ID: <612dae0f-16b3-e5f8-7458-1ec38aea163c@gmail.com> (raw)
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: vgarnashevich@gmail.com
Subject: Re: Tricking the optimizer
In-Reply-To: <612dae0f-16b3-e5f8-7458-1ec38aea163c@gmail.com>
References: <612dae0f-16b3-e5f8-7458-1ec38aea163c@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox