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 1tTNwD-005DE2-Up for pgsql-general@arkaria.postgresql.org; Thu, 02 Jan 2025 16:20:46 +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 1tTNwD-00GwNe-3K for pgsql-general@arkaria.postgresql.org; Thu, 02 Jan 2025 16:20:44 +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 1tTNwC-00GwNU-Ka for pgsql-general@lists.postgresql.org; Thu, 02 Jan 2025 16:20:44 +0000 Received: from mail-yb1-xb2a.google.com ([2607:f8b0:4864:20::b2a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tTNwA-001kNI-1T for pgsql-general@lists.postgresql.org; Thu, 02 Jan 2025 16:20:43 +0000 Received: by mail-yb1-xb2a.google.com with SMTP id 3f1490d57ef6-e46ebe19489so13796196276.2 for ; Thu, 02 Jan 2025 08:20:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735834842; x=1736439642; 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=6xl6vVvfWNzYr/7583E64esJsPw77w8TZz52/jPtTNY=; b=SNfDFmhWDFIfMwsBt/0t5jqwzGjlW4m+0Wkg+BykLYED7HPoG8CoC+KGx69A8yFL6H e0GvyT4Sn7agH5UUM/6jTRBm2lq0yEASVBAUHjAaXZ21JyKpvmLH2Q3eDjnbwpYJ46ev 04l2Juu8ocEDfsXkVr6QqijB65nLFXRUxij0o8b8bUr49dYpURCwverso3zmQLudQdxw 2Ai3OTcyNXcSPR7NKsf+ORgexwp0wKJwDAYe0HHYKUtBc/IncF8MVX+aZunDVYwq/eY/ dioUxhJPXmq17j0Zp6Wn4FJ3G6KM6ummx524VN9bSdL9gv6a1Eo7x4If56vVM+S19Yb5 dahg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735834842; x=1736439642; 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=6xl6vVvfWNzYr/7583E64esJsPw77w8TZz52/jPtTNY=; b=Zb1+XTjuGIVTmT5xvWoGd+Ql1BH6AONIpT/fthXHtcP1x3BwJYr2Kgbcvts2HvoR+P aae+K0UiXVxUmDWO2rmYQZwgSIQICgd4splZBOeC/F3MPWOxACwFB/6lxxpbkuHwPQst xNvUdsX5DzevU2OfzMBC+K1mbX6JZv2nKifV4ZSPbPum5R41ImZZakeaM42AGDUKLjOS HCsqsQRLSEPuzZXdiWdPzjHabHfD+XL7xrYbrAnfXDlJUzExahFvOqz8UDPnfg3wPAR9 tV0vF6d0bR7FYML3geUo5J2wZlpgDVtktpDA240SeiydXtKzF6kwS/qj/DyeHrA3Be4+ BY+Q== X-Forwarded-Encrypted: i=1; AJvYcCW9+6aCA/Jsg4ITR1jAX9gfX8mZLvlJckWaqLfdzl/u4emimT4IiKVgZDn8GYiEsDxSxjO3CSdoPGtWoMsa@lists.postgresql.org X-Gm-Message-State: AOJu0YznOELqIawNOdAIeIaYn5xW5fxcxbhSmmsbPLH8jijkpZ7j49T4 cM9qmAvLOvtRhbjMTuiS3NDCqVwoA708zU5qT8ba8KWiQm4AQAVMv1Y14YTTM8azxXTLBtLV1VM Gz5DkLSf2tjL5duJxKpdvbb2igKba/znK X-Gm-Gg: ASbGncuJDOSULDGBEVP8rm90hicwfgMClVB46WnRIPZ0AQ/0+lzqaKWJO2df+N6e/Tt RXoiNbf/beuwc97KPyzBzq6NafdmXxSBA9KRqcnLdEmqXE3hf5nqyxyd8NibdbTnt++0/7bQ= X-Google-Smtp-Source: AGHT+IEenLIHSfdhB02ohGo+muS+/G0w0mSUgOHyw7dsz/jy4emAT+/Di7f6qgjyNc1Y53DDYY0DT/TwnPsiwqQtBvs= X-Received: by 2002:a05:690c:6901:b0:6ef:7c4d:4cca with SMTP id 00721157ae682-6f3f8238c40mr360588857b3.37.1735834841645; Thu, 02 Jan 2025 08:20:41 -0800 (PST) MIME-Version: 1.0 References: <20241227205025.1d059f72c7c08d23c9648c26@magnetkern.de> <20241228004009.267f21b78394c934f27f9974@magnetkern.de> <20250101185504.3d50c571c3448512e94288e8@magnetkern.de> <20250102113727.1574b14fd677d164c32160bc@magnetkern.de> <20250102131547.f47d7eeb27c7a8256a8cd189@magnetkern.de> <20250102163440.17acbac07e2e32016cb5433c@magnetkern.de> In-Reply-To: <20250102163440.17acbac07e2e32016cb5433c@magnetkern.de> From: Pavel Stehule Date: Thu, 2 Jan 2025 17:20:05 +0100 Message-ID: Subject: Re: search_path for PL/pgSQL functions partially cached? To: Jan Behrens Cc: "David G. Johnston" , "pgsql-general@lists.postgresql.org" Content-Type: multipart/alternative; boundary="000000000000200a1e062abb8b11" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000200a1e062abb8b11 Content-Type: text/plain; charset="UTF-8" Hi > > > > some times can be pretty ineffective to have database per customer - more > > connect, disconnect in postgres is much more expensive than SET > search_path > > TO .. and maybe RESET plans; > > I guess that means there is a practical application where search_path > MAY change at runtime IF done in different sessions or if the cache is > reset using the DISCARD command: > > https://www.postgresql.org/docs/17/sql-discard.html > > I assume DISCARD PLANS would be the right command? > that depends. plan inside plan cache is invalidated when search_path is different. You use RESET plans because you want to release all plans quickly. Unfortunately, the types assigned to plpgsql variables are not invalidated. This is the source of problems. It is a classical problem - it is hard to say when you should invalidate cache. Current design is not ideal - but it is almost a good enough compromise between correctness and performance. It is true, so nobody did some work to fix it. So maybe the impact to performance should not be too bad, but it is not an easy issue. plans are isolated - and the impact of one plan to the second plan is zero. For variables it is exactly opposite. > > This seems to be a very special case though. I think there should be a > warning in the documentation of CREATE FUNCTION with regard to schemas > anyway, though. > I am not sure. If you want to use this warning, then it should be everywhere where any non-qualified identifier can be used. Maybe in plpgsql can be more accented so almost everything in plpgsql depends on the current setting of search_path. Lot of people don't understand, so every expression in plpgsql is SQL and every expression is executed like part of a query. And unfortunately there are some different caches - plpgsql cache and plan cache and both caches are invalidated at different times (I think so plpgsql cache is not resetted by RESET PLANS). Maybe it is better to explain how plpgsql works. It is a little bit different from well known interpreted languages. > Regards, > Jan > --000000000000200a1e062abb8b11 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi
=C2=A0
>
> some times can be pretty ineffective to have database per customer - m= ore
> connect, disconnect in postgres is much more expensive than SET search= _path
> TO .. and maybe RESET plans;

I guess that means there is a practical application where search_path
MAY change at runtime IF done in different sessions or if the cache is
reset using the DISCARD command:

https://www.postgresql.org/docs/17/sql-discard.ht= ml

I assume DISCARD PLANS would be the right command?
that depends. plan inside plan cache is invalidated when search= _path is different. You use RESET plans because you want to release all pla= ns quickly.

Unfortunately, the types assigned to p= lpgsql variables are not invalidated. This is the source of problems. It is= a classical problem - it is hard to say when you should invalidate cache. =
Current design is not ideal - but it is almost a good enough= compromise between correctness and performance. It is true, so nobody did = some work to fix it. So maybe the impact to performance should not be too b= ad, but it is not an easy issue. plans are isolated - and the impact of one= plan to the second plan is zero. For variables it is exactly opposite.
=
=C2=A0

This seems to be a very special case though. I think there should be a
warning in the documentation of CREATE FUNCTION with regard to schemas
anyway, though.

I am not sure. If you w= ant to use this warning, then it should be everywhere where any non-qualifi= ed identifier can be used. Maybe in plpgsql can be more accented so almost = everything in plpgsql depends on the current setting of search_path. Lot of= people don't understand, so every expression in plpgsql is SQL and eve= ry expression is executed like part of a query. And unfortunately there are= some different caches - plpgsql cache and plan cache and both caches are i= nvalidated at different times (I think so plpgsql cache is not resetted by = RESET PLANS). Maybe it is better to explain how plpgsql works. It is a litt= le bit different from well known interpreted languages.

<= /div>

Regards,
Jan
--000000000000200a1e062abb8b11--