Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n7IvM-00029i-Ob for pgsql-general@arkaria.postgresql.org; Tue, 11 Jan 2022 15:19:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1n7IvL-0006Qp-I5 for pgsql-general@arkaria.postgresql.org; Tue, 11 Jan 2022 15:18:59 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n7IvL-0006Qg-76 for pgsql-general@lists.postgresql.org; Tue, 11 Jan 2022 15:18:59 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n7IvI-00088i-Q7 for pgsql-general@postgresql.org; Tue, 11 Jan 2022 15:18:58 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 20BFIl6d2589877; Tue, 11 Jan 2022 10:18:47 -0500 From: Tom Lane To: benj.dev@laposte.net cc: pgsql-general@postgresql.org Subject: Re: plpgsql function problem whith creating temp table - not correctly using search_path ? In-reply-to: <617527791.607206.1641894840385@wlpnf0208> References: <617527791.607206.1641894840385@wlpnf0208> Comments: In-reply-to benj.dev@laposte.net message dated "Tue, 11 Jan 2022 10:54:00 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <2589875.1641914327.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 11 Jan 2022 10:18:47 -0500 Message-ID: <2589876.1641914327@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk benj.dev@laposte.net writes: > -- Executing each row on differents transactions but in the same session > /*Session A - Transaction 1*/ SELECT * FROM test_search_path_v1(true);=C2= =A0 -- =3D> OK takes table from pg_temp (no existing table in public) > /*Session A - Transaction 2*/ SELECT * FROM test_search_path_v1(false); = -- =3D> OK takes table from public > /*Session A - Transaction 3*/ SELECT * FROM test_search_path_v1(true);=C2= =A0 -- =3D> OK takes table from pg_temp (and the existing from public) > /*Session A - Transaction 4*/ SELECT * FROM test_search_path_v1(false); = -- =3D> OK takes table from public > /*Session A - Transaction 5*/ SELECT * FROM test_search_path_v1(true);=C2= =A0 -- =3D> NOK =3D> it takes public and not pg_temp I think what you've done here is maneuver the plpgsql function into a state where it has a cached query plan [1] using the public table. Dropping the public table, or changing your search_path, would invalidate that cached plan ... but creating a new temp table does not. (This isn't specific to temp tables --- in any situation where you have more than one schema in the search path, creation of a new object could mask objects later in the path, and we won't invalidate plans just because that possibly happened.) My advice is "don't do that", ie, avoid masking permanent objects with temporary ones. It's extremely confusing to humans as well as machines. If you must do it, DISCARD PLANS might help you with keeping plpgsql functions in line. regards, tom lane [1] https://www.postgresql.org/docs/current/plpgsql-implementation.html#PL= PGSQL-PLAN-CACHING