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 1tRHQY-008St3-UJ for pgsql-general@arkaria.postgresql.org; Fri, 27 Dec 2024 20:59:23 +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 1tRHQY-00D4pg-AB for pgsql-general@arkaria.postgresql.org; Fri, 27 Dec 2024 20:59:22 +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 1tRHQX-00D4pY-3q for pgsql-general@lists.postgresql.org; Fri, 27 Dec 2024 20:59:21 +0000 Received: from fout-b3-smtp.messagingengine.com ([202.12.124.146]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tRHQU-000qzm-34 for pgsql-general@lists.postgresql.org; Fri, 27 Dec 2024 20:59:19 +0000 Received: from phl-compute-09.internal (phl-compute-09.phl.internal [10.202.2.49]) by mailfout.stl.internal (Postfix) with ESMTP id 8A12A11401D0; Fri, 27 Dec 2024 15:59:17 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-09.internal (MEProxy); Fri, 27 Dec 2024 15:59:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm1; t=1735333157; x=1735419557; bh=LRWOJssPZwcOigESO7GaQXXkcEHr2Hc4kkmnhRDLWfA=; b= R5Odlp+MFgqQAqkhXXuJTQcVLg4m7iAv78OanUhj3dkMkHHg+i5/g3wRgYn9RV6P rn+XoxOCzguN+0EFbKZsMppCkAEqbXeRygqEVH33/pRXQTi9ZYUQH68tr6FLko7o 03EjhikQfgtldrO8030jq1JS4pBRSxltqxXIhQEdfdIiUBSK4ajf0JdjiTpgWcFU uLnd78IsB7M7MSCgHIybEqw7vt6rSnwUT99fjQFrkPaBIFYv7qqA3Go/bmGpH++g Rc58gWBDZNsHDktXINEHZF0DkYKy2dvjHnwZVmqk2PlKaCEhro1hmxo6KYaRbheP ymTktH+BqX6IAu7D+/poUw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1735333157; x= 1735419557; bh=LRWOJssPZwcOigESO7GaQXXkcEHr2Hc4kkmnhRDLWfA=; b=A 17qYpkJ2cwBA3xdQr+s0dljFDfE7b+S0De0eeWpRMA6BwmziN2nmscAZ9+2SEefY zUiHoXOfGYDbmIGYHQFFwJYnNOj468ahJ6Yt+b2o/vFkQf136MYpsJWNG7SnJVnR iexLgERCtW/FPlz43iuGTQkd3mWm0WSI4W3aUJITpxMR7uqKTj0XcyjCHgGdWqzc DyLTvP3oba6wcAwxe7iyP/E0c4T4a6ub/g5qRb69JRZh9er2XeEaBDlP+gdfHCll Y4MGQWlHXnRXfUYqi80uLIK0g+zGXm4JiXvhbPBY7yojvl7Rs3Ua22+yYtffBk8d 56YdGYl8ipFo2XWJZGF0Q== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddruddvtddgudeggecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefkffggfgfuvfevfhfhjggtgfesthekredttddv jeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvg hrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpefgudefkefhveefleev ieeuveehvdduudekuddvvdelhfeuueeijedtuedvvedvueenucffohhmrghinhepphhosh htghhrvghsqhhlrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehm rghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhdpnh gspghrtghpthhtohepfedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepuggrvhhi ugdrghdrjhhohhhnshhtohhnsehgmhgrihhlrdgtohhmpdhrtghpthhtohepjhgsvgdqmh hlihhsthesmhgrghhnvghtkhgvrhhnrdguvgdprhgtphhtthhopehpghhsqhhlqdhgvghn vghrrghlsehlihhsthhsrdhpohhsthhgrhgvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 27 Dec 2024 15:59:16 -0500 (EST) Message-ID: <58f46d3c-7c62-4518-90d7-03d4e3dfe3cf@aklaver.com> Date: Fri, 27 Dec 2024 12:59:15 -0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: search_path for PL/pgSQL functions partially cached? To: "David G. Johnston" , Jan Behrens Cc: "pgsql-general@lists.postgresql.org" References: <20241227205025.1d059f72c7c08d23c9648c26@magnetkern.de> Content-Language: en-US From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 12/27/24 12:26, David G. Johnston wrote: > On Friday, December 27, 2024, Jan Behrens > wrote: > > > It seems that it matters *both* how the search_path was set during > the *first* invocation of the function within a session *and* how it > is set during the actual call of the function. So even if there are > just two schemas involved, there are 4 possible outcomes for the > "run" function's result ('2.4', '2', '5', and '5.4'). To me, this > behavior seems to be somewhat dangerous. Maybe it is even considered > a bug? > > > It is what it is - and if one is not careful one can end up writing > hard-to-understand and possibly buggy code due to the various execution > environments and caches involved. > > I’ve never really understood why “%TYPE’ exists… Per: https://www.postgresql.org/docs/current/plpgsql-declarations.html#PLPGSQL-DECLARATION-TYPE "By using %TYPE you don't need to know the data type of the structure you are referencing, and most importantly, if the data type of the referenced item changes in the future (for instance: you change the type of user_id from integer to real), you might not need to change your function definition. %TYPE is particularly valuable in polymorphic functions, since the data types needed for internal variables can change from one call to the next. Appropriate variables can be created by applying %TYPE to the function's arguments or result placeholders." The second case I can buy, the first I am not so sure of. It seems to me the first case it can be 'solved' by the second case. > > > Or is it documented somewhere? > > > https://www.postgresql.org/docs/current/plpgsql-implementation.html#PLPGSQL-PLAN-CACHING > > Can someone explain to me what's going on, and what is the best > practice to deal with it? Is there a way to avoid fully qualifying > every type and expression? Which parts do I have to qualify or is > this something that could be fixed in a future version of PostgreSQL? > > > Add qualification or attach a “set search_path” clause to “create > function”.  Code stored in the server should not rely on the session > search_path. > > David J. > -- Adrian Klaver adrian.klaver@aklaver.com