Re: Optimizer Parameters

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Martin Foster <martin(at)ethereal-realms(dot)org>
Cc: PostgreSQL Novice <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Optimizer Parameters
Date: 2003-07-12 20:46:10
Message-ID: 18388.1058042770@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice pgsql-performance

Martin Foster <martin(at)ethereal-realms(dot)org> writes:
> force Postgres into using Indexes when available. So I changed the
> following two lines in the .conf file:

> enable_seqscan = false
> enable_nestloop = false

> This was recommended in the documentation,

Where would you say that setting those off in the config file is
"recommended"?

> Now how sane is it to keep those options turned off?

It isn't. If you have to force them off for a particular query, do
so right before you issue that query, and turn them on again after.
Turning them off globally is sure to cause you pain later.

> And any
> way to have the planner quiet guessing tens of thousands of rows will be
> return when there are at most hundred?

> AND Po.PostTimestamp > (LOCALTIMESTAMP - INTERVAL '10 minutes')
> AND Po.PuppetName IS NOT NULL

> -> Seq Scan on post po (cost=0.00..14369.84 rows=40513 width=41) (actual time=2820.88..2826.30 rows=392 loops=1)
> Filter: ((posttimestamp > (('now'::text)::timestamp(6) without time zone - '00:10'::interval)) AND (puppetname IS NOT NULL))

Not with that coding technique; "LOCALTIMESTAMP - INTERVAL '10 minutes'"
isn't a constant and so the planner can't look at its statistics to
see that only a small part of the table will be selected.

There are two standard workarounds for this:

1. Do the timestamp arithmetic on the client side, so that the query
you send the backend has a simple constant:

... AND Po.PostTimestamp > '2003-07-12 16:27'

2. Create a function that is falsely marked immutable, viz:

create function ago(interval) returns timestamp without time zone as
'select localtimestamp - $1' language sql immutable strict;

... AND Po.PostTimestamp > ago('10 minutes')

Because the function is marked immutable, the planner will reduce
"ago('10 minutes')" to a constant on sight, and then use that value
for planning purposes. This technique can cause problems, since
in some contexts the reduction will occur prematurely, but as long
as you only use ago() in interactively-issued queries it works okay.

regards, tom lane

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Ron Johnson 2003-07-12 21:13:30 Re: Problem with psql interface
Previous Message Grant Rhodes 2003-07-12 18:28:28 Problem with psql interface

Browse pgsql-performance by date

  From Date Subject
Next Message Ron Johnson 2003-07-12 21:09:03 [OT] Such incredible h/w (was Re: Dual Xeon + HW RAID question)
Previous Message Nikolaus Dilger 2003-07-12 18:25:14 Re: Dual Xeon + HW RAID question