Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ehqMf-0005dG-CR for pgsql-general@arkaria.postgresql.org; Sat, 03 Feb 2018 05:27:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ehqMe-0006bS-4m for pgsql-general@arkaria.postgresql.org; Sat, 03 Feb 2018 05:27:48 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1ehqMd-0006bH-Sz for pgsql-general@lists.postgresql.org; Sat, 03 Feb 2018 05:27:47 +0000 Received: from mx1.mailbox.org ([80.241.60.212]) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ehqMW-0005pC-PA for pgsql-general@postgresql.org; Sat, 03 Feb 2018 05:27:47 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.mailbox.org (Postfix) with ESMTPS id D039142B17; Sat, 3 Feb 2018 06:27:38 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter02.heinlein-hosting.de (spamfilter02.heinlein-hosting.de [80.241.56.116]) (amavisd-new, port 10030) with ESMTP id 9pG_x7YFV7TC; Sat, 3 Feb 2018 06:27:38 +0100 (CET) Message-ID: <1517635655.2397.7.camel@cybertec.at> Subject: Re: Postgres Planner "Inconsistency"? From: Laurenz Albe To: Renzo Bertuzzi , pgsql-general@postgresql.org Date: Sat, 03 Feb 2018 06:27:35 +0100 In-Reply-To: References: Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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