dbTalk Databases Forums  

[BUGS] BUG #6416: Expression index not used with UNION ALL queries

mailing.database.pgsql-bugs mailing.database.pgsql-bugs


Discuss [BUGS] BUG #6416: Expression index not used with UNION ALL queries in the mailing.database.pgsql-bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
php@beccati.com
 
Posts: n/a

Default [BUGS] BUG #6416: Expression index not used with UNION ALL queries - 01-29-2012 , 09:06 AM






The following bug has been logged on the website:

Bug reference: 6416
Logged by: Matteo Beccati
Email address: php (AT) beccati (DOT) 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:

regression=# CREATE FUNCTION ab(in text, in text, out ab text) AS $$BEGINab
:= $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.073rows=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=1loops=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=1loops=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)


--
Sent via pgsql-bugs mailing list (pgsql-bugs (AT) postgresql (DOT) org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Reply With Quote
  #2  
Old   
Tom Lane
 
Posts: n/a

Default Re: [BUGS] BUG #6416: Expression index not used with UNION ALL queries - 01-29-2012 , 03:33 PM






Matteo Beccati <php (AT) beccati (DOT) com> writes:
Quote:
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.
Looks like I broke this back in November :-(. Fixed, thanks for the
report.

regards, tom lane

--
Sent via pgsql-bugs mailing list (pgsql-bugs (AT) postgresql (DOT) org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Reply With Quote
  #3  
Old   
Matteo Beccati
 
Posts: n/a

Default Re: [BUGS] BUG #6416: Expression index not used with UNION ALL queries - 01-30-2012 , 03:52 AM



On 29/01/2012 22:33, Tom Lane wrote:
Quote:
Matteo Beccati <php (AT) beccati (DOT) com> writes:
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.

Looks like I broke this back in November :-(. Fixed, thanks for the
report.
Thanks a lot Tom!


Cheers
--
Matteo Beccati

Development & Consulting - http://www.beccati.com/

--
Sent via pgsql-bugs mailing list (pgsql-bugs (AT) postgresql (DOT) org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.