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 1tU47b-00Dm0a-NN for pgsql-general@arkaria.postgresql.org; Sat, 04 Jan 2025 13:23:20 +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 1tU47Y-00ASAC-Sd for pgsql-general@arkaria.postgresql.org; Sat, 04 Jan 2025 13:23:16 +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 1tU47Y-00ASA3-IP for pgsql-general@lists.postgresql.org; Sat, 04 Jan 2025 13:23:16 +0000 Received: from gaoxing.magnetkern.de ([167.235.225.147]) by magus.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1tU47W-000C48-0i for pgsql-general@lists.postgresql.org; Sat, 04 Jan 2025 13:23:15 +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 078FC736E6; Sat, 04 Jan 2025 14:24:28 +0100 (CET) Date: Sat, 4 Jan 2025 14:23:10 +0100 From: Jan Behrens To: Isaac Morland Cc: Adrian Klaver , "David G. Johnston" , "pgsql-general@lists.postgresql.org" Subject: Re: search_path for PL/pgSQL functions partially cached? Message-Id: <20250104142310.7ff3bc68cc824b9af354cac7@magnetkern.de> In-Reply-To: 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> 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 Fri, 3 Jan 2025 18:36:13 -0500 Isaac Morland wrote: > On Fri, 3 Jan 2025 at 18:22, Jan Behrens wrote: > > > > Instead, I plan to expect the function to receive a query string that > > will get the data that is being processed by the function. > > > > That query string should be allowed to refer to tables in the > > search_path at the caller's side. > > > > Therefore, I cannot use the "SET search_path FROM CURRENT" in my > > "CREATE FUNCTION" statement, because it would overwrite the current > > search_path on each call of the function. > > > > I wonder if it would help if EXECUTE took an optional search_path to use > while executing the query. That wouldn't solve my problem, because the function that includes the EXECUTE still needs to know the search_path set on the caller side. This only works if I omit the "SET search_path FROM CURRENT" option in the function's definition OR if I pass a search_path as an argument. I guess I could write a wrapper: ============ BEGIN; CREATE SCHEMA "some_schema"; SET LOCAL search_path TO "some_schema"; CREATE TYPE "some_type" AS ("name" TEXT, "height" INT8); CREATE FUNCTION "foo_impl"("query_p" TEXT, "search_path_p" TEXT) RETURNS "some_type" LANGUAGE plpgsql SET search_path FROM CURRENT AS $$ DECLARE "old_search_path" TEXT; "result" "some_type"; BEGIN "old_search_path" = current_setting('search_path'); PERFORM set_config('search_path', "search_path_p", TRUE); EXECUTE "query_p" INTO "result"; PERFORM set_config('search_path', "old_search_path", TRUE); RETURN "result"; END; $$; CREATE FUNCTION "foo"("query_p" TEXT) RETURNS "some_type" RETURN "foo_impl"("query_p", current_setting('search_path')); COMMIT; CREATE TABLE "tbl" ("id" SERIAL8, "name" TEXT, "height" INT8); INSERT INTO "tbl" ("name", "height") VALUES ('Alice', 200); SELECT * FROM "some_schema"."foo"('SELECT "name" FROM "tbl"'); ============ Not sure which variant (this or my previous attempt) is better and if either is safe/correct. Regards, Jan Behrens