Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tUO6S-00FwlO-3q for pgsql-general@arkaria.postgresql.org; Sun, 05 Jan 2025 10:43:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tUO6R-0000Ad-H7 for pgsql-general@arkaria.postgresql.org; Sun, 05 Jan 2025 10:43:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tUO6R-0000AU-6p for pgsql-general@lists.postgresql.org; Sun, 05 Jan 2025 10:43:26 +0000 Received: from gaoxing.magnetkern.de ([167.235.225.147]) by magus.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1tUO6P-000KYI-1R for pgsql-general@lists.postgresql.org; Sun, 05 Jan 2025 10:43:26 +0000 Received: from titanium.fritz.box (p200300c26f20ef00264bfefffe54b09c.dip0.t-ipconnect.de [IPv6:2003:c2:6f20:ef00:264b:feff:fe54:b09c]) by gaoxing.magnetkern.de (Postfix) with ESMTPSA id 7C67573AAA; Sun, 05 Jan 2025 11:44:38 +0100 (CET) Date: Sun, 5 Jan 2025 11:43:21 +0100 From: Jan Behrens To: Laurenz Albe Cc: Adrian Klaver , Tom Lane , "David G. Johnston" , "pgsql-general@lists.postgresql.org" Subject: Re: search_path for PL/pgSQL functions partially cached? Message-Id: <20250105114321.bbe1fdad3f888db9383c0e28@magnetkern.de> In-Reply-To: <75510e9d2663573f1d32835eb6bcaf326e6a7997.camel@cybertec.at> References: <20241227205025.1d059f72c7c08d23c9648c26@magnetkern.de> <20241228004009.267f21b78394c934f27f9974@magnetkern.de> <20250103135332.7910a008714abaa04d32e5e2@magnetkern.de> <20250103174849.f071d9c6942f76a9b812157c@magnetkern.de> <20250103223312.dde69ae482776e4f1b0c2258@magnetkern.de> <14e035fb-ab35-4a73-a637-ad0180067b07@aklaver.com> <20250104002203.2aed83d48d795ce6fbbd3b61@magnetkern.de> <20250105001249.cdca9dd84a3061c06f936ca7@magnetkern.de> <75510e9d2663573f1d32835eb6bcaf326e6a7997.camel@cybertec.at> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; amd64-portbld-freebsd14.1) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sun, 05 Jan 2025 07:48:56 +0100 Laurenz Albe wrote: > So what you should do is set the "search_path" *on* the function, not *in* > the function: > > CREATE FUNCTION "run"() RETURNS TEXT LANGUAGE plpgsql > SET search_path = myschema > AS $$ > DECLARE > "variable" "tbl"."col"%TYPE; > BEGIN > "variable" := "foo"(); > RETURN "variable"; > END; > $$; > > Yours, > Laurenz Albe Yes, that's what works and what I would also do whenever possible (probably in the form "SET search_path FROM CURRENT"). Summarizing the remaining thread, some issues are: * The documentation isn't providing a prominent warning that behavior can be surprising if "SET search_path" is not used in the function's or procedure's defintion. (E.g. searching for "schema" in the documentation page for "CREATE FUNCTION" doesn't give any helpful hints or warning.) * Things get more complicated when it's impossible to use "SET search_path" in the function's/procedure's definition, for which there are two scenarios: Scenario 1: The function or procedure needs or wants to access or use the search_path of the caller. Scenario 2: A procedure wants to execute transactional statements such as COMMIT or ROLLBACK within its body. In scenario 1, using "SET search_path" will overwrite the caller's search_path at runtime, so I cannot access it. (In my post from Sat, 4 Jan 2025 14:23:10 +0100, I have proposed a wrapper function to work around that.) In scenario 2, using "SET search_path" is simply not possible and will be rejected by PostgreSQL. * It is a bit unclear how the exact behavior is when I set a search_path from within the functions body (e.g. due to one of the two scenarios above). There are some examples that show some quite surprising behavior, at least if you don't fully understand the plan caching mechanism that is used. Kind regards, Jan Behrens