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 1tT2w4-0035Hi-5w for pgsql-general@arkaria.postgresql.org; Wed, 01 Jan 2025 17:55:12 +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 1tT2w3-009O8v-Bz for pgsql-general@arkaria.postgresql.org; Wed, 01 Jan 2025 17:55:11 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tT2w3-009O8n-0s for pgsql-general@lists.postgresql.org; Wed, 01 Jan 2025 17:55:10 +0000 Received: from gaoxing.magnetkern.de ([2a01:4f8:c012:f130::1]) by makus.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1tT2w0-001afN-2p for pgsql-general@lists.postgresql.org; Wed, 01 Jan 2025 17:55:09 +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 2D3A27314E; Wed, 01 Jan 2025 18:56:19 +0100 (CET) Date: Wed, 1 Jan 2025 18:55:04 +0100 From: Jan Behrens To: "pgsql-general@lists.postgresql.org" Cc: "David G. Johnston" Subject: Re: search_path for PL/pgSQL functions partially cached? Message-Id: <20250101185504.3d50c571c3448512e94288e8@magnetkern.de> In-Reply-To: <20241228004009.267f21b78394c934f27f9974@magnetkern.de> References: <20241227205025.1d059f72c7c08d23c9648c26@magnetkern.de> <20241228004009.267f21b78394c934f27f9974@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 Sat, 28 Dec 2024 00:40:09 +0100 Jan Behrens wrote: > On Fri, 27 Dec 2024 13:26:28 -0700 > "David G. Johnston" wrote: > > > > Or is it documented somewhere? > > > > https://www.postgresql.org/docs/current/plpgsql-implementation.html#PLPGSQL-PLAN-CACHING > > I can't find any notes regarding functions and schemas in that section. Actually, I found another note in the documentation. But it doesn't explain things correctly. In the documentation for PostgreSQL 17, section 36.17.6.1. (Security Considerations for Extension Functions) says: "SQL-language and PL-language functions provided by extensions are at risk of search-path-based attacks when they are executed, since parsing of these functions occurs at execution time not creation time." https://www.postgresql.org/docs/17/extend-extensions.html#EXTEND-EXTENSIONS-SECURITY So here, the manual explicity states that functions are parsed at execution, not creation time. As seen in my original example in this thread, this isn't (fully) true. Moreover, it isn't true for all SQL-language functions, as can be demonstrated with the following code: ============ CREATE SCHEMA s1; CREATE SCHEMA s2; CREATE VIEW s1.v AS SELECT 'creation' AS col; CREATE VIEW s2.v AS SELECT 'runtime' AS col; SET search_path TO 'public', 's1'; CREATE FUNCTION use_sql_atomic() RETURNS TEXT LANGUAGE sql BEGIN ATOMIC SELECT 'use_sql_atomic = ' || col FROM v; END; CREATE FUNCTION use_sql_string() RETURNS TEXT LANGUAGE sql AS $$ SELECT 'use_sql_string = ' || col FROM v; $$; CREATE FUNCTION use_plpgsql() RETURNS TEXT LANGUAGE plpgsql AS $$ BEGIN RETURN (SELECT 'use_plpgsql = ' || col FROM v); END; $$; SET search_path TO 'public', 's2'; SELECT use_sql_atomic() AS "output" UNION ALL SELECT use_sql_string() AS "output" UNION ALL SELECT use_plpgsql() AS "output"; ============ This generates the following output: output --------------------------- use_sql_atomic = creation use_sql_string = runtime use_plpgsql = runtime (3 rows) Overall, PostgreSQL doesn't behave consistent, and to me it seems that the documentation isn't describing its behavior correctly either. I understand if fixing this is too much work (even though I would really like to see this fixed). But given that the current behavior is highly surprising and inconsistent - and keeping in mind that this is a subject that may affect security - I think the documentation should reflect the current behavior at least. I thus see this as a documentation issue. Kind regards, Jan Behrens