pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
index-only-scan when there is an index on all columns
9+ messages / 6 participants
[nested] [flat]

* index-only-scan when there is an index on all columns
@ 2018-02-27 20:37 Hadi Moshayedi <hadi@moshayedi.net>
  0 siblings, 0 replies; 9+ messages in thread

From: Hadi Moshayedi @ 2018-02-27 20:37 UTC (permalink / raw)
  To: pgsql-general; +Cc: Hadi Moshayedi <hadi@citusdata.com>

With the following definition:

create table t1 (a int, b int, c int);
create index on t1 (a,b,c);

I get the following plan for the following query:

postgres=# explain select sum(c) from t1 where a > 0;
                                    QUERY
PLAN
-----------------------------------------------------------------------------------
 Aggregate  (cost=29.62..29.63 rows=1 width=8)
   ->  Bitmap Heap Scan on t1  (cost=9.42..27.92 rows=680 width=4)
         Recheck Cond: (a > 0)
         ->  Bitmap Index Scan on t1_a_b_c_idx  (cost=0.00..9.25 rows=680
width=0)
               Index Cond: (a > 0)
(5 rows)

I am wondering why is it not using index-only-scan (which would use the
cache better) and instead it does a bitmap scan?

Thanks,
Hadi

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

* index-only-scan when there is an index on all columns
@ 2018-02-27 20:49 Hadi Moshayedi <hadi@citusdata.com>
  2018-02-27 21:08 ` Re: index-only-scan when there is an index on all columns Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 9+ messages in thread

From: Hadi Moshayedi @ 2018-02-27 20:49 UTC (permalink / raw)
  To: pgsql-general

With the following definition:

create table t1 (a int, b int, c int);
create index on t1 (a,b,c);

I get the following plan for the following query:

postgres=# explain select sum(c) from t1 where a > 0;
                                    QUERY PLAN

------------------------------------------------------------
-----------------------
 Aggregate  (cost=29.62..29.63 rows=1 width=8)
   ->  Bitmap Heap Scan on t1  (cost=9.42..27.92 rows=680 width=4)
         Recheck Cond: (a > 0)
         ->  Bitmap Index Scan on t1_a_b_c_idx  (cost=0.00..9.25 rows=680
width=0)
               Index Cond: (a > 0)
(5 rows)

I am wondering why is it not using index-only-scan (which would use the
cache better) and instead it does a bitmap scan?

Thanks,
Hadi

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

* Re: index-only-scan when there is an index on all columns
  2018-02-27 20:49 index-only-scan when there is an index on all columns Hadi Moshayedi <hadi@citusdata.com>
@ 2018-02-27 21:08 ` Tom Lane <tgl@sss.pgh.pa.us>
  2018-02-27 21:18   ` Re: index-only-scan when there is an index on all columns David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 9+ messages in thread

From: Tom Lane @ 2018-02-27 21:08 UTC (permalink / raw)
  To: Hadi Moshayedi <hadi@citusdata.com>; +Cc: pgsql-general

Hadi Moshayedi <hadi@citusdata.com> writes:
> I am wondering why is it not using index-only-scan (which would use the
> cache better) and instead it does a bitmap scan?

Never experiment on an empty table and assume that the resulting plan
is the same as you'd get on a large table.

In this case, not only don't you have any meaningful amount of data
loaded, but the planner can see that none of the table's pages are
marked all-visible, meaning that the "index-only" scan would degrade
to a regular indexscan, which is how it gets costed.  And on a single-page
table, an indexscan is going to have a hard time beating other
alternatives.

			regards, tom lane




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

* Re: index-only-scan when there is an index on all columns
  2018-02-27 20:49 index-only-scan when there is an index on all columns Hadi Moshayedi <hadi@citusdata.com>
  2018-02-27 21:08 ` Re: index-only-scan when there is an index on all columns Tom Lane <tgl@sss.pgh.pa.us>
@ 2018-02-27 21:18   ` David G. Johnston <david.g.johnston@gmail.com>
  2018-02-27 21:58     ` Re: index-only-scan when there is an index on all columns Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 9+ messages in thread

From: David G. Johnston @ 2018-02-27 21:18 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Hadi Moshayedi <hadi@citusdata.com>; pgsql-general

On Tue, Feb 27, 2018 at 2:08 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

> Hadi Moshayedi <hadi@citusdata.com> writes:
> > I am wondering why is it not using index-only-scan (which would use the
> > cache better) and instead it does a bitmap scan?
>
> Never experiment on an empty table and assume that the resulting plan
> is the same as you'd get on a large table.
>
> In this case, not only don't you have any meaningful amount of data
> loaded, but the planner can see that none of the table's pages are
> marked all-visible, meaning that the "index-only" scan would degrade
> to a regular indexscan, which is how it gets costed.  And on a single-page
> table, an indexscan is going to have a hard time beating other
> alternatives.
>

