Query plan when using currval

From: Steve Baldwin <steve(dot)baldwin(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Query plan when using currval
Date: 2018-05-09 06:24:10
Message-ID: CAKE1Aib_H1Si0wNv+27-RKNGoKv+afmciZhxPaKsoYrMfSAJfw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hi,

Can someone please help me understand why the first query doesn't use the
primary key index whereas the second query does use it:

bcaas=> explain analyze select id from log_statement where id =
currval('log_statement_id_seq');
QUERY PLAN

--------------------------------------------------------------------------------------------------------
Seq Scan on log_statement (cost=0.00..14.05 rows=1 width=8) (actual
time=0.027..0.027 rows=0 loops=1)
Filter: (id = currval('log_statement_id_seq'::regclass))
Rows Removed by Filter: 9
Planning time: 0.081 ms
Execution time: 0.048 ms
(5 rows)

bcaas=> explain analyze select id from log_statement where id = (select
currval('log_statement_id_seq'));
QUERY PLAN

---------------------------------------------------------------------------------------------------------------------------------------
Index Only Scan using log_statement_pkey on log_statement
(cost=0.16..8.18 rows=1 width=8) (actual time=0.017..0.017 rows=0 loops=1)
Index Cond: (id = $0)
Heap Fetches: 0
InitPlan 1 (returns $0)
-> Result (cost=0.00..0.01 rows=1 width=8) (actual time=0.003..0.004
rows=1 loops=1)
Planning time: 0.105 ms
Execution time: 0.045 ms
(7 rows)

(This is with pg 10.3)

Thanks,

Steve

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2018-05-09 14:17:52 Re: Query plan when using currval
Previous Message Antonio Silva 2018-05-09 00:55:55 Re: issues when installing postgres