Index on timestamp field, and now() - Mailing list pgsql-general
From | Denis Perchine |
---|---|
Subject | Index on timestamp field, and now() |
Date | |
Msg-id | 20020211103256.0F98A1FEDF@mx.webmailstation.com Whole thread Raw |
List | pgsql-general |
Hello, I have quite interesting problem. I have a table with a timestamp field. I have an index on it. When I use constant date in where clause, PostgreSQL uses index. But when I try to use now() there, it uses a sequence scan. As far as I can understand in inside any query now() is a constant. What is the problem here. webmailstation=> explain analyze select id,send_date from queue where send_date > now()+'20 year'::interval; NOTICE: QUERY PLAN: Seq Scan on queue (cost=0.00..10568.09 rows=30268 width=12) (actual time=29347.82..29347.82 rows=0 loops=1) Total runtime: 29347.97 msec EXPLAIN webmailstation=> explain analyze select id,send_date from queue where send_date > '2022-1-1'; NOTICE: QUERY PLAN: Index Scan using queue_senddate_key on queue (cost=0.00..37.71 rows=9 width=12) (actual time=29.20..29.20 rows=0 loops=1) Total runtime: 30.26 msec EXPLAIN webmailstation=> set enable_seqscan to off; SET VARIABLE webmailstation=> explain analyze select id,send_date from queue where send_date > now()+'20 year'::interval; NOTICE: QUERY PLAN: Seq Scan on queue (cost=100000000.00..100010568.09 rows=30268 width=12) (actual time=24775.73..24775.73 rows=0 loops=1) Total runtime: 24775.88 msec EXPLAIN webmailstation=> select id,send_date from queue where send_date > '2022-1-1'; id | send_date ----+----------- (0 rows) webmailstation=> select count(*) from queue; count ------- 89461 (1 row) This is a statistics for this column. webmailstation=# select * from pg_statistic where starelid=16729 and staattnum=9; starelid | staattnum | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | staop1 | staop2 | staop3 | staop4 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stavalues1 | stavalues2 | stavalues3 | stavalues4 ----------+-----------+-------------+----------+-------------+----------+---- ------+----------+----------+--------+--------+--------+--------+------------ -+-------------+-------------+-------------+--------------------------------- ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- ----------------------------------------------------------------------------- -------------------------------------------------------+------------+-------- ----+------------ 16729 | 9 | 0 | 8 | -1 | 2 | 3 | 0 | 0 | 1322 | 1322 | 0 | 0 | | {-0.142043} | | | {"2001-12-27 08:01:04-05","2002-02-11 07:21:53.995488-05","2002-02-11 11:11:03.998751-05","2002-02-11 17:09:21.375388-05","2002-02-12 07:50:49.80789-05","2002-02-12 23:46:06-05","2002-02-13 23:58:54-05","2002-02-15 15:13:30.445873-05","2002-02-17 15:54:10.359982-05","2002-02-26 07:41:59-05","2003-07-21 13:10:06-04"} | (1 row) Any ides would be appreciated. -- Denis
pgsql-general by date: