pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedindex only scan question
8+ messages / 3 participants
[nested] [flat]
* index only scan question
@ 2018-11-09 12:58 Daniel Westermann <daniel.westermann@dbi-services.com>
0 siblings, 2 replies; 8+ messages in thread
From: Daniel Westermann @ 2018-11-09 12:58 UTC (permalink / raw)
To: pgsql-general
Hi quick
question: Given these steps:
postgres=# explain (analyze,buffers,costs off) select a from t1 where b = 5;
QUERY PLAN
--------------------------------------------------------------------------
Index Only Scan using i2 on t1 (actual time=0.056..0.058 rows=1 loops=1)
Index Cond: (b = 5)
Heap Fetches: 0
Buffers: shared hit=4
Planning time: 0.421 ms
Execution time: 0.111 ms
(6 rows)
postgres=# update t1 set a = 30 where b = 5;
UPDATE 1
postgres=# explain (analyze,buffers,costs off) select a from t1 where b = 5;
QUERY PLAN
--------------------------------------------------------------------------
Index Only Scan using i2 on t1 (actual time=0.039..0.042 rows=1 loops=1)
Index Cond: (b = 5)
Heap Fetches: 2
Buffers: shared hit=5
Planning time: 0.176 ms
Execution time: 0.082 ms
The 2 heap fetches for the second run are clear to me, because of the pointer from the old version of the row to the new one. But why does the next execution only need one heap fetch?
postgres=# explain (analyze,buffers,costs off) select a from t1 where b = 5;
QUERY PLAN
--------------------------------------------------------------------------
Index Only Scan using i2 on t1 (actual time=0.046..0.049 rows=1 loops=1)
Index Cond: (b = 5)
Heap Fetches: 1
Buffers: shared hit=5
Planning time: 0.194 ms
Execution time: 0.097 ms
Is that because of some sort of caching?
Thanks in advance
Daniel
=
Attachments:
Outlook-wtwhwnpb.png (image/png, 3.8K) download
view image
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: index only scan question
@ 2018-11-09 13:12 Laurenz Albe <laurenz.albe@cybertec.at>
parent: Daniel Westermann <daniel.westermann@dbi-services.com>
1 sibling, 2 replies; 8+ messages in thread
From: Laurenz Albe @ 2018-11-09 13:12 UTC (permalink / raw)
To: Daniel Westermann <daniel.westermann@dbi-services.com>; pgsql-general
Daniel Westermann wrote:
> question: Given these steps:
>
>
> postgres=# explain (analyze,buffers,costs off) select a from t1 where b = 5;
> QUERY PLAN
> --------------------------------------------------------------------------
> Index Only Scan using i2 on t1 (actual time=0.056..0.058 rows=1 loops=1)
> Index Cond: (b = 5)
> Heap Fetches: 0
> Buffers: shared hit=4
> Planning time: 0.421 ms
> Execution time: 0.111 ms
> (6 rows)
>
> postgres=# update t1 set a = 30 where b = 5;
> UPDATE 1
> postgres=# explain (analyze,buffers,costs off) select a from t1 where b = 5;
> QUERY PLAN
> --------------------------------------------------------------------------
> Index Only Scan using i2 on t1 (actual time=0.039..0.042 rows=1 loops=1)
> Index Cond: (b = 5)
> Heap Fetches: 2
> Buffers: shared hit=5
> Planning time: 0.176 ms
> Execution time: 0.082 ms
>
> The 2 heap fetches for the second run are clear to me, because of the pointer from the old version of the row to the new one. But why does the next execution only need one heap fetch?
>
>
> postgres=# explain (analyze,buffers,costs off) select a from t1 where b = 5;
> QUERY PLAN
> --------------------------------------------------------------------------
> Index Only Scan using i2 on t1 (actual time=0.046..0.049 rows=1 loops=1)
> Index Cond: (b = 5)
> Heap Fetches: 1
> Buffers: shared hit=5
> Planning time: 0.194 ms
> Execution time: 0.097 ms
>
> Is that because of some sort of caching?
I'd say the old index tuple was killed during the first scan:
https://www.cybertec-postgresql.com/en/killed-index-tuples/
Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.com
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: index only scan question
@ 2018-11-09 13:38 Andreas Kretschmer <andreas@a-kretschmer.de>
parent: Daniel Westermann <daniel.westermann@dbi-services.com>
1 sibling, 1 reply; 8+ messages in thread
From: Andreas Kretschmer @ 2018-11-09 13:38 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Am 09.11.2018 um 13:58 schrieb Daniel Westermann:
> Is that because of some sort of caching?
no, but vacuum updated the visibility map in the meantime.
Regards, Andreas
--
2ndQuadrant - The PostgreSQL Support Company.
www.2ndQuadrant.com
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: index only scan question
@ 2018-11-09 13:48 Daniel Westermann <daniel.westermann@dbi-services.com>
parent: Andreas Kretschmer <andreas@a-kretschmer.de>
0 siblings, 0 replies; 8+ messages in thread
From: Daniel Westermann @ 2018-11-09 13:48 UTC (permalink / raw)
To: Andreas Kretschmer <andreas@a-kretschmer.de>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
>Am 09.11.2018 um 13:58 schrieb Daniel Westermann:
>> Is that because of some sort of caching?
>no, but vacuum updated the visibility map in the meantime.
No, it do not, double checked that with:
select pg_visibility_map('t1'::regclass, 0);
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: index only scan question
@ 2018-11-09 14:00 Daniel Westermann <daniel.westermann@dbi-services.com>
parent: Laurenz Albe <laurenz.albe@cybertec.at>
1 sibling, 0 replies; 8+ messages in thread
From: Daniel Westermann @ 2018-11-09 14:00 UTC (permalink / raw)
To: Laurenz Albe <laurenz.albe@cybertec.at>; pgsql-general
>I'd say the old index tuple was killed during the first scan:
>https://www.cybertec-postgresql.com/en/killed-index-tuples/<https://www.cybertec-postgresql.com/e...;
Thanks Laurenz, I will check that
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: index only scan question
@ 2018-11-09 14:57 Daniel Westermann <daniel.westermann@dbi-services.com>
parent: Laurenz Albe <laurenz.albe@cybertec.at>
1 sibling, 1 reply; 8+ messages in thread
From: Daniel Westermann @ 2018-11-09 14:57 UTC (permalink / raw)
To: Laurenz Albe <laurenz.albe@cybertec.at>; pgsql-general
>I'd say the old index tuple was killed during the first scan:
>https://www.cybertec-postgresql.com/en/killed-index-tuples/<https://www.cybertec-postgresql.com/e...;
... from your blog: "Whenever an index scan fetches a heap tuple only to find that it is dead (that the entire “HOT chain” of tuples is dead, to be more precise), it marks the index tuple as “killed”. Then future index scans can simply ignore it.
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: index only scan question
@ 2018-11-09 14:59 Daniel Westermann <daniel.westermann@dbi-services.com>
parent: Daniel Westermann <daniel.westermann@dbi-services.com>
0 siblings, 1 reply; 8+ messages in thread
From: Daniel Westermann @ 2018-11-09 14:59 UTC (permalink / raw)
To: Laurenz Albe <laurenz.albe@cybertec.at>; pgsql-general
sorry, hit the wrong key
>I'd say the old index tuple was killed during the first scan:
>https://www.cybertec-postgresql.com/en/killed-index-tuples/<https://www.cybertec-postgresql.com/e...;
... from your blog: "Whenever an index scan fetches a heap tuple only to find that it is dead (that the entire “HOT chain” of tuples is dead, to be more precise), it marks the index tuple as “killed”. Then future index scans can simply ignore it.
I understand that, but in my case the chain is not dead so this does not explain the difference. Do I miss something?
Regards
Daniel
^ permalink raw reply [nested|flat] 8+ messages in thread
* Re: index only scan question
@ 2018-11-09 15:51 Laurenz Albe <laurenz.albe@cybertec.at>
parent: Daniel Westermann <daniel.westermann@dbi-services.com>
0 siblings, 0 replies; 8+ messages in thread
From: Laurenz Albe @ 2018-11-09 15:51 UTC (permalink / raw)
To: Daniel Westermann <daniel.westermann@dbi-services.com>; pgsql-general
Daniel Westermann wrote:
> >I'd say the old index tuple was killed during the first scan:
> >https://www.cybertec-postgresql.com/en/killed-index-tuples/
>
> ... from your blog: "Whenever an index scan fetches a heap tuple only to find that it is dead
> (that the entire “HOT chain” of tuples is dead, to be more precise), it marks the index tuple
> as “killed”. Then future index scans can simply ignore it.
>
> I understand that, but in my case the chain is not dead so this does not explain the difference.
> Do I miss something?
I assume that the UPDATE was not HOT, because the first scan had to fetch two tuples.
After the UPDATE, the original tuple was dead (the HOT chain consists
only of a single tuple here, because it was no HOT update).
The first index scan detects that and marks the index tuple as killed.
The second index scan only visits the new tuple.
Yours,
Laurenz Albe
^ permalink raw reply [nested|flat] 8+ messages in thread
end of thread, other threads:[~2018-11-09 15:51 UTC | newest]
Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-11-09 12:58 index only scan question Daniel Westermann <daniel.westermann@dbi-services.com>
2018-11-09 13:12 ` Laurenz Albe <laurenz.albe@cybertec.at>
2018-11-09 14:00 ` Daniel Westermann <daniel.westermann@dbi-services.com>
2018-11-09 14:57 ` Daniel Westermann <daniel.westermann@dbi-services.com>
2018-11-09 14:59 ` Daniel Westermann <daniel.westermann@dbi-services.com>
2018-11-09 15:51 ` Laurenz Albe <laurenz.albe@cybertec.at>
2018-11-09 13:38 ` Andreas Kretschmer <andreas@a-kretschmer.de>
2018-11-09 13:48 ` Daniel Westermann <daniel.westermann@dbi-services.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