Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W7jw6-0006B1-FO for pgsql-sql@arkaria.postgresql.org; Mon, 27 Jan 2014 11:01:02 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1W7jw5-0000xk-Oo for pgsql-sql@arkaria.postgresql.org; Mon, 27 Jan 2014 11:01:01 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W7jw5-0000xe-0N for pgsql-sql@postgresql.org; Mon, 27 Jan 2014 11:01:01 +0000 Received: from nm3-vm6.bullet.mail.ir2.yahoo.com ([212.82.96.95]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W7jvx-0000ty-KG for pgsql-sql@postgresql.org; Mon, 27 Jan 2014 11:01:00 +0000 Received: from [212.82.98.56] by nm3.bullet.mail.ir2.yahoo.com with NNFMP; 27 Jan 2014 11:00:51 -0000 Received: from [212.82.98.96] by tm9.bullet.mail.ir2.yahoo.com with NNFMP; 27 Jan 2014 11:00:51 -0000 Received: from [127.0.0.1] by omp1033.mail.ir2.yahoo.com with NNFMP; 27 Jan 2014 11:00:51 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 524128.29241.bm@omp1033.mail.ir2.yahoo.com Received: (qmail 123 invoked by uid 60001); 27 Jan 2014 11:00:51 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.co.uk; s=s1024; t=1390820451; bh=Kz2/sUKM3pA9heHEBNCjMcX8Xc4Q78cQSnAoTnKSous=; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=ExmmWrprU8bS8bbacBK77SsxPsB/uhrYkuaG7vw1rTAiwnXaCVVI20I0MvbZp1ITV04aYuCL087/nXaeZ3eEYoJ77Sw6z7csCmFrW0gt/7HfUjyQGUawuc7u8m4+tzJjNhnUlF+p32xcayYnsNChSKu2drTS2aMto6fVfgCSh90= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.co.uk; h=X-YMail-OSG:Received:X-Rocket-MIMEInfo:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=XyOierfwFeO29Prg5GcaJnRGFD2Q1XF65VGemKsnIQ14Gtq8U94M8vMSR4ohTQnU1WOJy1BiEFZhdQ9dYXQ+mevL0aYv26gFRKdv2UZIbBwgoJZQwZ6YbOK8yBrNI0ovh36fZQ6xRopGO3vrTSzJgW4u+DZmf7ccYZEQPiWbXXU=; X-YMail-OSG: dKYjzIgVM1lGh5MMMOCKywyeZF1pXj3EiyspIH3oZYPvwql aY8oKpP10xuNWQXAA19DmcrJplnJb3EOBOOAIyYCHme_Y7zQQ8xxddIsuSAZ kz21OMyLt3cYTEhDBakK5c68.MHr4_NLSideZ1R945aRA5F2hGPaVz4PJq7z xM5HHif6RPE4hRvSPBzl2lKHIlQvfOhZQoqCzQCvvFuGzpId58BVAzgzcfDk q9FDfniKbryMKtxBNI9A9Yjv5IXXsTwgSI1Zbi8cxX.EKgDdu43AG0tmIoyK Z6VOc1f74cDkgnNVVnw9E5iN9GxIlOLxcSXURIFrH8hREbNE.hFG.pA5YdMp OwZG2M7Cobkq_WltjNpAnIkRpYhDaMc6fm6yW8n_OcFcOGcr1baSHzACSEZ1 r93otiU7wsHLWnNjWv0YwtWtSB8NBpY0opcbuGNwTeE22v1VKVTpOutQFiy6 T1MTuQNryfmCJcqczjQh4tUH68L4F6pF0ylfb5ygEszPBkrT0yPZUkgdLWr8 T9uUjlOIe9_YDB1GLmsSHGhn8itr.XvhGFmBm_MpcJ0VLLmw_sRMB8ktg5iO B51MLlwFW5EpkfvNExCKFqhnfF4nIZDMvCKZhkEOTaq2vP8PC2jbIXe86SgE 8Kss5aQw- Received: from [194.168.202.210] by web133202.mail.ir2.yahoo.com via HTTP; Mon, 27 Jan 2014 11:00:51 GMT X-Rocket-MIMEInfo: 002.001, CgoKCi0tLS0tIE9yaWdpbmFsIE1lc3NhZ2UgLS0tLS0KPiBGcm9tOiBHbHluIEFzdGlsbCA8Z2x5bmFzdGlsbEB5YWhvby5jby51az4KPiBUbzogc3N5bGxhIDxzdGVmYW5zeWxsYUBnbXguZGU.OyAicGdzcWwtc3FsQHBvc3RncmVzcWwub3JnIiA8cGdzcWwtc3FsQHBvc3RncmVzcWwub3JnPgo.IENjOiAKPiBTZW50OiBNb25kYXksIDI3IEphbnVhcnkgMjAxNCwgMTA6NDcKPiBTdWJqZWN0OiBSZTogW1NRTF0gVHJpZ2dlciBmdW5jdGlvbiAtIHZhcmlhYmxlIGZvciBzY2hlbWEgbmFtZQo.IAo.PiAgRnJvbToBMAEBAQE- X-Mailer: YahooMailWebService/0.8.174.629 References: <1390811996770-5788931.post@n5.nabble.com> <1390819626.71225.YahooMailNeo@web133206.mail.ir2.yahoo.com> Message-ID: <1390820451.59644.YahooMailNeo@web133202.mail.ir2.yahoo.com> Date: Mon, 27 Jan 2014 11:00:51 +0000 (GMT) From: Glyn Astill Reply-To: Glyn Astill Subject: Re: Trigger function - variable for schema name To: ssylla , "pgsql-sql@postgresql.org" In-Reply-To: <1390819626.71225.YahooMailNeo@web133206.mail.ir2.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Pg-Spam-Score: -2.0 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org ----- Original Message ----- > From: Glyn Astill > To: ssylla ; "pgsql-sql@postgresql.org" > Cc:=20 > Sent: Monday, 27 January 2014, 10:47 > Subject: Re: [SQL] Trigger function - variable for schema name >=20 >> From: ssylla >=20 >> To: pgsql-sql@postgresql.org=20 >> Sent: Monday, 27 January 2014, 8:39 >> Subject: [SQL] Trigger function - variable for schema name >>=20 >>=20 >> Dear list, >>=20 >> I have the following trigger function an try to use TG_ARGV as a variable >> for the schema name of the table that caused the trigger: >>=20 >> CREATE OR REPLACE FUNCTION trigger_function1() >> =A0 RETURNS trigger AS >> $BODY$ >> =A0 =A0 declare my_schema text; >> =A0 =A0 begin >> =A0 =A0 =A0 =A0 my_schema :=3D TG_ARGV[0]; >> =A0 =A0 =A0 =A0 select table2.id into new.id from my_schema.table2; >> =A0 =A0 =A0 =A0 new.columnx=3Dfunction1(my_schema,value1); >> =A0 =A0=A0=A0return new; >> end: >> $$ >> language plpgsql >> CREATE TRIGGER trigger_function1 >> =A0 BEFORE INSERT >> =A0 ON schema1.table1 >> =A0 FOR EACH ROW >> =A0 EXECUTE PROCEDURE trigger_function1('schema1'); >>=20 >> Using the trigger I get the following message: >> ERROR: schema "my_schema" does not exist >>=20 >=20 > To do what you're trying to do there you'd probably be best to use=20 > EXECUTE: >=20 > CREATE OR REPLACE FUNCTION trigger_function1() > =A0 RETURNS trigger AS > $BODY$ > =A0=A0=A0 declare my_schema text; > =A0=A0=A0 begin > =A0=A0=A0=A0=A0=A0=A0 my_schema :=3D TG_ARGV[0]; > =A0=A0=A0=A0=A0=A0=A0 EXECUTE 'select table2.id into new.id from ' ||=20 > quote_ident(my_schema) || '.table2'; Oops, missed the select into there, and you only want that query to return = one record: EXECUTE 'select table2.id from ' || quote_ident(my_schema) || '.table2' int= o new.id; >=20 > =A0=A0=A0=A0=A0=A0=A0 new.columnx=3Dfunction1(my_schema,value1); > =A0=A0=A0 return new; > end: > $$ > language plpgsql >=20 >=20 >> So far I tried another option by temporarily changing the search path, b= ut >> that might cause problems with other users who are working on other sche= mas >> of the database at the same time. That's why I would like to write the >> trigger in a way that it will only perform on the specified schema, but = not >> changing the global search_path of the database. >> I also tried using dynamic sql with "execute format('...',=20 > TG_TABLE_SCHEMA); >> but that will only work inside the trigger, not if I want to pass the sc= hema >> name to another function that is called from within the trigger. >=20 >=20 > We'll I don't see why you couldn't pull the current schema with=20 > TG_TABLE_SCHEMA and pass it as a variable to your other function, but I'm= =20 > not entirely sure what you're trying to do to be honest. > --=20 Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql