pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #6662: Database do not push condition to subquery, test case for bug 6658
5+ messages / 3 participants
[nested] [flat]

* BUG #6662: Database do not push condition to subquery, test case for bug 6658
@ 2012-05-23 01:13 maxim.boguk@gmail.com
  2012-05-23 01:46 ` Re: BUG #6662: Database do not push condition to subquery, test case for bug 6658 Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 5+ messages in thread

From: maxim.boguk@gmail.com @ 2012-05-23 01:13 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      6662
Logged by:          Maxim Boguk
Email address:      maxim.boguk@gmail.com
PostgreSQL version: 9.1.3
Operating system:   Linux
Description:        

I managed create simple self-contained test case for 6658.



create table test as select * from generate_series(1,100000) as g(val);
create index test_val_special on test((val || ''));
analyze test;
select count(*) from test;

--ok
--index scan
explain analyze
SELECT val
FROM test
WHERE (val || '')='something';


                                                      QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
 Index Scan using test_val_special on test  (cost=0.01..8.29 rows=1 width=4)
(actual time=0.011..0.011 rows=0 loops=1)
   Index Cond: (((val)::text || ''::text) = 'something'::text)
 Total runtime: 0.038 ms


--not ok
--seq scan on 9.1.3
explain analyze
SELECT val 
FROM
(
SELECT
val,
(val || '') AS search_string
FROM test
) AS t1
WHERE search_string='something';

                                                    QUERY PLAN
------------------------------------------------------------------------------------------------------------------
 Subquery Scan on t1  (cost=0.00..3443.00 rows=500 width=4) (actual
time=47.076..47.076 rows=0 loops=1)
   Filter: (t1.search_string = 'something'::text)
   ->  Seq Scan on test  (cost=0.00..2193.00 rows=100000 width=4) (actual
time=0.012..34.949 rows=100000 loops=1)
 Total runtime: 47.091 ms


--the same second query on 8.3.13
--plan correct
                                                      QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
 Index Scan using test_val_special on test  (cost=0.01..8.29 rows=1 width=4)
(actual time=0.004..0.004 rows=0 loops=1)
   Index Cond: (((val)::text || ''::text) = 'something'::text)
 Total runtime: 0.018 ms

Kind Regards,
Maksym




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #6662: Database do not push condition to subquery, test case for bug 6658
  2012-05-23 01:13 BUG #6662: Database do not push condition to subquery, test case for bug 6658 maxim.boguk@gmail.com
@ 2012-05-23 01:46 ` Tom Lane <tgl@sss.pgh.pa.us>
  2012-05-23 01:53   ` Re: BUG #6662: Database do not push condition to subquery, test case for bug 6658 Maxim Boguk <maxim.boguk@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Tom Lane @ 2012-05-23 01:46 UTC (permalink / raw)
  To: maxim.boguk@gmail.com; +Cc: pgsql-bugs

maxim.boguk@gmail.com writes:
> I managed create simple self-contained test case for 6658.

This works fine in HEAD.  The reason it doesn't work fine in 9.1 (or
9.0) is that in those branches, anytextcat() and textanycat() are marked
volatile, for reasons that were good at the time but were superseded by
later policy changes.  So you have a subselect containing a volatile
output expression, which prevents flattening of the subselect, so the
restriction clause doesn't get pushed down to where it could be used
with the index.  For some history see
http://git.postgresql.org/gitweb/?p=postgresql.git&a=commitdiff&h=c82d931dd180965a9a0c06acc7...
http://git.postgresql.org/gitweb/?p=postgresql.git&a=commitdiff&h=aab353a60b95aadc00f81da0c6...
http://git.postgresql.org/gitweb/?p=postgresql.git&a=commitdiff&h=3db6524fe63f0598dcb2b307bb...

If this is a big problem for you, you could safely adjust the
provolatile marking of those functions by hand in 9.1.  It might be
better just to include an explicit cast to text, though, instead of
relying on the assumption that the system will let you concat an integer
directly to a text string.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #6662: Database do not push condition to subquery, test case for bug 6658
  2012-05-23 01:13 BUG #6662: Database do not push condition to subquery, test case for bug 6658 maxim.boguk@gmail.com
  2012-05-23 01:46 ` Re: BUG #6662: Database do not push condition to subquery, test case for bug 6658 Tom Lane <tgl@sss.pgh.pa.us>
@ 2012-05-23 01:53   ` Maxim Boguk <maxim.boguk@gmail.com>
  2012-05-23 02:14     ` Re: BUG #6662: Database do not push condition to subquery, test case for bug 6658 Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 5+ messages in thread

From: Maxim Boguk @ 2012-05-23 01:53 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs

Hi,

Thank you very much for answer.
Explicit casting resolved an issue.

Just single question:
if anytextcat() and textanycat() are marked volatile,
why the database allows create index on supposedly to be volatile
expression:
create index test_val_special on test((val || ''));
?

P

On Wed, May 23, 2012 at 11:46 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

