Re: How to EXPLAIN statements inside a trigger function?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "John D(dot) Burger" <john(at)mitre(dot)org>
Cc: General PostgreSQL List <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to EXPLAIN statements inside a trigger function?
Date: 2007-12-09 00:09:33
Message-ID: 29059.1197158973@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"John D. Burger" <john(at)mitre(dot)org> writes:
> I'm developing some triggers for the first time, and I'm having
> trouble analyzing their performance. Does anyone have any advice for
> doing EXPLAIN and the like on statements involving NEW?

The critical thing you're probably running into is that the planner
sees such things as parameterized queries, and may not be able to
produce plans as good as what it comes up with for queries that have
simple constants in place of the parameters.

You can investigate what the parameterized plans look like with
PREPARE and EXPLAIN EXECUTE. For instance, given your problem query

> select array[NEW.sense1ID] || p.sensePath, NEW.weight + p.weight
> from allSenseRelationPaths as p
> where (NEW.sense2ID = p.sensePath[1])
> and not NEW.sense1ID = any (sensePath)
> and NEW.weight + p.weight > minWeight
> and not exists (select 1 from allSenseRelationPaths as EXISTING
> where EXISTING.sensepath = array[NEW.sense1ID] || p.sensePath)

you'd do something like

PREPARE foo(type-of-sense1ID, type-of-sense2ID, type-of-weight, type-of-minWeight) AS
select array[$1] || p.sensePath, $3 + p.weight
from allSenseRelationPaths as p
where ($1 = p.sensePath[1])
and not $1 = any (sensePath)
and $3 + p.weight > $4
and not exists (select 1 from allSenseRelationPaths as EXISTING
where EXISTING.sensepath = array[$1] || p.sensePath);

EXPLAIN [ANALYZE] EXECUTE foo(... some realistic values ...);

Note that this isn't specific to trigger functions --- any
plpgsql function that does queries will have the same issues.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Efraín López 2007-12-09 04:15:14 Re: libpq messages language
Previous Message Greg Smith 2007-12-08 19:53:13 Re: wriring a file to a database