From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Renzo Bertuzzi <kuthulu@gmail.com>
To: pgsql-general@postgresql.org
Subject: Re: Postgres Planner "Inconsistency"?
Date: Sat, 03 Feb 2018 06:27:35 +0100
Message-ID: <1517635655.2397.7.camel@cybertec.at> (raw)
In-Reply-To: <CAJ6K5CWmdsrrpf6dyhMwDj-BQU57fsAq_j919GGuw5mni7+U3w@mail.gmail.com>
References: <CAJ6K5CWmdsrrpf6dyhMwDj-BQU57fsAq_j919GGuw5mni7+U3w@mail.gmail.com>
Renzo Bertuzzi wrote:
> I have a strange problem with a query where the planner only uses and index
> if I use a constant value, but if I use a subquery it will prefer a seq scan.
>
> I have table "sample_table" with columns id serial primary key, and int_flag, with an index on int_flag.
> I inserted 240387 values with int_flag=1 and 1 value with int_flag=2
>
> so the table has 240388 total rows, the last row of the table has int_flag=2
>
> If I execute this query, the planner chooses the index:
>
> explain (analyze ,verbose,buffers)
> SELECT id
> FROM sample_table
> WHERE
> int_flag = any((array[2])::int[])
>
> QUERY PLAN
> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> Index Scan using index_sample_table_int_flag_ix on public.sample_table (cost=0.42..39.86 rows=1 width=8) (actual time=0.023..0.023 rows=1 loops=1)
> Output: id
> Index Cond: (sample_table.int_flag = ANY ('{2}'::integer[]))
> Buffers: shared hit=28
> Planning time: 0.087 ms
> Execution time: 0.046 ms
>
>
> but if I slightly change the query to:
>
> explain (analyze ,verbose,buffers)
> SELECT id
> FROM sample_table
> WHERE
> int_flag = any((select array[2])::int[])
>
> now postgres will do a seq scan.
> I have run vacuum and analyze but the result is the same.
>
> QUERY PLAN
> ---------------------------------------------------------------------------------------------------------------------------------------
> Seq Scan on public.sample_table (cost=0.01..8843.74 rows=240388 width=8) (actual time=44.993..44.995 rows=1 loops=1)
> Output: id
> Filter: (sample_table.int_flag = ANY ($0))
> Rows Removed by Filter: 240387
> Buffers: shared hit=3435
> InitPlan 1 (returns $0)
> -> Result (cost=0.00..0.01 rows=1 width=32) (actual time=0.002..0.003 rows=1 loops=1)
> Output: '{2}'::integer[]
> Planning time: 0.092 ms
> Execution time: 45.017 ms
>
> I suppose postgres prefers a seq scan because it treats the subquery as a non-deterministic
> value while in the first case the planner has all the values before hand???
>
> I'm using PostgreSQL 9.6.6 on x86_64-pc-linux-gnu, compiled by gcc (Debian 4.9.2-10) 4.9.2, 64-bit
>
> PS: This is a simplified query, the actual query will use another small table to build the array
> with less than 100 values and sample_table can have up to 5 millions entries.
> I have tried using a CTE with the array, but it still will do a seq scan.
In the second case, the optimizer does not think hard enough to figure out
that it actually could know that the InitPlan has a result of 2, and with
your real query it probably couldn't know for sure even if it tried hard.
So it has to come up with a plan without knowing what the search values will
be, and it chooses a sequential scan as the lesser evil, since it guesses
that it will have to retrieve most of the tuples anyway.
Maybe you can write your query as a join instead.
Yours,
Laurenz Albe
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: laurenz.albe@cybertec.at, kuthulu@gmail.com
Subject: Re: Postgres Planner "Inconsistency"?
In-Reply-To: <1517635655.2397.7.camel@cybertec.at>
References: <CAJ6K5CWmdsrrpf6dyhMwDj-BQU57fsAq_j919GGuw5mni7+U3w@mail.gmail.com> <1517635655.2397.7.camel@cybertec.at>
* 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