Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W7jik-0005ZA-Kn for pgsql-sql@arkaria.postgresql.org; Mon, 27 Jan 2014 10:47:15 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1W7jik-0006iN-5C for pgsql-sql@arkaria.postgresql.org; Mon, 27 Jan 2014 10:47:14 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W7jii-0006iG-MS for pgsql-sql@postgresql.org; Mon, 27 Jan 2014 10:47:13 +0000 Received: from nm24-vm9.bullet.mail.ir2.yahoo.com ([212.82.97.33]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W7jie-0006kW-LN for pgsql-sql@postgresql.org; Mon, 27 Jan 2014 10:47:12 +0000 Received: from [212.82.98.59] by nm24.bullet.mail.ir2.yahoo.com with NNFMP; 27 Jan 2014 10:47:07 -0000 Received: from [212.82.98.91] by tm12.bullet.mail.ir2.yahoo.com with NNFMP; 27 Jan 2014 10:47:06 -0000 Received: from [127.0.0.1] by omp1028.mail.ir2.yahoo.com with NNFMP; 27 Jan 2014 10:47:06 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 947328.74217.bm@omp1028.mail.ir2.yahoo.com Received: (qmail 57231 invoked by uid 60001); 27 Jan 2014 10:47:06 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.co.uk; s=s1024; t=1390819626; bh=Rj8ElYiKDxmH2Mgc8xsAy2b5z4MrHNixNUbB1GzxaYQ=; 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=a0MmQtCq1SrEtvy2FbJW5HWu/guVBe+JdISUDbrhyrm0dVqVA5t8xNRlCF0CLFP2ndoMH92cldfcoHBBI3W3GFi05HBtx2TzutIdyXzWEuUizSKHnCCHmbyk0r171DQtn9PxT2Hud/9xn6pQdC84Z3WR1sBhYgOuDH2Ncbxvn8I= 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=Rk1YokeqeDzv01TEoCyHJdr6/MnMHGzYt4niU9JwqfE/1X5bgbIR6MDVOmfmThbTteGV4hdWC7bxavMYq0XsomsReZLmBj7olgt1/5Wr1wZvh7kvHTdk4+IsAy4GEJESKhCA5PSS347yJ9xdVM1RzjFaLHpZexPh2GdNYMPmmYk=; X-YMail-OSG: VGx4v5sVM1kvqDWIW_qROfrGHScBE7z_t.5wmWZm7.a1LMM uUqdqZtpXmg7DxtraqcpgVClswf2GulPHW2tvrN9uRBphw1tbEEQ2t68IFw4 jl0E_H5ELvSbg6KzIsNXljtqqzOzy1Tnl1dI_KTSwb7SEEkT4ryQrjEWkTUL Y70sR2jvWVSorbmZjQMgwT0b1kmEdRNW4xKmeFYS5x_nRM_pUf87j2Z4naOw I8oOgTFDBwbuHo_lb0rQ5S9Iv22WJIyvivegc6Gq5oXofsvaGrkx_kZK2Y_Q Iw1VtM0quU1KL616o6ecIJNVGl.fp3jI266oRw72Ptoxjbul.RnsZLJ89Fw1 p7_mqCYnFsrkET1lBmq5QGomFu6DD.T4quKXqtHi_BD2pYzM2r1zcFIDGGvS WvJ4zrQN9Oh2lU_6LzUroHHVs9l1FgADs2i5owRPXvJi6X25lBHbe6dx5mPT tH5HaQ7HwuZhTeisDK1jQXGsA6lhX2X2FVClLwHPlgRLsgQDEWZEuiyLxEtN B3nQ3YLXSHMMAGXHaxL_ORWhrvjpluRLScEcGZzqKC5Q.hhxEfExiiMBh7T6 5E598IdK51oyPwtMKluSNb_QwIlW9nMympgBsoIf6gwYBPYfPTK4t7hDDWop nZefcIM4- Received: from [194.168.202.210] by web133206.mail.ir2.yahoo.com via HTTP; Mon, 27 Jan 2014 10:47:06 GMT X-Rocket-MIMEInfo: 002.001, PiBGcm9tOiBzc3lsbGEgPHN0ZWZhbnN5bGxhQGdteC5kZT4KCj5UbzogcGdzcWwtc3FsQHBvc3RncmVzcWwub3JnIAo.U2VudDogTW9uZGF5LCAyNyBKYW51YXJ5IDIwMTQsIDg6MzkKPlN1YmplY3Q6IFtTUUxdIFRyaWdnZXIgZnVuY3Rpb24gLSB2YXJpYWJsZSBmb3Igc2NoZW1hIG5hbWUKPiAKPgo.RGVhciBsaXN0LAo.Cj5JIGhhdmUgdGhlIGZvbGxvd2luZyB0cmlnZ2VyIGZ1bmN0aW9uIGFuIHRyeSB0byB1c2UgVEdfQVJHViBhcyBhIHZhcmlhYmxlCj5mb3IgdGhlIHNjaGVtYSBuYW1lIG9mIHRoZSB0YWIBMAEBAQE- X-Mailer: YahooMailWebService/0.8.174.629 References: <1390811996770-5788931.post@n5.nabble.com> Message-ID: <1390819626.71225.YahooMailNeo@web133206.mail.ir2.yahoo.com> Date: Mon, 27 Jan 2014 10:47:06 +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: <1390811996770-5788931.post@n5.nabble.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Pg-Spam-Score: 0.7 (/) 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 > From: ssylla >To: pgsql-sql@postgresql.org=20 >Sent: Monday, 27 January 2014, 8:39 >Subject: [SQL] Trigger function - variable for schema name >=20 > >Dear list, > >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: > >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'); > >Using the trigger I get the following message: >ERROR: schema "my_schema" does not exist > To do what you're trying to do there you'd probably be best to use EXECUTE: 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 ' || quote= _ident(my_schema) || '.table2'; =A0=A0=A0=A0=A0=A0=A0 new.columnx=3Dfunction1(my_schema,value1); =A0=A0=A0 return new; end: $$ language plpgsql >So far I tried another option by temporarily changing the search path, but >that might cause problems with other users who are working on other schemas >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('...', TG_TABLE_SCHEMA= ); >but that will only work inside the trigger, not if I want to pass the sche= ma >name to another function that is called from within the trigger. We'll I don't see why you couldn't pull the current schema with TG_TABLE_SC= HEMA and pass it as a variable to your other function, but I'm 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