Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Z9fu3-0006u1-S1 for pgsql-sql@arkaria.postgresql.org; Mon, 29 Jun 2015 20:43:43 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1Z9fu3-00061W-4X for pgsql-sql@arkaria.postgresql.org; Mon, 29 Jun 2015 20:43:43 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1Z9fu2-00061Q-GL for pgsql-sql@postgresql.org; Mon, 29 Jun 2015 20:43:42 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1Z9ftz-0001KO-0T for pgsql-sql@postgresql.org; Mon, 29 Jun 2015 20:43:41 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 16CEB211C8 for ; Mon, 29 Jun 2015 16:43:37 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute5.internal (MEProxy); Mon, 29 Jun 2015 16:43:37 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=8kwO4OyMUMtpx6y37s7pLdwjhhY=; b=GfXqYA YniYOvPwxICZNlER91H6oiP+0ypDepxaTz+V24jTkF24x9MwdnqsEvQe8c9EhGW+ H4ARsP5dhZPjO762mGZMxn2CBxeTe503aE5vYBAQjDsA4ggYXJNvaI7SetknrPRV uaVwtgoZocG/asD05ODsyKkXzHgECAeQ5FlH8= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=8kwO4OyMUMtpx6y 37s7pLdwjhhY=; b=Gqf1cdkvWTj1ALCy1fts/o0/Pl68qaKYhgGLjjrn8hz9nfB LqP3IMbJPdptk7Et4BdLB1KdqSLFydQU4PKouj9fqxUv07o62mVbYHjnKs/PyI4c f0Ppr+XhsrrZgud7BW9Wwkg7Zi0ozb7fHnob51DL+eLeXqliJzg0ptBslMXU= X-Sasl-enc: p736gN9vkeaVjqm7O1U26RV08s0P0nqNfsepGCthPdm+ 1435610616 Received: from [192.168.1.2] (unknown [174.21.228.230]) by mail.messagingengine.com (Postfix) with ESMTPA id 9BA4CC00287; Mon, 29 Jun 2015 16:43:36 -0400 (EDT) Message-ID: <5591ADF7.4090509@aklaver.com> Date: Mon, 29 Jun 2015 13:43:35 -0700 From: Adrian Klaver User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: Greg Sabino Mullane , pgsql-sql@postgresql.org Subject: Re: Disable Trigger for session only References: <88967b6b1d22d1cbdace2f0351b4ec6a@biglumber.com> In-Reply-To: <88967b6b1d22d1cbdace2f0351b4ec6a@biglumber.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.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 On 06/29/2015 01:34 PM, Greg Sabino Mullane wrote: > > -----BEGIN PGP SIGNED MESSAGE----- > Hash: RIPEMD160 > >> "gmb" asks: > >> I'm in a position where the most logical/effective way of doing an update >> (data fix) is this: >> ALTER TABLE temp DISABLE TRIGGER trigname; >> UPDATE temp ..... DO SOME STUFF.... >> ALTER TABLE temp DISABLE TRIGGER trigname; > > Presume you meant ENABLE here. > >> It cannot be guaranteed that the above happens as a single transaction. >> >> I'm aware that session_replication_role can be used as alternative to >> disable triggers, and have been using it in other scenarios. But in this >> case i'd like to choose which trigger to disable (I want other triggers on >> table temp to still occur). >> >> Is there any other alternatives to this ? > > You can use session_replication_role (srr). One of its settings is 'local', which > basically means "act the exact same as the default, 'origin', but with > a different name". Thus, you can teach the trigger you want to get disabled > to short-circuit if srr is set to local. Inside plpgsql it would look something > like this: > > ... > DECLARE > myst TEXT; > BEGIN > SELECT INTO myst setting FROM pg_settings WHERE name = 'session_replication_role'; > IF myst = 'local' THEN > RETURN; > END IF; > > ...normal trigger code here... > END; > ... > > Then, just issue a SET session_replication_role = 'local', and the trigger will > not do anything for that session only: > > BEGIN; > SET LOCAL session_replication_role = 'local'; > UPDATE temp ..... DO SOME STUFF.... > COMMIT; Wow, that is a whole lot cleaner solution then what I came up with. I will have to remember that for future use. > > >> If I encapsulate the "disable trigger/update/enable trigger" in BEGIN/COMMIT >> to handle as single transaction, are there guarantees that the disabling of >> the trigger will not have an effect on other sessions ? > > It will cause heavy locking but should otherwise have no effect. But using > session_replication_role is a cleaner solution, IMHO. > > - -- > Greg Sabino Mullane greg@turnstep.com > End Point Corporation http://www.endpoint.com/ > PGP Key: 0x14964AC8 201506291631 > http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8 > -----BEGIN PGP SIGNATURE----- > > iEYEAREDAAYFAlWRq6oACgkQvJuQZxSWSsh9uwCfe9K+xSYIMthcV9xM7EJh/eQb > vEQAnjo4Quo4Rq9WC50Yuh6aCTHgPlGn > =Ap56 > -----END PGP SIGNATURE----- > > > > -- Adrian Klaver adrian.klaver@aklaver.com -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql