Why sequential scan for currval?

From: John Barham <jbarham(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Why sequential scan for currval?
Date: 2005-04-27 07:28:18
Message-ID: 4f34febc0504270028579a6da5@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

test=# create table tt (id serial unique, s varchar);
[populate tt w/ 100000 rows]
test=# insert into tt (s) values ('foo');
test=# select currval('tt_id_seq');
currval
---------
100002
(1 row)
test=# explain select s from tt where id = 100002;
QUERY PLAN
---------------------------------------------------------------------
Index Scan using tt_id_key on tt (cost=0.00..6.01 rows=1 width=32)
Index Cond: (id = 100002)
(2 rows)
test=# explain select s from tt where id = currval('tt_id_key');
QUERY PLAN
------------------------------------------------------
Seq Scan on tt (cost=0.00..1734.42 rows=1 width=32)
Filter: (id = currval('tt_id_key'::text))
(2 rows)

Why is a sequential scan used when comparing id to currval() value vs.
index scan when compared to a constant?

TIA,

John

Responses

Browse pgsql-general by date

  From Date Subject
Next Message John Barham 2005-04-27 07:32:27 Re: Why sequential scan for currval?
Previous Message Stephane Bortzmeyer 2005-04-27 07:12:11 Re: PRIMARY KEY on a *group* of columns imply that each column is NOT