Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n7JQz-0003bi-JD for pgsql-general@arkaria.postgresql.org; Tue, 11 Jan 2022 15:51:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1n7JQx-0004ua-IE for pgsql-general@arkaria.postgresql.org; Tue, 11 Jan 2022 15:51:39 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n7JQw-0004uQ-TC for pgsql-general@lists.postgresql.org; Tue, 11 Jan 2022 15:51:39 +0000 Received: from smtp-outgoing-1901.laposte.net ([160.92.124.105]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n7JQt-0008UI-0h for pgsql-general@postgresql.org; Tue, 11 Jan 2022 15:51:37 +0000 X-mail-filterd: {"version":"1.3.4","queueID":"4JYFYH0GfGz14K0Z","contextId":"f66ac3a8-2d69-4f28-a624-839301c061f5"} Received: from outgoing-mail.laposte.net (localhost.localdomain [127.0.0.1]) by mlpnf0109.laposte.net (SMTP Server) with ESMTP id 4JYFYH0GfGz14K0Z for ; Tue, 11 Jan 2022 16:51:31 +0100 (CET) X-mail-filterd: {"version":"1.3.4","queueID":"4JYFYG6wrzz14K0Y","contextId":"258daab9-d0d3-4299-baf7-3af8ced22e07"} X-lpn-mailing: LEGIT X-lpn-spamrating: 40 X-lpn-spamlevel: not-spam X-lpn-spamcause: OK, (0)(0000)gggruggvucftvghtrhhoucdtuddrgedvuddrudehfedgkeduucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecunfetrffquffvgfdpggftfghnshhusghstghrihgsvgenuceurghilhhouhhtmecufedttdenucenucfjughrpeffhfhrvffkufggtghisegrtdgssgdttdejnecuhfhrohhmpegsvghnjhdruggvvheslhgrphhoshhtvgdrnhgvthenucggtffrrghtthgvrhhnpeetffetvddtfefhieduleeuffeigfetgedugfdtteehtdefheetueelieektefggfenucffohhmrghinhepphhoshhtghhrvghsqhhlrdhorhhgnecukfhppeekuddrkedtrdegfedrvdegtdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepihhnvghtpeekuddrkedtrdegfedrvdegtddphhgvlhhopeiflhhpnhhftddvtdekrdhshihsrdhmvghshhgtohhrvgdrnhgvthdpmhgrihhlfhhrohhmpegsvghnjhdruggvvheslhgrphhoshhtvgdrnhgvthdprhgtphhtthhopehpghhsqhhlqdhgvghnvghrrghlsehpohhsthhgrhgvshhqlhdrohhrghdpmhhouggvpehsmhhtphhouhht Received: from wlpnf0208.sys.meshcore.net (wlpnf0208.sys.meshcore.net [81.80.43.240]) by mlpnf0109.laposte.net (SMTP Server) with ESMTPA id 4JYFYG6wrzz14K0Y for ; Tue, 11 Jan 2022 16:51:30 +0100 (CET) Date: Tue, 11 Jan 2022 16:51:30 +0100 (CET) From: benj.dev@laposte.net Reply-To: benj.dev@laposte.net To: pgsql-general@postgresql.org Message-ID: <844844667.836008.1641916290929@wlpnf0208> Subject: Re: plpgsql function problem whith creating temp table - not correctly using search_path ? MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_836007_646582406.1641916290929" X-ORIGINATING-IP: 81.80.43.240 X-Wum-Nature: EMAIL-NATURE X-SAVECOPY: true X-WUM-FROM: |~| X-WUM-TO: |~| X-WUM-REPLYTO: |~| DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=laposte.net; s=lpn-wlmd; t=1641916292; bh=RHaDH/JMFdTsr1N0Age6l8yQcq4CHjFwbeQMzjI3oQY=; h=Date:From:Reply-To:To:Message-ID:Subject:MIME-Version:Content-Type; b=Mi74oFNIxyufir213RETtAXOEnsVpUl2/anm3Jm9vSoGvTK96ZX+GYaO0BcL+1yIq0BJbCqvNk3BpB8ayt28ZyPvdr6mEZDO/HMC4N2vs4dguNvcNRh58ayoAmiijtzBMVhegAaF0rTbiZRXPJA8u6AKsEc0fgLf7zJr4pXTlPHo0WKntjKRHZtqHRXKusHKLxiGwO3KUpd1IKekt4rkDgSkzvc1dNqApUWvnaFJZRYtgX29RA3PFy1siTWP3L4JCpPRZcIU5XrNaaJjlq1iAABoDkTy1LKPYu+P8/o4N7DhtldEEMKHw4MGUCLtN3oLfLwQ6RNALYUa2VFJtfsDLA==; List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------=_Part_836007_646582406.1641916290929 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable =E2=80=8CHi, Thank you for pointing this part of the documentation. It's actually works with EXECUTE 'INSERT INTO my_table VALUES((random() * 1= 00)::INT);'; INSTEAD OF INSERT INTO my_table VALUES((random() * 100)::INT); And it's possible to suppose that the=C2=A0test_search_path_v2 worked befor= e because of the=C2=A0 PERFORM set_config('search_path', '"$user", public', true);=C2=A0 I imagine that the changement of search_path have on side effect to invalid= ate the cached plans here and force to recompute the query plan. I imagine that the probleme appears in =C2=A0test_search_path_v1 after seco= nd call maybe because=C2=A0 the cache is kept by following rules such as a = certain number of executions In this example, use EXECUTE only in INSERT INTO my_table is sufficient to = remove the problem. subsequent SELECT works (without the EXECUTE). Does doing an EXECUTE on a table have the side effect of invalidating cache= s using that table name or is it just a "chance" here and for added securit= y I should use EXECUTE everywhere? Thanks =C2=A0 De : "David G. Johnston" A : "benj.dev@laposte.net" ,"pgsql-general@postgresql.org" Envoy=C3=A9: mardi 11 Janvier 2022 15:18 Objet : Re: plpgsql function problem whith creating temp table - not correc= tly using search_path ? =C2=A0 On Tuesday, January 11, 2022, wrote: SHOW search_path -- =3D> "$user", public; DROP TABLE IF EXISTS my_table; -- Executing each row on differents transactions but in the same session /*Session A - Transaction 1*/ SELECT * FROM test_search_path_v1(true);=C2= =A0 -- =3D> OK takes table from pg_temp (no existing table in public) /*Session A - Transaction 2*/ SELECT * FROM test_search_path_v1(false); -- = =3D> OK takes table from public /*Session A - Transaction 3*/ SELECT * FROM test_search_path_v1(true);=C2= =A0 -- =3D> OK takes table from pg_temp (and the existing from public) /*Session A - Transaction 4*/ SELECT * FROM test_search_path_v1(false); -- = =3D> OK takes table from public /*Session A - Transaction 5*/ SELECT * FROM test_search_path_v1(true);=C2= =A0 -- =3D> NOK =3D> it takes public and not pg_temp =C2=A0 Per [1], you are violating: =C2=A0 =E2=80=9CBecause=C2=A0PL/pgSQL=C2=A0saves prepared statements and sometimes= execution plans in this way, SQL commands that appear directly in a=C2=A0P= L/pgSQL=C2=A0function must refer to the same tables and columns on every ex= ecution; that is, you cannot use a parameter as the name of a table or colu= mn in an SQL command.=E2=80=9D =C2=A0 While that wording is odd, it is not even possible to use variables in plac= e of table and column names, what you are doing here is effectively the sam= e.=C2=A0 I cannot explain the precise observed behavior, and maybe there is= a bug here, but on its face what you are doing is simply invalid in face o= f how the session relation cache works. =C2=A0 [1]=C2=A0https://www.postgresql.org/docs/current/plpgsql-implementation.htm= l#PLPGSQL-PLAN-CACHING=C2=A0 =C2=A0 David J. =C2=A0 ------=_Part_836007_646582406.1641916290929 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
=E2=80=8CHi, Thank you for pointing this part of the documentati= on.
It's actually works with EXECUTE 'INSERT INTO my_table VALUES((random() * 1= 00)::INT);'; INSTEAD OF INSERT INTO my_table VALUES((random() * 100)::INT);=
And it's possible to suppose that the test_search_path_v2 worked befor= e because of the 
PERFORM set_config('search_path', '"$user", public', true); 
I imagine that the changement of search_path have on side effect to invalid= ate the cached plans here and force to recompute the query plan.
I imagine that the probleme appears in  test_search_path_v1 after seco= nd call maybe because  the cache is kept by following rules such as a = certain number of executions

In this example, use EXECUTE only in INSERT INTO my_table is sufficient to = remove the problem.
subsequent SELECT works (without the EXECUTE).
Does doing an EXECUTE on a table have the side effect of invalidating cache= s using that table name or is it just a "chance" here and for added securit= y I should use EXECUTE everywhere?

Thanks
 
De : = "David G. Johnston"
A : "benj.dev@laposte.net" ,"pgsql-general@postgresql.org"
Envoy=C3=A9: mardi 11 Janvier 2022 15:18
Objet : Re: plpgsql function problem whith creating temp table - not correc= tly using search_path ?
 
On Tuesday, January 11, 2022, <benj.dev@laposte.net> wrote:

SHOW search_path -- =3D> "$user", public;
DROP TABLE IF EXISTS my_table;
-- Executing each row on differents transactions but in the same session /*Session A - Transaction 1*/ SELECT * FROM test_search_path_v1(true); = ; -- =3D> OK takes table from pg_temp (no existing table in public)
/*Session A - Transaction 2*/ SELECT * FROM test_search_path_v1(false); -- = =3D> OK takes table from public
/*Session A - Transaction 3*/ SELECT * FROM test_search_path_v1(true); = ; -- =3D> OK takes table from pg_temp (and the existing from public)
/*Session A - Transaction 4*/ SELECT * FROM test_search_path_v1(false); -- = =3D> OK takes table from public
/*Session A - Transaction 5*/ SELECT * FROM test_search_path_v1(true); = ; -- =3D> NOK =3D> it takes public and not pg_temp
 
Per [1], you are violating:
 
=E2=80=9CBecause PL/pgSQL saves prepared statements and sometimes exec= ution plans in this way, SQL commands that appear directly in a PL= /pgSQL function must refer to the sa= me tables and columns on every execution; that is, you cannot use a paramet= er as the name of a table or column in an SQL command.=E2=80=9D
 
While that wording is odd, it is not even p= ossible to use variables in place of table and column names, what you are d= oing here is effectively the same.  I cannot explain the precise obser= ved behavior, and maybe there is a bug here, but on its face what you are d= oing is simply invalid in face of how the session relation cache works.
 
 
David J.
 
------=_Part_836007_646582406.1641916290929--