Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 752C69F9C73 for ; Wed, 15 Aug 2007 15:28:41 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 40550-06-5 for ; Wed, 15 Aug 2007 15:28:33 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from noel.decibel.org (noel.decibel.org [67.100.216.10]) by postgresql.org (Postfix) with ESMTP id D996D9F97A7 for ; Wed, 15 Aug 2007 15:28:35 -0300 (ADT) Received: by noel.decibel.org (Postfix, from userid 1001) id 1049E564FF; Wed, 15 Aug 2007 13:28:35 -0500 (CDT) Received: (hashcash-sendmail, from uid 1001); Wed, 15 Aug 2007 13:28:33 -0500 Date: Wed, 15 Aug 2007 13:28:33 -0500 From: Decibel! To: "Javier Fonseca V." Cc: pgsql-general@postgresql.org Subject: Re: Trigger Procedure Error: NEW used in query that is not in a rule Message-ID: <20070815182833.GP54135@nasby.net> References: <86d227fd0708110043q341a2908q67e87570bca89492@mail.gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="AX2clmtSw6ieHBMM" Content-Disposition: inline In-Reply-To: <86d227fd0708110043q341a2908q67e87570bca89492@mail.gmail.com> X-Operating-System: FreeBSD 6.1-RELEASE-p10 amd64 X-Distributed: Join the Effort! http://www.distributed.net User-Agent: Mutt/1.5.13 (2006-08-11) X-Hashcash: 1:20:070815:fonsecajavier@gmail.com::SXavVqSr2pbuF6HE:00000000000000 000000000000000000000000241l X-Hashcash: 1:20:070815:pgsql-general@postgresql.org::nLDtiDdi9JsGKLS1:000000000 0000000000000000000000001+bx X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200708/878 X-Sequence-Number: 118727 --AX2clmtSw6ieHBMM Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sat, Aug 11, 2007 at 02:43:30AM -0500, Javier Fonseca V. wrote: > Hello. >=20 > I'm doing a Trigger Procedure in pl/pgSQL. It makes some kind of auditin= g. >=20 > I think that it's working alright except for the next line: >=20 > EXECUTE 'INSERT INTO ' || quote_ident(somedynamictablename) || ' SELECT > new.*'; >=20 > 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 EXEC= UTE > 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). >=20 > 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. --=20 Decibel!, aka Jim Nasby decibel@decibel.org EnterpriseDB http://enterprisedb.com 512.569.9461 (cell) --AX2clmtSw6ieHBMM Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.3 (FreeBSD) iD8DBQFGw0XRdO30qud8SkgRArunAKCJhij2v9ngYeLa5o/8kiUsEW53ugCaA4mL 2X6x62CkNWsC5B9vkZPpyWo= =PPHW -----END PGP SIGNATURE----- --AX2clmtSw6ieHBMM--