If one runs vacuum on a table (small or otherwise) that is currently
choosing an index scan as its best plan how likely is it that post-vacuum
an index-only plan would be chosen if the index type and column presence
conditions are met?

Also, I recall discussion that select statements will touch the visibility
map (hence causing write I/O even in a read-only query) but [1] indicates
that only vacuum will set them ddl will clear them.

[1] https://www.postgresql.org/docs/10/static/storage-vm.html

David J.

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

* Re: index-only-scan when there is an index on all columns
  2018-02-27 20:49 index-only-scan when there is an index on all columns Hadi Moshayedi <hadi@citusdata.com>
  2018-02-27 21:08 ` Re: index-only-scan when there is an index on all columns Tom Lane <tgl@sss.pgh.pa.us>
  2018-02-27 21:18   ` Re: index-only-scan when there is an index on all columns David G. Johnston <david.g.johnston@gmail.com>
@ 2018-02-27 21:58     ` Tom Lane <tgl@sss.pgh.pa.us>
  2018-02-27 22:10       ` Re: index-only-scan when there is an index on all columns David G. Johnston <david.g.johnston@gmail.com>
  2018-02-27 22:11       ` Re: index-only-scan when there is an index on all columns Andres Freund <andres@anarazel.de>
  0 siblings, 2 replies; 9+ messages in thread

From: Tom Lane @ 2018-02-27 21:58 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Hadi Moshayedi <hadi@citusdata.com>; pgsql-general

"David G. Johnston" <david.g.johnston@gmail.com> writes:
> If one runs vacuum on a table (small or otherwise) that is currently
> choosing an index scan as its best plan how likely is it that post-vacuum
> an index-only plan would be chosen if the index type and column presence
> conditions are met?

Offhand I think it would always prefer IOS over regular indexscan if the
table is mostly all-visible.  The problem in this example was that other
choices dominate both.

> Also, I recall discussion that select statements will touch the visibility
> map (hence causing write I/O even in a read-only query) but [1] indicates
> that only vacuum will set them ddl will clear them.

Hm, I don't recall that, but I've not been involved in the last few rounds
of hacking on that mechanism.

			regards, tom lane




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

* Re: index-only-scan when there is an index on all columns
  2018-02-27 20:49 index-only-scan when there is an index on all columns Hadi Moshayedi <hadi@citusdata.com>
  2018-02-27 21:08 ` Re: index-only-scan when there is an index on all columns Tom Lane <tgl@sss.pgh.pa.us>
  2018-02-27 21:18   ` Re: index-only-scan when there is an index on all columns David G. Johnston <david.g.johnston@gmail.com>
  2018-02-27 21:58     ` Re: index-only-scan when there is an index on all columns Tom Lane <tgl@sss.pgh.pa.us>
@ 2018-02-27 22:10       ` David G. Johnston <david.g.johnston@gmail.com>
  1 sibling, 0 replies; 9+ messages in thread

From: David G. Johnston @ 2018-02-27 22:10 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Hadi Moshayedi <hadi@citusdata.com>; pgsql-general

On Tue, Feb 27, 2018 at 2:58 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

> "David G. Johnston" <david.g.johnston@gmail.com> writes:
>


> > Also, I recall discussion that select statements will touch the
> visibility
> > map (hence causing write I/O even in a read-only query) but [1] indicates
> > that only vacuum will set them ddl will clear them.
>
> Hm, I don't recall that, but I've not been involved in the last few rounds
> of hacking on that mechanism.
>

​I was confusing hint bits with the visibility map, sorry for the noise
and/or confusion.

David J.
​

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

* Re: index-only-scan when there is an index on all columns
  2018-02-27 20:49 index-only-scan when there is an index on all columns Hadi Moshayedi <hadi@citusdata.com>
  2018-02-27 21:08 ` Re: index-only-scan when there is an index on all columns Tom Lane <tgl@sss.pgh.pa.us>
  2018-02-27 21:18   ` Re: index-only-scan when there is an index on all columns David G. Johnston <david.g.johnston@gmail.com>
  2018-02-27 21:58     ` Re: index-only-scan when there is an index on all columns Tom Lane <tgl@sss.pgh.pa.us>
@ 2018-02-27 22:11       ` Andres Freund <andres@anarazel.de>
  2018-02-28 00:15         ` Re: index-only-scan when there is an index on all columns David Rowley <david.rowley@2ndquadrant.com>
  1 sibling, 1 reply; 9+ messages in thread

From: Andres Freund @ 2018-02-27 22:11 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: David G. Johnston <david.g.johnston@gmail.com>; Hadi Moshayedi <hadi@citusdata.com>; pgsql-general

On 2018-02-27 16:58:11 -0500, Tom Lane wrote:
> > Also, I recall discussion that select statements will touch the visibility
> > map (hence causing write I/O even in a read-only query) but [1] indicates
> > that only vacuum will set them ddl will clear them.
> 
> Hm, I don't recall that, but I've not been involved in the last few rounds
> of hacking on that mechanism.

I'm fairly certain that only vacuum and table rewrites like cluster sets
all-visible, and that there was never any released code that did so
during SELECT. I think there were a few patches debating whether we
could change that, but they never really got anywhere afair.

Greetings,

Andres Freund




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

* Re: index-only-scan when there is an index on all columns
  2018-02-27 20:49 index-only-scan when there is an index on all columns Hadi Moshayedi <hadi@citusdata.com>
  2018-02-27 21:08 ` Re: index-only-scan when there is an index on all columns Tom Lane <tgl@sss.pgh.pa.us>
  2018-02-27 21:18   ` Re: index-only-scan when there is an index on all columns David G. Johnston <david.g.johnston@gmail.com>
  2018-02-27 21:58     ` Re: index-only-scan when there is an index on all columns Tom Lane <tgl@sss.pgh.pa.us>
  2018-02-27 22:11       ` Re: index-only-scan when there is an index on all columns Andres Freund <andres@anarazel.de>
@ 2018-02-28 00:15         ` David Rowley <david.rowley@2ndquadrant.com>
  2018-02-28 00:19           ` Re: index-only-scan when there is an index on all columns Andres Freund <andres@anarazel.de>
  0 siblings, 1 reply; 9+ messages in thread

From: David Rowley @ 2018-02-28 00:15 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; David G. Johnston <david.g.johnston@gmail.com>; Hadi Moshayedi <hadi@citusdata.com>; pgsql-general

On 28 February 2018 at 11:11, Andres Freund <andres@anarazel.de> wrote:
> I'm fairly certain that only vacuum and table rewrites like cluster sets
> all-visible,

I don't think the pages are set all visible again after a rewrite.

-- 
 David Rowley                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services




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

* Re: index-only-scan when there is an index on all columns
  2018-02-27 20:49 index-only-scan when there is an index on all columns Hadi Moshayedi <hadi@citusdata.com>
  2018-02-27 21:08 ` Re: index-only-scan when there is an index on all columns Tom Lane <tgl@sss.pgh.pa.us>
  2018-02-27 21:18   ` Re: index-only-scan when there is an index on all columns David G. Johnston <david.g.johnston@gmail.com>
  2018-02-27 21:58     ` Re: index-only-scan when there is an index on all columns Tom Lane <tgl@sss.pgh.pa.us>
  2018-02-27 22:11       ` Re: index-only-scan when there is an index on all columns Andres Freund <andres@anarazel.de>
  2018-02-28 00:15         ` Re: index-only-scan when there is an index on all columns David Rowley <david.rowley@2ndquadrant.com>
@ 2018-02-28 00:19           ` Andres Freund <andres@anarazel.de>
  0 siblings, 0 replies; 9+ messages in thread

From: Andres Freund @ 2018-02-28 00:19 UTC (permalink / raw)
  To: David Rowley <david.rowley@2ndquadrant.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; David G. Johnston <david.g.johnston@gmail.com>; Hadi Moshayedi <hadi@citusdata.com>; pgsql-general

On 2018-02-28 13:15:45 +1300, David Rowley wrote:
> On 28 February 2018 at 11:11, Andres Freund <andres@anarazel.de> wrote:
> > I'm fairly certain that only vacuum and table rewrites like cluster sets
> > all-visible,
> 
> I don't think the pages are set all visible again after a rewrite.

You're right. We freeze the tuples, but don't set the heap / FSM bits.

Greetings,

Andres Freund




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


end of thread, other threads:[~2018-02-28 00:19 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-02-27 20:37 index-only-scan when there is an index on all columns Hadi Moshayedi <hadi@moshayedi.net>
2018-02-27 20:49 index-only-scan when there is an index on all columns Hadi Moshayedi <hadi@citusdata.com>
2018-02-27 21:08 ` Tom Lane <tgl@sss.pgh.pa.us>
2018-02-27 21:18   ` David G. Johnston <david.g.johnston@gmail.com>
2018-02-27 21:58     ` Tom Lane <tgl@sss.pgh.pa.us>
2018-02-27 22:10       ` David G. Johnston <david.g.johnston@gmail.com>
2018-02-27 22:11       ` Andres Freund <andres@anarazel.de>
2018-02-28 00:15         ` David Rowley <david.rowley@2ndquadrant.com>
2018-02-28 00:19           ` Andres Freund <andres@anarazel.de>

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