> maxim.boguk@gmail.com writes:
> > I managed create simple self-contained test case for 6658.
>
> This works fine in HEAD.  The reason it doesn't work fine in 9.1 (or
> 9.0) is that in those branches, anytextcat() and textanycat() are marked
> volatile, for reasons that were good at the time but were superseded by
> later policy changes.  So you have a subselect containing a volatile
> output expression, which prevents flattening of the subselect, so the
> restriction clause doesn't get pushed down to where it could be used
> with the index.  For some history see
>
> http://git.postgresql.org/gitweb/?p=postgresql.git&a=commitdiff&h=c82d931dd180965a9a0c06acc7...
>
> http://git.postgresql.org/gitweb/?p=postgresql.git&a=commitdiff&h=aab353a60b95aadc00f81da0c6...
>
> http://git.postgresql.org/gitweb/?p=postgresql.git&a=commitdiff&h=3db6524fe63f0598dcb2b307bb...
>
> If this is a big problem for you, you could safely adjust the
> provolatile marking of those functions by hand in 9.1.  It might be
> better just to include an explicit cast to text, though, instead of
> relying on the assumption that the system will let you concat an integer
> directly to a text string.
>
>                        regards, tom lane
>



-- 
Maxim Boguk
Senior Postgresql DBA.

Phone RU: +7 910 405 4718
Phone AU: +61 45 218 5678

Skype: maxim.boguk
Jabber: maxim.boguk@gmail.com
МойКруг: http://mboguk.moikrug.ru/

"People problems are solved with people.
If people cannot solve the problem, try technology.
People will then wish they'd listened at the first stage."

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #6662: Database do not push condition to subquery, test case for bug 6658
  2012-05-23 01:13 BUG #6662: Database do not push condition to subquery, test case for bug 6658 maxim.boguk@gmail.com
  2012-05-23 01:46 ` Re: BUG #6662: Database do not push condition to subquery, test case for bug 6658 Tom Lane <tgl@sss.pgh.pa.us>
  2012-05-23 01:53   ` Re: BUG #6662: Database do not push condition to subquery, test case for bug 6658 Maxim Boguk <maxim.boguk@gmail.com>
@ 2012-05-23 02:14     ` Tom Lane <tgl@sss.pgh.pa.us>
  2012-05-23 02:18       ` Re: BUG #6662: Database do not push condition to subquery, test case for bug 6658 Maxim Boguk <maxim.boguk@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Tom Lane @ 2012-05-23 02:14 UTC (permalink / raw)
  To: Maxim Boguk <maxim.boguk@gmail.com>; +Cc: pgsql-bugs

Maxim Boguk <maxim.boguk@gmail.com> writes:
> if anytextcat() and textanycat() are marked volatile,
> why the database allows create index on supposedly to be volatile
> expression:
> create index test_val_special on test((val || ''));

CREATE INDEX inlines anytextcat (which is just a SQL function) before
making the volatility test, cf commit 5a86e5e19.  The reason the
subselect flattening code has difficulty with this case is that that
happens before function inlining does, so the expression still looks
volatile even though it really isn't.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #6662: Database do not push condition to subquery, test case for bug 6658
  2012-05-23 01:13 BUG #6662: Database do not push condition to subquery, test case for bug 6658 maxim.boguk@gmail.com
  2012-05-23 01:46 ` Re: BUG #6662: Database do not push condition to subquery, test case for bug 6658 Tom Lane <tgl@sss.pgh.pa.us>
  2012-05-23 01:53   ` Re: BUG #6662: Database do not push condition to subquery, test case for bug 6658 Maxim Boguk <maxim.boguk@gmail.com>
  2012-05-23 02:14     ` Re: BUG #6662: Database do not push condition to subquery, test case for bug 6658 Tom Lane <tgl@sss.pgh.pa.us>
@ 2012-05-23 02:18       ` Maxim Boguk <maxim.boguk@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Maxim Boguk @ 2012-05-23 02:18 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs

On Wed, May 23, 2012 at 12:14 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

> Maxim Boguk <maxim.boguk@gmail.com> writes:
> > if anytextcat() and textanycat() are marked volatile,
> > why the database allows create index on supposedly to be volatile
> > expression:
> > create index test_val_special on test((val || ''));
>
> CREATE INDEX inlines anytextcat (which is just a SQL function) before
> making the volatility test, cf commit 5a86e5e19.  The reason the
> subselect flattening code has difficulty with this case is that that
> happens before function inlining does, so the expression still looks
> volatile even though it really isn't.
>
>                        regards, tom lane
>



Thank you very much for all detailed explanation and all help.

Kind Regards,
Maksym

^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2012-05-23 02:18 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2012-05-23 01:13 BUG #6662: Database do not push condition to subquery, test case for bug 6658 maxim.boguk@gmail.com
2012-05-23 01:46 ` Tom Lane <tgl@sss.pgh.pa.us>
2012-05-23 01:53   ` Maxim Boguk <maxim.boguk@gmail.com>
2012-05-23 02:14     ` Tom Lane <tgl@sss.pgh.pa.us>
2012-05-23 02:18       ` Maxim Boguk <maxim.boguk@gmail.com>

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