From: Mathias Kunter <mathiaskunter@gmail.com>
To: pgsql-bugs@postgresql.org <pgsql-bugs@postgresql.org>
Subject: Re: BUG #14107: Major query planner bug regarding subqueries and indices
Date: Fri, 22 Apr 2016 22:59:49 +0200
Message-ID: <44df2e58-87f5-282c-9871-c735240c55e4@gmail.com> (raw)
In-Reply-To: <CAGnEbogfdNPxN7vvY_8b4bPj4VVDzEw5NaXVBRSWUMQgKZE-CA@mail.gmail.com>
References: <20160421115638.22895.78902@wrigleys.postgresql.org>
<CAKJS1f_Hv+gBpg8JF0T8PEyW0TzVgydkcp2dPZHeomkDUGsRyA@mail.gmail.com>
<69e376c7-6c2c-c96d-2ad6-3734affb59b7@gmail.com>
<CAGnEboiCo+NuGab0PEnUQ2Nf0DXZv2Rvo2xWhMbi88n=guZB5w@mail.gmail.com>
<CA+bJJbyUF6CZPgH45JUdRRJ96qHMd31Fb-qHw0iN2aohRYMM6g@mail.gmail.com>
<CAGnEbogfdNPxN7vvY_8b4bPj4VVDzEw5NaXVBRSWUMQgKZE-CA@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
> Queries return different number of rows, meaning they're not fully equivalent.
Well, I think in the given example they should actually be fully
equivalent, since the unique id column is selected as well. Thus, UNION
can't do any unwanted row eliminations.
> Well, then `EXPLAIN (analyze, buffers)` is also wanted, together with
> object definitions.
So here are the exact SQL commands to reproduce the problem.
-- Create table "book"
CREATE TABLE book (id SERIAL NOT NULL, name VARCHAR, author INTEGER,
CONSTRAINT book_pkey PRIMARY KEY (id));
CREATE INDEX book_name_index ON book (name);
CREATE INDEX book_author_index ON book (author);
-- Create table "author"
CREATE TABLE author (id SERIAL NOT NULL, name VARCHAR, CONSTRAINT
author_pkey PRIMARY KEY (id));
CREATE INDEX author_name_index ON author (name);
-- Insert some test data so that the planner would never assume a
sequential scan could be faster
INSERT INTO book (id, name, author) SELECT generate_series(1, 100000),
md5(random()::text), (generate_series(1, 100000) - 1) % 10000 + 1;
INSERT INTO author (id, name) SELECT generate_series(1, 10000),
md5(random()::text);
ANALYZE book;
ANALYZE author;
-- Check query plan when using OR operator
EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM book WHERE name = 'Harry
Potter' OR author IN (SELECT id FROM author WHERE name = 'Rowling');
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------
Seq Scan on book (cost=8.30..2443.30 rows=50001 width=41) (actual
time=25.527..25.527 rows=0 loops=1)
Filter: (((name)::text = 'Harry Potter'::text) OR (hashed SubPlan 1))
Rows Removed by Filter: 100000
Buffers: shared hit=937
SubPlan 1
-> Index Scan using author_name_index on author (cost=0.29..8.30
rows=1 width=4) (actual time=0.041..0.041 rows=0 loops=1)
Index Cond: ((name)::text = 'Rowling'::text)
Buffers: shared hit=2
Planning time: 0.237 ms
Execution time: 25.603 ms
-- Check query plan when using UNION operator
EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM book WHERE name = 'Harry
Potter' UNION SELECT * FROM book WHERE author IN (SELECT id FROM author
WHERE name = 'Rowling');
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------
HashAggregate (cost=58.42..58.53 rows=11 width=41) (actual
time=0.066..0.066 rows=0 loops=1)
Group Key: book.id, book.name, book.author
Buffers: shared hit=5
-> Append (cost=0.42..58.34 rows=11 width=41) (actual
time=0.061..0.061 rows=0 loops=1)
Buffers: shared hit=5
-> Index Scan using book_name_index on book (cost=0.42..8.44
rows=1 width=41) (actual time=0.035..0.035 rows=0 loops=1)
Index Cond: ((name)::text = 'Harry Potter'::text)
Buffers: shared hit=3
-> Nested Loop (cost=4.66..49.79 rows=10 width=41) (actual
time=0.019..0.019 rows=0 loops=1)
Buffers: shared hit=2
-> Index Scan using author_name_index on author
(cost=0.29..8.30 rows=1 width=4) (actual time=0.015..0.015 rows=0 loops=1)
Index Cond: ((name)::text = 'Rowling'::text)
Buffers: shared hit=2
-> Bitmap Heap Scan on book book_1 (cost=4.37..41.39
rows=10 width=41) (never executed)
Recheck Cond: (author = author.id)
-> Bitmap Index Scan on book_author_index
(cost=0.00..4.37 rows=10 width=0) (never executed)
Index Cond: (author = author.id)
Planning time: 0.669 ms
Execution time: 0.183 ms
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
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-bugs@postgresql.org
Cc: mathiaskunter@gmail.com
Subject: Re: BUG #14107: Major query planner bug regarding subqueries and indices
In-Reply-To: <44df2e58-87f5-282c-9871-c735240c55e4@gmail.com>
References: <20160421115638.22895.78902@wrigleys.postgresql.org> <CAKJS1f_Hv+gBpg8JF0T8PEyW0TzVgydkcp2dPZHeomkDUGsRyA@mail.gmail.com> <69e376c7-6c2c-c96d-2ad6-3734affb59b7@gmail.com> <CAGnEboiCo+NuGab0PEnUQ2Nf0DXZv2Rvo2xWhMbi88n=guZB5w@mail.gmail.com> <CA+bJJbyUF6CZPgH45JUdRRJ96qHMd31Fb-qHw0iN2aohRYMM6g@mail.gmail.com> <CAGnEbogfdNPxN7vvY_8b4bPj4VVDzEw5NaXVBRSWUMQgKZE-CA@mail.gmail.com> <44df2e58-87f5-282c-9871-c735240c55e4@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