Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gd7Il-0004bl-Vj for pgsql-general@arkaria.postgresql.org; Sat, 29 Dec 2018 05:36:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gd7Ik-0008Cz-J4 for pgsql-general@arkaria.postgresql.org; Sat, 29 Dec 2018 05:36:46 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1gd7Ii-0007tO-OD for pgsql-general@lists.postgresql.org; Sat, 29 Dec 2018 05:36:46 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gd7IZ-0006Mv-8s for pgsql-general@lists.postgresql.org; Sat, 29 Dec 2018 05:36:43 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 64C3921687; Sat, 29 Dec 2018 00:36:33 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Sat, 29 Dec 2018 00:36:33 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=5 q/EH4YifVoDU3LiHfgc9zWOYMv1PkON/TNTTbFTrlY=; b=X3aoTeho98tpURgIx FwjwHMEd5Vl6EQxQdDuBL9rGJqGwQwL6zQFQ1Dd72LNsOCuhHCnTNLbaySY7XvMd pDCl3sLHYUyCEZ5H1ZCBVWJki+VsoFDbKKjMpd2L0YWNyQWDVhQ7U63usdJQHHsZ 5dtenrB/YgQ3mUDBFbGlfOuQ1i59k09tnXlTTrM6tyMfnOysjbszJDZ4mPAiK2pY Let80saQ0VYsmGeNFfBCyEGZ1MZFuVb+L4XdLKosW0/QBaQTF6UjeNid19BvQvji 1Sm+E4edUBVUDxGjzSnj5W9ndz3e0v4FZ0RPQKY5nVqrO6pfXlnu2daN3pi6QwoO noKJg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=5q/EH4YifVoDU3LiHfgc9zWOYMv1PkON/TNTTbFTr lY=; b=O3eWpSp/j0XXphlXl25ZhJmXkn70aahnknnJkzIwouIaPK/U0CXnd4/8X v/yZG37OiIs2Ot0iO3RkPEz/0rPT/1iT8D4ODm5biTrT0J2nrB8Ul6vHUGqKEt4l 2/KJJV9p+/6zwH1XsDa277J/3cmbTdWvND/EbM1HDLk9layxN9lMdCbTVZDdx6nr y+WBTeqDxt5mR8vUzQ+S+v6/zvpLjmiqyomeixZH46K3lzOGUxQpG/XKaK6bJQwH 3YOBaIE0I5FonXojJldGUcOW7AeCdqJImynE5fpmZxN1DeRCoz+kFNVM0qG2VtGE qA/MajEwUVmny20u0RvGsPdLBpeyg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrtdejgdektdculddtuddrgedtkedrtddtmd cutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfhuthen uceurghilhhouhhtmecufedttdenucenucfjughrpefuvfhfhffkffgfgggjtgfgsehtje ertddtfeejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhk lhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecukfhppeejuddrvdduvddrudefgedrfe enucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgr vhgvrhdrtghomhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.2] (unknown [71.212.134.3]) by mail.messagingengine.com (Postfix) with ESMTPA id 4D9BEE40E1; Sat, 29 Dec 2018 00:36:32 -0500 (EST) Subject: Re: In which session context is a trigger run? To: Mitar Cc: pgsql-general@lists.postgresql.org References: From: Adrian Klaver Message-ID: <0a13079f-27c7-68dd-b52f-af9385185d2b@aklaver.com> Date: Fri, 28 Dec 2018 21:36:31 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 12/28/18 7:56 PM, Mitar wrote: > Hi! > > On Fri, Dec 28, 2018 at 3:25 PM Adrian Klaver wrote: >>> Sure, but why is a temporary function used as a temporary trigger made >> >> There is no such thing as a temporary trigger. > > A trigger defined using a temporary function gets deleted once a > function gets deleted, which is at the end of the session. Thus, it is > a temporary trigger. Feel free to try it. Create a function in pg_temp > and then define a trigger, disconnect, and you will see that trigger > is deleted as well. That is because the function is temporary and when the session ends the function is dropped and it cascades to the trigger. The important part to note is pg_temp.* is an alias to whatever pg_temp_nn the temporary objects are created in. This is why what you want to do is not working. When you create the temporary function it is 'pinned' to a particular session/pg_temp_nn. Running the trigger in another session 'pins' it to that session and it is not able to see the posts_temp table in the original session. Postgres does not have global temp tables at this time. There have been rumblings about making that happen, but I do not what the status of that is. > >>> from my session not run inside my session? Then it could see a >>> temporary table made in my session. >> >> Except that is not what your OP stated: >> >> "And I add it to a regular table as a trigger: >> >> CREATE TRIGGER posts_insert AFTER INSERT ON posts REFERENCING NEW >> TABLE AS new_table FOR EACH STATEMENT EXECUTE FUNCTION >> pg_temp.my_function(); >> >> When a row is added to "posts" table outside of my session, function >> "my_function" is called, but it seems it cannot access "posts_temp" >> table." >> >> So are you talking about another case now? > > No. Still the same case. I have a regular table "posts" and a > temporary table "posts_temp". I want to create a trigger on "posts" > which calls "my_function". "my_function" then copies data from "posts" > to "posts_temp". The problem is that if "posts" is modified in another > session, the trigger cannot access "posts_temp". I wonder if there is > a way to call "my_function" inside the same temporary context / > session where it was defined, because in that same session also > "posts_temp" was defined. > > > Mitar > -- Adrian Klaver adrian.klaver@aklaver.com