Re: Trigger Procedure Error: NEW used in query that is not in a rule

From: Decibel! <decibel(at)decibel(dot)org>
To: "Javier Fonseca V(dot)" <fonsecajavier(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Trigger Procedure Error: NEW used in query that is not in a rule
Date: 2007-08-15 18:28:33
Message-ID: 20070815182833.GP54135@nasby.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

On Sat, Aug 11, 2007 at 02:43:30AM -0500, Javier Fonseca V. wrote:
> Hello.
>
> I'm doing a Trigger Procedure in pl/pgSQL. It makes some kind of auditing.
>
> I think that it's working alright except for the next line:
>
> EXECUTE 'INSERT INTO ' || quote_ident(somedynamictablename) || ' SELECT
> new.*';
>
> PostgreSQL keeps telling me: "ERROR: NEW used in query that is not in a
> rule". I think that this NEW problem is because of the scope of the EXECUTE
> statement (outside the scope of the trigger), so it doesn't recognize the
> NEW record.

Sort-of... the issue is that EXECUTE hands the string off to the
backend, which has no clue what "NEW" is; only the trigger procedure
understands NEW.

> Maybe I could fix it concatenating column names and the 'new' values but I
> want to do my trigger as flexible as possible (I have several tables to
> audit).
>
> Somebody has any suggestion?

You could theoretically make the trigger entirely dynamic by having it
pull the needed info out of the system catalogs... but I wouldn't want
to see the performance of that... If you care about performance *at
all*, I'd suggest writing some code that will generate the triggers for
a given table for you. I don't expect it'd be much harder than writing a
completely dynamic trigger.
--
Decibel!, aka Jim Nasby decibel(at)decibel(dot)org
EnterpriseDB http://enterprisedb.com 512.569.9461 (cell)

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Leif B. Kristensen 2007-08-15 18:31:45 Re: pg_dump on local Windows, pg_restore on Linux?
Previous Message Dmitry Koterov 2007-08-15 18:27:53 Re: Deadlocks caused by a foreign key constraint

Browse pgsql-sql by date

  From Date Subject
Next Message Jon Collette 2007-08-15 18:46:02 Boolean without default declared
Previous Message Jon Horsman 2007-08-15 13:16:23 Re: When is a shared library unloaded?