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 1geoq9-00072x-2p for pgsql-bugs@arkaria.postgresql.org; Wed, 02 Jan 2019 22:18:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1geoq7-0008Fr-HO for pgsql-bugs@arkaria.postgresql.org; Wed, 02 Jan 2019 22:18:15 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1geoq7-0008FZ-7e for pgsql-bugs@lists.postgresql.org; Wed, 02 Jan 2019 22:18:15 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1geoq4-0006qH-Qn for pgsql-bugs@postgresql.org; Wed, 02 Jan 2019 22:18:14 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x02MI8rX010858; Wed, 2 Jan 2019 17:18:08 -0500 From: Tom Lane To: Alexey Bashtanov cc: pgsql-bugs@postgresql.org, mmitar@gmail.com Subject: Re: Is temporary functions feature official/supported? Found some issues with it. In-reply-to: <52794060-3339-e699-a8d5-e3dd7c81fa95@imap.cc> References: <5d910e2e-0db8-ec06-dd5f-baec420513c3@imap.cc> <22938.1546456949@sss.pgh.pa.us> <52794060-3339-e699-a8d5-e3dd7c81fa95@imap.cc> Comments: In-reply-to Alexey Bashtanov message dated "Wed, 02 Jan 2019 21:55:21 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <10856.1546467488.1@sss.pgh.pa.us> Date: Wed, 02 Jan 2019 17:18:08 -0500 Message-ID: <10857.1546467488@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alexey Bashtanov writes: > session1: > l=> begin; > BEGIN > l=> create function pg_temp.foo() returns void as $$ begin end; $$ > language plpgsql; > CREATE FUNCTION > l=> prepare transaction 'z'; > PREPARE TRANSACTION > l=> \q > session2: > l=> create temp table t(); > ^CCancel request sent > ... blabla when inserting (0,15) into "pg_namespace_nspname_index" ... > (sorry, server was running in non-english locale) Hm. I can reproduce this if I start in a virgin database, but not otherwise. I think the problem is that the prepared transaction has created the pg_temp_NN schema for its session, and therefore there is an uncommitted pg_namespace entry for that schema name, and the second session is also trying to create that schema (because it has the same backend ID) so it blocks waiting to see if that index entry commits. Another problem is that if the pg_temp_NN schema did already exist the original session gets hung up on exit, trying to delete the uncommitted function from its temp schema. (This prevents other sessions from acquiring the same backend ID, thus masking the problem from casual inspection.) None of this is specific to functions, it'd happen for any temp object. Maybe we ought to forbid prepared transactions from creating (or deleting?) any temp objects. I seem to remember that we already made some restrictions of that sort, but they clearly weren't sufficient to prevent all problems. Or we could just say "if it hurts, don't do that". The whole thing is only a problem if you leave prepared transactions sitting around for a long time, and that's already a bad idea. > It isn't the case for without prepared transactions, neither can it be > achieved with CREATE TEMP TABLE only with no functions involved. I think your experiments likely didn't account for the different behavior depending on whether pg_temp_NN exists yet. There's no reason this would be special to functions. regards, tom lane