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

Re: Trigger functions with dynamic SQL

From: Andreas Haumer <andreas(at)xss(dot)co(dot)at>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Trigger functions with dynamic SQL
Date: 2004-07-24 15:46:33
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-sql
Hash: SHA1


Many thanks for your reply!

Tom Lane wrote:
> Andreas Haumer <andreas(at)xss(dot)co(dot)at> writes:
>>It seems I would have to use EXECUTE on dynamically constructed
>>PL/PGSQL statements in order to have my trigger function recognize
>>the parameters given to the trigger in TG_ARGV[]
> Yup, that's exactly right.  plpgsql isn't designed for this; it's

Ok, that's what I thought. Thanks for confirmation.

> designed for situations where it can pre-plan and cache plans for
> queries, and dynamically-inserted table and column names would just
> break that completely.  So you have to fall back to the mechanisms
> for fully general constructed-on-the-fly queries, which work but
> are a bit painful to use.
"a bit painful" is the understatement of the year!  :-)

I just can't figure out where and how many quotation marks
I have to place in my function. Some examples would be helpful
(the examples in the manual are quite simple and always end
where it begins to become interesting for me...)

Also: what is the performance impact of using EXECUTE and
constructed-on-the-fly queries?

> You might want to look at pltcl instead, which is much friendlier
> to dynamically generated queries (since that's the only way it
> does things).  Of course, if you've never used Tcl there'll be
> a bit of a learning curve :-(
My experience with Tcl is almost zero. :-(

What about writing trigger functions in C? It should be
quite easy to dynamically create the SQL statements needed
here and it seems I can access the trigger arguments through
the tg_trigger->tgargs array. Would this be a better way to
go? (faster code, but perhaps painful to write as well, too)
I found some examples in the PostgreSQL sources under .../contrib/spi/
I think I will have to take a deeper look at that...

- - andreas

- --
Andreas Haumer                     | mailto:andreas(at)xss(dot)co(dot)at
*x Software + Systeme              |
Karmarschgasse 51/2/20             | Tel: +43-1-6060114-0
A-1100 Vienna, Austria             | Fax: +43-1-6060114-71
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla -


In response to


pgsql-sql by date

Next:From: Tom LaneDate: 2004-07-24 16:13:19
Subject: Re: Trigger functions with dynamic SQL
Previous:From: Tom LaneDate: 2004-07-24 15:01:47
Subject: Re: Trigger functions with dynamic SQL

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group