Re: query gone haywire :) - Mailing list pgsql-general

From Tom Lane
Subject Re: query gone haywire :)
Date
Msg-id 12037.1097244696@sss.pgh.pa.us
Whole thread Raw
In response to query gone haywire :)  (Robin Ericsson <robin.ericsson@profecta.se>)
Responses Re: query gone haywire :)  (Robin Ericsson <robin.ericsson@profecta.se>)
List pgsql-general
Robin Ericsson <robin.ericsson@profecta.se> writes:
> The query have been running ok for some time now, but this morning I
> decided to run vacuum analyze (vacuumdb -a -z) on the cluster, and
> suddenly the query isn't running very well at all.

>          ->  Index Scan using idx_d_entered on data  (cost=0.00..18024.04 rows=50360 width=16) (actual
time=0.210..0.247rows=1 loops=1) 
>                Index Cond: (((('now'::text)::timestamp(6) with time zone)::timestamp without time zone -
'00:01:00'::interval)< entered) 

You're running into the well-known problem that the planner can't make
good estimates for index conditions that involve non-constant terms
(such as CURRENT_TIMESTAMP).  Lacking a decent estimate, it guesses that
this scan will produce many more rows than it really will, and so it
tends to favor plans that would be good in that scenario, but are not
optimal for retrieving just a couple of rows.

One workaround is to do the date arithmetic on the client side; another
is to cheat by hiding the arithmetic in a function like "ago(interval)"
that you lyingly claim is IMMUTABLE.  See the pgsql-performance
archives.

            regards, tom lane

pgsql-general by date:

Previous
From: Phil Endecott
Date:
Subject: pg_restore -l and schema name
Next
From: David Garamond
Date:
Subject: Re: when to use NULL and when to NOT NULL DEFAULT ''