Re: Query plans for plpgsql triggers

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Eric B(dot) Ridge" <ebr(at)tcdi(dot)com>
Cc: Postgresql <pgsql-general(at)postgresql(dot)org>
Subject: Re: Query plans for plpgsql triggers
Date: 2006-03-25 04:39:40
Message-ID: 14507.1143261580@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Eric B. Ridge" <ebr(at)tcdi(dot)com> writes:
> When is the UPDATE statement inside foo() planned? When the trigger
> is first created, or when it's first used per backend, or every time
> it's used per backend?

First use per backend, ignoring corner cases such as replacing the
function definition.

> I dunno what plan is being generated, but it's gotta be using a
> sequential scan.

The issue is probably that the planner is seeing a parameterized
query. Try this:

prepare foo(int8) as update some_other_table SET field = 'value' WHERE id = $1;
explain execute foo(42);

and see what plan you get. If the id field has sufficiently
discouraging statistics then the planner may think that a seqscan
is the safest plan. In a "normal" query where you're comparing id
to a constant, the planner can see whether the constant matches any
of the most common values for the column --- if it doesn't then an
indexscan is a good plan.

If you really want a replan every time, you can get it by using
EXECUTE.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Eric B. Ridge 2006-03-25 05:10:40 Re: Query plans for plpgsql triggers
Previous Message Eric B. Ridge 2006-03-25 04:25:04 Query plans for plpgsql triggers