pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Decibel! <decibel@decibel.org>
To: Javier Fonseca V. <fonsecajavier@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: Trigger Procedure Error: NEW used in query that is not in a rule
Date: Wed, 15 Aug 2007 13:28:33 -0500
Message-ID: <20070815182833.GP54135@nasby.net> (raw)
In-Reply-To: <86d227fd0708110043q341a2908q67e87570bca89492@mail.gmail.com>
References: <86d227fd0708110043q341a2908q67e87570bca89492@mail.gmail.com>

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@decibel.org
EnterpriseDB      http://enterprisedb.com      512.569.9461 (cell)

view thread (11+ messages)  latest in thread

Message-ID: <20070815182833.GP54135@nasby.net>
Permalink:  ../20070815182833.GP54135@nasby.net/
Also on:    postgresql.org/message-id/20070815182833.GP54135@nasby.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: decibel@decibel.org, fonsecajavier@gmail.com, pgsql-general@postgresql.org
  Subject: Re: Trigger Procedure Error: NEW used in query that is not in a rule
  In-Reply-To: <20070815182833.GP54135@nasby.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox