Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

Re: Query plans for plpgsql triggers


  • From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
  • To: "Eric B. Ridge" <ebr(at)tcdi(dot)com>
  • Cc: Postgresql <pgsql-general(at)postgresql(dot)org>
  • Subject: Re: Query plans for plpgsql triggers
  • Date: Fri, 24 Mar 2006 23:39:40 -0500
  • Message-id: <14507(dot)1143261580(at)sss(dot)pgh(dot)pa(dot)us>

"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



Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group