From: Matteo Beccati <php@beccati.com>
To: pgsql-bugs@postgresql.org
Subject: Re: BUG #6416: Expression index not used with UNION ALL queries
Date: Sun, 29 Jan 2012 16:16:06 +0100
Message-ID: <4F2562B6.50509@beccati.com> (raw)
In-Reply-To: <E1RrWKS-0006xF-3s@wrigleys.postgresql.org>
References: <E1RrWKS-0006xF-3s@wrigleys.postgresql.org>
On 29/01/2012 16:06, php@beccati.com wrote:
> The following bug has been logged on the website:
>
> Bug reference: 6416
> Logged by: Matteo Beccati
> Email address: php@beccati.com
> PostgreSQL version: 9.1.2
> Operating system: Debian Sqeeze
> Description:
>
> I've just noticed that an expression index I've created was not used with a
> view contiaining a UNION ALL. Switching to UNION or querying the table
> directly works as expected.
>
> A self contained test case follows:
Sorry about the formatting issue. You will find it attached.
Cheers
--
Matteo Beccati
Development & Consulting - http://www.beccati.com/Attachments:bug6416.txt (text/plain, 3.0K) download
regression=# CREATE FUNCTION ab(in text, in text, out ab text) AS $$BEGIN ab := $1 || $2; END;$$ LANGUAGE plpgsql IMMUTABLE;
CREATE FUNCTION
regression=# CREATE table t1 (a text, b text);
CREATE TABLE
regression=# CREATE INDEX t1_ab_idx on t1 (ab(a, b));
CREATE INDEX
regression=# CREATE table t2 (ab text primary key);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t2_pkey" for table "t2"
CREATE TABLE
regression=# INSERT INTO t1 VALUES ('a', 'b');
INSERT 0 1
regression=# INSERT INTO t2 VALUES ('ab');
INSERT 0 1
regression=# VACUUM ANALYZE ;
VACUUM
regression=# SET enable_seqscan = false;
SET
regression=# EXPLAIN ANALYZE SELECT * FROM (SELECT ab(a, b) AS ab FROM t1 UNION ALL SELECT * FROM t2) t WHERE ab = 'ab';
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------
Result (cost=10000000000.00..10000000009.53 rows=2 width=18) (actual time=0.052..0.066 rows=2 loops=1)
-> Append (cost=10000000000.00..10000000009.53 rows=2 width=18) (actual time=0.052..0.065 rows=2 loops=1)
-> Seq Scan on t1 (cost=10000000000.00..10000000001.26 rows=1 width=32) (actual time=0.051..0.052 rows=1 loops=1)
Filter: (ab(a, b) = 'ab'::text)
-> Index Scan using t2_pkey on t2 (cost=0.00..8.27 rows=1 width=3) (actual time=0.010..0.011 rows=1 loops=1)
Index Cond: (ab = 'ab'::text)
Total runtime: 0.106 ms
(7 rows)
regression=# EXPLAIN ANALYZE SELECT * FROM (SELECT ab(a, b) AS ab FROM t1 UNION SELECT * FROM t2) t WHERE ab = 'ab';
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------
Unique (cost=17.07..17.08 rows=2 width=4) (actual time=0.071..0.073 rows=1 loops=1)
-> Sort (cost=17.07..17.07 rows=2 width=4) (actual time=0.070..0.070 rows=2 loops=1)
Sort Key: (ab(t1.a, t1.b))
Sort Method: quicksort Memory: 25kB
-> Append (cost=0.25..17.06 rows=2 width=4) (actual time=0.050..0.058 rows=2 loops=1)
-> Index Scan using t1_ab_idx on t1 (cost=0.25..8.77 rows=1 width=4) (actual time=0.049..0.050 rows=1 loops=1)
Index Cond: (ab(a, b) = 'ab'::text)
-> Index Scan using t2_pkey on t2 (cost=0.00..8.27 rows=1 width=3) (actual time=0.004..0.005 rows=1 loops=1)
Index Cond: (ab = 'ab'::text)
Total runtime: 0.116 ms
(10 rows)
regression=# EXPLAIN ANALYZE SELECT * FROM (SELECT ab(a, b) AS ab FROM t1) t WHERE ab = 'ab';
QUERY PLAN
--------------------------------------------------------------------------------------------------------------
Index Scan using t1_ab_idx on t1 (cost=0.25..8.77 rows=1 width=4) (actual time=0.030..0.032 rows=1 loops=1)
Index Cond: (ab(a, b) = 'ab'::text)
Total runtime: 0.048 ms
(3 rows)
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: php@beccati.com
Subject: Re: BUG #6416: Expression index not used with UNION ALL queries
In-Reply-To: <4F2562B6.50509@beccati.com>
References: <E1RrWKS-0006xF-3s@wrigleys.postgresql.org> <4F2562B6.50509@beccati.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