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 1tUFIj-00FCzJ-JX for pgsql-general@arkaria.postgresql.org; Sun, 05 Jan 2025 01:19:34 +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 1tUFIh-00Elnm-EC for pgsql-general@arkaria.postgresql.org; Sun, 05 Jan 2025 01:19:31 +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 1tUFIh-00Elnd-1g for pgsql-general@lists.postgresql.org; Sun, 05 Jan 2025 01:19:30 +0000 Received: from mail-ot1-x32a.google.com ([2607:f8b0:4864:20::32a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tUFIc-000Gpe-1k for pgsql-general@lists.postgresql.org; Sun, 05 Jan 2025 01:19:30 +0000 Received: by mail-ot1-x32a.google.com with SMTP id 46e09a7af769-71e3167b90dso6862514a34.0 for ; Sat, 04 Jan 2025 17:19:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736039963; x=1736644763; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=oqj3XC9e852UxjjQt9N452TNvwVjd6fYmRo1LV4Ra1g=; b=h9seGS0uSA1/r354c2nw0eJ8EOilArXcjv810238cDD4B6dL/rzzTmcPU3KkMflTyV 497IKGVZHHNLUzFZthzx5oq/x6jfMlM92Fyq1wTSePq7gLgPzxUIFQV3YTPh4AzivPjo vRbNEYAXvE5RhXQD2dihiE7xikP+tDKfZi5QphIeH0GzOthRQxncxT0hH8O6RjsB/RWC CiIkHy28OxZS0GIkAKpATsix3GXFJsHU4a8PEWm3WlaRW4T616NjtoAZ6GNNvXNlH+Ed S2x31FfYbrcD7FtNesK8vmrI/WgkJNh5N23q0u8ZcLdE+Wk55DUYSiXd6TKPDBAwTpcg 2Ssw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736039963; x=1736644763; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=oqj3XC9e852UxjjQt9N452TNvwVjd6fYmRo1LV4Ra1g=; b=t+Ep9jHR0EkorXcg1LsZx40Vw9/NNASiGiWui+vuK+4pLict2mvo3GwIE15fENC9d9 HesnyhKwxxOng0cMt5d0L6nqftFfOO2N3U/5yHI8Bt3F5rq6YVOgTcinQR1YQmASd/zp Q5hEM+6HHD044/4AgtRlW7DpA/PnDS67AxtwoL9vCaOkkN9vTqacV/9D2ySeEApGfIWX Yrc/wfMbB07dM3ATijB3EjaA3j5AmBPOY21fO5MgrgjXBqizcMViqwsx7FInBRr1p/5y jTOimwKSCKUHx5aTCPfsJNbak9LUuXw3VSijv+IQ53ce/5HTdKL1ahidvxYVop0P0a5G sNMQ== X-Forwarded-Encrypted: i=1; AJvYcCU5UMFGGdn0ePVWidEJ3IitdzDmffLsfpzZCea0qPleIABnoBpivgAHpYx7ag0r8XfPsFDjlA2v+UWNHQJR@lists.postgresql.org X-Gm-Message-State: AOJu0YzPs+4+8/PauY569YTJ2w6DOWWdDMvC18ckTZ12fiPTvYVvpgVN gCqHYiA/He93YN6Ec6AE/lhV0z9nrJEojpScv63jmZDbkxJ2TGdw3AmWc4r6xoGLxnB57X4Owlx TaYVp+nnfO7nxrrWu143dfeE/ek4= X-Gm-Gg: ASbGncsrWARPpDj/hC0aqByETn7CB5BbDCOe9rG6zDVRZJ7tTUfhX+7uSjQHlwntI7W fjQ7eB4VIfKVFzVMqqDR6VpjRo+iW6Sk/s+D/Xw== X-Google-Smtp-Source: AGHT+IGbwATzIhJV0l98ihZB/q9SJRARE4cME+3R/YZLRQX46E1lCKSBivjn23WNj7EqCseFk2n0uRGWLmYexejch9A= X-Received: by 2002:a05:6808:f06:b0:3ea:aa8a:c115 with SMTP id 5614622812f47-3ed88f9629dmr28806224b6e.21.1736039963648; Sat, 04 Jan 2025 17:19:23 -0800 (PST) MIME-Version: 1.0 Received: by 2002:a8a:1872:0:b0:577:9519:f64a with HTTP; Sat, 4 Jan 2025 17:19:22 -0800 (PST) In-Reply-To: <20250104142310.7ff3bc68cc824b9af354cac7@magnetkern.de> 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> <20250104142310.7ff3bc68cc824b9af354cac7@magnetkern.de> From: "David G. Johnston" Date: Sat, 4 Jan 2025 18:19:22 -0700 Message-ID: Subject: Re: search_path for PL/pgSQL functions partially cached? To: Jan Behrens Cc: Isaac Morland , Adrian Klaver , "pgsql-general@lists.postgresql.org" Content-Type: multipart/alternative; boundary="000000000000596d53062aeb4d80" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000596d53062aeb4d80 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Saturday, January 4, 2025, Jan Behrens wrote: > > 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" =3D 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; > $$; > You might consider adding a polymorphic argument for the result type. Then if you call the function with two different typed inputs it will be cached once for each. =E2=80=9C Likewise, functions having polymorphic argument types have a sepa= rate statement cache for each combination of actual argument types they have been invoked for, so that data type differences do not cause unexpected failures.=E2=80=9D David J. --000000000000596d53062aeb4d80 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Saturday, January 4, 2025, Jan Behrens <jbe-mlist@magnetkern.de> wrote:

CREATE FUNCTION "foo_impl"("query_p" TEXT, "search= _path_p" TEXT)
=C2=A0 RETURNS "some_type"
=C2=A0 LANGUAGE plpgsql SET search_path FROM CURRENT AS $$
=C2=A0 =C2=A0 DECLARE
=C2=A0 =C2=A0 =C2=A0 "old_search_path" TEXT;
=C2=A0 =C2=A0 =C2=A0 "result" "some_type";
=C2=A0 =C2=A0 BEGIN
=C2=A0 =C2=A0 =C2=A0 "old_search_path" =3D current_setting('s= earch_path');
=C2=A0 =C2=A0 =C2=A0 PERFORM set_config('search_path', "search= _path_p", TRUE);
=C2=A0 =C2=A0 =C2=A0 EXECUTE "query_p" INTO "result"; =C2=A0 =C2=A0 =C2=A0 PERFORM set_config('search_path', "old_se= arch_path", TRUE);
=C2=A0 =C2=A0 =C2=A0 RETURN "result";
=C2=A0 =C2=A0 END;
=C2=A0 $$;

You might consider adding a polymorphic ar= gument for the result type.=C2=A0 Then if you call the function with two di= fferent typed inputs it will be cached once for each.

<= div>=E2=80=9C=C2=A0Likewise, functions having polymorphic argument types= have a separate statement cache for each combination of actual argument ty= pes they have been invoked for, so that data type differences do not cause = unexpected failures.=E2=80=9D

David J.
=C2=A0
--000000000000596d53062aeb4d80--