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 1tRHoE-008V12-6p for pgsql-general@arkaria.postgresql.org; Fri, 27 Dec 2024 21:23:50 +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 1tRHoD-00DHpv-0U for pgsql-general@arkaria.postgresql.org; Fri, 27 Dec 2024 21:23:48 +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 1tRHoC-00DHpg-H8 for pgsql-general@lists.postgresql.org; Fri, 27 Dec 2024 21:23:48 +0000 Received: from mail-yb1-xb2f.google.com ([2607:f8b0:4864:20::b2f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tRHoA-000r9Y-2d for pgsql-general@lists.postgresql.org; Fri, 27 Dec 2024 21:23:47 +0000 Received: by mail-yb1-xb2f.google.com with SMTP id 3f1490d57ef6-e3f78f5fc07so7006506276.2 for ; Fri, 27 Dec 2024 13:23:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735334626; x=1735939426; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=XHLHCT+Uhup6/0zagPjbyaH9QvNx7XJgNsirN+mqf0w=; b=PYFY56AgRGMzmpbKb6GWLGXOYHhHqU8pymb4+yAuxLBnneqgrNaFxCtwCeN+mEwKHN y4fYlU+FXo8HXICSI/4405fl2A8LgId6UU+j3tCU/sUdTX3Q1pmVkudxrE1zsgCui7cW WqZmj7/S5uyV8tKMfaw35CHcFu8xOKBjmPskgu43MqG10b63mr+SSdDnjAxZSCjR3bwK TlQNsf2Jfoa344WY+V+N6QuxvBckUO9HXnwJl3zK3q9grvKO1nuq82bL45fPsI5TVKhK Cn+2lyVw2yk4D3k9R4IbhOuh3PSJq81IZEAwRfFJVo0r8Wr2k9EWJRec5d96LNysN+T1 8jmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735334626; x=1735939426; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=XHLHCT+Uhup6/0zagPjbyaH9QvNx7XJgNsirN+mqf0w=; b=eJGIVhHMetspDB1ao7ZC/mTFaemmYsM0Cv17K4rSTtw72V+iOBwRhstr89/ZydFoll vLLBZJ+5YMsT7v4Rp28Pl0HC/cSlFUvLyRIuPW8wdf7NPoJ5cOCnfS4crKzInP1t1Hhg cHgzFNg6Vxpt0bdK5yyyDexeSHpKCjJrtXSLnt3fiXlL8ces5TKd1JZIXq7paPV1FpEF Rn480MXeFvKN6KptoCVkF4CH8wkrIr8O1SqXAf0xDPyrKEks6fAp0YpqtClCRsDJyz0Y 156Etmf5jgFmqEAbaYFRtXbY95wWQN5gMCjv+7eblROsUZAilIzBX1pnXCMDfOzfCARO /qcQ== X-Forwarded-Encrypted: i=1; AJvYcCVtlXm/5fCpUAQCDLM9DW1AbplaJBb0gTom+E+01uudLb1affmc6NwmeL/qFaTR/ly4u6Yl/p1vr745M9ta@lists.postgresql.org X-Gm-Message-State: AOJu0YzILowbQLGN+HkyhCSL/pxG/xs0nhn81dk009vqLALHOppN/Z9V ByxfYhCdJb6rQu0OATL2gUd2YC5URwtITpNuVUYUHfZ1eoWVzW3oO9wNzk618tNZCDJgiTAe52k JmW6ZO/MWqtq3h6mNfqHnTqy6OTc= X-Gm-Gg: ASbGncvvClTLWZKnamFJjMXD55VkMuXTL3awOiakb+v/hnPjTttJQ4OwQLot1XOn/i8 w3kcnN8UY0Nz/fT4cp1jKV7p7eILfTconXnn5IY30cYqHIBPfFBLpordmEYQsUaAjPC0yNpk= X-Google-Smtp-Source: AGHT+IEydnrd5xnK0NrQp/X1J8VF8LntwPBiWpTMBZkpYGKW/TN0Qy82KZ+7bKpJMaQS4xD0EGobCCoQFdaUxwaMhig= X-Received: by 2002:a05:690c:368f:b0:6ef:6ba2:e851 with SMTP id 00721157ae682-6f3f812582dmr206040677b3.12.1735334625835; Fri, 27 Dec 2024 13:23:45 -0800 (PST) MIME-Version: 1.0 References: <20241227205025.1d059f72c7c08d23c9648c26@magnetkern.de> <2547821.1735333397@sss.pgh.pa.us> In-Reply-To: <2547821.1735333397@sss.pgh.pa.us> From: Pavel Stehule Date: Fri, 27 Dec 2024 22:23:09 +0100 Message-ID: Subject: Re: search_path for PL/pgSQL functions partially cached? To: Tom Lane Cc: "David G. Johnston" , Jan Behrens , "pgsql-general@lists.postgresql.org" Content-Type: multipart/alternative; boundary="000000000000f08243062a4713d3" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000f08243062a4713d3 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable p=C3=A1 27. 12. 2024 v 22:03 odes=C3=ADlatel Tom Lane n= apsal: > "David G. Johnston" writes: > > 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. > > Yeah, I don't see this changing. The actual answer is that we have > search_path-aware caching of expressions and query plans within a > plpgsql function, which is why the call to foo() reacts to the current > search path. But the types of plpgsql variables are only looked up > on the first use (within a session). Perhaps we ought to work harder > on that, but it seems like a lot of overhead to add for something that > will benefit next to nobody. > > > I=E2=80=99ve never really understood why =E2=80=9C%TYPE=E2=80=99 exists= =E2=80=A6 > > Compatibility with Oracle, I imagine. I agree it's a bizarre feature. > But you could get the same behavior without %TYPE, just by referencing > some other type that has different declarations in different schemas. > This feature is not bizarre - just the implementation in Postgres is not fully complete (and I am not sure if it is fixable). PLpgSQL uses plan cache, but there is nothing similar for types. It is designed for Oracle where search_path doesn't exist, and where change of schema invalidates code, and requires recompilation. PL/pgSQL and Postgres are much more dynamic systems than Oracle. Maybe PL/pgSQL functions can holds dependency on types, and when any related custom type is changed, then the cached function can be invalidated. Unfortunately, the frequent change of search path can kill the performance. > > Add qualification or attach a =E2=80=9Cset search_path=E2=80=9D clause = to =E2=80=9Ccreate > > function=E2=80=9D. Code stored in the server should not rely on the se= ssion > > search_path. > > Yeah, adding "set search_path" is recommendable if you don't want to > think hard about this stuff. > > regards, tom lane > > > --000000000000f08243062a4713d3 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


p=C3=A1 27. 12.= 2024 v=C2=A022:03 odes=C3=ADlatel Tom Lane <tgl@sss.pgh.pa.us> napsal:
"David G. Johnston" <david.g.johnston@gmail.c= om> writes:
> 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 executio= n
> environments and caches involved.

Yeah, I don't see this changing.=C2=A0 The actual answer is that we hav= e
search_path-aware caching of expressions and query plans within a
plpgsql function, which is why the call to foo() reacts to the current
search path.=C2=A0 But the types of plpgsql variables are only looked up on the first use (within a session).=C2=A0 Perhaps we ought to work harder<= br> on that, but it seems like a lot of overhead to add for something that
will benefit next to nobody.

> I=E2=80=99ve never really understood why =E2=80=9C%TYPE=E2=80=99 exist= s=E2=80=A6

Compatibility with Oracle, I imagine.=C2=A0 I agree it's a bizarre feat= ure.
But you could get the same behavior without %TYPE, just by referencing
some other type that has different declarations in different schemas.

This feature is not bizarre - just the imple= mentation in Postgres is not fully complete (and I am not sure if it is fix= able). PLpgSQL uses plan cache, but there is nothing similar for types.
It is designed for Oracle where search_path doesn't exist, and w= here change of schema invalidates code, and requires recompilation. PL/pgSQ= L and
Postgres are much more dynamic systems than Oracle. Maybe P= L/pgSQL functions can holds dependency on types, and when any related custo= m type
is changed, then the cached function can be invalidated. U= nfortunately, the frequent change of search path can kill the performance. =


> Add qualification or attach a =E2=80=9Cset search_path=E2=80=9D clause= to =E2=80=9Ccreate
> function=E2=80=9D.=C2=A0 Code stored in the server should not rely on = the session
> search_path.

Yeah, adding "set search_path" is recommendable if you don't = want to
think hard about this stuff.

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 regards, tom lane


--000000000000f08243062a4713d3--